-
Notifications
You must be signed in to change notification settings - Fork 51
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') | ||
|
||
def main(args=None): | ||
"""Main program. | ||
|
||
|
@@ -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, | ||
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
parser.error("Can't use --add-suffix without -n.") | ||
|
||
# Set up logging handler | ||
level = logging.DEBUG if options.verbose else logging.INFO | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
There was a problem hiding this comment.
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.