manifest.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # Copyright (C) 2009 The Android Open Source Project
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import json
  15. import os
  16. import sys
  17. from command import PagedCommand
  18. class Manifest(PagedCommand):
  19. common = False
  20. helpSummary = "Manifest inspection utility"
  21. helpUsage = """
  22. %prog [-o {-|NAME.xml}] [-m MANIFEST.xml] [-r]
  23. """
  24. _helpDescription = """
  25. With the -o option, exports the current manifest for inspection.
  26. The manifest and (if present) local_manifests/ are combined
  27. together to produce a single manifest file. This file can be stored
  28. in a Git repository for use during future 'repo init' invocations.
  29. The -r option can be used to generate a manifest file with project
  30. revisions set to the current commit hash. These are known as
  31. "revision locked manifests", as they don't follow a particular branch.
  32. In this case, the 'upstream' attribute is set to the ref we were on
  33. when the manifest was generated. The 'dest-branch' attribute is set
  34. to indicate the remote ref to push changes to via 'repo upload'.
  35. """
  36. @property
  37. def helpDescription(self):
  38. helptext = self._helpDescription + '\n'
  39. r = os.path.dirname(__file__)
  40. r = os.path.dirname(r)
  41. with open(os.path.join(r, 'docs', 'manifest-format.md')) as fd:
  42. for line in fd:
  43. helptext += line
  44. return helptext
  45. def _Options(self, p):
  46. p.add_option('-r', '--revision-as-HEAD',
  47. dest='peg_rev', action='store_true',
  48. help='Save revisions as current HEAD')
  49. p.add_option('-m', '--manifest-name',
  50. help='temporary manifest to use for this sync', metavar='NAME.xml')
  51. p.add_option('--suppress-upstream-revision', dest='peg_rev_upstream',
  52. default=True, action='store_false',
  53. help='If in -r mode, do not write the upstream field. '
  54. 'Only of use if the branch names for a sha1 manifest are '
  55. 'sensitive.')
  56. p.add_option('--suppress-dest-branch', dest='peg_rev_dest_branch',
  57. default=True, action='store_false',
  58. help='If in -r mode, do not write the dest-branch field. '
  59. 'Only of use if the branch names for a sha1 manifest are '
  60. 'sensitive.')
  61. p.add_option('--json', default=False, action='store_true',
  62. help='Output manifest in JSON format (experimental).')
  63. p.add_option('--pretty', default=False, action='store_true',
  64. help='Format output for humans to read.')
  65. p.add_option('-o', '--output-file',
  66. dest='output_file',
  67. default='-',
  68. help='File to save the manifest to',
  69. metavar='-|NAME.xml')
  70. def _Output(self, opt):
  71. # If alternate manifest is specified, override the manifest file that we're using.
  72. if opt.manifest_name:
  73. self.manifest.Override(opt.manifest_name, False)
  74. if opt.output_file == '-':
  75. fd = sys.stdout
  76. else:
  77. fd = open(opt.output_file, 'w')
  78. if opt.json:
  79. print('warning: --json is experimental!', file=sys.stderr)
  80. doc = self.manifest.ToDict(peg_rev=opt.peg_rev,
  81. peg_rev_upstream=opt.peg_rev_upstream,
  82. peg_rev_dest_branch=opt.peg_rev_dest_branch)
  83. json_settings = {
  84. # JSON style guide says Uunicode characters are fully allowed.
  85. 'ensure_ascii': False,
  86. # We use 2 space indent to match JSON style guide.
  87. 'indent': 2 if opt.pretty else None,
  88. 'separators': (',', ': ') if opt.pretty else (',', ':'),
  89. 'sort_keys': True,
  90. }
  91. fd.write(json.dumps(doc, **json_settings))
  92. else:
  93. self.manifest.Save(fd,
  94. peg_rev=opt.peg_rev,
  95. peg_rev_upstream=opt.peg_rev_upstream,
  96. peg_rev_dest_branch=opt.peg_rev_dest_branch)
  97. fd.close()
  98. if opt.output_file != '-':
  99. print('Saved manifest to %s' % opt.output_file, file=sys.stderr)
  100. def ValidateOptions(self, opt, args):
  101. if args:
  102. self.Usage()
  103. def Execute(self, opt, args):
  104. self._Output(opt)