Skip to content

Commit

Permalink
venv: systematically add benchkit as a dependency (open-s4c#88)
Browse files Browse the repository at this point in the history
Signed-off-by: Antonio Paolillo <[email protected]>
  • Loading branch information
apaolillo authored Aug 22, 2024
1 parent c51a0b5 commit cfee07b
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions scripts/add_paths_venv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python3
#!/usr/bin/python3
"""
Adds the paths specified in dependency-paths.txt to the benchkit-dep.pth files
in the site-packages directory.
Expand All @@ -12,37 +12,51 @@ def relative_to(path_to_return: Path, relto_path: Path) -> Path:
result = path_to_return.resolve().relative_to(relto_path, walk_up=True)
else:
import os

result = os.path.relpath(
os.path.realpath(path_to_return),
os.path.realpath(relto_path),
)
return result


def get_benchkit_path() -> Path:
this_script = Path(__file__)
script_dir = this_script.parent
benchkit_dir = script_dir.parent
result = benchkit_dir.resolve()
return result


def main() -> None:
target_filename = "benchkit-dep.pth"
dep_filepath = Path("./dependency-paths.txt").resolve()
venv_path = sys.argv[1] if len(sys.argv) >= 2 else "venv"
venv_path = Path(sys.argv[1] if len(sys.argv) >= 2 else "venv")

if not venv_path.is_dir():
print(f"No venv directory found in: {venv_path}", file=sys.stderr)
exit(1)

venv_site_packages_path = next(Path(f"{venv_path}/lib").glob("**/site-packages/")).resolve()
venv_site_packages_path = next(Path(venv_path / "lib").glob("**/site-packages/")).resolve()

venv_rel_paths = []
venv_abs_paths = [get_benchkit_path()]

if dep_filepath.is_file():
with open(dep_filepath) as dependency_types_file:
lines = dependency_types_file.readlines()
lines = [sline for line in dependency_types_file.readlines() if (sline := line.strip())]
for line in lines:
line_path = Path(line.rstrip())
if not line_path.is_dir():
print(f"[WARNING] {line_path} is not a directory", file=sys.stderr)
continue
# get the path relative to the site-packages path
venv_rel_path = relative_to(path_to_return=line_path.resolve(), relto_path=venv_site_packages_path)
venv_rel_paths.append(venv_rel_path)
line_path = Path(line)
if line_path.is_dir():
venv_abs_path = line_path.resolve()
venv_abs_paths.append(venv_abs_path)
else:
print(f"[WARNING] '{line_path}' is not a directory, skipping.", file=sys.stderr)

venv_abs_paths = list(dict.fromkeys(venv_abs_paths)) # remove duplicates

target_path = venv_site_packages_path / target_filename
with open(target_path, "a") as file:
file.writelines(map(lambda p: str(p) + '\n', venv_rel_paths))
with open(target_path, "w") as file:
file.writelines(f"{p}\n" for p in venv_abs_paths)


if __name__ == "__main__":
Expand Down

0 comments on commit cfee07b

Please sign in to comment.