Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # | |
| 3 # Copyright 2016 Google Inc. | |
| 4 # | |
| 5 # Use of this source code is governed by a BSD-style license that can be | |
| 6 # found in the LICENSE file. | |
| 7 | |
| 8 | |
| 9 """Utilities for managing assets.""" | |
| 10 | |
| 11 | |
| 12 import argparse | |
| 13 import os | |
| 14 import shlex | |
| 15 import subprocess | |
| 16 import sys | |
| 17 | |
| 18 SKIA_DIR = os.path.abspath(os.path.realpath(os.path.join( | |
| 19 os.path.dirname(os.path.abspath(__file__)), | |
| 20 os.pardir, os.pardir, os.pardir))) | |
| 21 INFRA_BOTS_DIR = os.path.join(SKIA_DIR, 'infra', 'bots') | |
| 22 sys.path.insert(0, INFRA_BOTS_DIR) | |
| 23 import utils | |
| 24 import zip_utils | |
| 25 | |
| 26 | |
| 27 DEFAULT_GS_BUCKET = 'skia-buildbots' | |
| 28 GS_SUBDIR_TMPL = 'gs://%s/assets/%s' | |
| 29 GS_PATH_TMPL = '%s/%s.zip' | |
| 30 VERSION_FILENAME = 'VERSION' | |
| 31 ZIP_BLACKLIST = ['.git', '.svn', '*.pyc', '.DS_STORE'] | |
| 32 | |
| 33 | |
| 34 class _GSWrapper(object): | |
| 35 """Wrapper object for interacting with Google Storage.""" | |
| 36 def __init__(self, gsutil): | |
| 37 gsutil = os.path.abspath(gsutil) if gsutil else 'gsutil' | |
| 38 self._gsutil = [gsutil] | |
| 39 if gsutil.endswith('.py'): | |
| 40 self._gsutil = ['python', gsutil] | |
| 41 | |
| 42 def copy(self, src, dst): | |
| 43 """Copy src to dst.""" | |
| 44 subprocess.check_call(self._gsutil + ['cp', src, dst]) | |
| 45 | |
| 46 def list(self, path): | |
| 47 """List objects in the given path.""" | |
| 48 try: | |
| 49 return subprocess.check_output(self._gsutil + ['ls', path]).splitlines() | |
| 50 except subprocess.CalledProcessError: | |
| 51 # If the prefix does not exist, we'll get an error, which is okay. | |
| 52 return [] | |
| 53 | |
| 54 | |
| 55 class Asset(object): | |
| 56 def __init__(self, name, gs_bucket, gsutil=None): | |
| 57 self._gs = _GSWrapper(gsutil) | |
| 58 self._gs_subdir = GS_SUBDIR_TMPL % (gs_bucket, name) | |
| 59 self._name = name | |
| 60 | |
| 61 @property | |
| 62 def version_file(self): | |
| 63 """Return the path to the version file for this asset.""" | |
| 64 return os.path.join(SKIA_DIR, 'infra', 'bots', 'assets', self._name, | |
| 65 VERSION_FILENAME) | |
| 66 | |
| 67 def get_current_version(self): | |
| 68 """Obtain the current version of the asset.""" | |
| 69 with open(self.version_file) as f: | |
| 70 return int(f.read()) | |
| 71 | |
| 72 def get_available_versions(self): | |
| 73 """Return the existing version numbers for this asset.""" | |
| 74 files = self._gs.list(self._gs_subdir) | |
| 75 bnames = [os.path.basename(f) for f in files] | |
| 76 suffix = '.zip' | |
| 77 versions = [int(f[:-len(suffix)]) for f in bnames if f.endswith(suffix)] | |
| 78 versions.sort() | |
| 79 return versions | |
| 80 | |
| 81 def get_next_version(self): | |
| 82 """Find the next available version number for the asset.""" | |
| 83 versions = self.get_available_versions() | |
| 84 if len(versions) == 0: | |
| 85 return 0 | |
| 86 return versions[-1] + 1 | |
| 87 | |
| 88 def download_version(self, version, target_dir): | |
| 89 """Download the specified version of the asset.""" | |
| 90 gs_path = GS_PATH_TMPL % (self._gs_subdir, str(version)) | |
| 91 target_dir = os.path.abspath(target_dir) | |
| 92 with utils.tmp_dir(): | |
| 93 zip_file = os.path.join(os.getcwd(), '%d.zip' % version) | |
| 94 self._gs.copy(gs_path, zip_file) | |
| 95 zip_utils.unzip(zip_file, target_dir) | |
| 96 | |
| 97 def download_current_version(self, target_dir): | |
| 98 """Download the version of the asset specified in its version file.""" | |
| 99 v = self.get_current_version() | |
| 100 self.download_version(v, target_dir) | |
| 101 | |
| 102 def upload_new_version(self, target_dir, commit=False): | |
| 103 """Upload a new version and update the version file for the asset.""" | |
| 104 version = self.get_next_version() | |
| 105 target_dir = os.path.abspath(target_dir) | |
| 106 with utils.tmp_dir(): | |
| 107 zip_file = os.path.join(os.getcwd(), '%d.zip' % version) | |
| 108 zip_utils.zip(target_dir, zip_file, blacklist=ZIP_BLACKLIST) | |
| 109 gs_path = GS_PATH_TMPL % (self._gs_subdir, str(version)) | |
| 110 self._gs.copy(zip_file, gs_path) | |
| 111 | |
| 112 def _write_version(): | |
| 113 with open(self.version_file, 'w') as f: | |
| 114 f.write(str(version)) | |
| 115 subprocess.check_call([utils.GIT, 'add', self.version_file]) | |
| 116 | |
| 117 with utils.chdir(SKIA_DIR): | |
|
rmistry
2016/06/15 13:47:18
Does this have to go to SKIA_DIR for the below to
borenet
2016/06/15 14:31:27
I change to SKIA_DIR for the git commit stuff, jus
| |
| 118 if commit: | |
| 119 with utils.git_branch(): | |
| 120 _write_version() | |
| 121 subprocess.check_call([ | |
| 122 utils.GIT, 'commit', '-m', 'Update %s version' % self._name]) | |
| 123 subprocess.check_call([utils.GIT, 'cl', 'upload', '--bypass-hooks']) | |
| 124 else: | |
| 125 _write_version() | |
| OLD | NEW |