blob: fd7212bd72cd7dc91047c18ec73eb7e0ff17c1af [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, '..'))
Mads Ager418d1ca2017-05-22 09:35:49 +020017
18def 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 Windf80f5a22017-06-16 09:15:57 +020025def IsWindows():
26 return os.name == 'nt'
27
Mads Ager418d1ca2017-05-22 09:35:49 +020028def DownloadFromGoogleCloudStorage(sha1_file, bucket='r8-deps'):
Rico Windf80f5a22017-06-16 09:15:57 +020029 suffix = '.bat' if IsWindows() else ''
30 download_script = 'download_from_google_storage%s' % suffix
31 cmd = [download_script, '-n', '-b', bucket, '-u', '-s',
Mads Ager418d1ca2017-05-22 09:35:49 +020032 sha1_file]
33 PrintCmd(cmd)
34 subprocess.check_call(cmd)
35
36def 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 Kenez971eec62017-05-24 11:08:40 +020046def makedirs_if_needed(path):
47 try:
48 os.makedirs(path)
49 except OSError:
50 if not os.path.isdir(path):
51 raise
52
Rico Winda94f01c2017-06-27 10:32:34 +020053def 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 Ager418d1ca2017-05-22 09:35:49 +020060class 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
72class 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 Windf80f5a22017-06-16 09:15:57 +020078 print 'Enter directory = ', self._working_directory
Mads Ager418d1ca2017-05-22 09:35:49 +020079 os.chdir(self._working_directory)
80
81 def __exit__(self, *_):
Rico Windf80f5a22017-06-16 09:15:57 +020082 print 'Enter directory = ', self._old_cwd
Mads Ager418d1ca2017-05-22 09:35:49 +020083 os.chdir(self._old_cwd)
Tamas Kenez82efeb52017-06-12 13:56:22 +020084
85# Reading Android CTS test_result.xml
86
87class CtsModule(object):
88 def __init__(self, module_name):
89 self.name = module_name
90
91class CtsTestCase(object):
92 def __init__(self, test_case_name):
93 self.name = test_case_name
94
95class 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.
102def 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 Windf80f5a22017-06-16 09:15:57 +0200119 assert outcome in ['fail', 'pass']
Tamas Kenez82efeb52017-06-12 13:56:22 +0200120 yield CtsTest(m.groups()[1], outcome == 'pass')