Index: client/cipd.py |
diff --git a/client/cipd.py b/client/cipd.py |
index 8881ef2ecad75d22fd150ea8733fd230e25d6854..b2d124cb65b6ee4b41d981d3c45f47ece9da6e26 100644 |
--- a/client/cipd.py |
+++ b/client/cipd.py |
@@ -9,6 +9,7 @@ __version__ = '0.2' |
import collections |
import contextlib |
import hashlib |
+import json |
import logging |
import optparse |
import os |
@@ -129,6 +130,10 @@ class CipdClient(object): |
tmp_dir (str): if not None, dir for temp files. |
timeout (int): if not None, timeout in seconds for this function to run. |
+ Returns: |
+ Pinned packages in the form of [(package_name, package_id)], which |
+ correspond 1:1 with the input packages argument. |
+ |
Raises: |
Error if could not install packages or timed out. |
""" |
@@ -137,6 +142,10 @@ class CipdClient(object): |
list_file_handle, list_file_path = tempfile.mkstemp( |
dir=tmp_dir, prefix=u'cipd-ensure-list-', suffix='.txt') |
+ json_out_file_handle, json_file_path = tempfile.mkstemp( |
+ dir=tmp_dir, prefix=u'cipd-ensure-result-', suffix='.json') |
+ os.close(json_out_file_handle) |
+ |
try: |
try: |
for pkg, version in packages: |
@@ -150,6 +159,7 @@ class CipdClient(object): |
'-root', site_root, |
'-list', list_file_path, |
'-verbose', # this is safe because cipd-ensure does not print a lot |
+ '-json-output', json_file_path, |
] |
if cache_dir: |
cmd += ['-cache-dir', cache_dir] |
@@ -180,8 +190,12 @@ class CipdClient(object): |
raise Error( |
'Could not install packages; exit code %d\noutput:%s' % ( |
exit_code, '\n'.join(output))) |
+ with open(json_file_path) as jfile: |
+ result_json = json.load(jfile) |
+ return [(x['package'], x['instance_id']) for x in result_json['result']] |
finally: |
fs.remove(list_file_path) |
+ fs.remove(json_file_path) |
def get_platform(): |
@@ -402,21 +416,26 @@ def get_client( |
yield CipdClient(binary_path) |
-def parse_package_args(packages): |
+def parse_package_args(packages, with_index=False): |
"""Parses --cipd-package arguments. |
Assumes |packages| were validated by validate_cipd_options. |
Returns: |
- A map {path: [(package, version)]}. |
+ A map {path: [(package, version)]}. If with_index is True, the tuples in the |
+ map have a third parameter which is the original command line index of that |
+ pin. |
""" |
result = collections.defaultdict(list) |
- for pkg in packages: |
+ for i, pkg in enumerate(packages): |
path, name, version = pkg.split(':', 2) |
path = path.replace('/', os.path.sep) |
if not name: |
raise Error('Invalid package "%s": package name is not specified' % pkg) |
if not version: |
raise Error('Invalid package "%s": version is not specified' % pkg) |
- result[path].append((name, version)) |
+ if with_index: |
+ result[path].append((name, version, i)) |
+ else: |
+ result[path].append((name, version)) |
return result |