Skip to content
This repository has been archived by the owner on Feb 23, 2019. It is now read-only.

Commit

Permalink
Move re-editing logic from cli to utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
kk6 committed Nov 14, 2015
1 parent b771672 commit 098f3e7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
18 changes: 2 additions & 16 deletions pir/cli.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# -*- coding: utf-8 -*-
from __future__ import division, print_function, absolute_import, unicode_literals

import re

import pip
import click

from . import echoes
from .groups import AliasedGroup
from .utils import classify_installed_or_not
from .utils import classify_installed_or_not, re_edit_requirements


@click.group(cls=AliasedGroup)
Expand Down Expand Up @@ -92,19 +90,7 @@ def uninstall(packages, requirement):
else:
uninstalled_packages.append(pkg)

output = []
pattern = re.compile('^[\w0-9\-.]+')
for line in requirement.readlines():
match_obj = pattern.match(line)
if match_obj:
matched_pkg = match_obj.group().lower()
if matched_pkg not in packages:
output.append(line)
else:
output.append(line)
content = '\n'.join(output)
if output[-1] != '\n':
output += '\n'
content = re_edit_requirements(requirement.readlines(), packages)
with open(requirement.name, 'w') as f:
f.write(content)
msg = "Remove the following packages from {requirement}: {packages}"
Expand Down
21 changes: 21 additions & 0 deletions pir/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import division, print_function, absolute_import, unicode_literals

import re

import pip


Expand All @@ -13,3 +15,22 @@ def classify_installed_or_not(packages):
will_install = set(packages) - installed
need_upgrade = set(packages) & installed
return will_install, need_upgrade


def re_edit_requirements(lines, will_remove):
"""
Re-Edit requirements file
:param lines: requirement.readlines()'s return value.
:param will_remove: Will be removed packages.
:return: The contents of the file after re-editing.
"""
pattern = re.compile('^[\w0-9\-.]+')
re_editing = []
for line in lines:
matched = pattern.match(line)
if not matched or matched.group().lower() not in will_remove:
re_editing.append(line)
if re_editing[-1] != '\n':
re_editing.append('\n')
return ''.join(re_editing)
16 changes: 16 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,19 @@ def mockreturn():

rv = classify_installed_or_not(packages)
assert rv == expected


def test_re_edit_requirements():
from pir.utils import re_edit_requirements
before = [
"# requirements.in\n",
"flask\n",
"pytest\n",
"\n",
]
after = """# requirements.in
pytest
"""
content = re_edit_requirements(before, ['flask'])
assert content == after

0 comments on commit 098f3e7

Please sign in to comment.