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

Add/remove duplicates #8

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,6 @@ dmypy.json

# Pyre type checker
.pyre/

# PyCharm
.idea
5 changes: 3 additions & 2 deletions sort_requirements/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import re


VERSION = (1, 3, 0)
VERSION = (1, 4, 0)
DEPS_RE = r"((?:#[^\n]+?\n)*)([^\n]+?)([=!~>]=)([^\\\n]+)((?:\\\n[^\\\n]+)*)"


__version__ = ".".join(str(v) for v in VERSION)


def sort_requirements(requirements):
def sort_requirements(requirements, remove_duplicates=False):

matches = re.findall(DEPS_RE, requirements)
data = re.sub(DEPS_RE, "{}", requirements)
matches = sorted(matches, key=lambda d: d[1].lower())
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/duplicates.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
bottle==1.0
bottle==1.0
apple!=0.2
core~=3.1.1
core~=3.1.1
east>=1.0
delta==2.0
8 changes: 8 additions & 0 deletions tests/test_sort_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ def test_complex_file(self):
expected = f.read()
txt = sort_requirements(txt)
assert txt == expected

def test_duplicates_file(self):
with open(os.path.join(FIXTURES_DIR, "duplicates.txt"), "r") as f:
txt = f.read()
with open(os.path.join(FIXTURES_DIR, "simple-sorted.txt"), "r") as f:
expected = f.read()
txt = sort_requirements(txt, remove_duplicates=True)
assert txt == expected