Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions etc/scripts/scanserv.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ def run_scan(location, **kwargs):


if __name__ == '__channelexec__':
from commoncode import compat
for kwargs in channel: # NOQA
# a mapping of kwargs or a location string
if isinstance(kwargs, (str, compat.unicode)):
if isinstance(kwargs, (str, str)):
channel.send(run_scan(kwargs)) # NOQA
elif isinstance(kwargs, dict):
channel.send(run_scan(**kwargs)) # NOQA
Expand Down
3 changes: 1 addition & 2 deletions etc/scripts/synclic.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@

from commoncode import fetch
from commoncode import fileutils
from commoncode import compat

import licensedcode
from licensedcode.models import load_licenses
Expand Down Expand Up @@ -898,7 +897,7 @@ def update_external(_attrib, _sc_val, _ext_val):

continue

if (isinstance(scancode_value, compat.unicode) and isinstance(external_value, compat.unicode)):
if (isinstance(scancode_value, str) and isinstance(external_value, str)):
# keep the stripped and normalized spaces value
# normalized spaces
normalized_scancode_value = ' '.join(scancode_value.split())
Expand Down
6 changes: 2 additions & 4 deletions src/cluecode/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
from six import string_types
import urlpy

from commoncode import compat
from commoncode.system import py3
from commoncode.text import toascii
from cluecode import finder_data
from textcode import analysis
Expand Down Expand Up @@ -251,7 +249,7 @@ def find_urls(location, unique=True):
if TRACE_URL:
logger_debug('find_urls: lineno:', lineno, '_line:', repr(_line),
'type(url):', type(url), 'url:', repr(url))
yield compat.unicode(url), lineno
yield str(url), lineno


EMPTY_URLS = set(['https', 'http', 'ftp', 'www', ])
Expand Down Expand Up @@ -462,7 +460,7 @@ def get_ip(s):
return False

try:
ip = ipaddress.ip_address(compat.unicode(s))
ip = ipaddress.ip_address(str(s))
return ip
except ValueError:
return False
Expand Down
5 changes: 2 additions & 3 deletions src/formattedcode/output_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
from six import string_types
import unicodecsv

from commoncode import compat
from formattedcode import FileOptionType
from plugincode.output import output_impl
from plugincode.output import OutputPlugin
Expand Down Expand Up @@ -320,7 +319,7 @@ def flatten_package(_package, path, prefix='package__'):
if isinstance(component_val, list):
component_val = '\n'.join(component_val)

if not isinstance(component_val, compat.unicode):
if not isinstance(component_val, str):
component_val = repr(component_val)

existing = pack.get(component_new_key) or []
Expand All @@ -338,7 +337,7 @@ def flatten_package(_package, path, prefix='package__'):

pack[nk] = ''

if isinstance(val, compat.unicode):
if isinstance(val, str):
pack[nk] = val
else:
# Use repr if not a string
Expand Down
22 changes: 3 additions & 19 deletions src/formattedcode/output_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,13 @@
import click
import simplejson

from commoncode import compat
from commoncode.fileutils import PATH_TYPE
from commoncode.fileutils import as_posixpath
from commoncode.fileutils import copytree
from commoncode.fileutils import delete
from commoncode.fileutils import file_name
from commoncode.fileutils import file_base_name
from commoncode.fileutils import fsencode
from commoncode.fileutils import parent_directory
from commoncode.system import on_linux
from commoncode.system import py2
from commoncode.system import py3
from formattedcode import FileOptionType
from commoncode.cliutils import PluggableCommandLineOption
from commoncode.cliutils import OUTPUT_GROUP
Expand Down Expand Up @@ -121,10 +116,6 @@ def is_enabled(self, custom_output, custom_template, **kwargs):
def process_codebase(self, codebase, custom_output, custom_template, **kwargs):
results = self.get_files(codebase, **kwargs)
version = codebase.get_or_create_current_header().tool_version

if on_linux and py2:
custom_template = fsencode(custom_template)

template_loc = custom_template
output_file = custom_output
write_templated(output_file, results, version, template_loc)
Expand All @@ -139,7 +130,7 @@ def write_templated(output_file, results, version, template_loc):
template = get_template(template_loc)

for template_chunk in generate_output(results, version, template):
assert isinstance(template_chunk, compat.unicode)
assert isinstance(template_chunk, str)
try:
output_file.write(template_chunk)
except Exception:
Expand Down Expand Up @@ -326,16 +317,9 @@ def create_html_app(output_file, results, version, scanned_path): # NOQA
with io.open(join(target_assets_dir, 'help.html'), 'w', encoding='utf-8') as f:
f.write(rendered_help)

# write json data
# FIXME: this should a regular JSON scan format
if py2:
mode = 'wb'
prefix = b'data='
if py3:
mode = 'w'
prefix = u'data='
with io.open(join(target_assets_dir, 'data.js'), mode) as f:
f.write(prefix)
with io.open(join(target_assets_dir, 'data.js'), 'w') as f:
f.write('data=')
simplejson.dump(results, f, iterable_as_array=True)

except HtmlAppAssetCopyWarning as w:
Expand Down
31 changes: 6 additions & 25 deletions src/formattedcode/output_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import jsonstreams
from six import string_types

from commoncode.system import py2
from commoncode.system import py3
from formattedcode import FileOptionType
from commoncode.cliutils import PluggableCommandLineOption
from commoncode.cliutils import OUTPUT_GROUP
Expand Down Expand Up @@ -64,27 +62,12 @@ def logger_debug(*args):
and a or repr(a) for a in args))


if py2:
mode = 'wb'
space = b' '
comma = b','
colon = b':'
eol = b'\n'

if py3:
mode = 'w'
space = u' '
comma = u','
colon = u':'
eol = u'\n'


@output_impl
class JsonCompactOutput(OutputPlugin):

options = [
PluggableCommandLineOption(('--json', 'output_json',),
type=FileOptionType(mode=mode, lazy=True),
type=FileOptionType(mode='w', lazy=True),
metavar='FILE',
help='Write scan output as compact JSON to FILE.',
help_group=OUTPUT_GROUP,
Expand All @@ -103,7 +86,7 @@ class JsonPrettyOutput(OutputPlugin):

options = [
PluggableCommandLineOption(('--json-pp', 'output_json_pp',),
type=FileOptionType(mode=mode, lazy=True),
type=FileOptionType(mode='w', lazy=True),
metavar='FILE',
help='Write scan output as pretty-printed JSON to FILE.',
help_group=OUTPUT_GROUP,
Expand Down Expand Up @@ -134,10 +117,10 @@ def write_results(codebase, output_file, pretty=False, **kwargs):
# If `output_file` is a path string, open the file at path `output_file` and use it as `output_file`
close_fd = False
if isinstance(output_file, string_types):
output_file = open(output_file, mode)
output_file = open(output_file, 'w')
close_fd = True

# Begin writing JSON to `output_file`
# Begin wri'w' JSON to `output_file`
with jsonstreams.Stream(jsonstreams.Type.object, fd=output_file, close_fd=close_fd, **jsonstreams_kwargs) as s:
# Write headers
codebase.add_files_count_to_current_header()
Expand All @@ -151,10 +134,8 @@ def write_results(codebase, output_file, pretty=False, **kwargs):

# Write files
codebase_files = OutputPlugin.get_files(codebase, **kwargs)
if py3:
# OutputPlugin.get_files() returns a `map()`, which isn's JSON
# serializable in Python 3
codebase_files = list(codebase_files)
# OutputPlugin.get_files() returns a generator, not JSON-serializable
codebase_files = list(codebase_files)
s.write('files', codebase_files)


Expand Down
31 changes: 6 additions & 25 deletions src/formattedcode/output_jsonlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@

import simplejson

from commoncode.system import py2
from commoncode.system import py3
from formattedcode import FileOptionType
from commoncode.cliutils import OUTPUT_GROUP
from commoncode.cliutils import PluggableCommandLineOption
Expand All @@ -42,29 +40,12 @@
"""


if py2:
mode = 'wb'
space = b' '
comma = b','
colon = b':'
eol = b'\n'
file_key = b'files'

if py3:
mode = 'w'
space = u' '
comma = u','
colon = u':'
eol = u'\n'
file_key = u'files'


@output_impl
class JsonLinesOutput(OutputPlugin):

options = [
PluggableCommandLineOption(('--json-lines', 'output_json_lines',),
type=FileOptionType(mode=mode, lazy=True),
type=FileOptionType(mode='w', lazy=True),
metavar='FILE',
help='Write scan output as JSON Lines to FILE.',
help_group=OUTPUT_GROUP,
Expand All @@ -86,21 +67,21 @@ def process_codebase(self, codebase, output_json_lines, **kwargs):
simplejson_kwargs = dict(
iterable_as_array=True,
encoding='utf-8',
separators=(comma, colon,)
separators=(u',', u':',)
)
output_json_lines.write(
simplejson.dumps(headers, **simplejson_kwargs))
output_json_lines.write(eol)
output_json_lines.write('\n')

for name, value in codebase.attributes.to_dict().items():
if value:
smry = {name: value}
output_json_lines.write(
simplejson.dumps(smry, **simplejson_kwargs))
output_json_lines.write(eol)
output_json_lines.write('\n')

for scanned_file in files:
scanned_file_line = {file_key: [scanned_file]}
scanned_file_line = {'files': [scanned_file]}
output_json_lines.write(
simplejson.dumps(scanned_file_line, **simplejson_kwargs))
output_json_lines.write(eol)
output_json_lines.write('\n')
16 changes: 6 additions & 10 deletions src/formattedcode/output_spdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ def write_spdx(output_file, files, tool_name, tool_version, notice, input_file,
"""
Write scan output as SPDX Tag/value or RDF.
"""
as_rdf = not as_tagvalue
_patch_license_list()

absinput = abspath(input_file)

if isdir(absinput):
Expand Down Expand Up @@ -341,24 +341,20 @@ def write_spdx(output_file, files, tool_name, tool_version, notice, input_file,

if as_tagvalue:
from spdx.writers.tagvalue import write_document # NOQA
else:
elif as_rdf:
from spdx.writers.rdf import write_document # NOQA

if as_tagvalue:
# unicode text everywhere
spdx_output = StringIO()
else:
# rdf as utf-encoded bytes on Py2
elif as_rdf:
# rdf is utf-encoded bytes
spdx_output = BytesIO()

write_document(doc, spdx_output, validate=False)
result = spdx_output.getvalue()

if as_tagvalue:
# unicode text everywhere
pass
else:
# rdf as utf-encoded bytes on Py2
if as_rdf:
# rdf is utf-encoded bytes
result = result.decode('utf-8')

output_file.write(result)
9 changes: 2 additions & 7 deletions src/licensedcode/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
from commoncode.fileutils import resource_iter
from commoncode.fileutils import create_dir
from commoncode import ignore
from commoncode.system import py3

from scancode_config import scancode_cache_dir
from scancode_config import scancode_src_dir
Expand Down Expand Up @@ -285,10 +284,7 @@ def load_index(cache_file, use_loads=False):
'Please delete "{cache_file}" and retry.\n'
'If the problem persists, copy this error message '
'and submit a bug report.\n'.format(**locals()))
if py3:
raise ex_type(message).with_traceback(ex_traceback)
else:
six.reraise(ex_type, message, ex_traceback)
raise ex_type(message).with_traceback(ex_traceback)


_ignored_from_hash = partial(
Expand All @@ -314,8 +310,7 @@ def tree_checksum(tree_base_dir=scancode_src_dir, _ignored=_ignored_from_hash):
resources = resource_iter(tree_base_dir, ignored=_ignored, with_dirs=False)
hashable = (pth + str(getmtime(pth)) + str(getsize(pth)) for pth in resources)
hashable = ''.join(sorted(hashable))
if py3:
hashable=hashable.encode('utf-8')
hashable=hashable.encode('utf-8')
return md5(hashable).hexdigest()


Expand Down
1 change: 1 addition & 0 deletions src/licensedcode/data/licenses/ecfonts-1.0.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ short_name: latex-ec-fonts
name: Copyright notice to the ec fonts
category: Permissive
homepage_url: http://dante.ctan.org
owner: Joerg Knappen
text_urls:
- http://dante.ctan.org/tex-archive/fonts/ec/src/copyrite.txt
6 changes: 0 additions & 6 deletions src/licensedcode/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@
import sys
from time import time

# Python 2 and 3 support
try:
import itertools.izip as zip # NOQA
except ImportError:
pass

from intbitset import intbitset
from six import string_types

Expand Down
7 changes: 1 addition & 6 deletions src/licensedcode/match_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@

from six import string_types

from commoncode.system import py2
from commoncode.system import py3
from licensedcode.match import LicenseMatch
from licensedcode.spans import Span

Expand Down Expand Up @@ -65,10 +63,7 @@ def tokens_hash(tokens):
"""
Return a digest binary string computed from a sequence of numeric token ids.
"""
if py2:
as_bytes = array('h', tokens).tostring()
if py3:
as_bytes = array('h', tokens).tobytes()
as_bytes = array('h', tokens).tobytes()
return md5(as_bytes).digest()


Expand Down
Loading