OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Utilities for all our deps-management stuff.""" |
| 7 |
| 8 import hashlib |
| 9 import os |
| 10 import shutil |
| 11 import sys |
| 12 import tarfile |
| 13 import zipfile |
| 14 |
| 15 |
| 16 def ComputeSHA1(path): |
| 17 if not os.path.exists(path): |
| 18 return 0 |
| 19 |
| 20 sha1 = hashlib.sha1() |
| 21 file_to_hash = open(path, 'rb') |
| 22 try: |
| 23 sha1.update(file_to_hash.read()) |
| 24 finally: |
| 25 file_to_hash.close() |
| 26 |
| 27 return sha1.hexdigest() |
| 28 |
| 29 |
| 30 def DeleteDirNextToGclient(directory): |
| 31 # Sanity check to avoid nuking the wrong dirs. |
| 32 if not os.path.exists('.gclient'): |
| 33 raise Exception('Invoked from wrong dir; invoke from dir with .gclient') |
| 34 print 'Deleting %s in %s...' % (directory, os.getcwd()) |
| 35 shutil.rmtree(directory, ignore_errors=True) |
| 36 |
| 37 |
| 38 def UnpackToWorkingDir(archive_path): |
| 39 extension = os.path.splitext(archive_path)[1] |
| 40 if extension == '.zip': |
| 41 _Unzip(archive_path) |
| 42 else: |
| 43 _Untar(archive_path) |
| 44 |
| 45 |
| 46 def _Unzip(path): |
| 47 print 'Unzipping %s in %s...' % (path, os.getcwd()) |
| 48 zip_file = zipfile.ZipFile(path) |
| 49 try: |
| 50 zip_file.extractall() |
| 51 finally: |
| 52 zip_file.close() |
| 53 |
| 54 |
| 55 def _Untar(path): |
| 56 print 'Untarring %s in %s...' % (path, os.getcwd()) |
| 57 tar_file = tarfile.open(path, 'r:gz') |
| 58 try: |
| 59 tar_file.extractall() |
| 60 finally: |
| 61 tar_file.close() |
| 62 |
| 63 |
| 64 def GetPlatform(): |
| 65 if sys.platform.startswith('win'): |
| 66 return 'win' |
| 67 if sys.platform.startswith('linux'): |
| 68 return 'linux' |
| 69 if sys.platform.startswith('darwin'): |
| 70 return 'mac' |
| 71 raise Exception("Can't run on platform %s." % sys.platform) |
| 72 |
OLD | NEW |