Flutter Engine
The Flutter Engine
bot_utils.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2#
3# Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
4# for details. All rights reserved. Use of this source code is governed by a
5# BSD-style license that can be found in the LICENSE file.
6
7import hashlib
8import importlib.util
9import importlib.machinery
10import os
11import subprocess
12import sys
13
14DART_DIR = os.path.abspath(
15 os.path.normpath(os.path.join(__file__, '..', '..', '..')))
16
17
18def load_source(modname, filename):
19 loader = importlib.machinery.SourceFileLoader(modname, filename)
20 spec = importlib.util.spec_from_file_location(modname,
21 filename,
22 loader=loader)
23 module = importlib.util.module_from_spec(spec)
24 # The module is always executed and not cached in sys.modules.
25 # Uncomment the following line to cache the module.
26 # sys.modules[module.__name__] = module
27 loader.exec_module(module)
28 return module
29
30
32 '''Dynamically load the tools/utils.py python module.'''
33 return load_source('utils', os.path.join(DART_DIR, 'tools', 'utils.py'))
34
35
36def run(command, env=None, shell=False, throw_on_error=True):
37 print("Running command: ", command)
38
39 p = subprocess.Popen(command,
40 stdout=subprocess.PIPE,
41 stderr=subprocess.PIPE,
42 env=env,
43 shell=shell,
44 universal_newlines=True)
45 (stdout, stderr) = p.communicate()
46 if throw_on_error and p.returncode != 0:
47 print("Failed to execute '%s'. Exit code: %s." %
48 (command, p.returncode),
49 file=sys.stderr)
50 print("stdout: ", stdout, file=sys.stderr)
51 print("stderr: ", stderr, file=sys.stderr)
52 raise Exception("Failed to execute %s." % command)
53 return (stdout, stderr, p.returncode)
54
55
56class GSUtil(object):
57 GSUTIL_PATH = None
58 USE_DART_REPO_VERSION = False
59
60 def _layzCalculateGSUtilPath(self):
61 if not GSUtil.GSUTIL_PATH:
62 dart_gsutil = os.path.join(DART_DIR, 'third_party', 'gsutil',
63 'gsutil')
64 if os.path.isfile(dart_gsutil):
65 GSUtil.GSUTIL_PATH = dart_gsutil
66 elif GSUtil.USE_DART_REPO_VERSION:
67 raise Exception("Dart repository version of gsutil required, "
68 "but not found.")
69 else:
70 # We did not find gsutil, look in path
71 possible_locations = list(os.environ['PATH'].split(os.pathsep))
72 for directory in possible_locations:
73 location = os.path.join(directory, 'gsutil')
74 if os.path.isfile(location):
75 GSUtil.GSUTIL_PATH = location
76 break
77 assert GSUtil.GSUTIL_PATH
78
79 def execute(self, gsutil_args):
81
82 gsutil_command = [sys.executable, GSUtil.GSUTIL_PATH]
83
84 return run(gsutil_command + gsutil_args)
85
86 def upload(self,
87 local_path,
88 remote_path,
89 recursive=False,
90 multithread=False):
91 assert remote_path.startswith('gs://')
92
93 if multithread:
94 args = ['-m', 'cp']
95 else:
96 args = ['cp']
97 if recursive:
98 args += ['-R']
99 args += [local_path, remote_path]
100 self.execute(args)
101
102 def cat(self, remote_path):
103 assert remote_path.startswith('gs://')
104
105 args = ['cat', remote_path]
106 (stdout, _, _) = self.execute(args)
107 return stdout
108
109 def setGroupReadACL(self, remote_path, group):
110 args = ['acl', 'ch', '-g', '%s:R' % group, remote_path]
111 self.execute(args)
112
113 def setContentType(self, remote_path, content_type):
114 args = ['setmeta', '-h', 'Content-Type:%s' % content_type, remote_path]
115 self.execute(args)
116
117 def remove(self, remote_path, recursive=False):
118 assert remote_path.startswith('gs://')
119
120 args = ['rm']
121 if recursive:
122 args += ['-R']
123 args += [remote_path]
124 self.execute(args)
def setContentType(self, remote_path, content_type)
Definition: bot_utils.py:113
def upload(self, local_path, remote_path, recursive=False, multithread=False)
Definition: bot_utils.py:90
def execute(self, gsutil_args)
Definition: bot_utils.py:79
def cat(self, remote_path)
Definition: bot_utils.py:102
def _layzCalculateGSUtilPath(self)
Definition: bot_utils.py:60
def setGroupReadACL(self, remote_path, group)
Definition: bot_utils.py:109
def remove(self, remote_path, recursive=False)
Definition: bot_utils.py:117
def run(command, env=None, shell=False, throw_on_error=True)
Definition: bot_utils.py:36
def load_source(modname, filename)
Definition: bot_utils.py:18
def GetUtils()
Definition: bot_utils.py:31
def print(*args, **kwargs)
Definition: run_tests.py:49
Definition: run.py:1