diff --git a/conftest.py b/conftest.py new file mode 100644 --- /dev/null +++ b/conftest.py @@ -0,0 +1 @@ +pytest_plugins = ["swh.storage.pytest_plugin"] \ No newline at end of file diff --git a/mypy.ini b/mypy.ini --- a/mypy.ini +++ b/mypy.ini @@ -11,5 +11,8 @@ [mypy-pytest.*] ignore_missing_imports = True +[mypy-psycopg2.*] +ignore_missing_imports = True + # [mypy-add_your_lib_here.*] # ignore_missing_imports = True diff --git a/requirements-swh.txt b/requirements-swh.txt --- a/requirements-swh.txt +++ b/requirements-swh.txt @@ -1,2 +1,3 @@ # Add here internal Software Heritage dependencies, one per line. swh.core[http] >= 0.3 +swh.model >= 0.9.0 diff --git a/requirements.txt b/requirements.txt --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,11 @@ -# Add here external Python modules dependencies, one per line. Module names -# should match https://pypi.python.org/pypi names. For the full spec or -# dependency lines, see https://pip.readthedocs.org/en/1.1/requirements.html - +attrs==20.3.0 +attrs-strict==0.2.0 +Deprecated==1.2.10 +hypothesis==5.49.0 +iso8601==0.1.13 +psycopg2==2.8.6 +python-dateutil==2.8.1 +six==1.15.0 +sortedcontainers==2.3.0 +typing-extensions==3.7.4.3 +wrapt==1.12.1 diff --git a/swh/clearlydefined/mapping_utils.py b/swh/clearlydefined/mapping_utils.py new file mode 100644 --- /dev/null +++ b/swh/clearlydefined/mapping_utils.py @@ -0,0 +1,23 @@ +# 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 diff --git a/swh/clearlydefined/tests/test_mapping.py b/swh/clearlydefined/tests/test_mapping.py new file mode 100644 --- /dev/null +++ b/swh/clearlydefined/tests/test_mapping.py @@ -0,0 +1,22 @@ +from swh.clearlydefined.mapping_utils import map_sha1_with_swhid +import psycopg2 + + +def test_mapping(swh_storage_backend_config): + dsn = swh_storage_backend_config["db"] + read_connection = psycopg2.connect(dsn=dsn) + cur = read_connection.cursor() + sha1_byte = "\\xa6628c8a4fbc08c29b8472e2222975e5b9918131" + sha1_git_byte = "\\xe103b11cbfecbc116dacbb1f9ab2a02176092a32" + sha256_byte = "\\x6ac599151a7aaa8ca5d38dc5bb61b49193a3cadc1ed33de5a57e4d1ecc53c846" + blake2s256_byte = "\\xc509b320abede3580bb1de75a0efa09ba7416db9c8de845a4e1b4317c0b8a8d9" + length = 717 + cur.execute( + "INSERT INTO content (sha1, sha1_git, sha256, blake2s256, length) VALUES (%s,%s,%s,%s,%s);", + (sha1_byte, sha1_git_byte, sha256_byte, blake2s256_byte, length), + ) + read_connection.commit() + sha1 = "a6628c8a4fbc08c29b8472e2222975e5b9918131" + assert "swh:1:cnt:e103b11cbfecbc116dacbb1f9ab2a02176092a32" == map_sha1_with_swhid( + sha1=sha1, dsn=dsn + )