Changeset View
Changeset View
Standalone View
Standalone View
swh/clearlydefined/mapping_utils.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import psycopg2 | import psycopg2 | ||||
import swh.model.hashutil | import swh.model.hashutil | ||||
def map_sha1_with_swhid(sha1, database, user, password, host, port): | def map_sha1_with_swhid(sha1, dsn): | ||||
if not sha1: | if not sha1: | ||||
return None | return None | ||||
read_connection = psycopg2.connect( | read_connection = psycopg2.connect(dsn=dsn) | ||||
database=database, user=user, password=password, host=host, port=port | |||||
) | |||||
cur = read_connection.cursor() | cur = read_connection.cursor() | ||||
sha1 = swh.model.hashutil.hash_to_hex(sha1) | sha1 = swh.model.hashutil.hash_to_hex(sha1) | ||||
sha1 = "\\x{sha1}".format(sha1=sha1) | sha1 = "\\x{sha1}".format(sha1=sha1) | ||||
cur.execute("SELECT sha1_git from content WHERE sha1=%s;", (sha1)) | cur.execute("SELECT sha1_git FROM content where sha1= %s;", (sha1,)) | ||||
sha1_git = cur.fetchall() | sha1_git = cur.fetchall() | ||||
swh_id = None | |||||
if len(sha1_git) == 0: | if len(sha1_git) == 0: | ||||
return None | return None | ||||
sha1_git = swh.model.hashutil.hash_to_hex(sha1_git[0][0]) | sha1_git = swh.model.hashutil.hash_to_hex(sha1_git[0][0]) | ||||
swh_id = "swh:1:cnt:{sha1_git}".format(sha1_git=sha1_git) | swh_id = "swh:1:cnt:{sha1_git}".format(sha1_git=sha1_git) | ||||
return swh_id | return swh_id |