This repository was archived by the owner on Jan 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy path__main__.py
202 lines (156 loc) · 6.5 KB
/
__main__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# Copyright 2014 Google Inc. All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function
from __future__ import unicode_literals
from caniusepython3 import dependencies
from caniusepython3 import projects as projects_
import packaging.utils
import argparse
import io
import logging
import sys
# Without this, the 'ciu' logger will emit nothing.
logging.basicConfig(format='[%(levelname)s] %(message)s')
def projects_from_cli(args):
"""Take arguments through the CLI can create a list of specified projects."""
description = ('Determine if a set of project dependencies will work with '
'Python 3')
parser = argparse.ArgumentParser(description=description)
req_help = 'path(s) to a pip requirements file (e.g. requirements.txt)'
parser.add_argument('--requirements', '-r', nargs='+', default=(),
help=req_help)
meta_help = 'path(s) to a PEP 426 metadata file (e.g. PKG-INFO, pydist.json)'
parser.add_argument('--metadata', '-m', nargs='+', default=(),
help=meta_help)
parser.add_argument('--projects', '-p', nargs='+', default=(),
help='name(s) of projects to test for Python 3 support')
parser.add_argument('--verbose', '-v', action='store_true',
help='verbose output (e.g. list compatibility overrides)')
parser.add_argument('--exclude', '-e', action='append', default=[],
help='Ignore list')
parsed = parser.parse_args(args)
if not (parsed.requirements or parsed.metadata or parsed.projects):
parser.error("Missing 'requirements', 'metadata', or 'projects'")
projects = []
if parsed.verbose:
logging.getLogger('ciu').setLevel(logging.INFO)
projects.extend(projects_.projects_from_requirements(parsed.requirements))
metadata = []
for metadata_path in parsed.metadata:
with io.open(metadata_path) as file:
metadata.append(file.read())
projects.extend(projects_.projects_from_metadata(metadata))
projects.extend(map(packaging.utils.canonicalize_name, parsed.projects))
projects = {i for i in projects if i not in parsed.exclude}
return projects
def message(blockers):
"""Create a sequence of key messages based on what is blocking."""
encoding = getattr(sys.stdout, 'encoding', '')
if encoding:
encoding = encoding.lower()
if not blockers:
if encoding == 'utf-8':
# party hat
flair = "\U0001F389 "
else:
flair = ''
return [flair + 'You have no projects known to block you from using Python 3!']
flattened_blockers = set()
for blocker_reasons in blockers:
for blocker in blocker_reasons:
flattened_blockers.add(blocker)
if encoding == 'utf-8':
# sad face
flair = "\U00002639 "
else:
flair = ''
need = '{0}You need {1} project{2} to transition to Python 3.'
formatted_need = need.format(
flair,
len(flattened_blockers),
's' if len(flattened_blockers) != 1 else ''
)
can_port = ('Of {0} {1} project{2}, {3} {4} no direct dependencies '
'known to block {5} transition:')
formatted_can_port = can_port.format(
'those' if len(flattened_blockers) != 1 else 'that',
len(flattened_blockers),
's' if len(flattened_blockers) != 1 else '',
len(blockers),
'have' if len(blockers) != 1 else 'has',
'their' if len(blockers) != 1 else 'its')
return [formatted_need, formatted_can_port]
def pprint_blockers(blockers):
"""Pretty print blockers into a sequence of strings.
Results will be sorted by top-level project name. This means that if a
project is blocking another project then the dependent project will be
what is used in the sorting, not the project at the bottom of the
dependency graph.
"""
pprinted = []
for blocker in sorted(blockers, key=lambda x: tuple(reversed(x))):
buf = [blocker[0]]
if len(blocker) > 1:
buf.append(' (which is blocking ')
buf.append(', which is blocking '.join(blocker[1:]))
buf.append(')')
pprinted.append(''.join(buf))
return pprinted
def output_not_on_pypi(not_on_pypi):
lines = []
if len(not_on_pypi) > 0:
compat_status = 'The following {0} project{1} could not be found on pypi.org:'.format(
len(not_on_pypi),
's' if len(not_on_pypi) != 1 else '',
)
lines.append(compat_status)
for project in not_on_pypi:
lines.append(' {0}'.format(project))
return lines
def output_not_on_distlib(not_on_distlib):
lines = []
if len(not_on_distlib) > 0:
compat_status = 'The depedencies of following {0} project{1} could not be found using distlib:'.format(
len(not_on_distlib),
's' if len(not_on_distlib) != 1 else '',
)
lines.append(compat_status)
for project in not_on_distlib:
lines.append(' {0}'.format(project))
return lines
def check(projects):
"""Check the specified projects for Python 3 compatibility."""
log = logging.getLogger('ciu')
log.info('{0} top-level projects to check'.format(len(projects)))
print('Finding and checking dependencies ...')
blockers, not_on_pypi, not_on_distlib = dependencies.blockers(projects)
print('')
for line in message(blockers):
print(line)
if len(blockers) > 0:
print('')
for line in pprint_blockers(blockers):
print(' ', line)
if len(not_on_pypi) > 0:
print('')
for line in output_not_on_pypi(not_on_pypi):
print(line)
if len(not_on_distlib) > 0:
print('')
for line in output_not_on_distlib(not_on_distlib):
print(line)
return len(blockers) == 0
def main(args=sys.argv[1:]):
passed = check(projects_from_cli(args))
if not passed:
sys.exit(3)
if __name__ == '__main__': #pragma: no cover
main()