Mercurial > repos > shellac > guppy_basecaller
comparison env/lib/python3.7/site-packages/libpasteurize/main.py @ 0:26e78fe6e8c4 draft
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author | shellac |
---|---|
date | Sat, 02 May 2020 07:14:21 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:26e78fe6e8c4 |
---|---|
1 """ | |
2 pasteurize: automatic conversion of Python 3 code to clean 2/3 code | |
3 =================================================================== | |
4 | |
5 ``pasteurize`` attempts to convert existing Python 3 code into source-compatible | |
6 Python 2 and 3 code. | |
7 | |
8 Use it like this on Python 3 code: | |
9 | |
10 $ pasteurize --verbose mypython3script.py | |
11 | |
12 This removes any Py3-only syntax (e.g. new metaclasses) and adds these | |
13 import lines: | |
14 | |
15 from __future__ import absolute_import | |
16 from __future__ import division | |
17 from __future__ import print_function | |
18 from __future__ import unicode_literals | |
19 from future import standard_library | |
20 standard_library.install_hooks() | |
21 from builtins import * | |
22 | |
23 To write changes to the files, use the -w flag. | |
24 | |
25 It also adds any other wrappers needed for Py2/3 compatibility. | |
26 | |
27 Note that separate stages are not available (or needed) when converting from | |
28 Python 3 with ``pasteurize`` as they are when converting from Python 2 with | |
29 ``futurize``. | |
30 | |
31 The --all-imports option forces adding all ``__future__`` imports, | |
32 ``builtins`` imports, and standard library aliases, even if they don't | |
33 seem necessary for the current state of each module. (This can simplify | |
34 testing, and can reduce the need to think about Py2 compatibility when editing | |
35 the code further.) | |
36 | |
37 """ | |
38 | |
39 from __future__ import (absolute_import, print_function, unicode_literals) | |
40 | |
41 import sys | |
42 import logging | |
43 import optparse | |
44 from lib2to3.main import main, warn, StdoutRefactoringTool | |
45 from lib2to3 import refactor | |
46 | |
47 from future import __version__ | |
48 from libpasteurize.fixes import fix_names | |
49 | |
50 | |
51 def main(args=None): | |
52 """Main program. | |
53 | |
54 Returns a suggested exit status (0, 1, 2). | |
55 """ | |
56 # Set up option parser | |
57 parser = optparse.OptionParser(usage="pasteurize [options] file|dir ...") | |
58 parser.add_option("-V", "--version", action="store_true", | |
59 help="Report the version number of pasteurize") | |
60 parser.add_option("-a", "--all-imports", action="store_true", | |
61 help="Adds all __future__ and future imports to each module") | |
62 parser.add_option("-f", "--fix", action="append", default=[], | |
63 help="Each FIX specifies a transformation; default: all") | |
64 parser.add_option("-j", "--processes", action="store", default=1, | |
65 type="int", help="Run 2to3 concurrently") | |
66 parser.add_option("-x", "--nofix", action="append", default=[], | |
67 help="Prevent a fixer from being run.") | |
68 parser.add_option("-l", "--list-fixes", action="store_true", | |
69 help="List available transformations") | |
70 # parser.add_option("-p", "--print-function", action="store_true", | |
71 # help="Modify the grammar so that print() is a function") | |
72 parser.add_option("-v", "--verbose", action="store_true", | |
73 help="More verbose logging") | |
74 parser.add_option("--no-diffs", action="store_true", | |
75 help="Don't show diffs of the refactoring") | |
76 parser.add_option("-w", "--write", action="store_true", | |
77 help="Write back modified files") | |
78 parser.add_option("-n", "--nobackups", action="store_true", default=False, | |
79 help="Don't write backups for modified files.") | |
80 | |
81 # Parse command line arguments | |
82 refactor_stdin = False | |
83 flags = {} | |
84 options, args = parser.parse_args(args) | |
85 fixer_pkg = 'libpasteurize.fixes' | |
86 avail_fixes = fix_names | |
87 flags["print_function"] = True | |
88 | |
89 if not options.write and options.no_diffs: | |
90 warn("not writing files and not printing diffs; that's not very useful") | |
91 if not options.write and options.nobackups: | |
92 parser.error("Can't use -n without -w") | |
93 if options.version: | |
94 print(__version__) | |
95 return 0 | |
96 if options.list_fixes: | |
97 print("Available transformations for the -f/--fix option:") | |
98 for fixname in sorted(avail_fixes): | |
99 print(fixname) | |
100 if not args: | |
101 return 0 | |
102 if not args: | |
103 print("At least one file or directory argument required.", | |
104 file=sys.stderr) | |
105 print("Use --help to show usage.", file=sys.stderr) | |
106 return 2 | |
107 if "-" in args: | |
108 refactor_stdin = True | |
109 if options.write: | |
110 print("Can't write to stdin.", file=sys.stderr) | |
111 return 2 | |
112 | |
113 # Set up logging handler | |
114 level = logging.DEBUG if options.verbose else logging.INFO | |
115 logging.basicConfig(format='%(name)s: %(message)s', level=level) | |
116 | |
117 unwanted_fixes = set() | |
118 for fix in options.nofix: | |
119 if ".fix_" in fix: | |
120 unwanted_fixes.add(fix) | |
121 else: | |
122 # Infer the full module name for the fixer. | |
123 # First ensure that no names clash (e.g. | |
124 # lib2to3.fixes.fix_blah and libfuturize.fixes.fix_blah): | |
125 found = [f for f in avail_fixes | |
126 if f.endswith('fix_{0}'.format(fix))] | |
127 if len(found) > 1: | |
128 print("Ambiguous fixer name. Choose a fully qualified " | |
129 "module name instead from these:\n" + | |
130 "\n".join(" " + myf for myf in found), | |
131 file=sys.stderr) | |
132 return 2 | |
133 elif len(found) == 0: | |
134 print("Unknown fixer. Use --list-fixes or -l for a list.", | |
135 file=sys.stderr) | |
136 return 2 | |
137 unwanted_fixes.add(found[0]) | |
138 | |
139 extra_fixes = set() | |
140 if options.all_imports: | |
141 prefix = 'libpasteurize.fixes.' | |
142 extra_fixes.add(prefix + 'fix_add_all__future__imports') | |
143 extra_fixes.add(prefix + 'fix_add_future_standard_library_import') | |
144 extra_fixes.add(prefix + 'fix_add_all_future_builtins') | |
145 | |
146 explicit = set() | |
147 if options.fix: | |
148 all_present = False | |
149 for fix in options.fix: | |
150 if fix == 'all': | |
151 all_present = True | |
152 else: | |
153 if ".fix_" in fix: | |
154 explicit.add(fix) | |
155 else: | |
156 # Infer the full module name for the fixer. | |
157 # First ensure that no names clash (e.g. | |
158 # lib2to3.fixes.fix_blah and libpasteurize.fixes.fix_blah): | |
159 found = [f for f in avail_fixes | |
160 if f.endswith('fix_{0}'.format(fix))] | |
161 if len(found) > 1: | |
162 print("Ambiguous fixer name. Choose a fully qualified " | |
163 "module name instead from these:\n" + | |
164 "\n".join(" " + myf for myf in found), | |
165 file=sys.stderr) | |
166 return 2 | |
167 elif len(found) == 0: | |
168 print("Unknown fixer. Use --list-fixes or -l for a list.", | |
169 file=sys.stderr) | |
170 return 2 | |
171 explicit.add(found[0]) | |
172 if len(explicit & unwanted_fixes) > 0: | |
173 print("Conflicting usage: the following fixers have been " | |
174 "simultaneously requested and disallowed:\n" + | |
175 "\n".join(" " + myf for myf in (explicit & unwanted_fixes)), | |
176 file=sys.stderr) | |
177 return 2 | |
178 requested = avail_fixes.union(explicit) if all_present else explicit | |
179 else: | |
180 requested = avail_fixes.union(explicit) | |
181 | |
182 fixer_names = requested | extra_fixes - unwanted_fixes | |
183 | |
184 # Initialize the refactoring tool | |
185 rt = StdoutRefactoringTool(sorted(fixer_names), flags, set(), | |
186 options.nobackups, not options.no_diffs) | |
187 | |
188 # Refactor all files and directories passed as arguments | |
189 if not rt.errors: | |
190 if refactor_stdin: | |
191 rt.refactor_stdin() | |
192 else: | |
193 try: | |
194 rt.refactor(args, options.write, None, | |
195 options.processes) | |
196 except refactor.MultiprocessingUnsupported: | |
197 assert options.processes > 1 | |
198 print("Sorry, -j isn't " \ | |
199 "supported on this platform.", file=sys.stderr) | |
200 return 1 | |
201 rt.summarize() | |
202 | |
203 # Return error status (0 if rt.errors is zero) | |
204 return int(bool(rt.errors)) |