-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.py
More file actions
213 lines (171 loc) · 7.93 KB
/
Copy pathcommon.py
File metadata and controls
213 lines (171 loc) · 7.93 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from os import path, chdir, makedirs
from subprocess import Popen, PIPE
import subprocess
import xml.etree.ElementTree as ET
import json
import requests
import io
from argparse import ArgumentParser
from zipfile import ZipFile
with open("environment.json") as f:
env = json.load(f)
with open("projects.json") as f:
projects = json.load(f)
resharper_build = env["build directory"]
inspect_code_exe = "inspectcode.x86.exe"
inspect_code_path = path.join(resharper_build, inspect_code_exe)
cli_test_dir = env["test directory"]
projects_dir = path.join(cli_test_dir, "projects")
caches_home = env.get("caches home")
if not caches_home:
caches_home = path.join(cli_test_dir, "caches-home")
def git_clone_if_needed(target_dir, url):
if not path.exists(path.join(target_dir, ".git")):
subprocess.run(["git", "clone", url, target_dir], check=True)
def git_checkout_commit_and_overwrite_local_changes(commit):
subprocess.run(["git", "checkout", commit], check=True, stdout=PIPE, stderr=PIPE)
subprocess.run(["git", "reset", "--hard"], check=True, stdout=PIPE, stderr=PIPE)
def get_sources_from_git(project_input, target_dir):
git_clone_if_needed(target_dir, project_input["repo"])
chdir(target_dir)
subrepo = project_input.get("subrepo")
if subrepo:
subrepo_dir = subrepo["path"]
git_clone_if_needed(subrepo_dir, subrepo["url"])
chdir(subrepo_dir)
git_checkout_commit_and_overwrite_local_changes(subrepo["commit"])
chdir(target_dir)
git_checkout_commit_and_overwrite_local_changes(project_input["commit"])
custom_update_source_script = project_input.get("custom update source script")
if custom_update_source_script:
subprocess.run(custom_update_source_script, check=True, stdout=PIPE, stderr=PIPE)
subprocess.run(["git", "submodule", "update", "--init"], check=True, stdout=PIPE, stderr=PIPE)
root_dir = project_input.get("root")
if root_dir:
return path.join(target_dir, root_dir)
else:
return target_dir
def get_sources_from_zip(project_input, target_dir):
root_dir = path.join(target_dir, project_input["root"])
if not path.exists(root_dir):
response = requests.get(project_input["url"])
with ZipFile(io.BytesIO(response.content)) as zipfile:
zipfile.extractall(path=target_dir)
return root_dir
def get_sources(project_input, target_dir):
kind = project_input.get("kind")
if not kind:
return get_sources_from_git(project_input, target_dir)
elif kind == "zip":
return get_sources_from_zip(project_input, target_dir)
else:
raise ValueError("Unknown source kind: {0}".format(kind))
def invoke_cmake(build_dir, cmake_generator, cmake_options, required_dependencies):
cmd_line_args = ["cmake", "..", "-G", cmake_generator["name"]]
architecture = cmake_generator.get("architecture")
if architecture:
cmd_line_args.append("-A")
cmd_line_args.append(architecture)
if required_dependencies:
vcpkg = env.get("vcpkg")
if vcpkg:
vcpkg_dir = vcpkg["path"]
chdir(vcpkg_dir)
subprocess.run(["vcpkg", "install"] + required_dependencies + ["--triplet", vcpkg["triplet"]], check=True, stdout=PIPE)
cmd_line_args.append("-DCMAKE_TOOLCHAIN_FILE={0}/scripts/buildsystems/vcpkg.cmake".format(vcpkg_dir))
else:
raise Exception("project has required dependencies {0}, but environment doesn't containt path to vcpkg".format(required_dependencies))
if cmake_options:
cmd_line_args.extend(cmake_options)
makedirs(build_dir, exist_ok=True)
chdir(build_dir)
subprocess.run(cmd_line_args, check=True, stdout=PIPE)
with open(path.join(build_dir, "CMakeCache.txt")) as cmake_cache:
for line in cmake_cache.readlines():
if line.startswith("CMAKE_PROJECT_NAME"):
project_name = line[line.find('=') + 1:].rstrip()
sln_file = path.join(build_dir, project_name + ".sln")
if not path.exists(sln_file):
raise Exception("solution file {0} does not exist".format(sln_file))
return sln_file
proj_config_dir = path.abspath("proj-config")
def read_conf_if_needed(project):
if isinstance(project, str):
with open(path.join(proj_config_dir, project)) as pf:
return json.load(pf)
else:
return project
def inspect_code_run_arguments(project_dir, sln_file, project_to_check, msbuild_props):
report_file = path.join(project_dir, "resharper-report.xml")
args = ["--severity=ERROR", "-f=Xml", "-no-build", "-o=" + report_file, "--caches-home=" + caches_home]
if project_to_check:
if isinstance(project_to_check, list):
for p in project_to_check:
args.append("--project=" + p)
else:
assert(isinstance(project_to_check, str))
args.append("--project=" + project_to_check)
if msbuild_props:
props = ["{0}={1}".format(key, value) for key, value in msbuild_props.items()]
args.append("--properties:" + ";".join(props))
args.append(sln_file)
return args, report_file
def count_substring(text, substr):
start = 0
result = 0
while True:
start = text.find(substr, start)
if start == -1:
return result
result += 1
start += len(substr)
def inspected_files_count(inspect_code_output):
return count_substring(inspect_code_output, "Inspecting ")
def add_entry(node, key, value):
entry = ET.SubElement(node, "s:Boolean")
entry.text = str(value)
entry.set("x:Key", key)
def generate_settings(files_to_skip):
root = ET.Element("wpf:ResourceDictionary")
root.set("xml:space", "preserve")
root.set("xmlns:x", "http://schemas.microsoft.com/winfx/2006/xaml")
root.set("xmlns:s", "clr-namespace:System;assembly=mscorlib")
root.set("xmlns:ss", "urn:shemas-jetbrains-com:settings-storage-xaml")
root.set("xmlns:wpf", "http://schemas.microsoft.com/winfx/2006/xaml/presentation")
add_entry(root, "/Default/CodeInspection/CppClangTidy/EnableClangTidySupport/@EntryValue", False)
if files_to_skip:
for f in files_to_skip:
section = "CodeInspection" if f.endswith('proto') else "Environment"
add_entry(root, "/Default/{1}/ExcludedFiles/FileMasksToSkip/={0}/@EntryIndexedValue".format(f, section), True)
return ET.ElementTree(root)
def prepare_project(project_name, project, cmake_generator):
target_dir = path.join(projects_dir, project_name)
project_dir = get_sources(project["sources"], target_dir)
custom_build_tool = project.get("custom build tool")
if custom_build_tool:
prepare_sln_script = custom_build_tool.get("script")
if prepare_sln_script:
chdir(project_dir)
subprocess.run(prepare_sln_script, check=True, stdout=PIPE)
build_step = custom_build_tool.get("build step")
if build_step:
for step in build_step:
subprocess.run(step.split(), check=True, stdout=PIPE)
sln_file = path.join(project_dir, custom_build_tool["path to .sln"])
assert(path.exists(sln_file))
else:
gen_name, gen_description = cmake_generator
build_dir = path.join(project_dir, project.get("build dir", "build") + "-" + gen_name)
project_dir = build_dir
sln_file = invoke_cmake(build_dir, gen_description, project.get("cmake options"), project.get("required dependencies"))
build_step = project.get("build step")
if build_step:
chdir(build_dir)
subprocess.run(build_step.split(), check=True, stdout=PIPE)
generate_settings(project.get("to skip")).write(sln_file + ".DotSettings")
return project_dir, sln_file
def duration(start, end):
minutes, seconds = divmod(end - start, 60)
return "{:02}:{:02}".format(int(minutes), int(seconds))
argparser = ArgumentParser()
argparser.add_argument("-p", "--project", dest="project")