Skip to content

Commit 48408a1

Browse files
Disable trace of configure.py
Signed-off-by: Abhishek Kumar <abhishek.kasyap09@gmail.com>
1 parent a97bf5f commit 48408a1

63 files changed

Lines changed: 967 additions & 221 deletions

Some content is hidden

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

etc/configure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
bin_dir_name = 'bin'
132132

133133
# set to True to trace command line executaion
134-
TRACE = True
134+
TRACE = False
135135

136136

137137
def call(cmd, root_dir):

setup.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,33 +119,39 @@ def read(*names, **kwargs):
119119
'License :: OSI Approved :: Apache Software License',
120120
'License :: CC0 1.0 Universal (CC0 1.0) Public Domain Dedication',
121121
'Programming Language :: Python',
122+
'Programming Language :: Python :: 2.7',
122123
'Programming Language :: Python :: 3.6',
123124
'Topic :: Utilities',
124125
],
125126
keywords=[
126127
'open source', 'scan', 'license', 'package', 'dependency',
127128
'copyright', 'filetype', 'author', 'extract', 'licensing',
128129
],
129-
python_requires='!=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*, <4',
130+
python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*, <4',
130131
install_requires=[
131132
# Hack to support pip 8 (for those poor sods forced to use ubuntu 16.04's system pip)
132133
# See https://github.com/nexB/scancode-toolkit/issues/1463
134+
'more_itertools < 6.0.0; python_version == "2.7"',
133135
# end hack
134136

135137
# cluecode
136138
# Some nltk version ranges are buggy
137139
'nltk >= 3.2, < 4.0',
140+
'py2_ipaddress >= 2.0, <3.5; python_version<"3"',
138141
'urlpy',
139142
'publicsuffix2',
140143
'fingerprints >= 0.6.0, < 1.0.0',
141144

142145
# extractcode
143146
'patch >= 1.15, < 1.20 ',
144147
# to work around bug http://bugs.python.org/issue19839
148+
# on multistream bzip2 files: this can removed in Python 3.
149+
'bz2file >= 0.98; python_version<"3"',
145150
'extractcode_libarchive',
146151
'extractcode_7z',
147152

148153
# commoncode
154+
'backports.os == 0.1.1; python_version<"3"',
149155
'future >= 0.16.0',
150156
'text_unidecode >= 1.0, < 2.0',
151157
'saneyaml',
@@ -186,7 +192,8 @@ def read(*names, **kwargs):
186192
'dparse >= 0.4.1',
187193

188194
# used to fix mojibake in Windows PE
189-
'ftfy>= 5.0.0',
195+
'ftfy < 5.0.0; python_version == "2.7"',
196+
'ftfy>= 5.0.0; python_version > "3"',
190197

191198
# scancode
192199
'click >= 6.0.0, < 7.0.0',

src/commoncode/codec.py

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,75 @@
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+
3336
"""
3437
Numbers to bytes or strings and URLs coder/decoders.
3538
"""
3639

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])
4148

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'
5149

52-
return num.to_bytes((num.bit_length() + 7) // 8, 'big')
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'
5360

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
5467

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)
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))
71+
72+
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'
94+
95+
return num.to_bytes((num.bit_length() + 7) // 8, 'big')
96+
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)
60102

61103

62104
def urlsafe_b64encode(s):

src/commoncode/command.py

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
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
4951
from commoncode import text
5052

5153

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

8183

82-
PATH_ENV_VAR = 'PATH'
83-
LD_LIBRARY_PATH = 'LD_LIBRARY_PATH'
84-
DYLD_LIBRARY_PATH = 'DYLD_LIBRARY_PATH'
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'
8592

8693

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

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

113124
sop = path.join(tmp_dir, stdout)
114125
sep = path.join(tmp_dir, stderr)
@@ -127,7 +138,10 @@ def execute2(cmd_loc, args, lib_dir=None, cwd=None, env=None, to_files=False, lo
127138
proc = None
128139
rc = 100
129140

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

132146
try:
133147
with io.open(sop, **okwargs) as stdout, io.open(sep, **okwargs) as stderr:
@@ -181,7 +195,13 @@ def get_env(base_vars=None, lib_dir=None):
181195
env_vars.update(
182196
{DYLD_LIBRARY_PATH: update_path_var(dyld_lib_path, new_path)})
183197

184-
env_vars = {text.as_unicode(k): text.as_unicode(v) for k, v in env_vars.items()}
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()}
185205

186206
return env_vars
187207

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

231-
if not isinstance(dll_path, compat.unicode):
232-
dll_path = fsdecode(dll_path)
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)
233260

234261
try:
235262
with pushd(lib_dir):
@@ -277,10 +304,16 @@ def update_path_var(existing_path_var, new_path, pathsep=PATH_ENV_SEP):
277304

278305
# ensure we use unicode or bytes depending on OSes
279306
# TODO: deal also with Python versions
280-
# ... and unicode otherwise
281-
existing_path_var = fsdecode(existing_path_var)
282-
new_path = fsdecode(new_path)
283-
pathsep = fsdecode(pathsep)
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)
284317

285318
path_elements = existing_path_var.split(pathsep)
286319

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

299-
# ... else use unicode
300-
if not isinstance(updated_path_var, compat.unicode):
301-
updated_path_var = fsdecode(updated_path_var)
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)
302340

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

src/commoncode/fileset.py

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

3637

3738
TRACE = False
@@ -44,8 +45,8 @@
4445
logger.setLevel(logging.DEBUG)
4546

4647

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

5051

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

0 commit comments

Comments
 (0)