manifest.py 4.6 KB

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