Commit c2960246 authored by Edward Lesmes's avatar Edward Lesmes Committed by Commit Bot

gclient revinfo: Add an option to output a JSON file.

Add a --output-json flag to gclient revinfo so is easier for other
programs to process its output.

Bug: None
Change-Id: Ic120e7a279f3ed537ead624ab9bfd1f141485487
Reviewed-on: https://chromium-review.googlesource.com/952206
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
Reviewed-by: 's avatarAaron Gable <agable@chromium.org>
parent 8797cab3
......@@ -1377,20 +1377,9 @@ solutions = [
cache_dir = %(cache_dir)r
""")
DEFAULT_SNAPSHOT_SOLUTION_TEXT = ("""\
{ "name" : "%(solution_name)s",
"url" : "%(solution_url)s",
"deps_file" : "%(deps_file)s",
"managed" : %(managed)s,
"custom_deps" : {
%(solution_deps)s },
},
""")
DEFAULT_SNAPSHOT_FILE_TEXT = ("""\
# Snapshot generated with gclient revinfo --snapshot
solutions = [
%(solution_list)s]
solutions = %(solution_list)s
""")
def __init__(self, root_dir, options):
......@@ -1783,7 +1772,7 @@ it or fix the checkout.
return '%s@%s' % (url, scm.revinfo(self._options, [], None))
if self._options.snapshot:
new_gclient = ''
json_output = []
# First level at .gclient
for d in self.dependencies:
entries = {}
......@@ -1795,22 +1784,23 @@ it or fix the checkout.
entries[d.name] = rev
GrabDeps(d)
GrabDeps(d)
custom_deps = []
for k in sorted(entries.keys()):
if entries[k]:
# Quotes aren't escaped...
custom_deps.append(' \"%s\": \'%s\',\n' % (k, entries[k]))
else:
custom_deps.append(' \"%s\": None,\n' % k)
new_gclient += self.DEFAULT_SNAPSHOT_SOLUTION_TEXT % {
'solution_name': d.name,
json_output.append({
'name': d.name,
'solution_url': d.url,
'deps_file': d.deps_file,
'managed': d.managed,
'solution_deps': ''.join(custom_deps),
}
# Print the snapshot configuration file
print(self.DEFAULT_SNAPSHOT_FILE_TEXT % {'solution_list': new_gclient})
'custom_deps': entries,
})
if self._options.output_json == '-':
print(json.dumps(json_output, indent=2, separators=(',', ': ')))
elif self._options.output_json:
with open(self._options.output_json, 'w') as f:
json.dump(json_output, f)
else:
# Print the snapshot configuration file
print(self.DEFAULT_SNAPSHOT_FILE_TEXT % {
'solution_list': pprint.pformat(json_output, indent=2),
})
else:
entries = {}
for d in self.root.subtree(False):
......@@ -1820,9 +1810,23 @@ it or fix the checkout.
rev = d.parsed_url
if ShouldPrintRevision(d.name, rev):
entries[d.name] = rev
keys = sorted(entries.keys())
for x in keys:
print('%s: %s' % (x, entries[x]))
if self._options.output_json:
json_output = {
name: {
'url': rev.split('@')[0],
'rev': rev.split('@')[1] if '@' in rev else None,
}
for name, rev in entries.iteritems()
}
if self._options.output_json == '-':
print(json.dumps(json_output, indent=2, separators=(',', ': ')))
else:
with open(self._options.output_json, 'w') as f:
json.dump(json_output, f)
else:
keys = sorted(entries.keys())
for x in keys:
print('%s: %s' % (x, entries[x]))
logging.info(str(self))
def ParseDepsFile(self):
......@@ -2803,6 +2807,9 @@ def CMDrevinfo(parser, args):
parser.add_option('-p', '--path', action='append',
help='Display revision information only for the specified '
'paths.')
parser.add_option('--output-json',
help='Output a json document to this path containing '
'information about the revisions.')
(options, args) = parser.parse_args(args)
client = GClient.LoadCurrentConfig(options)
if not client:
......
......@@ -693,6 +693,58 @@ class GClientSmokeGIT(GClientSmokeBase):
})
self.check((out, '', 0), results)
def testRevInfoJsonOutput(self):
if not self.enabled:
return
self.gclient(['config', self.git_base + 'repo_1', '--name', 'src'])
self.gclient(['sync', '--deps', 'mac'])
output_json = os.path.join(self.root_dir, 'output.json')
self.gclient(['revinfo', '--deps', 'mac', '--output-json', output_json])
with open(output_json) as f:
output_json = json.load(f)
out = {
'src': {
'url': self.git_base + 'repo_1',
'rev': None,
},
'src/repo2': {
'url': self.git_base + 'repo_2',
'rev': self.githash('repo_2', 1)[:7],
},
'src/repo2/repo_renamed': {
'url': self.git_base + 'repo_3',
'rev': None,
},
}
self.assertEqual(out, output_json)
def testRevInfoJsonOutputSnapshot(self):
if not self.enabled:
return
self.gclient(['config', self.git_base + 'repo_1', '--name', 'src'])
self.gclient(['sync', '--deps', 'mac'])
output_json = os.path.join(self.root_dir, 'output.json')
self.gclient(['revinfo', '--deps', 'mac', '--snapshot',
'--output-json', output_json])
with open(output_json) as f:
output_json = json.load(f)
out = [{
'solution_url': self.git_base + 'repo_1',
'managed': True,
'name': 'src',
'deps_file': 'DEPS',
'custom_deps': {
'foo/bar': None,
'src/repo2': '%srepo_2@%s' % (
self.git_base, self.githash('repo_2', 1)),
u'src/repo2/repo_renamed': '%srepo_3@%s' % (
self.git_base, self.githash('repo_3', 2)),
},
}]
self.assertEqual(out, output_json)
def testFlatten(self):
if not self.enabled:
return
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment