Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the --output-dir and --add-suffix options from 2to3 #160

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion libmodernize/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def format_usage(usage):
"""Method that doesn't output "Usage:" prefix"""
return usage

_lib2to3_has_output_dir = hasattr(StdoutRefactoringTool([], {}, False, False, False), '_output_dir')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can drop this check - we don't aim to support the older Pythons that don't have this.


def main(args=None):
"""Main program.

Expand Down Expand Up @@ -56,6 +58,15 @@ def main(args=None):
help="Write back modified files.")
parser.add_option("-n", "--nobackups", action="store_true", default=False,
help="Don't write backups for modified files.")
parser.add_option("-o", "--output-dir", action="store", default="",
help="Put output files in this directory "
"instead of overwriting the input files. Requires -n. "
"Available on Python==2.7 and Python>=3.2.")
parser.add_option("--add-suffix", action="store", default="",
help="Append this string to all output filenames."
" Requires -n if non-empty. "
"Available on Python==2.7 and Python>=3.2. "
"ex: --add-suffix='3' will generate .py3 files.")
parser.add_option("--six-unicode", action="store_true", default=False,
help="Wrap unicode literals in six.u().")
parser.add_option("--future-unicode", action="store_true", default=False,
Expand Down Expand Up @@ -96,6 +107,16 @@ def main(args=None):
return 2
if options.print_function:
flags["print_function"] = True
if not _lib2to3_has_output_dir and (options.output_dir or options.add_suffix):
print("--output-dir and --add-suffix are not supported "
"with this version of Python.", file=sys.stderr)
return 2
# If we allowed these, the original files would be renamed to backup names
# but not replaced.
if options.output_dir and not options.nobackups:
parser.error("Can't use --output-dir/-o without -n.")
if options.add_suffix and not options.nobackups:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These seem like strange checks. Does 2to3 do the same thing? Can we just make either of these options imply nobackups? The backups are only needed because it overwrites the source files - if you tell it not to do that, there's no need to think about backups.

parser.error("Can't use --add-suffix without -n.")

# Set up logging handler
level = logging.DEBUG if options.verbose else logging.INFO
Expand Down Expand Up @@ -128,8 +149,11 @@ def main(args=None):
else:
requested = default_fixes
fixer_names = requested.difference(unwanted_fixes)
newer_options = {}
if (options.output_dir or options.add_suffix):
newer_options = dict(output_dir=options.output_dir, append_suffix=options.add_suffix)
rt = StdoutRefactoringTool(sorted(fixer_names), flags, sorted(explicit),
options.nobackups, not options.no_diffs)
options.nobackups, not options.no_diffs, **newer_options)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified when we assume that we're on a new enough Python.


# Refactor all files and directories passed as arguments
if not rt.errors:
Expand Down