Skip to content

Commit

Permalink
fix: Use importlib.resources to get in-process script (#39)
Browse files Browse the repository at this point in the history
* - removed __file__ usage use importlib instead...

* - support 3.8 python...

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* - use if instead of exception with catch all...

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* - fixed mistake with as file...

* Apply suggestions from code review

---------

Co-authored-by: franz haas <franz.haas@ams-osram.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Frost Ming <mianghong@gmail.com>
  • Loading branch information
4 people authored Feb 4, 2024
1 parent 52b9768 commit e8a0e4f
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/pdm_packer/env.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import importlib.resources
import subprocess
import sys
from functools import cached_property
from pathlib import Path
from tempfile import TemporaryDirectory
Expand All @@ -14,7 +16,13 @@
except ImportError:
from pdm.models.environment import Environment as BaseEnvironment

IN_PROCESS_SCRIPT = Path(__file__).with_name("_compile_source.py")

def get_in_process_script():
if sys.version_info >= (3, 9):
script = importlib.resources.files("pdm_packer") / "_compile_source.py"
return importlib.resources.as_file(script)
else:
return importlib.resources.path("pdm_packer", "_compile_source.py")


class PackEnvironment(BaseEnvironment):
Expand All @@ -33,8 +41,9 @@ def __exit__(self, *args: Any) -> None:
self._dir.cleanup()

def _compile_to_pyc(self, dest: Path) -> None:
args = [str(self.interpreter.path), str(IN_PROCESS_SCRIPT), str(dest)]
subprocess.check_output(args, stderr=subprocess.STDOUT)
with get_in_process_script() as scriptpath:
args = [str(self.interpreter.path), str(scriptpath), str(dest)]
subprocess.check_output(args, stderr=subprocess.STDOUT)

def prepare_lib_for_pack(self, compile: bool = False) -> Path:
"""Get a lib path containing all dependencies for pack.
Expand Down

0 comments on commit e8a0e4f

Please sign in to comment.