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