Skip to content

Commit

Permalink
Replace os.path with pathlib for all outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoesters committed Jul 2, 2024
1 parent 9aa6381 commit b0f4f19
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions constructor/build_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import hashlib
import json
import logging
import os
from collections import defaultdict
from pathlib import Path

Expand Down Expand Up @@ -58,10 +57,9 @@ def dump_hash(info, algorithm=""):


def dump_info(info):
outpath = os.path.join(info["_output_dir"], "info.json")
with open(outpath, "w") as f:
json.dump(info, f, indent=2, default=repr)
return os.path.abspath(outpath)
outpath = Path(info["_output_dir"], "info.json")
outpath.write_text(json.dumps(info, indent=2, default=repr) + "\n")
return outpath.absolute()


def dump_packages_list(info, env="base"):
Expand All @@ -72,11 +70,10 @@ def dump_packages_list(info, env="base"):
else:
raise ValueError(f"env='{env}' is not a valid env name.")

outpath = os.path.join(info["_output_dir"], f'pkg-list.{env}.txt')
with open(outpath, 'w') as fo:
fo.write(f"# {info['name']} {info['version']}, env={env}\n")
fo.write("\n".join(dists))
return os.path.abspath(outpath)
outpath = Path(info["_output_dir"], f'pkg-list.{env}.txt')
outpath.write_text(f"# {info['name']} {info['version']}, env={env}\n")
outpath.write_text("\n".join(dists))
return outpath.absolute()


def dump_licenses(info, include_text=False, text_errors=None):
Expand Down Expand Up @@ -109,24 +106,23 @@ def dump_licenses(info, include_text=False, text_errors=None):
licenses = defaultdict(dict)
for pkg_record in info["_all_pkg_records"]:
extracted_package_dir = pkg_record.extracted_package_dir
licenses_dir = os.path.join(extracted_package_dir, "info", "licenses")
licenses_dir = Path(extracted_package_dir, "info", "licenses")
licenses[pkg_record.dist_str()]["type"] = pkg_record.license
licenses[pkg_record.dist_str()]["files"] = license_files = []
if not os.path.isdir(licenses_dir):
if not licenses_dir.is_dir():
continue

for directory, _, files in os.walk(licenses_dir):
for directory, _, files in licenses_dir.walk():
for filepath in files:
license_path = os.path.join(directory, filepath)
license_path = directory / filepath
license_file = {"path": license_path, "text": None}
if include_text:
license_file["text"] = Path(license_path).read_text(errors=text_errors)
license_file["text"] = license_path.read_text(errors=text_errors)
license_files.append(license_file)

outpath = os.path.join(info["_output_dir"], "licenses.json")
with open(outpath, "w") as f:
json.dump(licenses, f, indent=2, default=repr)
return os.path.abspath(outpath)
outpath = Path(info["_output_dir"], "licenses.json")
outpath.write_text(json.dumps(licenses, indent=2, default=repr) + "\n")
return outpath.absolute()


OUTPUT_HANDLERS = {
Expand Down

0 comments on commit b0f4f19

Please sign in to comment.