Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 1 | # |
| 2 | # Copyright (C) 2009 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | import os |
| 17 | import sys |
| 18 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 19 | from command import PagedCommand |
Shawn O. Pearce | 57272ba | 2009-07-03 18:06:22 -0700 | [diff] [blame] | 20 | from manifest_submodule import SubmoduleManifest |
Shawn O. Pearce | 67f4563 | 2009-05-19 18:17:51 -0700 | [diff] [blame] | 21 | from manifest_xml import XmlManifest |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 22 | |
Shawn O. Pearce | 050e4fd | 2009-06-03 14:16:14 -0700 | [diff] [blame] | 23 | def _doc(name): |
| 24 | r = os.path.dirname(__file__) |
| 25 | r = os.path.dirname(r) |
Shawn O. Pearce | 0125ae2 | 2009-07-03 18:05:23 -0700 | [diff] [blame] | 26 | fd = open(os.path.join(r, 'docs', name)) |
Shawn O. Pearce | 050e4fd | 2009-06-03 14:16:14 -0700 | [diff] [blame] | 27 | try: |
| 28 | return fd.read() |
| 29 | finally: |
| 30 | fd.close() |
| 31 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 32 | class Manifest(PagedCommand): |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 33 | common = False |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 34 | helpSummary = "Manifest inspection utility" |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 35 | helpUsage = """ |
Shawn O. Pearce | 67f4563 | 2009-05-19 18:17:51 -0700 | [diff] [blame] | 36 | %prog [options] |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 37 | """ |
Shawn O. Pearce | 050e4fd | 2009-06-03 14:16:14 -0700 | [diff] [blame] | 38 | _xmlHelp = """ |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 39 | |
| 40 | With the -o option, exports the current manifest for inspection. |
| 41 | The manifest and (if present) local_manifest.xml are combined |
| 42 | together to produce a single manifest file. This file can be stored |
| 43 | in a Git repository for use during future 'repo init' invocations. |
| 44 | |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 45 | """ |
| 46 | |
| 47 | @property |
| 48 | def helpDescription(self): |
Shawn O. Pearce | 050e4fd | 2009-06-03 14:16:14 -0700 | [diff] [blame] | 49 | help = '' |
| 50 | if isinstance(self.manifest, XmlManifest): |
| 51 | help += self._xmlHelp + '\n' + _doc('manifest_xml.txt') |
Shawn O. Pearce | 0125ae2 | 2009-07-03 18:05:23 -0700 | [diff] [blame] | 52 | if isinstance(self.manifest, SubmoduleManifest): |
| 53 | help += _doc('manifest_submodule.txt') |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 54 | return help |
| 55 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 56 | def _Options(self, p): |
Shawn O. Pearce | 67f4563 | 2009-05-19 18:17:51 -0700 | [diff] [blame] | 57 | if isinstance(self.manifest, XmlManifest): |
Shawn O. Pearce | 57272ba | 2009-07-03 18:06:22 -0700 | [diff] [blame] | 58 | p.add_option('--upgrade', |
| 59 | dest='upgrade', action='store_true', |
| 60 | help='Upgrade XML manifest to submodule') |
Shawn O. Pearce | 67f4563 | 2009-05-19 18:17:51 -0700 | [diff] [blame] | 61 | p.add_option('-r', '--revision-as-HEAD', |
| 62 | dest='peg_rev', action='store_true', |
| 63 | help='Save revisions as current HEAD') |
| 64 | p.add_option('-o', '--output-file', |
| 65 | dest='output_file', |
| 66 | help='File to save the manifest to', |
| 67 | metavar='-|NAME.xml') |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 68 | |
Shawn O. Pearce | 57272ba | 2009-07-03 18:06:22 -0700 | [diff] [blame] | 69 | def WantPager(self, opt): |
| 70 | if isinstance(self.manifest, XmlManifest) and opt.upgrade: |
| 71 | return False |
| 72 | return True |
| 73 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 74 | def _Output(self, opt): |
| 75 | if opt.output_file == '-': |
| 76 | fd = sys.stdout |
| 77 | else: |
| 78 | fd = open(opt.output_file, 'w') |
| 79 | self.manifest.Save(fd, |
| 80 | peg_rev = opt.peg_rev) |
| 81 | fd.close() |
| 82 | if opt.output_file != '-': |
| 83 | print >>sys.stderr, 'Saved manifest to %s' % opt.output_file |
| 84 | |
Shawn O. Pearce | 57272ba | 2009-07-03 18:06:22 -0700 | [diff] [blame] | 85 | def _Upgrade(self): |
| 86 | old = self.manifest |
| 87 | |
| 88 | if isinstance(old, SubmoduleManifest): |
| 89 | print >>sys.stderr, 'error: already upgraded' |
| 90 | sys.exit(1) |
| 91 | |
| 92 | old._Load() |
| 93 | for p in old.projects.values(): |
| 94 | if not os.path.exists(p.gitdir) \ |
| 95 | or not os.path.exists(p.worktree): |
| 96 | print >>sys.stderr, 'fatal: project "%s" missing' % p.relpath |
| 97 | sys.exit(1) |
| 98 | |
| 99 | new = SubmoduleManifest(old.repodir) |
| 100 | new.FromXml_Local_1(old, checkout=False) |
| 101 | new.FromXml_Definition(old) |
| 102 | new.FromXml_Local_2(old) |
| 103 | print >>sys.stderr, 'upgraded manifest; commit result manually' |
| 104 | |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 105 | def Execute(self, opt, args): |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 106 | if args: |
| 107 | self.Usage() |
| 108 | |
Shawn O. Pearce | 57272ba | 2009-07-03 18:06:22 -0700 | [diff] [blame] | 109 | if isinstance(self.manifest, XmlManifest): |
| 110 | if opt.upgrade: |
| 111 | self._Upgrade() |
| 112 | return |
| 113 | |
| 114 | if opt.output_file is not None: |
Shawn O. Pearce | 67f4563 | 2009-05-19 18:17:51 -0700 | [diff] [blame] | 115 | self._Output(opt) |
| 116 | return |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 117 | |
| 118 | print >>sys.stderr, 'error: no operation to perform' |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 119 | print >>sys.stderr, 'error: see repo help manifest' |
| 120 | sys.exit(1) |