From 1d6ad0d6edcb0e75e998537e8cdef3fd6ba7f4ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Socho=C5=84?= Date: Sun, 25 Mar 2018 23:28:04 +0200 Subject: [PATCH 1/4] Provide automatic removal of pkg-resources==0.0.0 Should help to deal with that pretty paintuly issue under Ubuntu/Debian family: https://bugs.launchpad.net/ubuntu/+source/python-pip/+bug/1635463 --- pre_commit_hooks/requirements_txt_fixer.py | 5 +++++ tests/requirements_txt_fixer_test.py | 2 ++ 2 files changed, 7 insertions(+) diff --git a/pre_commit_hooks/requirements_txt_fixer.py b/pre_commit_hooks/requirements_txt_fixer.py index 1ee6fac..623fede 100644 --- a/pre_commit_hooks/requirements_txt_fixer.py +++ b/pre_commit_hooks/requirements_txt_fixer.py @@ -69,6 +69,11 @@ def fix_requirements(f): else: rest = [] + for requirement in requirements: + if b'pkg-resources' in requirement.name: + if b'0.0.0' in requirement.value: + requirements.remove(requirement) + for requirement in sorted(requirements): after.extend(requirement.comments) after.append(requirement.value) diff --git a/tests/requirements_txt_fixer_test.py b/tests/requirements_txt_fixer_test.py index 87e7b0c..437cebd 100644 --- a/tests/requirements_txt_fixer_test.py +++ b/tests/requirements_txt_fixer_test.py @@ -28,6 +28,8 @@ from pre_commit_hooks.requirements_txt_fixer import Requirement FAIL, b'Django\n-e git+ssh://git_url@tag#egg=ocflib\nPyMySQL\n', ), + (b'bar\npkg-resources==0.0.0\nfoo\n', FAIL, b'bar\nfoo\n'), + (b'foo\npkg-resources==0.0.0\nbar\n', FAIL, b'bar\nfoo\n'), ), ) def test_integration(input_s, expected_retval, output, tmpdir): From 9e28aaf2752dc22adaacbd8f65fb8669d3da456a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Socho=C5=84?= Date: Mon, 26 Mar 2018 00:02:23 +0200 Subject: [PATCH 2/4] Simplify check, extend README --- README.md | 2 +- pre_commit_hooks/requirements_txt_fixer.py | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d650c17..eaf8d3e 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ Add this to your `.pre-commit-config.yaml` - `--indent ...` - Control the indentation (either a number for a number of spaces or a string of whitespace). Defaults to 4 spaces. - `--no-sort-keys` - when autofixing, retain the original key ordering (instead of sorting the keys) - `--top-keys comma,separated,keys` - Keys to keep at the top of mappings. -- `requirements-txt-fixer` - Sorts entries in requirements.txt +- `requirements-txt-fixer` - Sorts entries in requirements.txt and removes incorrect entry for `pkg-resources==0.0.0` - `sort-simple-yaml` - Sorts simple YAML files which consist only of top-level keys, preserving comments and blocks. - `trailing-whitespace` - Trims trailing whitespace. - Markdown linebreak trailing spaces preserved for `.md` and`.markdown`; diff --git a/pre_commit_hooks/requirements_txt_fixer.py b/pre_commit_hooks/requirements_txt_fixer.py index 623fede..ee432cb 100644 --- a/pre_commit_hooks/requirements_txt_fixer.py +++ b/pre_commit_hooks/requirements_txt_fixer.py @@ -69,10 +69,11 @@ def fix_requirements(f): else: rest = [] + # find and remove pkg-resources==0.0.0 + # which is automatically added by broken pip package under Debian for requirement in requirements: - if b'pkg-resources' in requirement.name: - if b'0.0.0' in requirement.value: - requirements.remove(requirement) + if requirement.value == b'pkg-resources==0.0.0\n': + requirements.remove(requirement) for requirement in sorted(requirements): after.extend(requirement.comments) From b0d44c7084461e23c16798a3dc3d5d65dfd3f97e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Socho=C5=84?= Date: Mon, 26 Mar 2018 00:17:13 +0200 Subject: [PATCH 3/4] Ensure not to alter list in 'for' loop --- pre_commit_hooks/requirements_txt_fixer.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pre_commit_hooks/requirements_txt_fixer.py b/pre_commit_hooks/requirements_txt_fixer.py index ee432cb..135fe2e 100644 --- a/pre_commit_hooks/requirements_txt_fixer.py +++ b/pre_commit_hooks/requirements_txt_fixer.py @@ -71,9 +71,10 @@ def fix_requirements(f): # find and remove pkg-resources==0.0.0 # which is automatically added by broken pip package under Debian - for requirement in requirements: - if requirement.value == b'pkg-resources==0.0.0\n': - requirements.remove(requirement) + requirements = [ + requirement for requirement in requirements + if requirement.value != b'pkg-resources==0.0.0\n' + ] for requirement in sorted(requirements): after.extend(requirement.comments) From 980fc9bdc302e2afa51c8c02443b51f0dbdc2fa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Socho=C5=84?= Date: Mon, 26 Mar 2018 00:24:36 +0200 Subject: [PATCH 4/4] Fix flake8 error Forgot to rename one var... --- pre_commit_hooks/requirements_txt_fixer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pre_commit_hooks/requirements_txt_fixer.py b/pre_commit_hooks/requirements_txt_fixer.py index 135fe2e..6dcf8d0 100644 --- a/pre_commit_hooks/requirements_txt_fixer.py +++ b/pre_commit_hooks/requirements_txt_fixer.py @@ -72,8 +72,8 @@ def fix_requirements(f): # find and remove pkg-resources==0.0.0 # which is automatically added by broken pip package under Debian requirements = [ - requirement for requirement in requirements - if requirement.value != b'pkg-resources==0.0.0\n' + req for req in requirements + if req.value != b'pkg-resources==0.0.0\n' ] for requirement in sorted(requirements):