Use CIPD JSON files

Read CIPD info from JSON files instead of ensure files. They're safer
to parse and will simplify automatic rolling of some CIPD packages.

Change-Id: I88d3f121a42427ecc73174885bbac3285e08d53f
Bug: 103
diff --git a/recipe_modules/environment/api.py b/recipe_modules/environment/api.py
index b31dbb4..6366c3d 100644
--- a/recipe_modules/environment/api.py
+++ b/recipe_modules/environment/api.py
@@ -40,33 +40,23 @@
     self._env = {}
     self._initialized = False
 
-  def _process_ensure_path(self, ensure_path, ensure_file):
-    """Add packages from ensure_path to ensure_file for later installation."""
-
-    ensure_text = self.m.file.read_text(
-        'read {}'.format(self.m.path.basename(ensure_path)), ensure_path)
-    packages = []
-    for line in ensure_text.splitlines():
-      line = line.strip()
-      if not line or line[0] in '$#':
-        continue
-      packages.append(Package(*line.split()))
-
-    for package in packages:
-      ensure_file.add_package(package.name, package.version)
-
   def _init_cipd(self, checkout_root):
     """Install CIPD packages."""
 
     with self.m.step.nest('setup cipd'):
-      ensure_paths = [
-          checkout_root.join('env_setup/cipd/pigweed.ensure'),
+      json_paths = [
+          checkout_root.join('env_setup/cipd/pigweed.json'),
           # TODO(mohrr) handle multiple files in non-predetermined places
       ]
 
       ensure_file = self.m.cipd.EnsureFile()
-      for ensure_path in ensure_paths:
-        self._process_ensure_path(ensure_path, ensure_file)
+      for json_path in json_paths:
+        packages = self.m.file.read_json(
+            'read {}'.format(self.m.path.basename(json_path)), json_path)
+        for pkg in packages:
+          # JSON files are read as unicode, need to encode to get str.
+          ensure_file.add_package(pkg['path'].encode(),
+                                  ' '.join(x.encode() for x in pkg['tags']))
 
       self._cipd_dir = self.m.path['start_dir'].join('cipd')
       self.m.cipd.max_threads = 0
diff --git a/recipe_modules/environment/test_api.py b/recipe_modules/environment/test_api.py
index accc40e..26f62e6 100644
--- a/recipe_modules/environment/test_api.py
+++ b/recipe_modules/environment/test_api.py
@@ -21,11 +21,10 @@
 
   def cipd_test_data(self):
     return self.step_data(
-        'environment.setup cipd.read pigweed.ensure',
-        self.m.file.read_text("""
-        # comment
-        cipd/path/${platform} version:42
-        """))
+        'environment.setup cipd.read pigweed.json',
+        self.m.file.read_json([
+            {'path': 'cipd/path/${platform}', 'tags': ['version:42']},
+        ]))
 
   def virtualenv_test_data(self):
     return self.step_data(
diff --git a/recipe_modules/environment/tests/full.expected/simple.json b/recipe_modules/environment/tests/full.expected/simple.json
index 8652199..ccb19fb 100644
--- a/recipe_modules/environment/tests/full.expected/simple.json
+++ b/recipe_modules/environment/tests/full.expected/simple.json
@@ -18,18 +18,15 @@
       "--json-output",
       "/path/to/tmp/json",
       "copy",
-      "[START_DIR]/env_setup/cipd/pigweed.ensure",
+      "[START_DIR]/env_setup/cipd/pigweed.json",
       "/path/to/tmp/"
     ],
     "infra_step": true,
-    "name": "environment.setup cipd.read pigweed.ensure",
+    "name": "environment.setup cipd.read pigweed.json",
     "~followup_annotations": [
       "@@@STEP_NEST_LEVEL@2@@@",
-      "@@@STEP_LOG_LINE@pigweed.ensure@@@@",
-      "@@@STEP_LOG_LINE@pigweed.ensure@        # comment@@@",
-      "@@@STEP_LOG_LINE@pigweed.ensure@        cipd/path/${platform} version:42@@@",
-      "@@@STEP_LOG_LINE@pigweed.ensure@        @@@",
-      "@@@STEP_LOG_END@pigweed.ensure@@@"
+      "@@@STEP_LOG_LINE@pigweed.json@[{\"path\": \"cipd/path/${platform}\", \"tags\": [\"version:42\"]}]@@@",
+      "@@@STEP_LOG_END@pigweed.json@@@"
     ]
   },
   {
diff --git a/recipes/pigweed.expected/pigweed.json b/recipes/pigweed.expected/pigweed.json
index b5e4799..87f8abe 100644
--- a/recipes/pigweed.expected/pigweed.json
+++ b/recipes/pigweed.expected/pigweed.json
@@ -295,18 +295,15 @@
       "--json-output",
       "/path/to/tmp/json",
       "copy",
-      "[START_DIR]/checkout/env_setup/cipd/pigweed.ensure",
+      "[START_DIR]/checkout/env_setup/cipd/pigweed.json",
       "/path/to/tmp/"
     ],
     "infra_step": true,
-    "name": "environment.setup cipd.read pigweed.ensure",
+    "name": "environment.setup cipd.read pigweed.json",
     "~followup_annotations": [
       "@@@STEP_NEST_LEVEL@2@@@",
-      "@@@STEP_LOG_LINE@pigweed.ensure@@@@",
-      "@@@STEP_LOG_LINE@pigweed.ensure@        # comment@@@",
-      "@@@STEP_LOG_LINE@pigweed.ensure@        cipd/path/${platform} version:42@@@",
-      "@@@STEP_LOG_LINE@pigweed.ensure@        @@@",
-      "@@@STEP_LOG_END@pigweed.ensure@@@"
+      "@@@STEP_LOG_LINE@pigweed.json@[{\"path\": \"cipd/path/${platform}\", \"tags\": [\"version:42\"]}]@@@",
+      "@@@STEP_LOG_END@pigweed.json@@@"
     ]
   },
   {
diff --git a/recipes/pigweed.expected/step.json b/recipes/pigweed.expected/step.json
index 421a561..ea7cb28 100644
--- a/recipes/pigweed.expected/step.json
+++ b/recipes/pigweed.expected/step.json
@@ -575,18 +575,15 @@
       "--json-output",
       "/path/to/tmp/json",
       "copy",
-      "[START_DIR]/checkout/env_setup/cipd/pigweed.ensure",
+      "[START_DIR]/checkout/env_setup/cipd/pigweed.json",
       "/path/to/tmp/"
     ],
     "infra_step": true,
-    "name": "environment.setup cipd.read pigweed.ensure",
+    "name": "environment.setup cipd.read pigweed.json",
     "~followup_annotations": [
       "@@@STEP_NEST_LEVEL@2@@@",
-      "@@@STEP_LOG_LINE@pigweed.ensure@@@@",
-      "@@@STEP_LOG_LINE@pigweed.ensure@        # comment@@@",
-      "@@@STEP_LOG_LINE@pigweed.ensure@        cipd/path/${platform} version:42@@@",
-      "@@@STEP_LOG_LINE@pigweed.ensure@        @@@",
-      "@@@STEP_LOG_END@pigweed.ensure@@@"
+      "@@@STEP_LOG_LINE@pigweed.json@[{\"path\": \"cipd/path/${platform}\", \"tags\": [\"version:42\"]}]@@@",
+      "@@@STEP_LOG_END@pigweed.json@@@"
     ]
   },
   {