aboutsummaryrefslogtreecommitdiffhomepage
path: root/infra/bots/zip_utils.py
diff options
context:
space:
mode:
authorGravatar borenet <borenet@chromium.org>2016-06-15 12:07:42 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2016-06-15 12:07:42 -0700
commit0f1469bcdad11cf8bfe79ace33d28052418ecb48 (patch)
tree5411db3cc9fecf963821f2d5a904565617d25ed0 /infra/bots/zip_utils.py
parent115e925dc85f3c6dbb140a2c1be3309ff72d3d8b (diff)
Add asset management scripts
These provide an easy way to create assets to be used by bots, eg. Android SDK. To create an asset: $ infra/bots/assets/assets.py add android_sdk (adds scripts in infra/bots/assets/android_sdk) To upload a new version of an asset: $ infra/bots/assets/android_sdk/upload.py -t $ANDROID_SDK_ROOT (uploads Android SDK to GS, writes a version file) $ git commit $ git cl upload To download the current version of the asset: $ infra/bots/assets/android_sdk/download.py -t ../tmp BUG=skia:5427 GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2069543002 Review-Url: https://codereview.chromium.org/2069543002
Diffstat (limited to 'infra/bots/zip_utils.py')
-rw-r--r--infra/bots/zip_utils.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/infra/bots/zip_utils.py b/infra/bots/zip_utils.py
new file mode 100644
index 0000000000..7f269b9e9b
--- /dev/null
+++ b/infra/bots/zip_utils.py
@@ -0,0 +1,61 @@
+#!/usr/bin/env python
+#
+# Copyright 2016 Google Inc.
+#
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+
+"""Utilities for zipping and unzipping files."""
+
+
+import fnmatch
+import os
+import zipfile
+
+
+def filtered(names, blacklist):
+ """Filter the list of file or directory names."""
+ rv = names[:]
+ for pattern in blacklist:
+ rv = [n for n in rv if not fnmatch.fnmatch(n, pattern)]
+ return rv
+
+
+def zip(target_dir, zip_file, blacklist=None): # pylint: disable=W0622
+ """Zip the given directory, write to the given zip file."""
+ if not os.path.isdir(target_dir):
+ raise IOError('%s does not exist!' % target_dir)
+ blacklist = blacklist or []
+ with zipfile.ZipFile(zip_file, 'w') as z:
+ for r, d, f in os.walk(target_dir, topdown=True):
+ d[:] = filtered(d, blacklist)
+ for filename in filtered(f, blacklist):
+ filepath = os.path.join(r, filename)
+ zi = zipfile.ZipInfo(filepath)
+ zi.filename = os.path.relpath(filepath, target_dir)
+ perms = os.stat(filepath).st_mode
+ zi.external_attr = perms << 16L
+ zi.compress_type = zipfile.ZIP_STORED
+ with open(filepath, 'rb') as f:
+ content = f.read()
+ z.writestr(zi, content)
+ for dirname in d:
+ dirpath = os.path.join(r, dirname)
+ z.write(dirpath, os.path.relpath(dirpath, target_dir))
+
+
+def unzip(zip_file, target_dir):
+ """Unzip the given zip file into the target dir."""
+ if not os.path.isdir(target_dir):
+ os.makedirs(target_dir)
+ with zipfile.ZipFile(zip_file, 'r') as z:
+ for zi in z.infolist():
+ dst_path = os.path.join(target_dir, zi.filename)
+ if zi.filename.endswith('/'):
+ os.mkdir(dst_path)
+ else:
+ with open(dst_path, 'w') as f:
+ f.write(z.read(zi))
+ perms = zi.external_attr >> 16L
+ os.chmod(dst_path, perms)