Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 1 | # Copyright (c) 2016, the R8 project authors. Please see the AUTHORS file |
| 2 | # for details. All rights reserved. Use of this source code is governed by a |
| 3 | # BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | # Different utility functions used accross scripts |
| 6 | |
| 7 | import hashlib |
| 8 | import os |
Tamas Kenez | 82efeb5 | 2017-06-12 13:56:22 +0200 | [diff] [blame] | 9 | import re |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 10 | import shutil |
| 11 | import subprocess |
| 12 | import sys |
| 13 | import tempfile |
| 14 | |
| 15 | TOOLS_DIR = os.path.abspath(os.path.normpath(os.path.join(__file__, '..'))) |
| 16 | REPO_ROOT = os.path.realpath(os.path.join(TOOLS_DIR, '..')) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 17 | |
| 18 | def PrintCmd(s): |
| 19 | if type(s) is list: |
| 20 | s = ' '.join(s) |
| 21 | print 'Running: %s' % s |
| 22 | # I know this will hit os on windows eventually if we don't do this. |
| 23 | sys.stdout.flush() |
| 24 | |
Rico Wind | f80f5a2 | 2017-06-16 09:15:57 +0200 | [diff] [blame] | 25 | def IsWindows(): |
| 26 | return os.name == 'nt' |
| 27 | |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 28 | def DownloadFromGoogleCloudStorage(sha1_file, bucket='r8-deps'): |
Rico Wind | f80f5a2 | 2017-06-16 09:15:57 +0200 | [diff] [blame] | 29 | suffix = '.bat' if IsWindows() else '' |
| 30 | download_script = 'download_from_google_storage%s' % suffix |
| 31 | cmd = [download_script, '-n', '-b', bucket, '-u', '-s', |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 32 | sha1_file] |
| 33 | PrintCmd(cmd) |
| 34 | subprocess.check_call(cmd) |
| 35 | |
| 36 | def get_sha1(filename): |
| 37 | sha1 = hashlib.sha1() |
| 38 | with open(filename, 'rb') as f: |
| 39 | while True: |
| 40 | chunk = f.read(1024*1024) |
| 41 | if not chunk: |
| 42 | break |
| 43 | sha1.update(chunk) |
| 44 | return sha1.hexdigest() |
| 45 | |
Tamas Kenez | 971eec6 | 2017-05-24 11:08:40 +0200 | [diff] [blame] | 46 | def makedirs_if_needed(path): |
| 47 | try: |
| 48 | os.makedirs(path) |
| 49 | except OSError: |
| 50 | if not os.path.isdir(path): |
| 51 | raise |
| 52 | |
Rico Wind | a94f01c | 2017-06-27 10:32:34 +0200 | [diff] [blame^] | 53 | def upload_html_to_cloud_storage(directory, destination): |
| 54 | # Upload and make the content encoding right for viewing directly |
| 55 | cmd = ['gsutil.py', 'cp', '-z', 'html', '-a', |
| 56 | 'public-read', '-R', directory, destination] |
| 57 | PrintCmd(cmd) |
| 58 | subprocess.check_call(cmd) |
| 59 | |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 60 | class TempDir(object): |
| 61 | def __init__(self, prefix=''): |
| 62 | self._temp_dir = None |
| 63 | self._prefix = prefix |
| 64 | |
| 65 | def __enter__(self): |
| 66 | self._temp_dir = tempfile.mkdtemp(self._prefix) |
| 67 | return self._temp_dir |
| 68 | |
| 69 | def __exit__(self, *_): |
| 70 | shutil.rmtree(self._temp_dir, ignore_errors=True) |
| 71 | |
| 72 | class ChangedWorkingDirectory(object): |
| 73 | def __init__(self, working_directory): |
| 74 | self._working_directory = working_directory |
| 75 | |
| 76 | def __enter__(self): |
| 77 | self._old_cwd = os.getcwd() |
Rico Wind | f80f5a2 | 2017-06-16 09:15:57 +0200 | [diff] [blame] | 78 | print 'Enter directory = ', self._working_directory |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 79 | os.chdir(self._working_directory) |
| 80 | |
| 81 | def __exit__(self, *_): |
Rico Wind | f80f5a2 | 2017-06-16 09:15:57 +0200 | [diff] [blame] | 82 | print 'Enter directory = ', self._old_cwd |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 83 | os.chdir(self._old_cwd) |
Tamas Kenez | 82efeb5 | 2017-06-12 13:56:22 +0200 | [diff] [blame] | 84 | |
| 85 | # Reading Android CTS test_result.xml |
| 86 | |
| 87 | class CtsModule(object): |
| 88 | def __init__(self, module_name): |
| 89 | self.name = module_name |
| 90 | |
| 91 | class CtsTestCase(object): |
| 92 | def __init__(self, test_case_name): |
| 93 | self.name = test_case_name |
| 94 | |
| 95 | class CtsTest(object): |
| 96 | def __init__(self, test_name, outcome): |
| 97 | self.name = test_name |
| 98 | self.outcome = outcome |
| 99 | |
| 100 | # Generator yielding CtsModule, CtsTestCase or CtsTest from |
| 101 | # reading through a CTS test_result.xml file. |
| 102 | def read_cts_test_result(file_xml): |
| 103 | re_module = re.compile('<Module name="([^"]*)"') |
| 104 | re_test_case = re.compile('<TestCase name="([^"]*)"') |
| 105 | re_test = re.compile('<Test result="(pass|fail)" name="([^"]*)"') |
| 106 | with open(file_xml) as f: |
| 107 | for line in f: |
| 108 | m = re_module.search(line) |
| 109 | if m: |
| 110 | yield CtsModule(m.groups()[0]) |
| 111 | continue |
| 112 | m = re_test_case.search(line) |
| 113 | if m: |
| 114 | yield CtsTestCase(m.groups()[0]) |
| 115 | continue |
| 116 | m = re_test.search(line) |
| 117 | if m: |
| 118 | outcome = m.groups()[0] |
Rico Wind | f80f5a2 | 2017-06-16 09:15:57 +0200 | [diff] [blame] | 119 | assert outcome in ['fail', 'pass'] |
Tamas Kenez | 82efeb5 | 2017-06-12 13:56:22 +0200 | [diff] [blame] | 120 | yield CtsTest(m.groups()[1], outcome == 'pass') |