Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* [Setting Up Multiple Mirrors](#setting-up-multiple-mirrors)
* [Mirroring From Other Indexes](#mirroring-from-other-indexes)
* [Prereleases](#prereleases)
* [Mirroring Only the Listed Requirements](#mirroring-only-the-listed-requirements)
* [How Does Morgan Resolve Dependencies?](#how-does-morgan-resolve-dependencies)
* [Limitations](#limitations)
* [Why Not Use X?](#why-not-use-x)
Expand Down Expand Up @@ -47,7 +48,7 @@ the mirror instead of pypi.org, which they can't access anyway.
- Package index contains a **configuration file** that lists **markers** for
different client **environments** and a list of **package requirement strings** as per [PEP 508](https://peps.python.org/pep-0508/).
- Mirrorer automatically and **recursively mirrors dependencies** of all direct
requirements.
requirements. This can be turned off with `--no-deps`.
- Only mirrors **optional dependencies** if they were part of the requirement
strings (a.k.a "extras"), or are relevant to the environment markers. This is
true for direct requirements and for dependencies of dependencies.
Expand Down Expand Up @@ -193,6 +194,18 @@ mirrored only if one of the following applies:
3. No final release satisfies the requirement, e.g. because the package has
only published prereleases.

### Mirroring Only the Listed Requirements

Morgan also mirrors the latest matching version of every dependency, even if
the `[requirements]` section pins that dependency to another version. If the
section already is a complete set of pinned versions, for example one
generated from a lock file, provide the `--no-deps` flag to mirror only the
listed requirements.

Morgan then does not mirror the build requirements of source distributions
either, such as setuptools or hatchling. Add them to `[requirements]` if the
clients have to build a package from source.

## How Does Morgan Resolve Dependencies?

Dependency resolution in Python is hard. The standard for specifying dependencies
Expand Down
31 changes: 29 additions & 2 deletions morgan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,14 @@ def __init__(self, args: argparse.Namespace):
self._processed_pkgs = Cache(allow_prereleases=self.prerelease)
self.target_registry: Registry = self._find_target_registry(args)
self.use_pypi_metadata: bool = args.use_pypi_metadata or False
# Library callers build the Namespace by hand, so no_deps may be missing.
self.no_deps: bool = getattr(args, "no_deps", False)

def mirror(self, requirement_string: str):
"""
Mirror a package according to a PEP 508-compliant requirement string.
Dependencies of the package are mirrored recursively, unless no_deps
is set.
"""

requirement = parse_requirement(requirement_string)
Expand Down Expand Up @@ -670,6 +674,13 @@ def _process_file(
):
self._download_file(fileinfo, filepath, hashalg)

if self.no_deps:
if not self.use_pypi_metadata:
# The result is not needed, but the call writes the .metadata
# file that the server serves to clients.
self._extract_metadata_from_file(filepath)
return None

if self.use_pypi_metadata:
pkg_metadata = self._extract_metadata_from_pypi(
requirement.name,
Expand Down Expand Up @@ -891,8 +902,13 @@ def mirror(args: argparse.Namespace):
if not m.config.has_section("requirements"):
msg = f"Config file {args.config} has no [requirements] section"
raise ValueError(msg)
if m.no_deps:
print("Not following dependencies (--no-deps)")
for package in m.config["requirements"]:
reqs = m.config["requirements"][package].splitlines()
# The multiline form below starts with a blank line, which would
# mirror the latest version of the package.
reqs = [req.strip() for req in m.config["requirements"][package].splitlines()]
reqs = [req for req in reqs if req]
if not reqs:
# empty requirements
# morgan =
Expand All @@ -904,7 +920,7 @@ def mirror(args: argparse.Namespace):
# >=2
# [brotli] # noqa: ERA001
for req in reqs:
m.mirror(f"{package}{req.strip()}")
m.mirror(f"{package}{req}")

if not args.skip_server_copy:
m.copy_server()
Expand Down Expand Up @@ -1031,6 +1047,17 @@ def my_url(arg):
"dependencies like poetry) are not considered package dependencies by PyPI."
),
)
parser.add_argument(
"--no-deps",
dest="no_deps",
action="store_true",
help=(
"Only mirror the packages listed in the [requirements] section and do "
"not follow their dependencies (including extras and build "
"requirements). Use this when the requirements are a complete, pinned "
"set, e.g. generated from a lock file."
),
)

server.add_arguments(parser)
configurator.add_arguments(parser)
Expand Down
Loading