blob: 939f4f7c43dff1cb5f954bb0658017b290da7963 [file] [log] [blame]
Benjamin Peterson8951b612008-09-03 02:27:16 +00001"""
2Main program for 2to3.
3"""
4
5import sys
6import os
7import logging
8import optparse
9
10from . import refactor
11
12
13def main(fixer_pkg, args=None):
14 """Main program.
15
16 Args:
17 fixer_pkg: the name of a package where the fixers are located.
18 args: optional; a list of command line arguments. If omitted,
19 sys.argv[1:] is used.
20
21 Returns a suggested exit status (0, 1, 2).
22 """
23 # Set up option parser
24 parser = optparse.OptionParser(usage="refactor.py [options] file|dir ...")
25 parser.add_option("-d", "--doctests_only", action="store_true",
26 help="Fix up doctests only")
27 parser.add_option("-f", "--fix", action="append", default=[],
28 help="Each FIX specifies a transformation; default all")
29 parser.add_option("-l", "--list-fixes", action="store_true",
30 help="List available transformations (fixes/fix_*.py)")
31 parser.add_option("-p", "--print-function", action="store_true",
32 help="Modify the grammar so that print() is a function")
33 parser.add_option("-v", "--verbose", action="store_true",
34 help="More verbose logging")
35 parser.add_option("-w", "--write", action="store_true",
36 help="Write back modified files")
37
38 # Parse command line arguments
39 refactor_stdin = False
40 options, args = parser.parse_args(args)
41 if options.list_fixes:
Benjamin Petersonc0a40ab2008-09-11 21:05:25 +000042 print("Available transformations for the -f/--fix option:")
Benjamin Peterson8951b612008-09-03 02:27:16 +000043 for fixname in refactor.get_all_fix_names(fixer_pkg):
Benjamin Petersonc0a40ab2008-09-11 21:05:25 +000044 print(fixname)
Benjamin Peterson8951b612008-09-03 02:27:16 +000045 if not args:
46 return 0
47 if not args:
Benjamin Petersonc0a40ab2008-09-11 21:05:25 +000048 print("At least one file or directory argument required.", file=sys.stderr)
49 print("Use --help to show usage.", file=sys.stderr)
Benjamin Peterson8951b612008-09-03 02:27:16 +000050 return 2
51 if "-" in args:
52 refactor_stdin = True
53 if options.write:
Benjamin Petersonc0a40ab2008-09-11 21:05:25 +000054 print("Can't write to stdin.", file=sys.stderr)
Benjamin Peterson8951b612008-09-03 02:27:16 +000055 return 2
56
57 # Set up logging handler
58 level = logging.DEBUG if options.verbose else logging.INFO
59 logging.basicConfig(format='%(name)s: %(message)s', level=level)
60
61 # Initialize the refactoring tool
62 rt_opts = {"print_function" : options.print_function}
63 avail_names = refactor.get_fixers_from_package(fixer_pkg)
64 explicit = []
65 if options.fix:
66 explicit = [fixer_pkg + ".fix_" + fix
67 for fix in options.fix if fix != "all"]
68 fixer_names = avail_names if "all" in options.fix else explicit
69 else:
70 fixer_names = avail_names
71 rt = refactor.RefactoringTool(fixer_names, rt_opts, explicit=explicit)
72
73 # Refactor all files and directories passed as arguments
74 if not rt.errors:
75 if refactor_stdin:
76 rt.refactor_stdin()
77 else:
78 rt.refactor(args, options.write, options.doctests_only)
79 rt.summarize()
80
81 # Return error status (0 if rt.errors is zero)
82 return int(bool(rt.errors))
83
84
85if __name__ == "__main__":
86 sys.exit(main())