Skip to content

Instantly share code, notes, and snippets.

@barseghyanartur
Forked from cheind/platform_generate.py
Created December 12, 2023 13:27
Show Gist options
  • Save barseghyanartur/09a7821eabce5fd1854e6f17c2a1df62 to your computer and use it in GitHub Desktop.
Save barseghyanartur/09a7821eabce5fd1854e6f17c2a1df62 to your computer and use it in GitHub Desktop.
cross platform pip-compile helper
import argparse
import platform
import sys
import subprocess
from pathlib import Path
def describe() -> dict:
meta = {
"python": "py" + ".".join(platform.python_version_tuple()[:2]),
"platform": sys.platform,
"machine": platform.machine(),
}
meta["file-prefix"] = "{python}-{platform}-{machine}-".format_map(meta)
return meta
def generate_in_files(args) -> list[Path]:
meta = describe()
outpaths = []
for fname in args.SRC_FILES:
inpath = Path(fname)
prefix = "{python}-{platform}-{machine}-".format_map(meta)
outpath = inpath.parent / f"{prefix}{inpath.name}"
outpaths.append(outpath)
print(f"Generating {str(outpath)}")
with open(inpath, "r") as f:
data = f.read().format_map(meta)
with open(outpath, "w") as of:
of.write(data)
return outpaths
def compile(paths: list[Path], pipcompile_args: list[str]):
for fpath in paths:
finalargs = ["pip-compile"] + pipcompile_args + [str(fpath)]
print(f'{" ".join(finalargs)}')
subprocess.check_call(finalargs)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("SRC_FILES", nargs="+")
parser.add_argument("--compile", action="store_true")
args, unknown = parser.parse_known_args()
outpaths = generate_in_files(args)
if args.compile:
compile(outpaths, unknown)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment