Skip to content

Commit 7d8fed7

Browse files
Abhishek-Dev09pombredanne
authored andcommitted
Drop Python 2 for scancode-toolkit
Signed-off-by: Abhishek Kumar <abhishek.kasyap09@gmail.com>
1 parent b4ea9c6 commit 7d8fed7

62 files changed

Lines changed: 218 additions & 960 deletions

Some content is hidden

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

src/commoncode/codec.py

Lines changed: 18 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -30,75 +30,33 @@
3030
from base64 import standard_b64decode as stddecode
3131
from base64 import urlsafe_b64encode as b64encode
3232

33-
from commoncode.system import py2
34-
35-
3633
"""
3734
Numbers to bytes or strings and URLs coder/decoders.
3835
"""
3936

37+
# noop
38+
c2i = lambda c: c
39+
i2c = lambda i: bytes([i])
4040

41-
if py2:
42-
c2i = ord
43-
i2c = chr
44-
else:
45-
# noop
46-
c2i = lambda c: c
47-
i2c = lambda i: bytes([i])
48-
49-
50-
if py2:
51-
def num_to_bin(num):
52-
"""
53-
Convert a `num` integer or long to a binary string byte-ordered such
54-
that the least significant bytes are at the beginning of the string
55-
(aka. big endian).
56-
"""
57-
# Zero is not encoded but returned as an empty value
58-
if num == 0:
59-
return b'\x00'
60-
61-
binstr = []
62-
while num > 0:
63-
# add the least significant byte value
64-
binstr.append(i2c(num & 0xFF))
65-
# shift the next byte to least significant and repeat
66-
num = num >> 8
67-
68-
# reverse the list now such that the most significant
69-
# byte is at the start of this string to speed decoding
70-
return b''.join(reversed(binstr))
7141

42+
def num_to_bin(num):
43+
"""
44+
Convert a `num` integer or long to a binary string byte-ordered such that
45+
the least significant bytes are at the beginning of the string (aka. big
46+
endian).
47+
"""
48+
# Zero is not encoded but returned as an empty value
49+
if num == 0:
50+
return b'\x00'
7251

73-
def bin_to_num(binstr):
74-
"""
75-
Convert a big endian byte-ordered binary string to an integer or long.
76-
"""
77-
num = 0
78-
for charac in binstr:
79-
# the most significant byte is a the start of the string so we multiply
80-
# that value by 256 (e.g. <<8) and add the value of the current byte,
81-
# then move to next byte in the string and repeat
82-
num = (num << 8) + c2i(charac)
83-
return num
84-
else:
85-
def num_to_bin(num):
86-
"""
87-
Convert a `num` integer or long to a binary string byte-ordered such that
88-
the least significant bytes are at the beginning of the string (aka. big
89-
endian).
90-
"""
91-
# Zero is not encoded but returned as an empty value
92-
if num == 0:
93-
return b'\x00'
52+
return num.to_bytes((num.bit_length() + 7) // 8, 'big')
9453

95-
return num.to_bytes((num.bit_length() + 7) // 8, 'big')
9654

97-
def bin_to_num(binstr):
98-
"""
99-
Convert a big endian byte-ordered binary string to an integer or long.
100-
"""
101-
return int.from_bytes(binstr, byteorder='big', signed=False)
55+
def bin_to_num(binstr):
56+
"""
57+
Convert a big endian byte-ordered binary string to an integer or long.
58+
"""
59+
return int.from_bytes(binstr, byteorder='big', signed=False)
10260

10361

10462
def urlsafe_b64encode(s):

src/commoncode/command.py

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@
4646
from commoncode.system import on_linux
4747
from commoncode.system import on_posix
4848
from commoncode.system import on_windows
49-
from commoncode.system import py2
50-
from commoncode.system import py3
5149
from commoncode import text
5250

5351

@@ -81,14 +79,9 @@
8179
curr_dir = path.dirname(path.dirname(path.abspath(__file__)))
8280

8381

84-
if py2:
85-
PATH_ENV_VAR = b'PATH'
86-
LD_LIBRARY_PATH = b'LD_LIBRARY_PATH'
87-
DYLD_LIBRARY_PATH = b'DYLD_LIBRARY_PATH'
88-
else:
89-
PATH_ENV_VAR = 'PATH'
90-
LD_LIBRARY_PATH = 'LD_LIBRARY_PATH'
91-
DYLD_LIBRARY_PATH = 'DYLD_LIBRARY_PATH'
82+
PATH_ENV_VAR = 'PATH'
83+
LD_LIBRARY_PATH = 'LD_LIBRARY_PATH'
84+
DYLD_LIBRARY_PATH = 'DYLD_LIBRARY_PATH'
9285

9386

9487
def execute2(cmd_loc, args, lib_dir=None, cwd=None, env=None, to_files=False, log=TRACE):
@@ -114,12 +107,8 @@ def execute2(cmd_loc, args, lib_dir=None, cwd=None, env=None, to_files=False, lo
114107
# temp files for stderr and stdout
115108
tmp_dir = get_temp_dir(prefix='cmd-')
116109

117-
if on_linux and py2:
118-
stdout = b'stdout'
119-
stderr = b'stderr'
120-
else:
121-
stdout = 'stdout'
122-
stderr = 'stderr'
110+
stdout = 'stdout'
111+
stderr = 'stderr'
123112

124113
sop = path.join(tmp_dir, stdout)
125114
sep = path.join(tmp_dir, stderr)
@@ -138,10 +127,7 @@ def execute2(cmd_loc, args, lib_dir=None, cwd=None, env=None, to_files=False, lo
138127
proc = None
139128
rc = 100
140129

141-
if py2:
142-
okwargs = dict(mode='wb')
143-
if py3:
144-
okwargs = dict(mode='w', encoding='utf-8')
130+
okwargs = dict(mode='w', encoding='utf-8')
145131

146132
try:
147133
with io.open(sop, **okwargs) as stdout, io.open(sep, **okwargs) as stderr:
@@ -195,13 +181,7 @@ def get_env(base_vars=None, lib_dir=None):
195181
env_vars.update(
196182
{DYLD_LIBRARY_PATH: update_path_var(dyld_lib_path, new_path)})
197183

198-
if py2:
199-
# ensure that we use bytes on py2 and unicode on py3
200-
def to_bytes(s):
201-
return s if isinstance(s, bytes) else s.encode('utf-8')
202-
env_vars = {to_bytes(k): to_bytes(v) for k, v in env_vars.items()}
203-
else:
204-
env_vars = {text.as_unicode(k): text.as_unicode(v) for k, v in env_vars.items()}
184+
env_vars = {text.as_unicode(k): text.as_unicode(v) for k, v in env_vars.items()}
205185

206186
return env_vars
207187

@@ -248,15 +228,8 @@ def load_shared_library(dll_path, lib_dir):
248228
if lib_dir and not path.exists(lib_dir):
249229
raise ImportError('Shared library "lib_dir" does not exists: %(lib_dir)r' % locals())
250230

251-
if on_linux and py2:
252-
# bytes only there ...
253-
if not isinstance(dll_path, bytes):
254-
dll_path = fsencode(dll_path)
255-
256-
else:
257-
# ... unicode everywhere else
258-
if not isinstance(dll_path, compat.unicode):
259-
dll_path = fsdecode(dll_path)
231+
if not isinstance(dll_path, compat.unicode):
232+
dll_path = fsdecode(dll_path)
260233

261234
try:
262235
with pushd(lib_dir):
@@ -304,16 +277,10 @@ def update_path_var(existing_path_var, new_path, pathsep=PATH_ENV_SEP):
304277

305278
# ensure we use unicode or bytes depending on OSes
306279
# TODO: deal also with Python versions
307-
if on_linux and py2:
308-
# bytes ...
309-
existing_path_var = fsencode(existing_path_var)
310-
new_path = fsencode(new_path)
311-
pathsep = fsencode(pathsep)
312-
else:
313-
# ... and unicode otherwise
314-
existing_path_var = fsdecode(existing_path_var)
315-
new_path = fsdecode(new_path)
316-
pathsep = fsdecode(pathsep)
280+
# ... and unicode otherwise
281+
existing_path_var = fsdecode(existing_path_var)
282+
new_path = fsdecode(new_path)
283+
pathsep = fsdecode(pathsep)
317284

318285
path_elements = existing_path_var.split(pathsep)
319286

@@ -329,14 +296,9 @@ def update_path_var(existing_path_var, new_path, pathsep=PATH_ENV_SEP):
329296
# new path is already in PATH, change nothing
330297
updated_path_var = existing_path_var
331298

332-
if py2:
333-
# always use bytes for env vars...
334-
if isinstance(updated_path_var, compat.unicode):
335-
updated_path_var = fsencode(updated_path_var)
336-
else:
337-
# ... else use unicode
338-
if not isinstance(updated_path_var, compat.unicode):
339-
updated_path_var = fsdecode(updated_path_var)
299+
# ... else use unicode
300+
if not isinstance(updated_path_var, compat.unicode):
301+
updated_path_var = fsdecode(updated_path_var)
340302

341303
# at this stage new_path_env is unicode on all OSes on Py3
342304
# and on Py2 it is bytes on Linux and unicode elsewhere

src/commoncode/fileset.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
from commoncode import fileutils
3333
from commoncode import paths
3434
from commoncode.system import on_linux
35-
from commoncode.system import py2
3635

3736

3837
TRACE = False
@@ -45,8 +44,8 @@
4544
logger.setLevel(logging.DEBUG)
4645

4746

48-
POSIX_PATH_SEP = b'/' if on_linux and py2 else u'/'
49-
EMPTY_STRING = b'' if on_linux and py2 else u''
47+
POSIX_PATH_SEP = u'/'
48+
EMPTY_STRING = u''
5049

5150

5251
"""
@@ -187,7 +186,7 @@ def load(location):
187186
fn = os.path.abspath(os.path.normpath(os.path.expanduser(location)))
188187
msg = ('File %(location)s does not exist or not a file.') % locals()
189188
assert (os.path.exists(fn) and os.path.isfile(fn)), msg
190-
mode = 'rb' if on_linux and py2 else 'r'
189+
mode = 'r'
191190
with open(fn, mode) as f:
192191
return [l.strip() for l in f if l and l.strip()]
193192

0 commit comments

Comments
 (0)