mirror of
https://github.com/bndr/pipreqs.git
synced 2025-06-06 03:25:21 +00:00
Merge pull request #53 from utgwkk/print-stdout
Implement --print to output list of requirements to stdout
This commit is contained in:
commit
a195c35233
@ -16,6 +16,7 @@ Options:
|
||||
--ignore <dirs>... Ignore extra directories, each separated by a comma
|
||||
--encoding <charset> Use encoding parameter for file open
|
||||
--savepath <file> Save the list of requirements in the given file
|
||||
--print Output the list of requirements in the standard output
|
||||
--force Overwrite existing requirements.txt
|
||||
"""
|
||||
from __future__ import print_function, absolute_import
|
||||
@ -117,6 +118,14 @@ def generate_requirements_file(path, imports):
|
||||
out_file.write('\n'.join(fmt.format(**item) if item['version'] else '{name}'.format(**item)
|
||||
for item in imports) + '\n')
|
||||
|
||||
def output_requirements(imports):
|
||||
logging.debug('Writing {num} requirements: {imports} to stdout'.format(
|
||||
num=len(imports),
|
||||
imports=", ".join([x['name'] for x in imports])
|
||||
))
|
||||
fmt = '{name}=={version}'
|
||||
print('\n'.join(fmt.format(**item) if item['version'] else '{name}'.format(**item)
|
||||
for item in imports))
|
||||
|
||||
def get_imports_info(imports, pypi_server="https://pypi.python.org/pypi/", proxy=None):
|
||||
result = []
|
||||
@ -241,10 +250,14 @@ def init(args):
|
||||
path = (args["--savepath"] if args["--savepath"] else
|
||||
os.path.join(args['<path>'], "requirements.txt"))
|
||||
|
||||
if not args["--savepath"] and not args["--force"] and os.path.exists(path):
|
||||
if not args["--print"] and not args["--savepath"] and not args["--force"] and os.path.exists(path):
|
||||
logging.warning("Requirements.txt already exists, "
|
||||
"use --force to overwrite it")
|
||||
return
|
||||
if args["--print"]:
|
||||
output_requirements(imports)
|
||||
logging.info("Successfully output requirements")
|
||||
else:
|
||||
generate_requirements_file(path, imports)
|
||||
logging.info("Successfully saved requirements file in " + path)
|
||||
|
||||
|
@ -85,7 +85,7 @@ class TestPipreqs(unittest.TestCase):
|
||||
"""
|
||||
Test that all modules we will test upon, are in requirements file
|
||||
"""
|
||||
pipreqs.init({'<path>': self.project, '--savepath': None,
|
||||
pipreqs.init({'<path>': self.project, '--savepath': None, '--print': False,
|
||||
'--use-local': None, '--force': True, '--proxy':None, '--pypi-server':None})
|
||||
assert os.path.exists(self.requirements_path) == 1
|
||||
with open(self.requirements_path, "r") as f:
|
||||
@ -97,7 +97,7 @@ class TestPipreqs(unittest.TestCase):
|
||||
"""
|
||||
Test that items listed in requirements.text are the same as locals expected
|
||||
"""
|
||||
pipreqs.init({'<path>': self.project, '--savepath': None,
|
||||
pipreqs.init({'<path>': self.project, '--savepath': None, '--print': False,
|
||||
'--use-local': True, '--force': True, '--proxy':None, '--pypi-server':None})
|
||||
assert os.path.exists(self.requirements_path) == 1
|
||||
with open(self.requirements_path, "r") as f:
|
||||
@ -111,7 +111,7 @@ class TestPipreqs(unittest.TestCase):
|
||||
Test that we can save requiremnts.tt correctly to a different path
|
||||
"""
|
||||
pipreqs.init({'<path>': self.project, '--savepath':
|
||||
self.alt_requirement_path, '--use-local': None, '--proxy':None, '--pypi-server':None})
|
||||
self.alt_requirement_path, '--use-local': None, '--proxy':None, '--pypi-server':None, '--print': False})
|
||||
assert os.path.exists(self.alt_requirement_path) == 1
|
||||
with open(self.alt_requirement_path, "r") as f:
|
||||
data = f.read().lower()
|
||||
@ -127,7 +127,7 @@ class TestPipreqs(unittest.TestCase):
|
||||
with open(self.requirements_path, "w") as f:
|
||||
f.write("should_not_be_overwritten")
|
||||
pipreqs.init({'<path>': self.project, '--savepath': None,
|
||||
'--use-local': None, '--force': None, '--proxy':None, '--pypi-server':None})
|
||||
'--use-local': None, '--force': None, '--proxy':None, '--pypi-server':None, '--print': False})
|
||||
assert os.path.exists(self.requirements_path) == 1
|
||||
with open(self.requirements_path, "r") as f:
|
||||
data = f.read().lower()
|
||||
@ -149,7 +149,7 @@ class TestPipreqs(unittest.TestCase):
|
||||
"""
|
||||
Test that trying to get a custom pypi sever fails correctly
|
||||
"""
|
||||
self.assertRaises(requests.exceptions.MissingSchema, pipreqs.init, {'<path>': self.project, '--savepath': None,
|
||||
self.assertRaises(requests.exceptions.MissingSchema, pipreqs.init, {'<path>': self.project, '--savepath': None, '--print': False,
|
||||
'--use-local': None, '--force': True, '--proxy': None, '--pypi-server': 'nonexistent'})
|
||||
|
||||
def test_ignored_directory(self):
|
||||
@ -157,7 +157,7 @@ class TestPipreqs(unittest.TestCase):
|
||||
Test --ignore parameter
|
||||
"""
|
||||
pipreqs.init(
|
||||
{'<path>': self.project_with_ignore_directory, '--savepath': None,
|
||||
{'<path>': self.project_with_ignore_directory, '--savepath': None, '--print': False,
|
||||
'--use-local': None, '--force': True,
|
||||
'--proxy':None,
|
||||
'--pypi-server':None,
|
||||
|
Loading…
x
Reference in New Issue
Block a user