From 23411d3f9c3df36b68080cf457ca093f8f1c1f21 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Wed, 2 Sep 2020 04:31:10 -0400 Subject: manifest: add a --json output option Sometimes parsing JSON is easier than parsing XML, especially when the XML format is limited (which ours is). Add a --json option to the manifest command to quickly emit that form. Bug: https://crbug.com/gerrit/11743 Change-Id: Ia2bb254a78ae2b70a851638b4545fcafe8c1a76b Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/280436 Reviewed-by: Michael Mortensen Tested-by: Mike Frysinger --- subcmds/manifest.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'subcmds/manifest.py') diff --git a/subcmds/manifest.py b/subcmds/manifest.py index f0a0d069..0052d7aa 100644 --- a/subcmds/manifest.py +++ b/subcmds/manifest.py @@ -15,6 +15,8 @@ # limitations under the License. from __future__ import print_function + +import json import os import sys @@ -68,6 +70,10 @@ to indicate the remote ref to push changes to via 'repo upload'. help='If in -r mode, do not write the dest-branch field. ' 'Only of use if the branch names for a sha1 manifest are ' 'sensitive.') + p.add_option('--json', default=False, action='store_true', + help='Output manifest in JSON format (experimental).') + p.add_option('--pretty', default=False, action='store_true', + help='Format output for humans to read.') p.add_option('-o', '--output-file', dest='output_file', default='-', @@ -83,10 +89,26 @@ to indicate the remote ref to push changes to via 'repo upload'. fd = sys.stdout else: fd = open(opt.output_file, 'w') - self.manifest.Save(fd, - peg_rev=opt.peg_rev, - peg_rev_upstream=opt.peg_rev_upstream, - peg_rev_dest_branch=opt.peg_rev_dest_branch) + if opt.json: + print('warning: --json is experimental!', file=sys.stderr) + doc = self.manifest.ToDict(peg_rev=opt.peg_rev, + peg_rev_upstream=opt.peg_rev_upstream, + peg_rev_dest_branch=opt.peg_rev_dest_branch) + + json_settings = { + # JSON style guide says Uunicode characters are fully allowed. + 'ensure_ascii': False, + # We use 2 space indent to match JSON style guide. + 'indent': 2 if opt.pretty else None, + 'separators': (',', ': ') if opt.pretty else (',', ':'), + 'sort_keys': True, + } + fd.write(json.dumps(doc, **json_settings)) + else: + self.manifest.Save(fd, + peg_rev=opt.peg_rev, + peg_rev_upstream=opt.peg_rev_upstream, + peg_rev_dest_branch=opt.peg_rev_dest_branch) fd.close() if opt.output_file != '-': print('Saved manifest to %s' % opt.output_file, file=sys.stderr) -- cgit v1.2.3-54-g00ecf