From dbea8f1cf03b986ce98d50faa39f8721048b280e Mon Sep 17 00:00:00 2001 From: Lei Maohui Date: Thu, 26 Oct 2017 16:08:39 +0900 Subject: [PATCH] Fix bugs because python from 2.x to 3.x. Signed-off-by: Lei Maohui --- dosocs2/render.py | 4 ++-- dosocs2/scannerbase.py | 6 +++--- dosocs2/spdxdb.py | 2 +- dosocs2/util.py | 30 +++++++++++++++--------------- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/dosocs2/render.py b/dosocs2/render.py index 7118e3d..434bb5b 100644 --- a/dosocs2/render.py +++ b/dosocs2/render.py @@ -54,7 +54,7 @@ def get_row(conn, query): def get_rows(conn, query): - return list(sorted(dict(**row) for row in conn.execute(query))) + return list(dict(**row) for row in conn.execute(query)) def render_template(templatefile, context): @@ -77,7 +77,7 @@ def render_document(conn, docid, template_file): query = queries.relationships(document['document_namespace_id'], package['id_string']) package['relationships'] = get_rows(conn, query) package['files'] = get_rows(conn, queries.documents_files(docid, package['package_id'])) - for file in sorted(package['files']): + for file in package['files']: file['license_info'] = get_rows(conn, queries.files_licenses(file['file_id'])) file['contributors'] = get_rows(conn, queries.file_contributors(file['file_id'])) file['annotations'] = get_rows(conn, queries.annotations(docid, file['id_string'])) diff --git a/dosocs2/scannerbase.py b/dosocs2/scannerbase.py index d7a38b6..a54dce7 100644 --- a/dosocs2/scannerbase.py +++ b/dosocs2/scannerbase.py @@ -274,8 +274,8 @@ class FileLicenseScanner(Scanner): Return the new or existing license object in any case. ''' - transtable = string.maketrans('()[]<>', '------') - short_name = string.translate(short_name, transtable) + transtable = str.maketrans('()[]<>', '------') + short_name = str.translate(short_name, transtable) existing_license = FileLicenseScanner.lookup_license(conn, short_name) if existing_license is not None: return existing_license @@ -311,7 +311,7 @@ class FileLicenseScanner(Scanner): def store_results(self, processed_files): licenses_to_add = [] - for (file, licenses_extracted) in processed_files.iteritems(): + for (file, licenses_extracted) in processed_files.items(): licenses = [] for license_name in licenses_extracted: license_kwargs = { diff --git a/dosocs2/spdxdb.py b/dosocs2/spdxdb.py index a8d3fd6..b8aef5f 100644 --- a/dosocs2/spdxdb.py +++ b/dosocs2/spdxdb.py @@ -135,7 +135,7 @@ def register_package(conn, package_root, name=None, version=None, comment=None, package['package_id'] = insert(conn, db.packages, package) # Create packages_files rows row_params = [] - for (file_path, file_sha256) in hashes.iteritems(): + for (file_path, file_sha256) in hashes.items(): fileobj = register_file(conn, file_path, known_sha256=file_sha256) package_file_params = { 'package_id': package['package_id'], diff --git a/dosocs2/util.py b/dosocs2/util.py index 5670f67..aba864c 100644 --- a/dosocs2/util.py +++ b/dosocs2/util.py @@ -43,23 +43,23 @@ def bool_from_str(s): def is_source(magic_string): return ( - ' source' in magic_string and ' text' in magic_string or - ' script' in magic_string and ' text' in magic_string or - ' program' in magic_string and ' text' in magic_string or - ' shell script' in magic_string or - ' text executable' in magic_string or - 'HTML' in magic_string and 'text' in magic_string or - 'XML' in magic_string and 'text' in magic_string + b' source' in magic_string and b' text' in magic_string or + b' script' in magic_string and b' text' in magic_string or + b' program' in magic_string and b' text' in magic_string or + b' shell script' in magic_string or + b' text executable' in magic_string or + b'HTML' in magic_string and b'text' in magic_string or + b'XML' in magic_string and b'text' in magic_string ) def is_binary(magic_string): return ( - ' executable' in magic_string or - ' relocatable' in magic_string or - ' shared object' in magic_string or - ' dynamically linked' in magic_string or - ' ar archive' in magic_string + b' executable' in magic_string or + b' relocatable' in magic_string or + b' shared object' in magic_string or + b' dynamically linked' in magic_string or + b' ar archive' in magic_string ) @@ -70,7 +70,7 @@ def spdx_filetype(filename): return 'SOURCE' if is_binary(magic_string): return 'BINARY' - if 'archive' in magic_string: + if b'archive' in magic_string: return 'ARCHIVE' return 'OTHER' @@ -158,7 +158,7 @@ def gen_ver_code(hashes, excluded_hashes=None): excluded_hashes = set() hashes_less_excluded = (h for h in hashes if h not in excluded_hashes) hashblob = ''.join(sorted(hashes_less_excluded)) - return hashlib.sha256(hashblob).hexdigest() + return hashlib.sha256((hashblob).encode('utf-8')).hexdigest() def get_dir_hashes(path, excluded_hashes=None): @@ -184,7 +184,7 @@ def get_dir_hashes(path, excluded_hashes=None): and hashes.get(abspath) not in excluded_hashes ) rel_listing_hashes = ( - hashlib.sha256(relpath).hexdigest() + hashlib.sha256((relpath).encode('utf-8')).hexdigest() for relpath in sorted(relative_listing) ) return (gen_ver_code(hashes.values(), excluded_hashes), -- 2.7.4