diff --git a/pipreqs/pipreqs.py b/pipreqs/pipreqs.py index 0877173..a05daaa 100644 --- a/pipreqs/pipreqs.py +++ b/pipreqs/pipreqs.py @@ -24,6 +24,8 @@ --no-follow-links Do not follow symbolic links in the project --encoding Use encoding parameter for file open --savepath Save the list of requirements in the given file + --output Save the list of requirements in the given file, or + use - for standard output --print Output the list of requirements in the standard output --force Overwrite existing requirements.txt @@ -510,6 +512,12 @@ def init(args): scan_noteboooks = args.get("--scan-notebooks", False) handle_scan_noteboooks() + output_path = args.get("--output") + if output_path == "-": + args["--print"] = True + elif output_path: + args["--savepath"] = output_path + input_path = args[""] if encoding is None: diff --git a/tests/test_pipreqs.py b/tests/test_pipreqs.py index 5e046e0..2c34e91 100644 --- a/tests/test_pipreqs.py +++ b/tests/test_pipreqs.py @@ -235,6 +235,61 @@ def test_init_savepath(self): for item in self.modules2: self.assertTrue(item.lower() in data) + def test_init_output_path(self): + """ + Test that --output can save requirements.txt to a different path + """ + pipreqs.init( + { + "": self.project, + "--savepath": None, + "--output": self.alt_requirement_path, + "--use-local": None, + "--proxy": None, + "--pypi-server": None, + "--print": False, + "--diff": None, + "--clean": None, + "--mode": None, + } + ) + assert os.path.exists(self.alt_requirement_path) == 1 + with open(self.alt_requirement_path, "r") as f: + data = f.read().lower() + for item in self.modules[:-3]: + self.assertTrue(item.lower() in data) + + def test_init_output_dash(self): + """ + Test that --output - prints requirements to stdout + """ + captured_output = StringIO() + original_stdout = sys.stdout + sys.stdout = captured_output + try: + pipreqs.init( + { + "": self.project, + "--savepath": None, + "--output": "-", + "--print": False, + "--use-local": None, + "--force": None, + "--proxy": None, + "--pypi-server": None, + "--diff": None, + "--clean": None, + "--mode": None, + } + ) + finally: + sys.stdout = original_stdout + + stdout_content = captured_output.getvalue().lower() + for item in self.modules[:-3]: + self.assertTrue(item.lower() in stdout_content) + self.assertFalse(os.path.exists(self.requirements_path)) + def test_init_overwrite(self): """ Test that if requiremnts.txt exists, it will not be