manifest.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. import os
  16. import sys
  17. from command import PagedCommand
  18. from manifest_submodule import SubmoduleManifest
  19. from manifest_xml import XmlManifest
  20. def _doc(name):
  21. r = os.path.dirname(__file__)
  22. r = os.path.dirname(r)
  23. fd = open(os.path.join(r, 'docs', name))
  24. try:
  25. return fd.read()
  26. finally:
  27. fd.close()
  28. class Manifest(PagedCommand):
  29. common = False
  30. helpSummary = "Manifest inspection utility"
  31. helpUsage = """
  32. %prog [options]
  33. """
  34. _xmlHelp = """
  35. With the -o option, exports the current manifest for inspection.
  36. The manifest and (if present) local_manifest.xml are combined
  37. together to produce a single manifest file. This file can be stored
  38. in a Git repository for use during future 'repo init' invocations.
  39. """
  40. @property
  41. def helpDescription(self):
  42. help = ''
  43. if isinstance(self.manifest, XmlManifest):
  44. help += self._xmlHelp + '\n' + _doc('manifest_xml.txt')
  45. if isinstance(self.manifest, SubmoduleManifest):
  46. help += _doc('manifest_submodule.txt')
  47. return help
  48. def _Options(self, p):
  49. if isinstance(self.manifest, XmlManifest):
  50. p.add_option('--upgrade',
  51. dest='upgrade', action='store_true',
  52. help='Upgrade XML manifest to submodule')
  53. p.add_option('-r', '--revision-as-HEAD',
  54. dest='peg_rev', action='store_true',
  55. help='Save revisions as current HEAD')
  56. p.add_option('-o', '--output-file',
  57. dest='output_file',
  58. help='File to save the manifest to',
  59. metavar='-|NAME.xml')
  60. def WantPager(self, opt):
  61. if isinstance(self.manifest, XmlManifest) and opt.upgrade:
  62. return False
  63. return True
  64. def _Output(self, opt):
  65. if opt.output_file == '-':
  66. fd = sys.stdout
  67. else:
  68. fd = open(opt.output_file, 'w')
  69. self.manifest.Save(fd,
  70. peg_rev = opt.peg_rev)
  71. fd.close()
  72. if opt.output_file != '-':
  73. print >>sys.stderr, 'Saved manifest to %s' % opt.output_file
  74. def _Upgrade(self):
  75. old = self.manifest
  76. if isinstance(old, SubmoduleManifest):
  77. print >>sys.stderr, 'error: already upgraded'
  78. sys.exit(1)
  79. old._Load()
  80. for p in old.projects.values():
  81. if not os.path.exists(p.gitdir) \
  82. or not os.path.exists(p.worktree):
  83. print >>sys.stderr, 'fatal: project "%s" missing' % p.relpath
  84. sys.exit(1)
  85. new = SubmoduleManifest(old.repodir)
  86. new.FromXml_Local_1(old, checkout=False)
  87. new.FromXml_Definition(old)
  88. new.FromXml_Local_2(old)
  89. print >>sys.stderr, 'upgraded manifest; commit result manually'
  90. def Execute(self, opt, args):
  91. if args:
  92. self.Usage()
  93. if isinstance(self.manifest, XmlManifest):
  94. if opt.upgrade:
  95. self._Upgrade()
  96. return
  97. if opt.output_file is not None:
  98. self._Output(opt)
  99. return
  100. print >>sys.stderr, 'error: no operation to perform'
  101. print >>sys.stderr, 'error: see repo help manifest'
  102. sys.exit(1)