Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 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 | |
Mike Frysinger | 23411d3 | 2020-09-02 04:31:10 -0400 | [diff] [blame] | 15 | import json |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 16 | import os |
| 17 | import sys |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 18 | import optparse |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 19 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 20 | from command import PagedCommand |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 21 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 22 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 23 | class Manifest(PagedCommand): |
Mike Frysinger | 4f21054 | 2021-06-14 16:05:19 -0400 | [diff] [blame] | 24 | COMMON = False |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 25 | helpSummary = "Manifest inspection utility" |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 26 | helpUsage = """ |
Sean McAllister | d38300c | 2020-02-20 13:49:01 -0700 | [diff] [blame] | 27 | %prog [-o {-|NAME.xml}] [-m MANIFEST.xml] [-r] |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 28 | """ |
| 29 | _helpDescription = """ |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 30 | |
| 31 | With the -o option, exports the current manifest for inspection. |
Mike Frysinger | 4e1fc10 | 2020-09-06 14:42:47 -0400 | [diff] [blame] | 32 | The manifest and (if present) local_manifests/ are combined |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 33 | together to produce a single manifest file. This file can be stored |
| 34 | in a Git repository for use during future 'repo init' invocations. |
| 35 | |
Sean McAllister | 74e8ed4 | 2020-04-15 12:24:43 -0600 | [diff] [blame] | 36 | The -r option can be used to generate a manifest file with project |
| 37 | revisions set to the current commit hash. These are known as |
| 38 | "revision locked manifests", as they don't follow a particular branch. |
| 39 | In this case, the 'upstream' attribute is set to the ref we were on |
Sean McAllister | af908cb | 2020-04-20 08:41:58 -0600 | [diff] [blame] | 40 | when the manifest was generated. The 'dest-branch' attribute is set |
| 41 | to indicate the remote ref to push changes to via 'repo upload'. |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 42 | """ |
| 43 | |
| 44 | @property |
| 45 | def helpDescription(self): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 46 | helptext = self._helpDescription + '\n' |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 47 | r = os.path.dirname(__file__) |
| 48 | r = os.path.dirname(r) |
Mike Frysinger | 3164d40 | 2019-11-11 05:40:22 -0500 | [diff] [blame] | 49 | with open(os.path.join(r, 'docs', 'manifest-format.md')) as fd: |
| 50 | for line in fd: |
| 51 | helptext += line |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 52 | return helptext |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 53 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 54 | def _Options(self, p): |
| 55 | p.add_option('-r', '--revision-as-HEAD', |
| 56 | dest='peg_rev', action='store_true', |
Mike Frysinger | c177f94 | 2021-05-04 08:06:36 -0400 | [diff] [blame] | 57 | help='save revisions as current HEAD') |
Sean McAllister | d38300c | 2020-02-20 13:49:01 -0700 | [diff] [blame] | 58 | p.add_option('-m', '--manifest-name', |
| 59 | help='temporary manifest to use for this sync', metavar='NAME.xml') |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 60 | p.add_option('--suppress-upstream-revision', dest='peg_rev_upstream', |
| 61 | default=True, action='store_false', |
Mike Frysinger | c177f94 | 2021-05-04 08:06:36 -0400 | [diff] [blame] | 62 | help='if in -r mode, do not write the upstream field ' |
| 63 | '(only of use if the branch names for a sha1 manifest are ' |
| 64 | 'sensitive)') |
Sean McAllister | af908cb | 2020-04-20 08:41:58 -0600 | [diff] [blame] | 65 | p.add_option('--suppress-dest-branch', dest='peg_rev_dest_branch', |
| 66 | default=True, action='store_false', |
Mike Frysinger | c177f94 | 2021-05-04 08:06:36 -0400 | [diff] [blame] | 67 | help='if in -r mode, do not write the dest-branch field ' |
| 68 | '(only of use if the branch names for a sha1 manifest are ' |
| 69 | 'sensitive)') |
Mike Frysinger | 23411d3 | 2020-09-02 04:31:10 -0400 | [diff] [blame] | 70 | p.add_option('--json', default=False, action='store_true', |
Mike Frysinger | c177f94 | 2021-05-04 08:06:36 -0400 | [diff] [blame] | 71 | help='output manifest in JSON format (experimental)') |
Mike Frysinger | 23411d3 | 2020-09-02 04:31:10 -0400 | [diff] [blame] | 72 | p.add_option('--pretty', default=False, action='store_true', |
Mike Frysinger | c177f94 | 2021-05-04 08:06:36 -0400 | [diff] [blame] | 73 | help='format output for humans to read') |
Michael Kelly | c34b91c | 2021-07-02 09:25:48 -0700 | [diff] [blame] | 74 | p.add_option('--no-local-manifests', default=False, action='store_true', |
| 75 | dest='ignore_local_manifests', help='ignore local manifests') |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 76 | p.add_option('-o', '--output-file', |
| 77 | dest='output_file', |
Conley Owens | 918ff85 | 2012-08-07 10:44:01 -0700 | [diff] [blame] | 78 | default='-', |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 79 | help='file to save the manifest to. (Filename prefix for multi-tree.)', |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 80 | metavar='-|NAME.xml') |
| 81 | |
| 82 | def _Output(self, opt): |
Sean McAllister | d38300c | 2020-02-20 13:49:01 -0700 | [diff] [blame] | 83 | # If alternate manifest is specified, override the manifest file that we're using. |
| 84 | if opt.manifest_name: |
| 85 | self.manifest.Override(opt.manifest_name, False) |
| 86 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 87 | for manifest in self.ManifestList(opt): |
| 88 | output_file = opt.output_file |
| 89 | if output_file == '-': |
| 90 | fd = sys.stdout |
| 91 | else: |
| 92 | if manifest.path_prefix: |
| 93 | output_file = f'{opt.output_file}:{manifest.path_prefix.replace("/", "%2f")}' |
| 94 | fd = open(output_file, 'w') |
Michael Kelly | c34b91c | 2021-07-02 09:25:48 -0700 | [diff] [blame] | 95 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 96 | manifest.SetUseLocalManifests(not opt.ignore_local_manifests) |
Michael Kelly | c34b91c | 2021-07-02 09:25:48 -0700 | [diff] [blame] | 97 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 98 | if opt.json: |
| 99 | print('warning: --json is experimental!', file=sys.stderr) |
| 100 | doc = manifest.ToDict(peg_rev=opt.peg_rev, |
| 101 | peg_rev_upstream=opt.peg_rev_upstream, |
| 102 | peg_rev_dest_branch=opt.peg_rev_dest_branch) |
Mike Frysinger | 23411d3 | 2020-09-02 04:31:10 -0400 | [diff] [blame] | 103 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 104 | json_settings = { |
| 105 | # JSON style guide says Uunicode characters are fully allowed. |
| 106 | 'ensure_ascii': False, |
| 107 | # We use 2 space indent to match JSON style guide. |
| 108 | 'indent': 2 if opt.pretty else None, |
| 109 | 'separators': (',', ': ') if opt.pretty else (',', ':'), |
| 110 | 'sort_keys': True, |
| 111 | } |
| 112 | fd.write(json.dumps(doc, **json_settings)) |
| 113 | else: |
| 114 | manifest.Save(fd, |
| 115 | peg_rev=opt.peg_rev, |
| 116 | peg_rev_upstream=opt.peg_rev_upstream, |
| 117 | peg_rev_dest_branch=opt.peg_rev_dest_branch) |
| 118 | if output_file != '-': |
| 119 | fd.close() |
| 120 | if manifest.path_prefix: |
| 121 | print(f'Saved {manifest.path_prefix} submanifest to {output_file}', |
| 122 | file=sys.stderr) |
| 123 | else: |
| 124 | print(f'Saved manifest to {output_file}', file=sys.stderr) |
| 125 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 126 | |
Mike Frysinger | ae6cb08 | 2019-08-27 01:10:59 -0400 | [diff] [blame] | 127 | def ValidateOptions(self, opt, args): |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 128 | if args: |
| 129 | self.Usage() |
| 130 | |
Mike Frysinger | ae6cb08 | 2019-08-27 01:10:59 -0400 | [diff] [blame] | 131 | def Execute(self, opt, args): |
| 132 | self._Output(opt) |