def set_python_env(prefix): """Set environment for a Python distribution. :param prefix: root directory of the python distribution :type prefix: str """ import e3.env env = e3.env.Env() if sys.platform == 'win32': # unix: no cover env.add_path(prefix) env.add_path(os.path.join(prefix, 'Scripts')) else: env.add_path(os.path.join(prefix, 'bin')) env.add_dll_path(os.path.join(prefix, 'lib'))
def set_python_env(prefix: str) -> None: """Set environment for a Python distribution. :param prefix: root directory of the python distribution """ import e3.env env = e3.env.Env() if sys.platform == "win32": # unix: no cover env.add_path(prefix) env.add_path(os.path.join(prefix, "Scripts")) else: env.add_path(os.path.join(prefix, "bin")) env.add_dll_path(os.path.join(prefix, "lib"))
def test_relocate_python_distrib(): env = e3.env.Env() # Create a venv and add pip manually to ensure no upgrade is done. # Create a venv and add pip manually to ensure no upgrade is done. if sys.platform == "win32": p = e3.os.process.Run([ sys.executable, "-m", "venv", "--copies", "--without-pip", "my_env" ]) else: p = e3.os.process.Run( [sys.executable, "-m", "venv", "--without-pip", "my_env"]) assert p.status == 0, f"output was:\n{p.out}" p = e3.os.process.Run([e3.sys.interpreter("./my_env"), "-m", "ensurepip"]) assert p.status == 0, f"output was:\n{p.out}" # Move the venv and check that calling a script inside it will # result in a failure as absolute location to Python interpreter will # be wrong e3.fs.mv("my_env", "moved_env") logging.info(e3.fs.ls("./moved_env/*/*")) if sys.platform == "win32": script_dir = "./Scripts" script = f"{script_dir}/pip3.exe" else: script_dir = "./bin" script = f"{script_dir}/pip3" # Add some additional files in either bin or Scripts to check that relocation # works even when other content has been added to a Python distrib. e3.fs.mkdir(os.path.join("./moved_env", script_dir, "dummy_dir")) with open(os.path.join("./moved_env", script_dir, "dummy_sh.sh"), "w") as fd: fd.write("#!/bin/bash\necho") try: p = e3.os.process.Run([os.path.join("./moved_env", script), "--help"]) # On Windows we will get a status != 0 in case of error assert p.status != 0 except FileNotFoundError: # On Unixes we get an exception (interpreter not found) pass # Apply relocation on the venv and freeze the interpreter e3.sys.relocate_python_distrib( python_distrib_dir=os.path.abspath("./moved_env"), freeze=True) p = e3.os.process.Run([os.path.join("./moved_env", script), "--help"]) assert p.status == 0 # Move the environment, relocate it but make it relocatable this time. e3.fs.mv("moved_env", "moved_env2") logging.info("Make venv non location specific") e3.sys.relocate_python_distrib( python_distrib_dir=os.path.abspath("./moved_env2")) # Moving the venv should result in a working environment providing PATH is # set correctly e3.fs.mv("moved_env2", "moved_env3") env.add_path(os.path.abspath(os.path.join("moved_env3", "bin"))) env.add_path(os.path.abspath(os.path.join("moved_env3", "Scripts"))) env.add_path(os.path.abspath("moved_env3")) p = e3.os.process.Run([os.path.join("moved_env3", script), "--help"]) assert p.status == 0, f"output was:\n{p.out}"