blob: dcd3df17b5daac8f9432d857fcac35310c614f9b [file] [log] [blame]
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -08001#
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
16import os
17import sys
18
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080019from command import PagedCommand
Shawn O. Pearce57272ba2009-07-03 18:06:22 -070020from manifest_submodule import SubmoduleManifest
Shawn O. Pearce67f45632009-05-19 18:17:51 -070021from manifest_xml import XmlManifest
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080022
Shawn O. Pearce050e4fd2009-06-03 14:16:14 -070023def _doc(name):
24 r = os.path.dirname(__file__)
25 r = os.path.dirname(r)
Shawn O. Pearce0125ae22009-07-03 18:05:23 -070026 fd = open(os.path.join(r, 'docs', name))
Shawn O. Pearce050e4fd2009-06-03 14:16:14 -070027 try:
28 return fd.read()
29 finally:
30 fd.close()
31
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080032class Manifest(PagedCommand):
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080033 common = False
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080034 helpSummary = "Manifest inspection utility"
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080035 helpUsage = """
Shawn O. Pearce67f45632009-05-19 18:17:51 -070036%prog [options]
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080037"""
Shawn O. Pearce050e4fd2009-06-03 14:16:14 -070038 _xmlHelp = """
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080039
40With the -o option, exports the current manifest for inspection.
41The manifest and (if present) local_manifest.xml are combined
42together to produce a single manifest file. This file can be stored
43in a Git repository for use during future 'repo init' invocations.
44
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080045"""
46
47 @property
48 def helpDescription(self):
Shawn O. Pearce050e4fd2009-06-03 14:16:14 -070049 help = ''
50 if isinstance(self.manifest, XmlManifest):
51 help += self._xmlHelp + '\n' + _doc('manifest_xml.txt')
Shawn O. Pearce0125ae22009-07-03 18:05:23 -070052 if isinstance(self.manifest, SubmoduleManifest):
53 help += _doc('manifest_submodule.txt')
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080054 return help
55
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080056 def _Options(self, p):
Shawn O. Pearce67f45632009-05-19 18:17:51 -070057 if isinstance(self.manifest, XmlManifest):
Shawn O. Pearce57272ba2009-07-03 18:06:22 -070058 p.add_option('--upgrade',
59 dest='upgrade', action='store_true',
60 help='Upgrade XML manifest to submodule')
Shawn O. Pearce67f45632009-05-19 18:17:51 -070061 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. Pearcec7a4eef2009-03-05 10:32:38 -080068
Shawn O. Pearce57272ba2009-07-03 18:06:22 -070069 def WantPager(self, opt):
70 if isinstance(self.manifest, XmlManifest) and opt.upgrade:
71 return False
72 return True
73
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080074 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. Pearce57272ba2009-07-03 18:06:22 -070085 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. Pearce43c3d9e2009-03-04 14:26:50 -0800105 def Execute(self, opt, args):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800106 if args:
107 self.Usage()
108
Shawn O. Pearce57272ba2009-07-03 18:06:22 -0700109 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. Pearce67f45632009-05-19 18:17:51 -0700115 self._Output(opt)
116 return
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800117
118 print >>sys.stderr, 'error: no operation to perform'
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800119 print >>sys.stderr, 'error: see repo help manifest'
120 sys.exit(1)