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