Skip to content

Commit

Permalink
Merge pull request #18 from IBM/ExtrasAllGroup
Browse files Browse the repository at this point in the history
ExtrasAllGroup: Add "all" group to extras_require with union of everything
  • Loading branch information
gabe-l-hart authored Jan 18, 2022
2 parents 33f87aa + 5b6da72 commit d568180
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
17 changes: 13 additions & 4 deletions import_tracker/setup_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
get_tracked_modules,
)

# Regex for parsing requirements
req_split_expr = re.compile(r"[=><!~\[]")

# Shared logger
log = logging.getLogger("SETUP")

# Regex for parsing requirements
_REQ_SPLIT_EXPR = re.compile(r"[=><!~\[]")

_ALL_GROUP = "all"


def _map_requirements(declared_dependencies, dependency_set):
"""Given the declared dependencies from requirements.txt and the given
Expand Down Expand Up @@ -66,7 +68,7 @@ def parse_requirements(
# Load all requirements from the requirements file
with open(requirements_file, "r") as handle:
requirements = {
_standardize_package_name(req_split_expr.split(line, 1)[0]): line.strip()
_standardize_package_name(_REQ_SPLIT_EXPR.split(line, 1)[0]): line.strip()
for line in handle.readlines()
if line.strip() and not line.startswith("#")
}
Expand Down Expand Up @@ -118,6 +120,13 @@ def parse_requirements(
)
common_imports = common_imports.union(missing_reqs)

# Add a special "all" group to the extras_require that will install all deps
# needed for all extras
if _ALL_GROUP not in extras_require_sets:
all_reqs = all_tracked_requirements.union(missing_reqs)
log.debug("Adding [%s] requirement group: %s", _ALL_GROUP, all_reqs)
extras_require_sets[_ALL_GROUP] = all_reqs

# Map all dependencies through those listed in requirements.txt
standardized_requirements = {
key.replace("-", "_"): val for key, val in requirements.items()
Expand Down
9 changes: 6 additions & 3 deletions test/test_setup_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def test_parse_requirements_happy():
"submod3": sorted(["PyYaml >= 6.0", "alchemy-logging>=1.0.3"]),
"submod1": sorted(["conditional_deps"]),
"submod2": sorted(["alchemy-logging>=1.0.3"]),
"all": sorted(sample_lib_requirements),
}


Expand All @@ -52,17 +53,19 @@ def test_parse_requirements_add_untracked_reqs():
with tempfile.NamedTemporaryFile("w") as requirements_file:
# Make a requirements file with an extra entry
extra_req = "something-ElSe[extras]~=1.2.3"
requirements_file.write("\n".join(sample_lib_requirements + [extra_req]))
reqs = sample_lib_requirements + [extra_req]
requirements_file.write("\n".join(reqs))
requirements_file.flush()

# Parse the reqs for "sample_lib"
requirements, _ = parse_requirements(
requirements, extras_require = parse_requirements(
requirements_file.name,
"sample_lib",
)

# Make sure the extra requirement was added
extra_req in requirements
assert extra_req in requirements
assert extras_require["all"] == sorted(reqs)


def test_parse_requirements_missing_import_tracker():
Expand Down

0 comments on commit d568180

Please sign in to comment.