blob: 0052d7aa86e9d894a9c54339165957b902c07471 [file] [log] [blame]
Mike Frysingerf6013762019-06-13 02:30:51 -04001# -*- coding:utf-8 -*-
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -08002#
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
Sarah Owenscecd1d82012-11-01 22:59:27 -070017from __future__ import print_function
Mike Frysinger23411d32020-09-02 04:31:10 -040018
19import json
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080020import os
21import sys
22
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080023from command import PagedCommand
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080024
David Pursehouse819827a2020-02-12 15:20:19 +090025
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080026class Manifest(PagedCommand):
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080027 common = False
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080028 helpSummary = "Manifest inspection utility"
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080029 helpUsage = """
Sean McAllisterd38300c2020-02-20 13:49:01 -070030%prog [-o {-|NAME.xml}] [-m MANIFEST.xml] [-r]
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080031"""
32 _helpDescription = """
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080033
34With the -o option, exports the current manifest for inspection.
Mike Frysinger4e1fc102020-09-06 14:42:47 -040035The manifest and (if present) local_manifests/ are combined
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080036together to produce a single manifest file. This file can be stored
37in a Git repository for use during future 'repo init' invocations.
38
Sean McAllister74e8ed42020-04-15 12:24:43 -060039The -r option can be used to generate a manifest file with project
40revisions set to the current commit hash. These are known as
41"revision locked manifests", as they don't follow a particular branch.
42In this case, the 'upstream' attribute is set to the ref we were on
Sean McAllisteraf908cb2020-04-20 08:41:58 -060043when the manifest was generated. The 'dest-branch' attribute is set
44to indicate the remote ref to push changes to via 'repo upload'.
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080045"""
46
47 @property
48 def helpDescription(self):
David Pursehouse8a68ff92012-09-24 12:15:13 +090049 helptext = self._helpDescription + '\n'
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080050 r = os.path.dirname(__file__)
51 r = os.path.dirname(r)
Mike Frysinger3164d402019-11-11 05:40:22 -050052 with open(os.path.join(r, 'docs', 'manifest-format.md')) as fd:
53 for line in fd:
54 helptext += line
David Pursehouse8a68ff92012-09-24 12:15:13 +090055 return helptext
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080056
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080057 def _Options(self, p):
58 p.add_option('-r', '--revision-as-HEAD',
59 dest='peg_rev', action='store_true',
60 help='Save revisions as current HEAD')
Sean McAllisterd38300c2020-02-20 13:49:01 -070061 p.add_option('-m', '--manifest-name',
62 help='temporary manifest to use for this sync', metavar='NAME.xml')
Brian Harring14a66742012-09-28 20:21:57 -070063 p.add_option('--suppress-upstream-revision', dest='peg_rev_upstream',
64 default=True, action='store_false',
65 help='If in -r mode, do not write the upstream field. '
66 'Only of use if the branch names for a sha1 manifest are '
67 'sensitive.')
Sean McAllisteraf908cb2020-04-20 08:41:58 -060068 p.add_option('--suppress-dest-branch', dest='peg_rev_dest_branch',
69 default=True, action='store_false',
70 help='If in -r mode, do not write the dest-branch field. '
71 'Only of use if the branch names for a sha1 manifest are '
72 'sensitive.')
Mike Frysinger23411d32020-09-02 04:31:10 -040073 p.add_option('--json', default=False, action='store_true',
74 help='Output manifest in JSON format (experimental).')
75 p.add_option('--pretty', default=False, action='store_true',
76 help='Format output for humans to read.')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080077 p.add_option('-o', '--output-file',
78 dest='output_file',
Conley Owens918ff852012-08-07 10:44:01 -070079 default='-',
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080080 help='File to save the manifest to',
81 metavar='-|NAME.xml')
82
83 def _Output(self, opt):
Sean McAllisterd38300c2020-02-20 13:49:01 -070084 # If alternate manifest is specified, override the manifest file that we're using.
85 if opt.manifest_name:
86 self.manifest.Override(opt.manifest_name, False)
87
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080088 if opt.output_file == '-':
89 fd = sys.stdout
90 else:
91 fd = open(opt.output_file, 'w')
Mike Frysinger23411d32020-09-02 04:31:10 -040092 if opt.json:
93 print('warning: --json is experimental!', file=sys.stderr)
94 doc = self.manifest.ToDict(peg_rev=opt.peg_rev,
95 peg_rev_upstream=opt.peg_rev_upstream,
96 peg_rev_dest_branch=opt.peg_rev_dest_branch)
97
98 json_settings = {
99 # JSON style guide says Uunicode characters are fully allowed.
100 'ensure_ascii': False,
101 # We use 2 space indent to match JSON style guide.
102 'indent': 2 if opt.pretty else None,
103 'separators': (',', ': ') if opt.pretty else (',', ':'),
104 'sort_keys': True,
105 }
106 fd.write(json.dumps(doc, **json_settings))
107 else:
108 self.manifest.Save(fd,
109 peg_rev=opt.peg_rev,
110 peg_rev_upstream=opt.peg_rev_upstream,
111 peg_rev_dest_branch=opt.peg_rev_dest_branch)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800112 fd.close()
113 if opt.output_file != '-':
Sarah Owenscecd1d82012-11-01 22:59:27 -0700114 print('Saved manifest to %s' % opt.output_file, file=sys.stderr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800115
Mike Frysingerae6cb082019-08-27 01:10:59 -0400116 def ValidateOptions(self, opt, args):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800117 if args:
118 self.Usage()
119
Mike Frysingerae6cb082019-08-27 01:10:59 -0400120 def Execute(self, opt, args):
121 self._Output(opt)