From 9e45cdf39364294d861f70426bb90dcd1a89e27f Mon Sep 17 00:00:00 2001 From: Christian Roth Date: Wed, 8 Feb 2023 22:19:57 +0100 Subject: [PATCH] support stripping auth from pypi packages --- conda_lock/conda_lock.py | 19 ++++++++++--------- tests/test-lockfile/no-auth.lock | 1 + tests/test-lockfile/test.lock | 1 + tests/test-stripped-lockfile/no-auth.lock | 1 + 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/conda_lock/conda_lock.py b/conda_lock/conda_lock.py index 16a64e8a..f1f27732 100644 --- a/conda_lock/conda_lock.py +++ b/conda_lock/conda_lock.py @@ -91,10 +91,14 @@ DEFAULT_FILES = [pathlib.Path("environment.yml")] # Captures basic auth credentials, if they exists, in the second capture group. -AUTH_PATTERN = re.compile(r"^(https?:\/\/)(.*:.*@)?(.*)") +AUTH_PATTERN = re.compile(r"^(# pip .* @ )?(https?:\/\/)(.*:.*@)?(.*)") + +# Do not substitute in comments, but do substitute in pip installable packages +# with the pattern: # pip package @ url. +PKG_PATTERN = re.compile(r"(^[^#@].*|^# pip .*)") # Captures the domain in the second group. -DOMAIN_PATTERN = re.compile(r"^(https?:\/\/)?([^\/]+)(.*)") +DOMAIN_PATTERN = re.compile(r"^(# pip .* @ )?(https?:\/\/)?([^\/]+)(.*)") # Captures the platform in the first group. PLATFORM_PATTERN = re.compile(r"^# platform: (.*)$") @@ -913,11 +917,8 @@ def _add_auth_to_line(line: str, auth: Dict[str, str]) -> str: def _add_auth_to_lockfile(lockfile: str, auth: Dict[str, str]) -> str: - # do not substitute in comments, but do substitute in pip installable packages - # with the pattern: # pip package @ url. - pkg_pattern = re.compile(r"(^[^#@].*|^# pip .*)") lockfile_with_auth = "\n".join( - _add_auth_to_line(line, auth) if pkg_pattern.match(line) else line + _add_auth_to_line(line, auth) if PKG_PATTERN.match(line) else line for line in lockfile.strip().split("\n") ) if lockfile.endswith("\n"): @@ -933,17 +934,17 @@ def _add_auth(lockfile: str, auth: Dict[str, str]) -> Iterator[pathlib.Path]: def _strip_auth_from_line(line: str) -> str: - return AUTH_PATTERN.sub(r"\1\3", line) + return AUTH_PATTERN.sub(r"\1\2\4", line) def _extract_domain(line: str) -> str: - return DOMAIN_PATTERN.sub(r"\2", line) + return DOMAIN_PATTERN.sub(r"\3", line) def _strip_auth_from_lockfile(lockfile: str) -> str: lockfile_lines = lockfile.strip().split("\n") stripped_lockfile_lines = tuple( - _strip_auth_from_line(line) if line[0] not in ("#", "@") else line + _strip_auth_from_line(line) if PKG_PATTERN.match(line) else line for line in lockfile_lines ) stripped_domains = sorted( diff --git a/tests/test-lockfile/no-auth.lock b/tests/test-lockfile/no-auth.lock index c8e30114..46d14367 100644 --- a/tests/test-lockfile/no-auth.lock +++ b/tests/test-lockfile/no-auth.lock @@ -1,3 +1,4 @@ http://a.mychannel.cloud/mypackage http://b.mychannel.cloud/mypackage http://c.mychannel.cloud/mypackage +# pip mypackage @ http://d.mychannel.cloud/mypackage diff --git a/tests/test-lockfile/test.lock b/tests/test-lockfile/test.lock index a54e2d72..2a5981bd 100644 --- a/tests/test-lockfile/test.lock +++ b/tests/test-lockfile/test.lock @@ -1,3 +1,4 @@ http://user:password@a.mychannel.cloud/mypackage http://b.mychannel.cloud/mypackage http://user:password@c.mychannel.cloud/mypackage +# pip mypackage @ http://user:password@d.mychannel.cloud/mypackage diff --git a/tests/test-stripped-lockfile/no-auth.lock b/tests/test-stripped-lockfile/no-auth.lock index c8e30114..46d14367 100644 --- a/tests/test-stripped-lockfile/no-auth.lock +++ b/tests/test-stripped-lockfile/no-auth.lock @@ -1,3 +1,4 @@ http://a.mychannel.cloud/mypackage http://b.mychannel.cloud/mypackage http://c.mychannel.cloud/mypackage +# pip mypackage @ http://d.mychannel.cloud/mypackage