diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 2194189..6aa2471 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -5,6 +5,7 @@ Change Log ------------------ * Windows compatibility: "" quote Python interpreter path (#115, @fcollonval) +* Add ``--dry-run`` flag (#122) 0.3.7 - 2020-01-05 ------------------ diff --git a/README.rst b/README.rst index ebad5b6..45eb741 100644 --- a/README.rst +++ b/README.rst @@ -100,6 +100,10 @@ configuration summary of filter and attributes if installed nbstripout --status +Do a dry run and only list which files would have been stripped: :: + + nbstripout --dry-run FILE.ipynb [FILE2.ipynb ...] + Print the version: :: nbstripout --version diff --git a/nbstripout/_nbstripout.py b/nbstripout/_nbstripout.py index 41b480c..678091f 100644 --- a/nbstripout/_nbstripout.py +++ b/nbstripout/_nbstripout.py @@ -63,6 +63,10 @@ nbstripout --status +Do a dry run and only list which files would have been stripped: :: + + nbstripout --dry-run FILE.ipynb [FILE2.ipynb ...] + Print the version: :: nbstripout --version @@ -234,6 +238,8 @@ def main(): from subprocess import check_output, CalledProcessError parser = ArgumentParser(epilog=__doc__, formatter_class=RawDescriptionHelpFormatter) task = parser.add_mutually_exclusive_group() + task.add_argument('--dry-run', action='store_true', + help='Print which notebooks would have been stripped') task.add_argument('--install', action='store_true', help='Install nbstripout in the current repository (set ' 'up the git filter and attributes)') @@ -306,6 +312,10 @@ def main(): with io.open(filename, 'r', encoding='utf8') as f: nb = read(f, as_version=NO_CONVERT) nb = strip_output(nb, args.keep_output, args.keep_count, extra_keys) + if args.dry_run: + output_stream.write('Dry run: would have stripped {}\n'.format( + filename)) + continue if args.textconv: write(nb, output_stream) output_stream.flush() @@ -324,8 +334,12 @@ def main(): try: nb = strip_output(read(input_stream, as_version=NO_CONVERT), args.keep_output, args.keep_count, extra_keys) - write(nb, output_stream) - output_stream.flush() + if args.dry_run: + output_stream.write('Dry run: would have stripped input from ' + 'stdin\n') + else: + write(nb, output_stream) + output_stream.flush() except NotJSONError: print('No valid notebook detected', file=sys.stderr) sys.exit(1) diff --git a/tests/test-dry-run.t b/tests/test-dry-run.t new file mode 100644 index 0000000..fce3ab4 --- /dev/null +++ b/tests/test-dry-run.t @@ -0,0 +1,4 @@ + $ cat ${TESTDIR}/test_metadata.ipynb | ${NBSTRIPOUT_EXE:-nbstripout} --dry-run + Dry run: would have stripped input from stdin + $ ${NBSTRIPOUT_EXE:-nbstripout} --dry-run ${TESTDIR}/test_metadata.ipynb + Dry run: would have stripped .*/test_metadata.ipynb (re) \ No newline at end of file