Skip to content

Commit

Permalink
add feature to validate the platform in the lockfile against the syst…
Browse files Browse the repository at this point in the history
…em platform
  • Loading branch information
janjagusch committed May 2, 2021
1 parent 9c4752b commit c513e66
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion conda_lock/conda_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
# Captures the domain in the second group.
DOMAIN_PATTERN = re.compile(r"^(https?:\/\/)?([^\/]+)(.*)")

# Captures the platform in the first group.
PLATFORM_PATTERN = re.compile(r"^# platform: (.*)$")

if not (sys.version_info.major >= 3 and sys.version_info.minor >= 6):
print("conda_lock needs to run under python >=3.6")
sys.exit(1)
Expand All @@ -59,6 +62,38 @@
CONDA_PKGS_DIRS = None
DEFAULT_PLATFORMS = ["osx-64", "linux-64", "win-64"]

_PLATFORM_MAP = {"osx-64": "darwin", "linux-64": "linux", "win-64": "win"}


def _extract_platform(line):
search = PLATFORM_PATTERN.search(line)
if search:
return search.group(1)


def extract_platform(lockfile: str) -> str:
for line in lockfile.strip().split("\n"):
platform = _extract_platform(line)
if platform:
return platform
raise RuntimeError("Cannot find platform in lockfile.")


def _do_validate_platform(platform: str) -> Tuple[bool, str]:
return sys.platform.startswith(_PLATFORM_MAP[platform]), sys.platform


def do_validate_platform(lockfile: str):
platform_lockfile = extract_platform(lockfile)
try:
success, platform_sys = _do_validate_platform(platform_lockfile)
except KeyError:
raise RuntimeError(f"Unknown platform type in lockfile '{platform_lockfile}'.")
if not success:
raise RuntimeError(
f"Platform in lockfile '{platform_lockfile}' is not compatible with system platform '{platform_sys}'."
)


def conda_pkgs_dir():
global CONDA_PKGS_DIRS
Expand Down Expand Up @@ -734,12 +769,36 @@ def lock(
default="",
)
@click.option("--auth-file", help="Path to the authentication file.", default="")
@click.option(
"--validate-platform",
is_flag=True,
default=True,
help="Whether the platform compatibility between your lockfile and the host system should be validated.",
)
@click.argument("lock-file")
def install(conda, mamba, micromamba, prefix, name, lock_file, auth, auth_file):
def install(
conda,
mamba,
micromamba,
prefix,
name,
lock_file,
auth,
auth_file,
validate_platform,
):
"""Perform a conda install"""
auth = json.loads(auth) if auth else read_json(auth_file) if auth_file else None
_conda_exe = determine_conda_executable(conda, mamba=mamba, micromamba=micromamba)
install_func = partial(do_conda_install, conda=_conda_exe, prefix=prefix, name=name)
if validate_platform:
lockfile = read_file(lock_file)
try:
do_validate_platform(lockfile)
except RuntimeError as error:
raise RuntimeError(
error.args[0] + " Disable validation with `--validate-platform=False`."
)
if auth:
lockfile = read_file(lock_file)
with _add_auth(lockfile, auth) as lockfile_with_auth:
Expand Down

0 comments on commit c513e66

Please sign in to comment.