Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI to install to Python running installer #94

Merged
merged 28 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a871069
main: implement CLI
FFY00 Jul 28, 2021
ec29913
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 24, 2021
514d71f
Remove script, use python -m installer for CLI
takluyver Jan 5, 2022
c600a6e
Remove Python version & dependency checks
takluyver Jan 5, 2022
0037f48
Fold destdir support, bytecode compilation into Python API
takluyver Jan 5, 2022
140fd61
Merge remote-tracking branch 'origin/main' into cli
takluyver Jan 6, 2022
d78df15
Default destdir argument to None
takluyver Jan 6, 2022
b04487e
Full stop in docstring
takluyver Jan 6, 2022
d9dd7d2
Rework command-line options for controlling bytecode generation
takluyver Jan 6, 2022
15c992b
Add some tests for CLI interface
takluyver Jan 12, 2022
fdfd28e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 12, 2022
62b632d
Remove unused entrypoint function
takluyver Jan 12, 2022
ac2d729
Remove unused import
takluyver Jan 12, 2022
c8a4390
Mark some code which can't be consistently covered by tests
takluyver Jan 12, 2022
86ff36e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 12, 2022
679106c
Add type hint for kwargs dict
takluyver Jan 12, 2022
73f4fae
Fix typo
takluyver Jan 13, 2022
464644b
Clearer metavar for destdir argument
takluyver Jan 13, 2022
d150edd
Don't generate bytecode by default in Python API
takluyver Jan 14, 2022
973ac95
Don't use distutils to get headers path
takluyver Jan 14, 2022
8263f76
Normalise distribution name to make headers directory
takluyver Jan 14, 2022
e29818e
Remove name normalisation for header install path for now
takluyver Jan 14, 2022
275e73d
Fix headers destination directory in a venv
takluyver Jan 14, 2022
cf6ba41
Remove unused import
takluyver Jan 14, 2022
5264e37
Rename private method
takluyver Jan 15, 2022
00810eb
Pass quiet=1 to compileall
takluyver Jan 19, 2022
546fd2d
Try to fix path embedded in bytecode files
takluyver Jan 19, 2022
05855a9
Merge branch 'main' into cli
takluyver Jan 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/installer/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""Installer CLI."""

import argparse
import os.path
import sys
import sysconfig
from typing import Dict, Optional, Sequence

import installer
import installer.destinations
import installer.sources
import installer.utils


def main_parser() -> argparse.ArgumentParser:
"""Construct the main parser."""
parser = argparse.ArgumentParser()
parser.add_argument("wheel", type=str, help="wheel file to install")
parser.add_argument(
"--destdir",
"-d",
metavar="path",
type=str,
help="destination directory (prefix to prepend to each file)",
)
parser.add_argument(
"--compile-bytecode",
action="append",
metavar="level",
type=int,
pradyunsg marked this conversation as resolved.
Show resolved Hide resolved
choices=[0, 1, 2],
help="generate bytecode for the specified optimization level(s) (default=0, 1)",
pradyunsg marked this conversation as resolved.
Show resolved Hide resolved
)
parser.add_argument(
"--no-compile-bytecode",
action="store_true",
help="don't generate bytecode for installed modules",
)
return parser


def get_scheme_dict(distribution_name: str) -> Dict[str, str]:
"""Calculate the scheme dictionary for the current Python environment."""
scheme_dict = sysconfig.get_paths()

# calculate 'headers' path, not currently in sysconfig - see
# https://bugs.python.org/issue44445. This is based on what distutils does.
# TODO: figure out original vs normalised distribution names
scheme_dict["headers"] = os.path.join(
sysconfig.get_path(
"include", vars={"installed_base": sysconfig.get_config_var("base")}
),
distribution_name,
)

return scheme_dict
Comment on lines +42 to +56
Copy link
Member

@pradyunsg pradyunsg Jan 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is... assuming folks are OK with this logic. :)



def main(cli_args: Sequence[str], program: Optional[str] = None) -> None:
"""Process arguments and perform the install."""
parser = main_parser()
if program:
parser.prog = program
args = parser.parse_args(cli_args)

bytecode_levels = args.compile_bytecode
if args.no_compile_bytecode:
bytecode_levels = []
elif not bytecode_levels:
bytecode_levels = [0, 1]

with installer.sources.WheelFile.open(args.wheel) as source:
destination = installer.destinations.SchemeDictionaryDestination(
get_scheme_dict(source.distribution),
sys.executable,
installer.utils.get_launcher_kind(),
bytecode_optimization_levels=bytecode_levels,
destdir=args.destdir,
)
installer.install(source, destination, {})


if __name__ == "__main__": # pragma: no cover
main(sys.argv[1:], "python -m installer")
58 changes: 53 additions & 5 deletions src/installer/destinations.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
"""Handles all file writing and post-installation processing."""

import compileall
import io
import os
from typing import TYPE_CHECKING, BinaryIO, Dict, Iterable, Optional, Tuple, Union
from pathlib import Path
from typing import (
TYPE_CHECKING,
BinaryIO,
Collection,
Dict,
Iterable,
Optional,
Tuple,
Union,
)

from installer.records import Hash, RecordEntry
from installer.scripts import Script
Expand Down Expand Up @@ -99,6 +110,8 @@ def __init__(
interpreter: str,
script_kind: "LauncherKind",
hash_algorithm: str = "sha256",
bytecode_optimization_levels: Collection[int] = (),
destdir: Optional[str] = None,
) -> None:
"""Construct a ``SchemeDictionaryDestination`` object.

Expand All @@ -108,11 +121,28 @@ def __init__(
:param hash_algorithm: the hashing algorithm to use, which is a member
of :any:`hashlib.algorithms_available` (ideally from
:any:`hashlib.algorithms_guaranteed`).
:param bytecode_optimization_levels: Compile cached bytecode for
installed .py files with these optimization levels. The bytecode
is specific to the minor version of Python (e.g. 3.10) used to
generate it.
:param destdir: A staging directory in which to write all files. This
is expected to be the filesystem root at runtime, so embedded paths
will be written as though this was the root.
"""
self.scheme_dict = scheme_dict
self.interpreter = interpreter
self.script_kind = script_kind
self.hash_algorithm = hash_algorithm
self.bytecode_optimization_levels = bytecode_optimization_levels
self.destdir = destdir

def _path_with_destdir(self, scheme: Scheme, path: str) -> str:
file = os.path.join(self.scheme_dict[scheme], path)
if self.destdir is not None:
file_path = Path(file)
rel_path = file_path.relative_to(file_path.anchor)
return os.path.join(self.destdir, rel_path)
return file

def write_to_fs(
self,
Expand All @@ -131,7 +161,7 @@ def write_to_fs(
- Ensures that an existing file is not being overwritten.
- Hashes the written content, to determine the entry in the ``RECORD`` file.
"""
target_path = os.path.join(self.scheme_dict[scheme], path)
target_path = self._path_with_destdir(scheme, path)
if os.path.exists(target_path):
message = f"File already exists: {target_path}"
raise FileExistsError(message)
Expand Down Expand Up @@ -201,20 +231,34 @@ def write_script(
Scheme("scripts"), script_name, stream, is_executable=True
)

path = os.path.join(self.scheme_dict[Scheme("scripts")], script_name)
path = self._path_with_destdir(Scheme("scripts"), script_name)
mode = os.stat(path).st_mode
mode |= (mode & 0o444) >> 2
os.chmod(path, mode)

return entry

def _compile_bytecode(self, scheme: Scheme, record: RecordEntry) -> None:
"""Compile bytecode for a single .py file."""
if scheme not in ("purelib", "platlib"):
pradyunsg marked this conversation as resolved.
Show resolved Hide resolved
return

target_path = self._path_with_destdir(scheme, record.path)
dir_path_to_embed = os.path.dirname( # Without destdir
os.path.join(self.scheme_dict[scheme], record.path)
)
for level in self.bytecode_optimization_levels:
compileall.compile_file(
target_path, optimize=level, quiet=1, ddir=dir_path_to_embed
)

def finalize_installation(
self,
scheme: Scheme,
record_file_path: str,
records: Iterable[Tuple[Scheme, RecordEntry]],
) -> None:
"""Finalize installation, by writing the ``RECORD`` file.
"""Finalize installation, by writing the ``RECORD`` file & compiling bytecode.

:param scheme: scheme to write the ``RECORD`` file in
:param record_file_path: path of the ``RECORD`` file with that scheme
Expand All @@ -230,7 +274,11 @@ def prefix_for_scheme(file_scheme: str) -> Optional[str]:
)
return path + "/"

with construct_record_file(records, prefix_for_scheme) as record_stream:
record_list = list(records)
with construct_record_file(record_list, prefix_for_scheme) as record_stream:
self.write_to_fs(
scheme, record_file_path, record_stream, is_executable=False
)

for scheme, record in record_list:
self._compile_bytecode(scheme, record)
75 changes: 75 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import textwrap
import zipfile

import pytest


@pytest.fixture
def fancy_wheel(tmp_path):
path = tmp_path / "fancy-1.0.0-py2.py3-none-any.whl"
files = {
"fancy/": b"""""",
"fancy/__init__.py": b"""\
def main():
print("I'm fancy.")
""",
"fancy/__main__.py": b"""\
if __name__ == "__main__":
from . import main
main()
""",
"fancy-1.0.0.data/data/fancy/": b"""""",
"fancy-1.0.0.data/data/fancy/data.py": b"""\
# put me in data
""",
"fancy-1.0.0.dist-info/": b"""""",
"fancy-1.0.0.dist-info/top_level.txt": b"""\
fancy
""",
"fancy-1.0.0.dist-info/entry_points.txt": b"""\
[console_scripts]
fancy = fancy:main

[gui_scripts]
fancy-gui = fancy:main
""",
"fancy-1.0.0.dist-info/WHEEL": b"""\
Wheel-Version: 1.0
Generator: magic (1.0.0)
Root-Is-Purelib: true
Tag: py3-none-any
""",
"fancy-1.0.0.dist-info/METADATA": b"""\
Metadata-Version: 2.1
Name: fancy
Version: 1.0.0
Summary: A fancy package
Author: Agendaless Consulting
Author-email: [email protected]
License: MIT
Keywords: fancy amazing
Platform: UNKNOWN
Classifier: Intended Audience :: Developers
""",
# The RECORD file is indirectly validated by the WheelFile, since it only
# provides the items that are a part of the wheel.
"fancy-1.0.0.dist-info/RECORD": b"""\
fancy/__init__.py,,
fancy/__main__.py,,
fancy-1.0.0.data/data/fancy/data.py,,
fancy-1.0.0.dist-info/top_level.txt,,
fancy-1.0.0.dist-info/entry_points.txt,,
fancy-1.0.0.dist-info/WHEEL,,
fancy-1.0.0.dist-info/METADATA,,
fancy-1.0.0.dist-info/RECORD,,
""",
}

with zipfile.ZipFile(path, "w") as archive:
for name, indented_content in files.items():
archive.writestr(
name,
textwrap.dedent(indented_content.decode("utf-8")).encode("utf-8"),
)

return path
38 changes: 38 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from installer.__main__ import get_scheme_dict, main


def test_get_scheme_dict():
d = get_scheme_dict(distribution_name="foo")
assert set(d.keys()) >= {"purelib", "platlib", "headers", "scripts", "data"}


def test_main(fancy_wheel, tmp_path):
destdir = tmp_path / "dest"

main([str(fancy_wheel), "-d", str(destdir)], "python -m installer")

installed_py_files = destdir.rglob("*.py")

assert {f.stem for f in installed_py_files} == {"__init__", "__main__", "data"}

installed_pyc_files = destdir.rglob("*.pyc")
assert {f.name.split(".")[0] for f in installed_pyc_files} == {
"__init__",
"__main__",
}


def test_main_no_pyc(fancy_wheel, tmp_path):
destdir = tmp_path / "dest"

main(
[str(fancy_wheel), "-d", str(destdir), "--no-compile-bytecode"],
"python -m installer",
)

installed_py_files = destdir.rglob("*.py")

assert {f.stem for f in installed_py_files} == {"__init__", "__main__", "data"}

installed_pyc_files = destdir.rglob("*.pyc")
assert set(installed_pyc_files) == set()
Loading