blob: 0fbdeac0f635124520ceab0818fafa8f02211ce1 [file] [log] [blame]
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -08001# 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 Frysinger23411d32020-09-02 04:31:10 -040015import json
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080016import os
17import sys
18
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080019from command import PagedCommand
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080020
David Pursehouse819827a2020-02-12 15:20:19 +090021
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080022class Manifest(PagedCommand):
Mike Frysinger4f210542021-06-14 16:05:19 -040023 COMMON = False
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080024 helpSummary = "Manifest inspection utility"
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080025 helpUsage = """
Sean McAllisterd38300c2020-02-20 13:49:01 -070026%prog [-o {-|NAME.xml}] [-m MANIFEST.xml] [-r]
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080027"""
28 _helpDescription = """
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080029
30With the -o option, exports the current manifest for inspection.
Mike Frysinger4e1fc102020-09-06 14:42:47 -040031The manifest and (if present) local_manifests/ are combined
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080032together to produce a single manifest file. This file can be stored
33in a Git repository for use during future 'repo init' invocations.
34
Sean McAllister74e8ed42020-04-15 12:24:43 -060035The -r option can be used to generate a manifest file with project
36revisions set to the current commit hash. These are known as
37"revision locked manifests", as they don't follow a particular branch.
38In this case, the 'upstream' attribute is set to the ref we were on
Sean McAllisteraf908cb2020-04-20 08:41:58 -060039when the manifest was generated. The 'dest-branch' attribute is set
40to indicate the remote ref to push changes to via 'repo upload'.
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080041"""
42
43 @property
44 def helpDescription(self):
David Pursehouse8a68ff92012-09-24 12:15:13 +090045 helptext = self._helpDescription + '\n'
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080046 r = os.path.dirname(__file__)
47 r = os.path.dirname(r)
Mike Frysinger3164d402019-11-11 05:40:22 -050048 with open(os.path.join(r, 'docs', 'manifest-format.md')) as fd:
49 for line in fd:
50 helptext += line
David Pursehouse8a68ff92012-09-24 12:15:13 +090051 return helptext
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080052
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080053 def _Options(self, p):
54 p.add_option('-r', '--revision-as-HEAD',
55 dest='peg_rev', action='store_true',
Mike Frysingerc177f942021-05-04 08:06:36 -040056 help='save revisions as current HEAD')
Sean McAllisterd38300c2020-02-20 13:49:01 -070057 p.add_option('-m', '--manifest-name',
58 help='temporary manifest to use for this sync', metavar='NAME.xml')
Brian Harring14a66742012-09-28 20:21:57 -070059 p.add_option('--suppress-upstream-revision', dest='peg_rev_upstream',
60 default=True, action='store_false',
Mike Frysingerc177f942021-05-04 08:06:36 -040061 help='if in -r mode, do not write the upstream field '
62 '(only of use if the branch names for a sha1 manifest are '
63 'sensitive)')
Sean McAllisteraf908cb2020-04-20 08:41:58 -060064 p.add_option('--suppress-dest-branch', dest='peg_rev_dest_branch',
65 default=True, action='store_false',
Mike Frysingerc177f942021-05-04 08:06:36 -040066 help='if in -r mode, do not write the dest-branch field '
67 '(only of use if the branch names for a sha1 manifest are '
68 'sensitive)')
Mike Frysinger23411d32020-09-02 04:31:10 -040069 p.add_option('--json', default=False, action='store_true',
Mike Frysingerc177f942021-05-04 08:06:36 -040070 help='output manifest in JSON format (experimental)')
Mike Frysinger23411d32020-09-02 04:31:10 -040071 p.add_option('--pretty', default=False, action='store_true',
Mike Frysingerc177f942021-05-04 08:06:36 -040072 help='format output for humans to read')
Michael Kellyc34b91c2021-07-02 09:25:48 -070073 p.add_option('--no-local-manifests', default=False, action='store_true',
74 dest='ignore_local_manifests', help='ignore local manifests')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080075 p.add_option('-o', '--output-file',
76 dest='output_file',
Conley Owens918ff852012-08-07 10:44:01 -070077 default='-',
Mike Frysingerc177f942021-05-04 08:06:36 -040078 help='file to save the manifest to',
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080079 metavar='-|NAME.xml')
80
81 def _Output(self, opt):
Sean McAllisterd38300c2020-02-20 13:49:01 -070082 # If alternate manifest is specified, override the manifest file that we're using.
83 if opt.manifest_name:
84 self.manifest.Override(opt.manifest_name, False)
85
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080086 if opt.output_file == '-':
87 fd = sys.stdout
88 else:
89 fd = open(opt.output_file, 'w')
Michael Kellyc34b91c2021-07-02 09:25:48 -070090
91 self.manifest.SetUseLocalManifests(not opt.ignore_local_manifests)
92
Mike Frysinger23411d32020-09-02 04:31:10 -040093 if opt.json:
94 print('warning: --json is experimental!', file=sys.stderr)
95 doc = self.manifest.ToDict(peg_rev=opt.peg_rev,
96 peg_rev_upstream=opt.peg_rev_upstream,
97 peg_rev_dest_branch=opt.peg_rev_dest_branch)
98
99 json_settings = {
100 # JSON style guide says Uunicode characters are fully allowed.
101 'ensure_ascii': False,
102 # We use 2 space indent to match JSON style guide.
103 'indent': 2 if opt.pretty else None,
104 'separators': (',', ': ') if opt.pretty else (',', ':'),
105 'sort_keys': True,
106 }
107 fd.write(json.dumps(doc, **json_settings))
108 else:
109 self.manifest.Save(fd,
110 peg_rev=opt.peg_rev,
111 peg_rev_upstream=opt.peg_rev_upstream,
112 peg_rev_dest_branch=opt.peg_rev_dest_branch)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800113 fd.close()
114 if opt.output_file != '-':
Sarah Owenscecd1d82012-11-01 22:59:27 -0700115 print('Saved manifest to %s' % opt.output_file, file=sys.stderr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800116
Mike Frysingerae6cb082019-08-27 01:10:59 -0400117 def ValidateOptions(self, opt, args):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800118 if args:
119 self.Usage()
120
Mike Frysingerae6cb082019-08-27 01:10:59 -0400121 def Execute(self, opt, args):
122 self._Output(opt)