diff --git a/.gitignore b/.gitignore index 38ce427..3ea542f 100644 --- a/.gitignore +++ b/.gitignore @@ -121,3 +121,6 @@ dmypy.json # Pyre type checker .pyre/ + +# PyCharm +.idea \ No newline at end of file diff --git a/sort_requirements/__init__.py b/sort_requirements/__init__.py index 73c49b2..cb5af96 100644 --- a/sort_requirements/__init__.py +++ b/sort_requirements/__init__.py @@ -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()) diff --git a/tests/fixtures/duplicates.txt b/tests/fixtures/duplicates.txt new file mode 100644 index 0000000..5e245a7 --- /dev/null +++ b/tests/fixtures/duplicates.txt @@ -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 diff --git a/tests/test_sort_requirements.py b/tests/test_sort_requirements.py index b4c132d..f4eb781 100644 --- a/tests/test_sort_requirements.py +++ b/tests/test_sort_requirements.py @@ -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