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, '..')) |
| 17 | DOWNLOAD_DEPS = os.path.join(REPO_ROOT, 'scripts', 'download-deps.sh') |
| 18 | |
| 19 | def PrintCmd(s): |
| 20 | if type(s) is list: |
| 21 | s = ' '.join(s) |
| 22 | print 'Running: %s' % s |
| 23 | # I know this will hit os on windows eventually if we don't do this. |
| 24 | sys.stdout.flush() |
| 25 | |
| 26 | def DownloadFromGoogleCloudStorage(sha1_file, bucket='r8-deps'): |
| 27 | cmd = ["download_from_google_storage", "-n", "-b", bucket, "-u", "-s", |
| 28 | sha1_file] |
| 29 | PrintCmd(cmd) |
| 30 | subprocess.check_call(cmd) |
| 31 | |
| 32 | def get_sha1(filename): |
| 33 | sha1 = hashlib.sha1() |
| 34 | with open(filename, 'rb') as f: |
| 35 | while True: |
| 36 | chunk = f.read(1024*1024) |
| 37 | if not chunk: |
| 38 | break |
| 39 | sha1.update(chunk) |
| 40 | return sha1.hexdigest() |
| 41 | |
Tamas Kenez | 971eec6 | 2017-05-24 11:08:40 +0200 | [diff] [blame] | 42 | def makedirs_if_needed(path): |
| 43 | try: |
| 44 | os.makedirs(path) |
| 45 | except OSError: |
| 46 | if not os.path.isdir(path): |
| 47 | raise |
| 48 | |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 49 | class TempDir(object): |
| 50 | def __init__(self, prefix=''): |
| 51 | self._temp_dir = None |
| 52 | self._prefix = prefix |
| 53 | |
| 54 | def __enter__(self): |
| 55 | self._temp_dir = tempfile.mkdtemp(self._prefix) |
| 56 | return self._temp_dir |
| 57 | |
| 58 | def __exit__(self, *_): |
| 59 | shutil.rmtree(self._temp_dir, ignore_errors=True) |
| 60 | |
| 61 | class ChangedWorkingDirectory(object): |
| 62 | def __init__(self, working_directory): |
| 63 | self._working_directory = working_directory |
| 64 | |
| 65 | def __enter__(self): |
| 66 | self._old_cwd = os.getcwd() |
| 67 | print "Enter directory = ", self._working_directory |
| 68 | os.chdir(self._working_directory) |
| 69 | |
| 70 | def __exit__(self, *_): |
| 71 | print "Enter directory = ", self._old_cwd |
| 72 | os.chdir(self._old_cwd) |
Tamas Kenez | 82efeb5 | 2017-06-12 13:56:22 +0200 | [diff] [blame] | 73 | |
| 74 | # Reading Android CTS test_result.xml |
| 75 | |
| 76 | class CtsModule(object): |
| 77 | def __init__(self, module_name): |
| 78 | self.name = module_name |
| 79 | |
| 80 | class CtsTestCase(object): |
| 81 | def __init__(self, test_case_name): |
| 82 | self.name = test_case_name |
| 83 | |
| 84 | class CtsTest(object): |
| 85 | def __init__(self, test_name, outcome): |
| 86 | self.name = test_name |
| 87 | self.outcome = outcome |
| 88 | |
| 89 | # Generator yielding CtsModule, CtsTestCase or CtsTest from |
| 90 | # reading through a CTS test_result.xml file. |
| 91 | def read_cts_test_result(file_xml): |
| 92 | re_module = re.compile('<Module name="([^"]*)"') |
| 93 | re_test_case = re.compile('<TestCase name="([^"]*)"') |
| 94 | re_test = re.compile('<Test result="(pass|fail)" name="([^"]*)"') |
| 95 | with open(file_xml) as f: |
| 96 | for line in f: |
| 97 | m = re_module.search(line) |
| 98 | if m: |
| 99 | yield CtsModule(m.groups()[0]) |
| 100 | continue |
| 101 | m = re_test_case.search(line) |
| 102 | if m: |
| 103 | yield CtsTestCase(m.groups()[0]) |
| 104 | continue |
| 105 | m = re_test.search(line) |
| 106 | if m: |
| 107 | outcome = m.groups()[0] |
| 108 | assert outcome in ["fail", "pass"] |
| 109 | yield CtsTest(m.groups()[1], outcome == 'pass') |