Skip to content

Commit 0c646b7

Browse files
committed
Remove Python 2 support
Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
1 parent 3ae7ac3 commit 0c646b7

43 files changed

Lines changed: 417 additions & 1015 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

setup.cfg

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
[wheel]
2-
universal=1
3-
41
[metadata]
52
license_file = apache-2.0.LICENSE
63
name = commoncode
@@ -25,7 +22,6 @@ packages=find:
2522
include_package_data = true
2623
zip_safe = false
2724
install_requires =
28-
backports.os == 0.1.1; python_version < "3"
2925
attrs >= 18.1, !=20.1.0
3026
click >= 6.0.0
3127
text_unidecode >= 1.0

src/commoncode/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222
# ScanCode is a free software code scanning tool from nexB Inc. and others.
2323
# Visit https://github.com/nexB/scancode-toolkit/ for support and download.
2424

25-
from __future__ import absolute_import
26-
from __future__ import print_function
27-
from __future__ import unicode_literals
28-
2925

3026
def set_re_max_cache(max_cache=1000000):
3127
"""

src/commoncode/archive.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,14 @@
2222
# ScanCode is a free software code scanning tool from nexB Inc. and others.
2323
# Visit https://github.com/nexB/scancode-toolkit/ for support and download.
2424

25-
from __future__ import absolute_import
26-
from __future__ import print_function
27-
from __future__ import division
28-
from __future__ import unicode_literals
29-
3025
from functools import partial
3126
import os
3227
from os import path
3328
import gzip
3429
import tarfile
3530
import zipfile
3631

37-
from commoncode import fileutils
38-
from commoncode.system import on_linux
3932
from commoncode.system import on_windows
40-
from commoncode.system import py2
4133

4234
"""
4335
Mimimal tar and zip file handling, primarily for testing.
@@ -49,10 +41,6 @@ def _extract_tar_raw(test_path, target_dir, to_bytes, *args, **kwargs):
4941
Raw simplified extract for certain really weird paths and file
5042
names.
5143
"""
52-
if to_bytes and py2:
53-
# use bytes for paths on ALL OSes (though this may fail on macOS)
54-
target_dir = fileutils.fsencode(target_dir)
55-
test_path = fileutils.fsencode(test_path)
5644
tar = None
5745
try:
5846
tar = tarfile.open(test_path)
@@ -74,10 +62,7 @@ def extract_tar(location, target_dir, verbatim=False, *args, **kwargs):
7462
"""
7563
# always for using bytes for paths on all OSses... tar seems to use bytes internally
7664
# and get confused otherwise
77-
location = fileutils.fsencode(location)
78-
if on_linux and py2:
79-
target_dir = fileutils.fsencode(target_dir)
80-
65+
location = os.fsencode(location)
8166
with open(location, 'rb') as input_tar:
8267
tar = None
8368
try:
@@ -102,10 +87,6 @@ def extract_zip(location, target_dir, *args, **kwargs):
10287
if not path.isfile(location) and zipfile.is_zipfile(location):
10388
raise Exception('Incorrect zip file %(location)r' % locals())
10489

105-
if on_linux and py2:
106-
location = fileutils.fsencode(location)
107-
target_dir = fileutils.fsencode(target_dir)
108-
10990
with zipfile.ZipFile(location) as zipf:
11091
for info in zipf.infolist():
11192
name = info.filename
@@ -129,10 +110,6 @@ def extract_zip_raw(location, target_dir, *args, **kwargs):
129110
if not path.isfile(location) and zipfile.is_zipfile(location):
130111
raise Exception('Incorrect zip file %(location)r' % locals())
131112

132-
if on_linux and py2:
133-
location = fileutils.fsencode(location)
134-
target_dir = fileutils.fsencode(target_dir)
135-
136113
with zipfile.ZipFile(location) as zipf:
137114
zipf.extractall(path=target_dir)
138115

src/commoncode/cliutils.py

Lines changed: 63 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,20 @@
2222
# ScanCode is a free software code scanning tool from nexB Inc. and others.
2323
# Visit https://github.com/nexB/scancode-toolkit/ for support and download.
2424

25-
from __future__ import absolute_import
26-
from __future__ import division
27-
from __future__ import print_function
28-
from __future__ import unicode_literals
29-
30-
from collections import OrderedDict
3125
import sys
3226

3327
import click
3428
click.disable_unicode_literals_warning = True
3529
from click.utils import echo
3630
from click.termui import style
3731
from click.types import BoolParamType
38-
# FIXME: this is NOT API
32+
# FIXME: this is NOT API
3933
from click._termui_impl import ProgressBar
40-
from six import string_types
4134

42-
from commoncode import compat
4335
from commoncode.fileutils import file_name
4436
from commoncode.fileutils import splitext
4537
from commoncode.text import toascii
4638

47-
4839
# Tracing flags
4940
TRACE = False
5041

@@ -60,10 +51,9 @@ def logger_debug(*args):
6051
logger.setLevel(logging.DEBUG)
6152

6253
def logger_debug(*args):
63-
return logger.debug(' '.join(isinstance(a, string_types)
54+
return logger.debug(' '.join(isinstance(a, str)
6455
and a or repr(a) for a in args))
6556

66-
6757
"""
6858
Command line UI utilities for improved options, help and progress reporting.
6959
"""
@@ -85,15 +75,21 @@ def get_usage(self, ctx):
8575
"""
8676
return super(BaseCommand, self).get_usage(ctx) + self.short_usage_help
8777

88-
def main(self, args=None, prog_name=None, complete_var=None,
89-
standalone_mode=True, **extra):
78+
def main(
79+
self, args=None, prog_name=None, complete_var=None,
80+
standalone_mode=True, **extra,
81+
):
9082
"""
9183
Workaround click 4.0 bug https://github.com/mitsuhiko/click/issues/365
9284
"""
93-
return click.Command.main(self, args=args, prog_name=self.name,
94-
complete_var=complete_var,
95-
standalone_mode=standalone_mode, **extra)
96-
85+
return click.Command.main(
86+
self,
87+
args=args,
88+
prog_name=self.name,
89+
complete_var=complete_var,
90+
standalone_mode=standalone_mode,
91+
**extra,
92+
)
9793

9894

9995
class GroupedHelpCommand(BaseCommand):
@@ -105,25 +101,27 @@ class GroupedHelpCommand(BaseCommand):
105101
short_usage_help = '''
106102
Try the '--help' option for help on options and arguments.'''
107103

108-
def __init__(self, name, context_settings=None, callback=None, params=None,
109-
help=None, # NOQA
110-
epilog=None, short_help=None,
111-
options_metavar='[OPTIONS]', add_help_option=True,
112-
plugin_options=()):
104+
def __init__(
105+
self, name, context_settings=None, callback=None, params=None,
106+
help=None, # NOQA
107+
epilog=None, short_help=None,
108+
options_metavar='[OPTIONS]', add_help_option=True,
109+
plugin_options=(),
110+
):
113111
"""
114112
Create a new GroupedHelpCommand using the `plugin_options` list of
115113
PluggableCommandLineOption instances.
116114
"""
117115

118116
super(GroupedHelpCommand, self).__init__(
119-
name,
120-
context_settings,
117+
name,
118+
context_settings,
121119
callback,
122-
params,
123-
help,
124-
epilog,
125-
short_help,
126-
options_metavar,
120+
params,
121+
help,
122+
epilog,
123+
short_help,
124+
options_metavar,
127125
add_help_option,
128126
)
129127

@@ -137,7 +135,7 @@ def format_options(self, ctx, formatter):
137135
to MISC_GROUP group.
138136
"""
139137
# this mapping defines the CLI help presentation order
140-
help_groups = OrderedDict([
138+
help_groups = dict([
141139
(SCAN_GROUP, []),
142140
(OTHER_SCAN_GROUP, []),
143141
(SCAN_OPTIONS_GROUP, []),
@@ -220,14 +218,13 @@ def render_finish(self):
220218

221219

222220
BAR_WIDTH = 20
223-
BAR_SEP = ' '
224-
BAR_SEP_LEN = len(BAR_SEP)
221+
BAR_SEP_LEN = len(' ')
225222

226223

227224
def progressmanager(iterable=None, length=None, label=None, show_eta=True,
228225
show_percent=None, show_pos=True, item_show_func=None,
229226
fill_char='#', empty_char='-', bar_template=None,
230-
info_sep=BAR_SEP, width=BAR_WIDTH, file=None, color=None, # NOQA
227+
info_sep=' ', width=BAR_WIDTH, file=None, color=None, # NOQA
231228
verbose=False):
232229

233230
"""
@@ -243,7 +240,7 @@ def progressmanager(iterable=None, length=None, label=None, show_eta=True,
243240
progress_class = ProgressLogger
244241
else:
245242
progress_class = EnhancedProgressBar
246-
bar_template = ('[%(bar)s]' + BAR_SEP + '%(info)s'
243+
bar_template = ('[%(bar)s]' + ' ' + '%(info)s'
247244
if bar_template is None else bar_template)
248245

249246
return progress_class(iterable=iterable, length=length,
@@ -292,22 +289,21 @@ def fixed_width_file_name(path, max_length=25):
292289
return '{prefix}{ellipsis}{suffix}{ext}'.format(**locals())
293290

294291

295-
def file_name_max_len(used_width=BAR_WIDTH + BAR_SEP_LEN + 7 + BAR_SEP_LEN + 8 + BAR_SEP_LEN):
292+
def file_name_max_len(used_width=BAR_WIDTH + 1 + 7 + 1 + 8 + 1):
296293
"""
297294
Return the max length of a path given the current terminal width.
298295
299296
A progress bar is composed of these elements:
300297
[-----------------------------------#] 1667 Scanned: tu-berlin.yml
301298
- the bar proper which is BAR_WIDTH characters
302-
- one BAR_SEP
299+
- one space
303300
- the number of files. We set it to 7 chars, eg. 9 999 999 files
304-
- one BAR_SEP
301+
- one space
305302
- the word Scanned: 8 chars
306-
- one BAR_SEP
303+
- one space
307304
- the file name proper
308305
The space usage is therefore:
309-
BAR_WIDTH + BAR_SEP_LEN + 7 + BAR_SEP_LEN + 8 + BAR_SEP_LEN
310-
+ the file name length
306+
BAR_WIDTH + 1 + 7 + 1 + 8 + 1 + the file name length
311307
"""
312308
term_width, _height = click.get_terminal_size()
313309
max_filename_length = term_width - used_width
@@ -323,7 +319,7 @@ def path_progress_message(item, verbose=False, prefix='Scanned: '):
323319
return ''
324320
location = item[0]
325321
errors = item[2]
326-
location = compat.unicode(toascii(location))
322+
location = toascii(location)
327323
progress_line = location
328324
if not verbose:
329325
max_file_name_len = file_name_max_len()
@@ -336,7 +332,6 @@ def path_progress_message(item, verbose=False, prefix='Scanned: '):
336332
return style(prefix) + style(progress_line, fg=color)
337333

338334

339-
340335
# CLI help groups
341336
SCAN_GROUP = 'primary scans'
342337
SCAN_OPTIONS_GROUP = 'scan options'
@@ -391,11 +386,21 @@ def __init__(
391386
**kwargs
392387
):
393388

394-
super(PluggableCommandLineOption, self).__init__(param_decls, show_default,
395-
prompt, confirmation_prompt,
396-
hide_input, is_flag, flag_value,
397-
multiple, count, allow_from_autoenv,
398-
type, help, **kwargs)
389+
super(PluggableCommandLineOption, self).__init__(
390+
param_decls,
391+
show_default,
392+
prompt,
393+
confirmation_prompt,
394+
hide_input,
395+
is_flag,
396+
flag_value,
397+
multiple,
398+
count,
399+
allow_from_autoenv,
400+
type,
401+
help,
402+
**kwargs
403+
)
399404

400405
self.help_group = help_group
401406
self.sort_order = sort_order
@@ -410,12 +415,16 @@ def __repr__(self, *args, **kwargs):
410415
required_options = self.required_options
411416
conflicting_options = self.conflicting_options
412417

413-
return ('PluggableCommandLineOption<name=%(name)r, '
414-
'required_options=%(required_options)r, conflicting_options=%(conflicting_options)r>' % locals())
418+
return (
419+
'PluggableCommandLineOption<name=%(name)r, '
420+
'required_options=%(required_options)r, '
421+
'conflicting_options=%(conflicting_options)r>' % locals()
422+
)
415423

416424
def validate_dependencies(self, ctx, value):
417425
"""
418-
Validate `value` against declared `required_options` or `conflicting_options` dependencies.
426+
Validate `value` against declared `required_options` or
427+
`conflicting_options` dependencies.
419428
"""
420429
_validate_option_dependencies(ctx, self, value, self.required_options, required=True)
421430
_validate_option_dependencies(ctx, self, value, self.conflicting_options, required=False)
@@ -427,8 +436,8 @@ def get_help_record(self, ctx):
427436

428437
def validate_option_dependencies(ctx):
429438
"""
430-
Validate all PluggableCommandLineOption dependencies in the `ctx` Click context.
431-
Ignore eager flags.
439+
Validate all PluggableCommandLineOption dependencies in the `ctx` Click
440+
context. Ignore eager flags.
432441
"""
433442
values = ctx.params
434443
if TRACE:
@@ -449,8 +458,7 @@ def validate_option_dependencies(ctx):
449458
param.validate_dependencies(ctx, value)
450459

451460

452-
def _validate_option_dependencies(ctx, param, value,
453-
other_option_names, required=False):
461+
def _validate_option_dependencies(ctx, param, value, other_option_names, required=False):
454462
"""
455463
Validate the `other_option_names` option dependencies and return a
456464
UsageError if the `param` `value` is set to a not-None non-default value and

0 commit comments

Comments
 (0)