Skip to content

Commit

Permalink
Merge pull request #54 from facundobatista/expose-pyz-path
Browse files Browse the repository at this point in the history
Expose the .pyz path to the project being run.
  • Loading branch information
facundobatista authored Apr 19, 2023
2 parents a5819f3 + f231414 commit cdde66b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 14 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ The following are examples of different configuration files (which were the ones
- [the full desktop app](https://github.com/facundobatista/pyempaq/blob/main/examples/pyempaq-desktop-app.yaml?raw=True)


## Context of the project being run

When the packed script/module/whatever is run in the unpacking phase, it will be executed inside the virtualenv with the needed requirements/dependencies (freshly created and installed or reused from a previous run).

Also, the following environment variable will be set:

- `PYEMPAQ_PYZ_PATH`: the absolute path to the `.pyz` run by the user


## How to install PyEmpaq

Directly from PyPI:
Expand Down
1 change: 1 addition & 0 deletions pyempaq/unpacker.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def run_command(venv_bin_dir: pathlib.Path, cmd: List[str]) -> None:
newenv["PATH"] = newenv["PATH"] + ":" + venv_bin_dir_str
else:
newenv["PATH"] = venv_bin_dir_str
newenv["PYEMPAQ_PYZ_PATH"] = os.path.dirname(__file__)
subprocess.run(cmd, env=newenv)


Expand Down
71 changes: 57 additions & 14 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,25 @@
import textwrap


def test_basic_cycle_full(tmp_path):
def _pack(tmp_path, monkeypatch, config_text):
"""Set up the project config and pack it."""
# write the proper config
config = tmp_path / "pyempaq.yaml"
config.write_text(textwrap.dedent(config_text))

# pack it calling current pyempaq externally
env = dict(os.environ) # need to replicate original env because of Windows
env["PYTHONPATH"] = os.getcwd()
cmd = [sys.executable, "-m", "pyempaq", str(config)]
monkeypatch.chdir(tmp_path)
subprocess.run(cmd, check=True, env=env)
packed_filepath = tmp_path / "testproject.pyz"
assert packed_filepath.exists()

return packed_filepath


def test_basic_cycle_full(tmp_path, monkeypatch):
"""Verify that the sane packing/unpacking works.
This checks that the unpacked/run project can:
Expand Down Expand Up @@ -50,23 +68,12 @@ def test_basic_cycle_full(tmp_path):
reqspath = projectpath / "requirements.txt"
reqspath.write_text("requests")

# write the proper config
config = tmp_path / "pyempaq.yaml"
config.write_text(textwrap.dedent(f"""
packed_filepath = _pack(tmp_path, monkeypatch, f"""
name: testproject
basedir: {projectpath}
exec:
script: ep.py
"""))

# pack it calling current pyempaq externally
env = dict(os.environ) # need to replicate original env because of Windows
env["PYTHONPATH"] = os.getcwd()
cmd = [sys.executable, "-m", "pyempaq", str(config)]
os.chdir(projectpath)
subprocess.run(cmd, check=True, env=env)
packed_filepath = projectpath / "testproject.pyz"
assert packed_filepath.exists()
""")

# run the packed file in a clean directory
cleandir = tmp_path / "cleandir"
Expand All @@ -89,3 +96,39 @@ def test_basic_cycle_full(tmp_path):
# XXX Facundo 2021-08-01: this check is disabled until we discover why venv.create
# not working in GA
# assert "virtualenv module ok" in output_lines


def test_pyz_location(tmp_path, monkeypatch):
"""Check that the environment variable for the .pyz location is set."""
# set up a basic test project, with an entrypoint that shows access to internals
projectpath = tmp_path / "fakeproject"
entrypoint = projectpath / "ep.py"
entrypoint.parent.mkdir()
entrypoint.write_text(textwrap.dedent("""
import os
print(os.environ.get("PYEMPAQ_PYZ_PATH"))
"""))

packed_filepath = _pack(tmp_path, monkeypatch, f"""
name: testproject
basedir: {projectpath}
exec:
script: ep.py
""")

# run the packed file in a clean directory
cleandir = tmp_path / "cleandir"
cleandir.mkdir()
packed_filepath.rename(cleandir / "renamed.pyz")
os.chdir(cleandir)
cmd = [sys.executable, "renamed.pyz"]
proc = subprocess.run(
cmd,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
output_lines = [line for line in proc.stdout.split("\n") if line]
assert proc.returncode == 0, "\n".join(output_lines)

# verify output
(exposed_pyz_path,) = output_lines
assert exposed_pyz_path == str(cleandir / "renamed.pyz")
12 changes: 12 additions & 0 deletions tests/test_unpacker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

"""Unpacker tests."""

import os
import platform
import zipfile
from pathlib import Path
Expand All @@ -12,6 +13,7 @@
import pytest
from logassert import Exact, NOTHING

import pyempaq.unpacker
from pyempaq.unpacker import (
PROJECT_VENV_DIR,
build_command,
Expand Down Expand Up @@ -114,6 +116,16 @@ def test_runcommand_no_env_path(monkeypatch):
assert passed_env["PATH"] == "test-venv-dir"


def test_runcommand_pyz_path():
"""Check the .pyz path is set."""
with patch("subprocess.run") as run_mock:
run_command(Path("test-venv-dir"), ["foo", "bar"])

(call1,) = run_mock.call_args_list
passed_env = call1[1]["env"]
assert passed_env["PYEMPAQ_PYZ_PATH"] == os.path.dirname(pyempaq.unpacker.__file__)


# --- tests for the project directory setup


Expand Down

0 comments on commit cdde66b

Please sign in to comment.