Skip to content

Commit

Permalink
Support comparison operators, close damnever#37
Browse files Browse the repository at this point in the history
  • Loading branch information
damnever committed Apr 11, 2019
1 parent a7fe92f commit ff81baf
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
33 changes: 21 additions & 12 deletions pigar/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ class Main(object):
def __init__(self):
# Parse command arguments.
(log_level, updatedb, check_path, names, ignores, save_path,
project_path) = parse_args()
project_path, comparison_operator) = parse_args()
# Enable logging.
enable_pretty_logging(log_level=log_level)
# Just allow do one thing at each time.
if updatedb:
self.update_db()
elif check_path:
self.check_reqs_latest_version(check_path, ignores)
self.check_reqs_latest_version(
check_path, ignores, comparison_operator)
elif names:
self.search_package_by_name(names)
else:
self.generate_reqs(save_path, project_path, ignores)
self.generate_reqs(save_path, project_path,
ignores, comparison_operator)

@property
def installed_pkgs(self):
Expand All @@ -57,7 +59,8 @@ def search_package_by_name(self, names):
msg += 'Maybe you need update database.'
print(Color.YELLOW(msg))

def check_reqs_latest_version(self, check_path, ignores):
def check_reqs_latest_version(self, check_path, ignores,
comparison_operator):
"""Check requirements latest version."""
print(Color.BLUE('Starting check requirements latest version ...'))
files = list()
Expand All @@ -74,7 +77,8 @@ def check_reqs_latest_version(self, check_path, ignores):
print(Color.YELLOW('Requirements file not found, '
'generate requirements ...'))
save_path = os.path.join(check_path, 'requirements.txt')
self.generate_reqs(save_path, check_path, ignores)
self.generate_reqs(save_path, check_path,
ignores, comparison_operator)
files.append(save_path)
else:
files.append(check_path)
Expand All @@ -97,19 +101,23 @@ def check_reqs_latest_version(self, check_path, ignores):
print()
print_table(pkg_versions)

def generate_reqs(self, save_path, check_path, ignores):
gr = GenerateReqs(save_path, check_path, ignores, self.installed_pkgs)
def generate_reqs(self, save_path, check_path,
ignores, comparison_operator):
gr = GenerateReqs(save_path, check_path, ignores,
self.installed_pkgs, comparison_operator)
gr.generate_reqs()


class GenerateReqs(object):

def __init__(self, save_path, project_path, ignores, installed_pkgs):
def __init__(self, save_path, project_path, ignores,
installed_pkgs, comparison_operator='=='):
self._save_path = save_path
self._project_path = project_path
self._ignores = ignores
self._installed_pkgs = installed_pkgs
self._maybe_local_mods = set()
self._comparison_operator = comparison_operator

def generate_reqs(self):
"""Generate requirements for `project_path`, save file in
Expand All @@ -136,7 +144,7 @@ def generate_reqs(self):
if answer in ('y', 'yes'):
print(Color.BLUE('Checking modules on the PyPI...'))
for name, detail in guess.sorted_items():
logger.info('Checking {0} on the PyPI ...'.format(name))
logger.info('Checking %s on the PyPI ...', name)
with database() as db:
rows = db.query_all(name)
pkgs = [row.package for row in rows]
Expand Down Expand Up @@ -179,7 +187,7 @@ def extract_reqs(self):

logger.info('Check module in local environment.')
for name in candidates:
logger.info('Checking module: {0}'.format(name))
logger.info('Checking module: %s', name)
if name in self._installed_pkgs:
pkg_name, version = self._installed_pkgs[name]
reqs.add(pkg_name, version, modules[name])
Expand All @@ -201,7 +209,8 @@ def _write_reqs(self, reqs):
if k == '-e':
f.write('{0} {1}\n'.format(k, v.version))
elif v:
f.write('{0} == {1}\n'.format(k, v.version))
f.write('{0} {1} {2}\n'.format(
k, self._comparison_operator, v.version))
else:
f.write('{0}\n'.format(k))

Expand All @@ -217,7 +226,7 @@ def _filter_modules(self, modules, local_mods):

logger.info('Filtering modules ...')
for module in modules:
logger.info('Checking module: {0}'.format(module))
logger.info('Checking module: %s', module)
if not module or module.startswith('.'):
continue
if module in local_mods:
Expand Down
18 changes: 17 additions & 1 deletion pigar/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,21 @@ def parse_args(args=None):
type=projectpath_check,
default=[os.getcwd()],
help='project path, which is directory, *used for* default action')
parser.add_argument(
'-o', '--comparison-operator',
dest='comparison_operator',
nargs=1,
type=comparison_operator_check,
default=['=='],
help='The comparison operator for versions, alternatives: [==, ~=, >=]'
)
if args is None:
args = parser.parse_args()
else:
args = parser.parse_args(args=args)
return (args.log_level[0], args.update_db, args.check_path,
args.search_names, args.ignores, args.save_path[0],
args.project_path[0])
args.project_path[0], args.comparison_operator[0])


def log_level_check(level):
Expand Down Expand Up @@ -115,3 +123,11 @@ def projectpath_check(path):
return path
msg = '"{0}" is not a valid directory path.'.format(path)
raise argparse.ArgumentTypeError(msg)


def comparison_operator_check(op, supported_ops=('==', '~=', '>=')):
if op in supported_ops:
return op
msg = 'invalid operator: {0}, supported operators: {1}'.format(
op, supported_ops)
raise argparse.ArgumentTypeError(msg)
9 changes: 8 additions & 1 deletion pigar/tests/test_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class CmdTests(unittest.TestCase):

def setUp(self):
reqs_path = os.path.join(CUR_DIR, 'requirements.txt')
self._default_args = ['error', False, None, [], [], reqs_path, CUR_DIR]
self._default_args = ['error', False, None, [], [], reqs_path,
CUR_DIR, '==']

def tearDown(self):
del self._default_args
Expand Down Expand Up @@ -71,3 +72,9 @@ def test_project_path(self):
target = self._default_args
target[6] = args[-1]
self.assertListEqual(list(parse_args(args)), target)

def test_comparison_operator(self):
args = ['-o', '>=']
target = self._default_args
target[7] = args[-1]
self.assertListEqual(list(parse_args(args)), target)

0 comments on commit ff81baf

Please sign in to comment.