From 6ff0b201a62d8f44e0381f105cfb47250d067d83 Mon Sep 17 00:00:00 2001 From: Stephen-RA-King <33905365+Stephen-RA-King@users.noreply.github.com> Date: Sat, 27 Aug 2022 17:51:54 +0100 Subject: [PATCH] feat: add generate_db function --- src/piptools_sync/piptools_sync.py | 56 ++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/piptools_sync/piptools_sync.py b/src/piptools_sync/piptools_sync.py index a1a2312..01effa5 100644 --- a/src/piptools_sync/piptools_sync.py +++ b/src/piptools_sync/piptools_sync.py @@ -201,3 +201,59 @@ def get_latest_pypi_repo_version(name: str) -> Union[str, int]: return version except requests.exceptions.RequestException as e: raise SystemExit(e) from None + + +def generate_db(force: int = 0) -> dict[str, str]: + """Generate a mapping from pre-commit repo to PyPI repo + + Generates a dictionary data structure: + key : pre-commit URL + value : PyPi project name + e.g. {"https://github.com/pre-commit/pre-commit-hooks": "pre-commit-hooks",} + + Parameters + force : int + When set to 1 will force the generation of a new mapping + + Returns + mapping : dict + the mapping dictionary + """ + + logger.debug("starting **** generate_db ****") + + def generate_file() -> dict: + mapping_db = {} + pyrepos = get_precommit_repos() + for repo in tqdm(pyrepos): + inta_repo, *_ = repo + inta_repo = inta_repo.lower() + mapping_db[inta_repo] = "" + if inta_repo in MANUAL_MAPPING: + logger.debug("adding value from manual mapping dict") + mapping_db[inta_repo] = MANUAL_MAPPING[inta_repo] + else: + *_, project = inta_repo.split("/") + result = get_latest_pypi_repo_version(project) + if result != 0: + logger.debug("project found on PyPI...mapping value to key") + mapping_db[inta_repo] = project + with open(MAPPING_FILE, "w") as outfile: + json.dump(mapping_db, outfile) + return mapping_db + + start = time.time() + if not MAPPING_FILE.exists() or force == 1 or MAPPING_FILE.stat().st_size < 5: + logger.debug("Generating new mapping") + mapping = generate_file() + elif int(time.time() - os.path.getmtime(MAPPING_FILE)) > REGEN_PERIOD: + logger.debug("mapping expired... Generating a new mapping") + mapping = generate_file() + else: + logger.debug("Reusing mapping") + with open(MAPPING_FILE) as infile: + mapping = json.load(infile) + end = time.time() + print(end - start) + + return mapping