~ubuntu-branches/ubuntu/lucid/python2.6/lucid

« back to all changes in this revision

Viewing changes to Lib/lib2to3/main.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-03-11 13:30:19 UTC
  • mto: (10.1.13 sid)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: james.westby@ubuntu.com-20100311133019-sblbooa3uqrkoe70
Tags: upstream-2.6.5~rc2
ImportĀ upstreamĀ versionĀ 2.6.5~rc2

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 
5
5
import sys
6
6
import os
 
7
import difflib
7
8
import logging
8
9
import shutil
9
10
import optparse
11
12
from . import refactor
12
13
 
13
14
 
14
 
class StdoutRefactoringTool(refactor.RefactoringTool):
 
15
def diff_texts(a, b, filename):
 
16
    """Return a unified diff of two strings."""
 
17
    a = a.splitlines()
 
18
    b = b.splitlines()
 
19
    return difflib.unified_diff(a, b, filename, filename,
 
20
                                "(original)", "(refactored)",
 
21
                                lineterm="")
 
22
 
 
23
 
 
24
class StdoutRefactoringTool(refactor.MultiprocessRefactoringTool):
15
25
    """
16
26
    Prints output to stdout.
17
27
    """
18
28
 
19
 
    def __init__(self, fixers, options, explicit, nobackups):
 
29
    def __init__(self, fixers, options, explicit, nobackups, show_diffs):
20
30
        self.nobackups = nobackups
 
31
        self.show_diffs = show_diffs
21
32
        super(StdoutRefactoringTool, self).__init__(fixers, options, explicit)
22
33
 
23
34
    def log_error(self, msg, *args, **kwargs):
24
35
        self.errors.append((msg, args, kwargs))
25
36
        self.logger.error(msg, *args, **kwargs)
26
37
 
27
 
    def write_file(self, new_text, filename, old_text):
 
38
    def write_file(self, new_text, filename, old_text, encoding):
28
39
        if not self.nobackups:
29
40
            # Make backup
30
41
            backup = filename + ".bak"
38
49
            except os.error, err:
39
50
                self.log_message("Can't rename %s to %s", filename, backup)
40
51
        # Actually write the new file
41
 
        super(StdoutRefactoringTool, self).write_file(new_text,
42
 
                                                      filename, old_text)
 
52
        write = super(StdoutRefactoringTool, self).write_file
 
53
        write(new_text, filename, old_text, encoding)
43
54
        if not self.nobackups:
44
55
            shutil.copymode(backup, filename)
45
56
 
46
 
    def print_output(self, lines):
47
 
        for line in lines:
48
 
            print line
 
57
    def print_output(self, old, new, filename, equal):
 
58
        if equal:
 
59
            self.log_message("No changes to %s", filename)
 
60
        else:
 
61
            self.log_message("Refactored %s", filename)
 
62
            if self.show_diffs:
 
63
                diff_lines = diff_texts(old, new, filename)
 
64
                try:
 
65
                    for line in diff_lines:
 
66
                        print line
 
67
                except UnicodeEncodeError:
 
68
                    warn("couldn't encode %s's diff for your terminal" %
 
69
                         (filename,))
 
70
                    return
 
71
 
 
72
 
 
73
def warn(msg):
 
74
    print >> sys.stderr, "WARNING: %s" % (msg,)
49
75
 
50
76
 
51
77
def main(fixer_pkg, args=None):
64
90
                      help="Fix up doctests only")
65
91
    parser.add_option("-f", "--fix", action="append", default=[],
66
92
                      help="Each FIX specifies a transformation; default: all")
 
93
    parser.add_option("-j", "--processes", action="store", default=1,
 
94
                      type="int", help="Run 2to3 concurrently")
67
95
    parser.add_option("-x", "--nofix", action="append", default=[],
68
96
                      help="Prevent a fixer from being run.")
69
97
    parser.add_option("-l", "--list-fixes", action="store_true",
72
100
                      help="Modify the grammar so that print() is a function")
73
101
    parser.add_option("-v", "--verbose", action="store_true",
74
102
                      help="More verbose logging")
 
103
    parser.add_option("--no-diffs", action="store_true",
 
104
                      help="Don't show diffs of the refactoring")
75
105
    parser.add_option("-w", "--write", action="store_true",
76
106
                      help="Write back modified files")
77
107
    parser.add_option("-n", "--nobackups", action="store_true", default=False,
79
109
 
80
110
    # Parse command line arguments
81
111
    refactor_stdin = False
 
112
    flags = {}
82
113
    options, args = parser.parse_args(args)
 
114
    if not options.write and options.no_diffs:
 
115
        warn("not writing files and not printing diffs; that's not very useful")
83
116
    if not options.write and options.nobackups:
84
117
        parser.error("Can't use -n without -w")
85
118
    if options.list_fixes:
89
122
        if not args:
90
123
            return 0
91
124
    if not args:
92
 
        print >>sys.stderr, "At least one file or directory argument required."
93
 
        print >>sys.stderr, "Use --help to show usage."
 
125
        print >> sys.stderr, "At least one file or directory argument required."
 
126
        print >> sys.stderr, "Use --help to show usage."
94
127
        return 2
95
128
    if "-" in args:
96
129
        refactor_stdin = True
97
130
        if options.write:
98
 
            print >>sys.stderr, "Can't write to stdin."
 
131
            print >> sys.stderr, "Can't write to stdin."
99
132
            return 2
 
133
    if options.print_function:
 
134
        flags["print_function"] = True
100
135
 
101
136
    # Set up logging handler
102
137
    level = logging.DEBUG if options.verbose else logging.INFO
103
138
    logging.basicConfig(format='%(name)s: %(message)s', level=level)
104
139
 
105
140
    # Initialize the refactoring tool
106
 
    rt_opts = {"print_function" : options.print_function}
107
141
    avail_fixes = set(refactor.get_fixers_from_package(fixer_pkg))
108
142
    unwanted_fixes = set(fixer_pkg + ".fix_" + fix for fix in options.nofix)
109
143
    explicit = set()
118
152
    else:
119
153
        requested = avail_fixes.union(explicit)
120
154
    fixer_names = requested.difference(unwanted_fixes)
121
 
    rt = StdoutRefactoringTool(sorted(fixer_names), rt_opts, sorted(explicit),
122
 
                               options.nobackups)
 
155
    rt = StdoutRefactoringTool(sorted(fixer_names), flags, sorted(explicit),
 
156
                               options.nobackups, not options.no_diffs)
123
157
 
124
158
    # Refactor all files and directories passed as arguments
125
159
    if not rt.errors:
126
160
        if refactor_stdin:
127
161
            rt.refactor_stdin()
128
162
        else:
129
 
            rt.refactor(args, options.write, options.doctests_only)
 
163
            try:
 
164
                rt.refactor(args, options.write, options.doctests_only,
 
165
                            options.processes)
 
166
            except refactor.MultiprocessingUnsupported:
 
167
                assert options.processes > 1
 
168
                print >> sys.stderr, "Sorry, -j isn't " \
 
169
                    "supported on this platform."
 
170
                return 1
130
171
        rt.summarize()
131
172
 
132
173
    # Return error status (0 if rt.errors is zero)