|
1
|
|
|
""" |
|
2
|
|
|
Command line interface implementation for pyclean. |
|
3
|
|
|
""" |
|
4
|
|
|
import argparse |
|
5
|
|
|
import logging |
|
6
|
|
|
|
|
7
|
|
|
from . import __version__, compat, modern |
|
8
|
|
|
|
|
9
|
|
|
log = logging.getLogger(__name__) |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
def parse_arguments(): |
|
13
|
|
|
""" |
|
14
|
|
|
Parse and handle CLI arguments |
|
15
|
|
|
""" |
|
16
|
|
|
parser = argparse.ArgumentParser( |
|
17
|
|
|
description='Remove byte-compiled files for a package', |
|
18
|
|
|
) |
|
19
|
|
|
|
|
20
|
|
|
parser.add_argument('--version', action='version', version=__version__) |
|
21
|
|
|
parser.add_argument('-V', metavar='VERSION', dest='version', |
|
22
|
|
|
help='specify Python version to clean') |
|
23
|
|
|
parser.add_argument('-p', '--package', metavar='PACKAGE', |
|
24
|
|
|
action='append', default=[], |
|
25
|
|
|
help='Debian package to byte-compile ' |
|
26
|
|
|
'(may be specified multiple times)') |
|
27
|
|
|
parser.add_argument('directory', nargs='*', |
|
28
|
|
|
help='Directory tree (or file) to byte-compile') |
|
29
|
|
|
parser.add_argument('-i', '--ignore', metavar='DIRECTORY', |
|
30
|
|
|
action='append', default=['.git', '.tox', '.venv'], |
|
31
|
|
|
help='Directory that should be ignored ' |
|
32
|
|
|
'(may be specified multiple times; ' |
|
33
|
|
|
'default: %(default)s)') |
|
34
|
|
|
parser.add_argument('--legacy', action='store_true', |
|
35
|
|
|
help='Use legacy Debian implementation (autodetect)') |
|
36
|
|
|
parser.add_argument('-n', '--dry-run', action='store_true', |
|
37
|
|
|
help='Show what would be done') |
|
38
|
|
|
|
|
39
|
|
|
verbosity = parser.add_mutually_exclusive_group() |
|
40
|
|
|
verbosity.add_argument('-q', '--quiet', action='store_true', |
|
41
|
|
|
help='Be quiet') |
|
42
|
|
|
verbosity.add_argument('-v', '--verbose', action='store_true', |
|
43
|
|
|
help='Be more verbose') |
|
44
|
|
|
|
|
45
|
|
|
args = parser.parse_args() |
|
46
|
|
|
init_logging(args) |
|
47
|
|
|
|
|
48
|
|
|
if not (args.package or args.directory): |
|
49
|
|
|
parser.error('A directory (or files) or a list of packages ' |
|
50
|
|
|
'must be specified.') |
|
51
|
|
|
|
|
52
|
|
|
args.ignore = parse_ignore_list(args.ignore) |
|
53
|
|
|
log.debug("Ignored directories: %s", ','.join(args.ignore)) |
|
54
|
|
|
|
|
55
|
|
|
return args |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
def parse_ignore_list(directory_list): |
|
59
|
|
|
""" |
|
60
|
|
|
Split up and reintegrate folder names in values having a comma-separated |
|
61
|
|
|
list of directory names. |
|
62
|
|
|
|
|
63
|
|
|
>>> parse_ignore_list(['a,b,c']) |
|
64
|
|
|
['a', 'b', 'c'] |
|
65
|
|
|
>>> parse_ignore_list(['a', 'b,c']) |
|
66
|
|
|
['a', 'b', 'c'] |
|
67
|
|
|
>>> parse_ignore_list(['a , b, c']) |
|
68
|
|
|
['a', 'b', 'c'] |
|
69
|
|
|
>>> parse_ignore_list([',,a,,,b,,']) |
|
70
|
|
|
['a', 'b'] |
|
71
|
|
|
>>> parse_ignore_list([',a', 'b,']) |
|
72
|
|
|
['a', 'b'] |
|
73
|
|
|
""" |
|
74
|
|
|
ignore_list = [] |
|
75
|
|
|
|
|
76
|
|
|
for dirname in directory_list: |
|
77
|
|
|
if ',' not in dirname: |
|
78
|
|
|
ignore_list += [dirname] |
|
79
|
|
|
else: |
|
80
|
|
|
ignore_list += [ |
|
81
|
|
|
token.strip() for token in dirname.split(',') if token.strip() |
|
82
|
|
|
] |
|
83
|
|
|
|
|
84
|
|
|
return sorted(list(set(ignore_list))) |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
def init_logging(args): |
|
88
|
|
|
""" |
|
89
|
|
|
Set the log level according to the -v/-q command line options. |
|
90
|
|
|
""" |
|
91
|
|
|
log_level = logging.FATAL if args.quiet \ |
|
92
|
|
|
else logging.DEBUG if args.verbose \ |
|
93
|
|
|
else logging.INFO |
|
94
|
|
|
log_format = "%(message)s" |
|
95
|
|
|
logging.basicConfig(level=log_level, format=log_format) |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
def main(override=None): |
|
99
|
|
|
""" |
|
100
|
|
|
Entry point for all scripts |
|
101
|
|
|
""" |
|
102
|
|
|
args = parse_arguments() |
|
103
|
|
|
if override or args.legacy: |
|
104
|
|
|
impl = compat.get_implementation(override=override) |
|
105
|
|
|
impl.main(args) |
|
106
|
|
|
else: |
|
107
|
|
|
modern.pyclean(args) |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
def py2clean(): |
|
111
|
|
|
""" |
|
112
|
|
|
Forces the use of the implementation for Python 2 |
|
113
|
|
|
""" |
|
114
|
|
|
main('CPython2') |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
def py3clean(): |
|
118
|
|
|
""" |
|
119
|
|
|
Forces the use of the implementation for Python 3 |
|
120
|
|
|
""" |
|
121
|
|
|
main('CPython3') |
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
def pypyclean(): |
|
125
|
|
|
""" |
|
126
|
|
|
Forces the use of the implementation for PyPy (2+3) |
|
127
|
|
|
""" |
|
128
|
|
|
main('PyPy2') |
|
129
|
|
|
|