blob: 7379e168d1c4f91c47c21fe47a5c5acb5d2ecf34 [file] [log] [blame]
Mads Ager418d1ca2017-05-22 09:35:49 +02001# 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
7import hashlib
8import os
Tamas Kenez82efeb52017-06-12 13:56:22 +02009import re
Mads Ager418d1ca2017-05-22 09:35:49 +020010import shutil
11import subprocess
12import sys
13import tempfile
14
15TOOLS_DIR = os.path.abspath(os.path.normpath(os.path.join(__file__, '..')))
16REPO_ROOT = os.path.realpath(os.path.join(TOOLS_DIR, '..'))
17DOWNLOAD_DEPS = os.path.join(REPO_ROOT, 'scripts', 'download-deps.sh')
18
19def 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
26def 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
32def 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 Kenez971eec62017-05-24 11:08:40 +020042def makedirs_if_needed(path):
43 try:
44 os.makedirs(path)
45 except OSError:
46 if not os.path.isdir(path):
47 raise
48
Mads Ager418d1ca2017-05-22 09:35:49 +020049class 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
61class 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 Kenez82efeb52017-06-12 13:56:22 +020073
74# Reading Android CTS test_result.xml
75
76class CtsModule(object):
77 def __init__(self, module_name):
78 self.name = module_name
79
80class CtsTestCase(object):
81 def __init__(self, test_case_name):
82 self.name = test_case_name
83
84class 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.
91def 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')