Skip to content

Commit b4aa623

Browse files
committed
code refactor
1 parent bde0dc4 commit b4aa623

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

scripts/create_repository.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,14 @@ def get_artifact_checksum(artifact) -> str:
7070
try:
7171
with urllib.request.urlopen(possible_url) as value:
7272
body = value.read()
73-
time.sleep(1)
7473
return hashlib.sha256(body).hexdigest()
7574
except urllib.error.HTTPError as e:
7675
print(f'RESOURCES NOT FOUND: {possible_url}')
7776

7877
def get_json_dependencies(artifact) -> List[MavenCoordinates]:
7978
with open('out.json') as file:
8079
data = json.load(file)
81-
for d in data["dependencies"]:
82-
if(d["coord"] == artifact):
83-
return get_mavens_coordinates_from_json(d["directDependencies"])
84-
return []
80+
return get_mavens_coordinates_from_json(dependency["directDependencies"]) if any((dependency := d)["coord"] == artifact for d in data["dependencies"]) else []
8581

8682
def get_label(coordinate) -> str:
8783
if ("org.scala-lang" in coordinate.group or "org.scalatest" in coordinate.group or "org.scalactic" in coordinate.group or "com.twitter" in coordinate.group or "javax.annotation" in coordinate.group) and "scala-collection" not in coordinate.artifact and "scalap" not in coordinate.artifact:
@@ -109,16 +105,14 @@ def map_to_resolved_artifacts(output) -> List[ResolvedArtifact]:
109105
def resolve_artifacts_with_checksums_and_direct_dependencies(root_artifacts) -> List[ResolvedArtifact]:
110106
command = f'cs resolve {' '.join(root_artifacts)}'
111107
output = subprocess.run(command, capture_output=True, text=True, shell=True).stdout.splitlines()
112-
artifacts = map_to_resolved_artifacts(output)
113-
return artifacts
108+
return map_to_resolved_artifacts(output)
114109

115110
def to_rules_scala_compatible_dict(artifacts, version) -> Dict[str, Dict]:
116111
temp = {}
117112

118113
for a in artifacts:
119114
label = get_label(a.coordinates).replace('scala3_', 'scala_').replace('scala_tasty_core', 'scala_scala_tasty_core')
120-
deps = ['@' + get_label(dep) for dep in a.direct_dependencies]
121-
deps = list(map(lambda dep: dep.replace('scala_library', 'scala_library_2'), deps)) if "scala3-library_3" in a.coordinates.artifact else deps
115+
deps = [f'@{get_label(dep)}_2' if "scala3-library_3" in a.coordinates.artifact else f'@{get_label(dep)}' for dep in a.direct_dependencies]
122116

123117
temp[label] = {
124118
"artifact": f"{a.coordinates.coordinate}",
@@ -161,9 +155,7 @@ def write_to_file(artifact_dict, version, file):
161155
with file.open('w') as data:
162156
data.write(f'scala_version = "{version}"\n')
163157
data.write('\nartifacts = ')
164-
written = get_with_trailing_commas(json.dumps(artifact_dict, indent=4).replace('true', 'True').replace('false', 'False'))
165-
data.write(written)
166-
data.write('\n')
158+
data.write(f'{get_with_trailing_commas(json.dumps(artifact_dict, indent=4).replace('true', 'True').replace('false', 'False'))}\n')
167159

168160
def create_file(version):
169161
path = os.getcwd().replace('/scripts', '/third_party/repositories')
@@ -172,11 +164,11 @@ def create_file(version):
172164
if not file.exists():
173165
file_to_copy = Path(sorted(glob.glob(f'{path}/*.bzl'))[-1])
174166
with file.open('w+') as data, file_to_copy.open('r') as data_to_copy:
175-
for line_number, line in enumerate(data_to_copy):
176-
if line_number > 1:
177-
data.write(line)
167+
for line in data_to_copy:
168+
data.write(line)
178169

179170
with file.open('r+') as data:
171+
excluded_artifacts = ["org.scala-lang.modules:scala-parser-combinators_2.11:1.0.4"]
180172
root_artifacts = select_root_artifacts(version)
181173
read_data = data.read()
182174
replaced_data = read_data[read_data.find('{'):]
@@ -189,14 +181,12 @@ def create_file(version):
189181
generated_labels = generated_artifact_dict.keys()
190182

191183
for label in labels:
192-
if label in generated_labels and generated_artifact_dict[label]["artifact"] != "org.scala-lang.modules:scala-parser-combinators_2.11:1.0.4":
184+
if label in generated_labels and generated_artifact_dict[label]["artifact"] not in excluded_artifacts:
193185
artifact = generated_artifact_dict[label]["artifact"]
194186
sha = generated_artifact_dict[label]["sha256"]
195187
deps = generated_artifact_dict[label]["deps:"] if "deps:" in generated_artifact_dict[label] else []
196-
197188
original_artifact_dict[label]["artifact"] = artifact
198189
original_artifact_dict[label]["sha256"] = sha
199-
200190
if deps:
201191
dependencies = [d for d in deps if d[1:] in labels and "runtime" not in d and "runtime" not in artifact]
202192
if dependencies:

0 commit comments

Comments
 (0)