forked from intel/llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
146 lines (116 loc) · 4.3 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Copyright (C) 2024 Intel Corporation
# Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
# See LICENSE.TXT
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
import gzip
import os
import shutil
import subprocess
import tarfile
import urllib # nosec B404
from options import options
from pathlib import Path
def run(
command,
env_vars={},
cwd=None,
add_sycl=False,
ld_library=[],
timeout=None,
):
try:
if timeout is None:
timeout = options.timeout
if isinstance(command, str):
command = command.split()
env = os.environ.copy()
for ldlib in ld_library:
env["LD_LIBRARY_PATH"] = ldlib + os.pathsep + env.get("LD_LIBRARY_PATH", "")
# order is important, we want provided sycl rt libraries to be first
if add_sycl:
sycl_bin_path = os.path.join(options.sycl, "bin")
env["PATH"] = sycl_bin_path + os.pathsep + env.get("PATH", "")
sycl_lib_path = os.path.join(options.sycl, "lib")
env["LD_LIBRARY_PATH"] = (
sycl_lib_path + os.pathsep + env.get("LD_LIBRARY_PATH", "")
)
env.update(env_vars)
result = subprocess.run(
command,
cwd=cwd,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env,
timeout=timeout,
) # nosec B603
if options.verbose:
print(result.stdout.decode())
print(result.stderr.decode())
return result
except subprocess.CalledProcessError as e:
print(e.stdout.decode())
print(e.stderr.decode())
raise
def git_clone(dir, name, repo, commit):
repo_path = os.path.join(dir, name)
if os.path.isdir(repo_path) and os.path.isdir(os.path.join(repo_path, ".git")):
run("git fetch", cwd=repo_path)
run("git reset --hard", cwd=repo_path)
run(f"git checkout {commit}", cwd=repo_path)
elif not os.path.exists(repo_path):
run(f"git clone --recursive {repo} {repo_path}")
run(f"git checkout {commit}", cwd=repo_path)
else:
raise Exception(
f"The directory {repo_path} exists but is not a git repository."
)
return repo_path
def prepare_bench_cwd(dir):
# we need 2 deep to workaround a problem with a fixed relative paths in some velocity benchmarks
options.benchmark_cwd = os.path.join(dir, "bcwd", "bcwd")
if os.path.exists(options.benchmark_cwd):
shutil.rmtree(options.benchmark_cwd)
os.makedirs(options.benchmark_cwd)
def prepare_workdir(dir, version):
version_file_path = os.path.join(dir, "BENCH_WORKDIR_VERSION")
if os.path.exists(dir):
if os.path.isfile(version_file_path):
with open(version_file_path, "r") as version_file:
workdir_version = version_file.read().strip()
if workdir_version == version:
prepare_bench_cwd(dir)
return
else:
print(f"Version mismatch, cleaning up benchmark directory {dir}")
shutil.rmtree(dir)
else:
raise Exception(
f"The directory {dir} exists but is a benchmark work directory."
)
os.makedirs(dir)
prepare_bench_cwd(dir)
with open(version_file_path, "w") as version_file:
version_file.write(version)
def create_build_path(directory, name):
build_path = os.path.join(directory, name)
if options.rebuild and Path(build_path).exists():
shutil.rmtree(build_path)
Path(build_path).mkdir(parents=True, exist_ok=True)
return build_path
def download(dir, url, file, untar=False, unzip=False):
data_file = os.path.join(dir, file)
if not Path(data_file).exists():
print(f"{data_file} does not exist, downloading")
urllib.request.urlretrieve(url, data_file)
if untar:
file = tarfile.open(data_file)
file.extractall(dir)
file.close()
if unzip:
[stripped_gz, _] = os.path.splitext(data_file)
with gzip.open(data_file, "rb") as f_in, open(stripped_gz, "wb") as f_out:
shutil.copyfileobj(f_in, f_out)
else:
print(f"{data_file} exists, skipping...")
return data_file