#!/usr/bin/env python
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import argparse
import os
import sys
import tempfile
import zipfile
from update_from_chromium import chromium_rev_number
from utils import commit
from utils import mojo_root_dir
from utils import system

sys.path.insert(0, os.path.join(mojo_root_dir, "tools"))
# pylint: disable=F0401
import gs

def roll(target_version):
  mojoms_gs_path = "gs://mojo/network/%s/mojoms.zip" % (target_version,)
  network_service_path = os.path.join(
      mojo_root_dir, "mojo", "services", "network")
  mojoms_path = os.path.join(network_service_path, "public", "interfaces")
  version_path = os.path.join(network_service_path, "VERSION")

  with tempfile.NamedTemporaryFile() as temp_zip_file:
    gs.download_from_public_bucket(mojoms_gs_path, temp_zip_file.name)
    with zipfile.ZipFile(temp_zip_file.name) as z:
      z.extractall(mojoms_path)

  with open(version_path, 'w') as stamp_file:
    stamp_file.write(target_version)

  system(["git", "add", "public"], cwd=network_service_path)
  system(["git", "add", "VERSION"], cwd=network_service_path)
  chromium_rev = chromium_rev_number(target_version)
  commit("Roll the network service to https://crrev.com/" + chromium_rev,
         cwd=mojo_root_dir)
  return 0

def main():
  parser = argparse.ArgumentParser(
      description="Update the pinned version of the network service " +
                  "and the corresponding checked out mojoms.")
  parser.add_argument("version", help="version to roll to")
  args = parser.parse_args()
  roll(args.version)
  return 0

if __name__ == "__main__":
  sys.exit(main())

@@ -231,16 +231,31 @@
 empfile%0A
+import urllib2%0A
 import z
@@ -261,16 +261,17 @@
 zipfile%0A
+%0A
 from upd
@@ -516,16 +516,303 @@
 rsion):%0A
+  try:%0A    chromium_rev = chromium_rev_number(target_version)%0A  except urllib2.HTTPError:%0A    print (%22Failed to identify a Chromium revision associated with %25s. %22%0A           %22Ensure that target_version is a Chromium origin/master %22%0A           %22commit.%22) %25 (target_version)%0A    return 1%0A%0A
   mojoms
@@ -1106,16 +1106,25 @@
 SION%22)%0A%0A
+  try:%0A  
   with t
@@ -1174,16 +1174,18 @@
 le:%0A    
+  
 gs.downl
@@ -1243,24 +1243,26 @@
 e.name)%0A    
+  
 with zipfile
@@ -1302,16 +1302,18 @@
 :%0A      
+ 
+ 
 z.extrac
@@ -1329,16 +1329,282 @@
 ms_path)
+%0A  # pylint: disable=C0302,bare-except%0A  except:%0A    print (%22Failed to download the mojom files associated with %25s. Ensure that %22%0A           %22the corresponding network service artifacts were uploaded to %22%0A           %22Google Storage.%22) %25 (target_version)%0A    return 1
 %0A%0A  with
@@ -1808,61 +1808,8 @@
 th)%0A
-  chromium_rev = chromium_rev_number(target_version)%0A
   co
@@ -2101,16 +2101,16 @@
 joms.%22)%0A
-
   parser
@@ -2158,16 +2158,91 @@
  roll to
+ (a Chromium %22%0A                                      %22origin/master commit)
 %22)%0A  arg

Add readable error messages in roll_network_service.