import argparse import os import re DEPS_REGEX = "dependencies = \[[^\]]*\]" PYPROJECT_FILENAME = "pyproject.toml" if __name__ == "__main__": """ build-deps retrieves all mandatory requirements in `requirements.txt` and copies them in `pyproject.toml` dependencies list. """ parser = argparse.ArgumentParser() parser.add_argument("-d", "--dry-run", action="store_true") args = parser.parse_args() # read and store requirements requirements: list[str] = [] with open("requirements.txt", "r") as f: for d in f.readlines(): d = d.replace("\n", "") if "#" in d: d = d.split("#")[0].strip() if "gdal" in d.lower(): print("gdal specific system version") continue requirements.append(f'"{d}"') # format requirements for pryproject.toml deps: str = "dependencies = [\n" for dep in requirements: deps += f"\t{dep},\n" deps += "]" # get and replace the pyproject.toml content content: str = "" with open(PYPROJECT_FILENAME, "r") as f: content = f.read() content = re.sub(DEPS_REGEX, deps, content, 1) # write the new content in a temp file with open(f"{PYPROJECT_FILENAME}.tmp", "w") as f: f.write(content) # if not dry run, override the original pyproject.toml if not args.dry_run: try: os.rename(f"{PYPROJECT_FILENAME}.tmp", PYPROJECT_FILENAME) except Exception as e: print(f"error occurred while overriding pyproject file: {e}") os.remove(f"{PYPROJECT_FILENAME}.tmp")