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

fix: setup.py update using script #77

Merged
merged 2 commits into from
Jan 30, 2025
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
1 change: 1 addition & 0 deletions MANIFEST.in
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ include LICENSE
include README.md
include requirements/base.in
include requirements/test.in
include requirements/constraints.txt
1 change: 0 additions & 1 deletion requirements/base.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ django-model-utils # Provides TimeStampedModel abstract
edx-opaque-keys>=0.2.1 # Create and introspect course and xblock identities
six # Utilities for supporting Python 2 & 3 in the same codebase
setuptools
backports.zoneinfo; python_version<'3.9'
4 changes: 2 additions & 2 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
asgiref==3.8.1
# via django
django==4.2.17
django==4.2.18
# via
# -c https://raw.githubusercontent.com/edx/edx-lint/master/edx_lint/files/common_constraints.txt
# -r requirements/base.in
Expand All @@ -19,7 +19,7 @@ edx-opaque-keys==2.11.0
# via -r requirements/base.in
pbr==6.1.0
# via stevedore
pymongo==4.10.1
pymongo==4.11
# via edx-opaque-keys
six==1.17.0
# via -r requirements/base.in
Expand Down
10 changes: 5 additions & 5 deletions requirements/ci.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
#
# make upgrade
#
cachetools==5.5.0
cachetools==5.5.1
# via tox
chardet==5.2.0
# via tox
colorama==0.4.6
# via tox
distlib==0.3.9
# via virtualenv
filelock==3.16.1
filelock==3.17.0
# via
# tox
# virtualenv
Expand All @@ -26,9 +26,9 @@ platformdirs==4.3.6
# virtualenv
pluggy==1.5.0
# via tox
pyproject-api==1.8.0
pyproject-api==1.9.0
# via tox
tox==4.23.2
tox==4.24.1
# via -r requirements/ci.in
virtualenv==20.28.1
virtualenv==20.29.1
# via tox
22 changes: 11 additions & 11 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ build==1.2.2.post1
# via
# -r requirements/pip-tools.txt
# pip-tools
cachetools==5.5.0
cachetools==5.5.1
# via
# -r requirements/ci.txt
# tox
Expand All @@ -37,7 +37,7 @@ click-log==0.4.0
# via
# -r requirements/quality.txt
# edx-lint
code-annotations==2.1.0
code-annotations==2.2.0
# via
# -r requirements/quality.txt
# edx-lint
Expand All @@ -57,7 +57,7 @@ distlib==0.3.9
# via
# -r requirements/ci.txt
# virtualenv
django==4.2.17
django==4.2.18
# via
# -c https://raw.githubusercontent.com/edx/edx-lint/master/edx_lint/files/common_constraints.txt
# -r requirements/quality.txt
Expand All @@ -68,11 +68,11 @@ dnspython==2.7.0
# via
# -r requirements/quality.txt
# pymongo
edx-lint==5.4.1
edx-lint==5.6.0
# via -r requirements/quality.txt
edx-opaque-keys==2.11.0
# via -r requirements/quality.txt
filelock==3.16.1
filelock==3.17.0
# via
# -r requirements/ci.txt
# tox
Expand All @@ -81,7 +81,7 @@ iniconfig==2.0.0
# via
# -r requirements/quality.txt
# pytest
isort==5.13.2
isort==6.0.0
# via
# -r requirements/quality.txt
# pylint
Expand Down Expand Up @@ -129,7 +129,7 @@ pycodestyle==2.12.1
# via -r requirements/quality.txt
pydocstyle==6.3.0
# via -r requirements/quality.txt
pylint==3.3.3
pylint==3.3.4
# via
# -r requirements/quality.txt
# edx-lint
Expand All @@ -149,11 +149,11 @@ pylint-plugin-utils==0.8.2
# -r requirements/quality.txt
# pylint-celery
# pylint-django
pymongo==4.10.1
pymongo==4.11
# via
# -r requirements/quality.txt
# edx-opaque-keys
pyproject-api==1.8.0
pyproject-api==1.9.0
# via
# -r requirements/ci.txt
# tox
Expand Down Expand Up @@ -204,13 +204,13 @@ tomlkit==0.13.2
# via
# -r requirements/quality.txt
# pylint
tox==4.23.2
tox==4.24.1
# via -r requirements/ci.txt
typing-extensions==4.12.2
# via
# -r requirements/quality.txt
# edx-opaque-keys
virtualenv==20.28.1
virtualenv==20.29.1
# via
# -r requirements/ci.txt
# tox
Expand Down
12 changes: 6 additions & 6 deletions requirements/quality.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ click==8.1.8
# edx-lint
click-log==0.4.0
# via edx-lint
code-annotations==2.1.0
code-annotations==2.2.0
# via
# -r requirements/test.txt
# edx-lint
Expand All @@ -30,7 +30,7 @@ coverage[toml]==7.6.10
# pytest-cov
dill==0.3.9
# via pylint
django==4.2.17
django==4.2.18
# via
# -c https://raw.githubusercontent.com/edx/edx-lint/master/edx_lint/files/common_constraints.txt
# -r requirements/test.txt
Expand All @@ -41,15 +41,15 @@ dnspython==2.7.0
# via
# -r requirements/test.txt
# pymongo
edx-lint==5.4.1
edx-lint==5.6.0
# via -r requirements/quality.in
edx-opaque-keys==2.11.0
# via -r requirements/test.txt
iniconfig==2.0.0
# via
# -r requirements/test.txt
# pytest
isort==5.13.2
isort==6.0.0
# via
# -r requirements/quality.in
# pylint
Expand Down Expand Up @@ -81,7 +81,7 @@ pycodestyle==2.12.1
# via -r requirements/quality.in
pydocstyle==6.3.0
# via -r requirements/quality.in
pylint==3.3.3
pylint==3.3.4
# via
# edx-lint
# pylint-celery
Expand All @@ -95,7 +95,7 @@ pylint-plugin-utils==0.8.2
# via
# pylint-celery
# pylint-django
pymongo==4.10.1
pymongo==4.11
# via
# -r requirements/test.txt
# edx-opaque-keys
Expand Down
4 changes: 2 additions & 2 deletions requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ asgiref==3.8.1
# django
click==8.1.8
# via code-annotations
code-annotations==2.1.0
code-annotations==2.2.0
# via -r requirements/test.in
coverage[toml]==7.6.10
# via
Expand Down Expand Up @@ -42,7 +42,7 @@ pbr==6.1.0
# stevedore
pluggy==1.5.0
# via pytest
pymongo==4.10.1
pymongo==4.11
# via
# -r requirements/base.txt
# edx-opaque-keys
Expand Down
90 changes: 79 additions & 11 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,95 @@
"""
Load all requirements from the specified requirements files.

Returns:
list: Requirements file relative path strings
Requirements will include any constraints from files specified
with -c in the requirements files.
Returns a list of requirement strings.
"""
requirements = set()
# UPDATED VIA SEMGREP - if you need to remove/modify this method remove this line and add a comment specifying why.

# e.g. {"django": "Django", "confluent-kafka": "confluent_kafka[avro]"}
by_canonical_name = {}

Check warning on line 39 in setup.py

View check run for this annotation

Codecov / codecov/patch

setup.py#L39

Added line #L39 was not covered by tests

def check_name_consistent(package):

Check warning on line 41 in setup.py

View check run for this annotation

Codecov / codecov/patch

setup.py#L41

Added line #L41 was not covered by tests
"""
Raise exception if package is named different ways.

This ensures that packages are named consistently so we can match
constraints to packages. It also ensures that if we require a package
with extras we don't constrain it without mentioning the extras (since
that too would interfere with matching constraints.)
"""
canonical = package.lower().replace('_', '-').split('[')[0]
seen_spelling = by_canonical_name.get(canonical)

Check warning on line 51 in setup.py

View check run for this annotation

Codecov / codecov/patch

setup.py#L50-L51

Added lines #L50 - L51 were not covered by tests
if seen_spelling is None:
by_canonical_name[canonical] = package

Check warning on line 53 in setup.py

View check run for this annotation

Codecov / codecov/patch

setup.py#L53

Added line #L53 was not covered by tests
elif seen_spelling != package:
raise Exception(

Check warning on line 55 in setup.py

View check run for this annotation

Codecov / codecov/patch

setup.py#L55

Added line #L55 was not covered by tests
f'Encountered both "{seen_spelling}" and "{package}" in requirements '
'and constraints files; please use just one or the other.'
)

requirements = {}
constraint_files = set()

Check warning on line 61 in setup.py

View check run for this annotation

Codecov / codecov/patch

setup.py#L60-L61

Added lines #L60 - L61 were not covered by tests

# groups "pkg<=x.y.z,..." into ("pkg", "<=x.y.z,...")
re_package_name_base_chars = r"a-zA-Z0-9\-_." # chars allowed in base package name

Check warning on line 64 in setup.py

View check run for this annotation

Codecov / codecov/patch

setup.py#L64

Added line #L64 was not covered by tests
# Two groups: name[maybe,extras], and optionally a constraint
requirement_line_regex = re.compile(

Check warning on line 66 in setup.py

View check run for this annotation

Codecov / codecov/patch

setup.py#L66

Added line #L66 was not covered by tests
r"([%s]+(?:\[[%s,\s]+\])?)([<>=][^#\s]+)?" # pylint: disable=consider-using-f-string
% (re_package_name_base_chars, re_package_name_base_chars)
)

def add_version_constraint_or_raise(current_line, current_requirements, add_if_not_present):
regex_match = requirement_line_regex.match(current_line)

Check warning on line 72 in setup.py

View check run for this annotation

Codecov / codecov/patch

setup.py#L71-L72

Added lines #L71 - L72 were not covered by tests
if regex_match:
package = regex_match.group(1)
version_constraints = regex_match.group(2)
check_name_consistent(package)
existing_version_constraints = current_requirements.get(package, None)

Check warning on line 77 in setup.py

View check run for this annotation

Codecov / codecov/patch

setup.py#L74-L77

Added lines #L74 - L77 were not covered by tests
# It's fine to add constraints to an unconstrained package,
# but raise an error if there are already constraints in place.
if existing_version_constraints and existing_version_constraints != version_constraints:
raise BaseException(f'Multiple constraint definitions found for {package}:'

Check warning on line 81 in setup.py

View check run for this annotation

Codecov / codecov/patch

setup.py#L81

Added line #L81 was not covered by tests
f' "{existing_version_constraints}" and "{version_constraints}".'
f'Combine constraints into one location with {package}'
f'{existing_version_constraints},{version_constraints}.')
if add_if_not_present or package in current_requirements:
current_requirements[package] = version_constraints

Check warning on line 86 in setup.py

View check run for this annotation

Codecov / codecov/patch

setup.py#L86

Added line #L86 was not covered by tests

# Read requirements from .in files and store the path to any
# constraint files that are pulled in.
for path in requirements_paths:
requirements.update(
line.split('#')[0].strip()
for line in open(path, encoding='utf-8').readlines() # pylint: disable=consider-using-with
if is_requirement(line.strip())
)
return list(requirements)
with open(path, encoding='utf-8') as reqs:

Check warning on line 91 in setup.py

View check run for this annotation

Codecov / codecov/patch

setup.py#L91

Added line #L91 was not covered by tests
for line in reqs:
if is_requirement(line):
add_version_constraint_or_raise(line, requirements, True)

Check warning on line 94 in setup.py

View check run for this annotation

Codecov / codecov/patch

setup.py#L94

Added line #L94 was not covered by tests
if line and line.startswith('-c') and not line.startswith('-c http'):
constraint_files.add(os.path.dirname(path) + '/' + line.split('#')[0].replace('-c', '').strip())

Check warning on line 96 in setup.py

View check run for this annotation

Codecov / codecov/patch

setup.py#L96

Added line #L96 was not covered by tests

# process constraint files: add constraints to existing requirements
for constraint_file in constraint_files:
with open(constraint_file, encoding='utf-8') as reader:

Check warning on line 100 in setup.py

View check run for this annotation

Codecov / codecov/patch

setup.py#L100

Added line #L100 was not covered by tests
for line in reader:
if is_requirement(line):
add_version_constraint_or_raise(line, requirements, False)

Check warning on line 103 in setup.py

View check run for this annotation

Codecov / codecov/patch

setup.py#L103

Added line #L103 was not covered by tests

# process back into list of pkg><=constraints strings
constrained_requirements = [f'{pkg}{version or ""}' for (pkg, version) in sorted(requirements.items())]
return constrained_requirements

Check warning on line 107 in setup.py

View check run for this annotation

Codecov / codecov/patch

setup.py#L106-L107

Added lines #L106 - L107 were not covered by tests


def is_requirement(line):
"""
Return True if the requirement line is a package requirement.

Returns:
bool: True if the line is not blank, a comment, a URL, or an included file
bool: True if the line is not blank, a comment,
a URL, or an included file
"""
return line and not line.startswith(('-r', '#', '-e', 'git+', '-c'))
# UPDATED VIA SEMGREP - if you need to remove/modify this method remove this line and add a comment specifying why

return line and line.strip() and not line.startswith(('-r', '#', '-e', 'git+', '-c'))

Check warning on line 120 in setup.py

View check run for this annotation

Codecov / codecov/patch

setup.py#L120

Added line #L120 was not covered by tests


if sys.argv[-1] == 'tag':
Expand Down