diff --git a/bin/swh-hashtree b/bin/swh-hashtree index 5b85b7b5e73d24afc9d4e53b0a275327c2586925..a4f8d7b70b303bf55159b7c44c895a293f9407ec 100755 --- a/bin/swh-hashtree +++ b/bin/swh-hashtree @@ -5,9 +5,10 @@ # --ignore-empty-folders # 38f8d2c3a951f6b94007896d0981077e48bbd702 -import click import os +import click + from swh.model import from_disk, hashutil diff --git a/bin/swh-revhash b/bin/swh-revhash index d3a8caf84fab120f7721bffd4d269ee77626982e..56b587d905826efbba8976ae13c5129c567f1663 100755 --- a/bin/swh-revhash +++ b/bin/swh-revhash @@ -11,7 +11,7 @@ import sys -from swh.model import identifiers, hashutil +from swh.model import hashutil, identifiers def revhash(revision_raw): diff --git a/setup.py b/setup.py index ecaac1058cb3dc356fe45ef910d5bed157dbc11b..f42058ca01a4bdd9cd25e6eda6dd07b77db5db54 100755 --- a/setup.py +++ b/setup.py @@ -4,10 +4,10 @@ # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information -from setuptools import setup, find_packages - -from os import path from io import open +from os import path + +from setuptools import find_packages, setup here = path.abspath(path.dirname(__file__)) diff --git a/swh/model/cli.py b/swh/model/cli.py index 53920700383775a17897620fc86ad27d0f911dc7..68a368050825a86926f0ccba39c2bc01cc22c9b0 100644 --- a/swh/model/cli.py +++ b/swh/model/cli.py @@ -3,12 +3,12 @@ # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information -# WARNING: do not import unnecessary things here to keep cli startup time under -# control -import click import os import sys +# WARNING: do not import unnecessary things here to keep cli startup time under +# control +import click CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"]) @@ -38,7 +38,7 @@ class SWHIDParamType(click.ParamType): def swhid_of_file(path): from swh.model.from_disk import Content - from swh.model.identifiers import swhid, CONTENT + from swh.model.identifiers import CONTENT, swhid object = Content.from_file(path=path).get_data() return swhid(CONTENT, object) @@ -46,7 +46,7 @@ def swhid_of_file(path): def swhid_of_file_content(data): from swh.model.from_disk import Content - from swh.model.identifiers import swhid, CONTENT + from swh.model.identifiers import CONTENT, swhid object = Content.from_bytes(mode=644, data=data).get_data() return swhid(CONTENT, object) @@ -54,24 +54,23 @@ def swhid_of_file_content(data): def swhid_of_dir(path): from swh.model.from_disk import Directory - from swh.model.identifiers import swhid, DIRECTORY + from swh.model.identifiers import DIRECTORY, swhid object = Directory.from_disk(path=path).get_data() return swhid(DIRECTORY, object) def swhid_of_origin(url): - from swh.model.identifiers import origin_identifier - from swh.model.identifiers import SWHID + from swh.model.identifiers import SWHID, origin_identifier return str(SWHID(object_type="origin", object_id=origin_identifier({"url": url}))) def swhid_of_git_repo(path): import dulwich.repo + from swh.model import hashutil - from swh.model.identifiers import snapshot_identifier - from swh.model.identifiers import SWHID + from swh.model.identifiers import SWHID, snapshot_identifier repo = dulwich.repo.Repo(path) diff --git a/swh/model/fields/__init__.py b/swh/model/fields/__init__.py index a5b1ed3f8642c3de1593861fc3b9780697683f98..7e3c2fef75cadbf876b937bd14e3e79fe9bbfc0c 100644 --- a/swh/model/fields/__init__.py +++ b/swh/model/fields/__init__.py @@ -6,13 +6,13 @@ # We do our imports here but we don't use them, so flake8 complains # flake8: noqa +from .compound import validate_against_schema, validate_all_keys, validate_any_key +from .hashes import validate_sha1, validate_sha1_git, validate_sha256 from .simple import ( - validate_type, - validate_int, - validate_str, validate_bytes, validate_datetime, validate_enum, + validate_int, + validate_str, + validate_type, ) -from .hashes import validate_sha1, validate_sha1_git, validate_sha256 -from .compound import validate_against_schema, validate_all_keys, validate_any_key diff --git a/swh/model/fields/compound.py b/swh/model/fields/compound.py index 3133f59cc705d497ab3cbfddd3cb5d098b92e04d..90b4685bafc0b9cf8cac78c87d5a95e521468515 100644 --- a/swh/model/fields/compound.py +++ b/swh/model/fields/compound.py @@ -6,7 +6,7 @@ from collections import defaultdict import itertools -from ..exceptions import ValidationError, NON_FIELD_ERRORS +from ..exceptions import NON_FIELD_ERRORS, ValidationError def validate_against_schema(model, schema, value): diff --git a/swh/model/fields/hashes.py b/swh/model/fields/hashes.py index 47e872c7b994881bd86a1c33a4a184acbb4732a2..9b5ee4ad4ec58d512ced2223f7577d935864b502 100644 --- a/swh/model/fields/hashes.py +++ b/swh/model/fields/hashes.py @@ -4,6 +4,7 @@ # See top-level LICENSE file for more information import string + from ..exceptions import ValidationError diff --git a/swh/model/from_disk.py b/swh/model/from_disk.py index 5ac97e257fcf30af80ba1a811fb92013eb7b90b3..719599d0563fef48b00973b8daf6e92168bf4a73 100644 --- a/swh/model/from_disk.py +++ b/swh/model/from_disk.py @@ -7,21 +7,18 @@ import datetime import enum import os import stat +from typing import Any, Iterable, List, Optional, Tuple import attr from attrs_strict import type_validator -from typing import Any, Iterable, List, Optional, Tuple from typing_extensions import Final +from . import model from .hashutil import MultiHash +from .identifiers import directory_entry_sort_key, directory_identifier +from .identifiers import identifier_to_bytes as id_to_bytes +from .identifiers import identifier_to_str as id_to_str from .merkle import MerkleLeaf, MerkleNode -from .identifiers import ( - directory_entry_sort_key, - directory_identifier, - identifier_to_bytes as id_to_bytes, - identifier_to_str as id_to_str, -) -from . import model @attr.s diff --git a/swh/model/hashutil.py b/swh/model/hashutil.py index 954ae9576452a00649040eb4662ecd3756fe00a5..cec87789cb7fffc5b9112a9a81e38de4d8eab0d8 100644 --- a/swh/model/hashutil.py +++ b/swh/model/hashutil.py @@ -54,9 +54,8 @@ Basic usage examples: import binascii import functools import hashlib -import os - from io import BytesIO +import os from typing import Callable, Dict ALGORITHMS = set(["sha1", "sha256", "sha1_git", "blake2s256", "blake2b512"]) diff --git a/swh/model/hypothesis_strategies.py b/swh/model/hypothesis_strategies.py index 21e922eed40cfc95db664bce1acc4ce0d344ef35..0c54a994e7d6dbb0df372868faea5f8a2c81be58 100644 --- a/swh/model/hypothesis_strategies.py +++ b/swh/model/hypothesis_strategies.py @@ -28,28 +28,27 @@ from hypothesis.strategies import ( ) from .from_disk import DentryPerms +from .identifiers import identifier_to_bytes, snapshot_identifier from .model import ( - Person, - Timestamp, - TimestampWithTimezone, + BaseContent, + Content, + Directory, + DirectoryEntry, + ObjectType, Origin, OriginVisit, OriginVisitStatus, - Snapshot, - SnapshotBranch, - ObjectType, - TargetType, + Person, Release, Revision, RevisionType, - BaseContent, - Directory, - DirectoryEntry, - Content, SkippedContent, + Snapshot, + SnapshotBranch, + TargetType, + Timestamp, + TimestampWithTimezone, ) -from .identifiers import snapshot_identifier, identifier_to_bytes - pgsql_alphabet = characters( blacklist_categories=("Cs",), blacklist_characters=["\u0000"] diff --git a/swh/model/identifiers.py b/swh/model/identifiers.py index e1cf0dfe3b80fe40f0df23beb3c493098f82c2fd..63d028d231a5840f04895f6652c479aded4a9f99 100644 --- a/swh/model/identifiers.py +++ b/swh/model/identifiers.py @@ -5,9 +5,8 @@ import binascii import datetime -import hashlib - from functools import lru_cache +import hashlib from typing import Any, Dict, Union import attr @@ -16,8 +15,7 @@ from deprecated import deprecated from .collections import ImmutableDict from .exceptions import ValidationError from .fields.hashes import validate_sha1 -from .hashutil import hash_git_data, hash_to_hex, MultiHash - +from .hashutil import MultiHash, hash_git_data, hash_to_hex ORIGIN = "origin" SNAPSHOT = "snapshot" diff --git a/swh/model/merkle.py b/swh/model/merkle.py index 0311d9d19dd02ab5cf68378d95a7913ff4b226d2..e84ef9d9823a3ffb3444a3c5586abae16f4b6772 100644 --- a/swh/model/merkle.py +++ b/swh/model/merkle.py @@ -7,7 +7,6 @@ import abc from collections.abc import Mapping - from typing import Iterator, List, Set diff --git a/swh/model/model.py b/swh/model/model.py index 219d7c5cef87bdd0a2c4e1824236a58978582569..d7199660049929ca57d0fcd32e09b2a495ecff4a 100644 --- a/swh/model/model.py +++ b/swh/model/model.py @@ -3,29 +3,28 @@ # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information -import datetime - from abc import ABCMeta, abstractmethod +import datetime from enum import Enum from hashlib import sha256 from typing import Any, Dict, Iterable, Optional, Tuple, TypeVar, Union -from typing_extensions import Final import attr from attrs_strict import type_validator import dateutil.parser import iso8601 +from typing_extensions import Final from .collections import ImmutableDict -from .hashutil import DEFAULT_ALGORITHMS, hash_to_bytes, MultiHash +from .hashutil import DEFAULT_ALGORITHMS, MultiHash, hash_to_bytes from .identifiers import ( - normalize_timestamp, + SWHID, directory_identifier, - revision_identifier, + normalize_timestamp, + parse_swhid, release_identifier, + revision_identifier, snapshot_identifier, - SWHID, - parse_swhid, ) diff --git a/swh/model/tests/generate_testdata.py b/swh/model/tests/generate_testdata.py index 0280a6ab15582e49d1d79b1ba9104d55aa5abd7e..f4093a4d5ab5db80ab1616110cd4426201f47ee8 100644 --- a/swh/model/tests/generate_testdata.py +++ b/swh/model/tests/generate_testdata.py @@ -4,12 +4,12 @@ # See top-level LICENSE file for more information from datetime import datetime -from pytz import all_timezones, timezone from random import choice, randint, random, shuffle -from typing import List, Dict +from typing import Dict, List -from swh.model.hashutil import MultiHash +from pytz import all_timezones, timezone +from swh.model.hashutil import MultiHash PROTOCOLS = ["git", "http", "https", "deb", "svn", "mock"] DOMAINS = ["example.com", "some.long.host.name", "xn--n28h.tld"] diff --git a/swh/model/tests/generate_testdata_from_disk.py b/swh/model/tests/generate_testdata_from_disk.py index 063e39093618a1e07c7dc9f55acc184a05d83643..3ad456463eb2b45a974c42953927b38d192763c3 100644 --- a/swh/model/tests/generate_testdata_from_disk.py +++ b/swh/model/tests/generate_testdata_from_disk.py @@ -7,7 +7,7 @@ from operator import itemgetter import os import sys -from swh.model.from_disk import Directory, DentryPerms +from swh.model.from_disk import DentryPerms, Directory from swh.model.hashutil import ALGORITHMS, hash_to_hex diff --git a/swh/model/tests/test_from_disk.py b/swh/model/tests/test_from_disk.py index 025615608f7240c1b28054cd63d2660e478ab3e0..497bf6c0ddf510d52f99ad2a1e7cdea883cac4fe 100644 --- a/swh/model/tests/test_from_disk.py +++ b/swh/model/tests/test_from_disk.py @@ -3,19 +3,18 @@ # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information +from collections import defaultdict import os -import pytest import tarfile import tempfile +from typing import ClassVar, Optional import unittest -from collections import defaultdict -from typing import ClassVar, Optional +import pytest -from swh.model import from_disk +from swh.model import from_disk, model from swh.model.from_disk import Content, DentryPerms, Directory, DiskBackedContent from swh.model.hashutil import DEFAULT_ALGORITHMS, hash_to_bytes, hash_to_hex -from swh.model import model TEST_DATA = os.path.join(os.path.dirname(__file__), "data") diff --git a/swh/model/tests/test_generate_testdata.py b/swh/model/tests/test_generate_testdata.py index aa9c8af305cff8cc6108853f6b01ceafcad3c8c5..6ed2e6383315196898d793a04c10cb4ee01ccb59 100644 --- a/swh/model/tests/test_generate_testdata.py +++ b/swh/model/tests/test_generate_testdata.py @@ -3,9 +3,9 @@ # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information -from .generate_testdata import gen_contents, gen_origins, ORIGINS +from swh.model.model import BaseContent, Origin -from swh.model.model import Origin, BaseContent +from .generate_testdata import ORIGINS, gen_contents, gen_origins def test_gen_origins_empty(): diff --git a/swh/model/tests/test_hypothesis_strategies.py b/swh/model/tests/test_hypothesis_strategies.py index e1ab9b73aae2cfa48b137cd26d14ed1a29a6359c..c93b24b91f91c0fb78eb648596f5a3cb7b6e5e4c 100644 --- a/swh/model/tests/test_hypothesis_strategies.py +++ b/swh/model/tests/test_hypothesis_strategies.py @@ -6,23 +6,22 @@ import datetime import attr -import iso8601 from hypothesis import given, settings +import iso8601 from swh.model.hashutil import DEFAULT_ALGORITHMS from swh.model.hypothesis_strategies import ( aware_datetimes, - objects, - object_dicts, contents, - skipped_contents, - snapshots, + object_dicts, + objects, origin_visits, persons, + skipped_contents, + snapshots, ) from swh.model.model import TargetType - target_types = ("content", "directory", "revision", "release", "snapshot", "alias") all_but_skipped_content = ( "origin", diff --git a/swh/model/tests/test_identifiers.py b/swh/model/tests/test_identifiers.py index 5acbd2d38c19466bda2c14b16595ac9d3d41cc0e..3741b70af906cff0f4b8c6af25112ccb126356c9 100644 --- a/swh/model/tests/test_identifiers.py +++ b/swh/model/tests/test_identifiers.py @@ -5,9 +5,10 @@ import binascii import datetime -import pytest import unittest +import pytest + from swh.model import hashutil, identifiers from swh.model.exceptions import ValidationError from swh.model.hashutil import hash_to_bytes as _x diff --git a/swh/model/tests/test_model.py b/swh/model/tests/test_model.py index 0404dcd111e6362346450c292c1203de817fc6aa..fdd5e044315024ac63c3d6c3afbfb755e729a103 100644 --- a/swh/model/tests/test_model.py +++ b/swh/model/tests/test_model.py @@ -12,41 +12,41 @@ from hypothesis import given from hypothesis.strategies import binary import pytest +from swh.model.hashutil import MultiHash, hash_to_bytes +import swh.model.hypothesis_strategies as strategies +from swh.model.identifiers import ( + SWHID, + directory_identifier, + parse_swhid, + release_identifier, + revision_identifier, + snapshot_identifier, +) from swh.model.model import ( BaseModel, Content, - SkippedContent, Directory, - Revision, - Release, - Snapshot, + MetadataAuthority, + MetadataAuthorityType, + MetadataFetcher, + MetadataTargetType, + MissingData, Origin, OriginVisit, OriginVisitStatus, - Timestamp, - TimestampWithTimezone, - MissingData, Person, RawExtrinsicMetadata, - MetadataTargetType, - MetadataAuthority, - MetadataAuthorityType, - MetadataFetcher, -) -from swh.model.hashutil import hash_to_bytes, MultiHash -import swh.model.hypothesis_strategies as strategies -from swh.model.identifiers import ( - directory_identifier, - revision_identifier, - release_identifier, - snapshot_identifier, - parse_swhid, - SWHID, + Release, + Revision, + SkippedContent, + Snapshot, + Timestamp, + TimestampWithTimezone, ) from swh.model.tests.test_identifiers import ( directory_example, - revision_example, release_example, + revision_example, snapshot_example, ) diff --git a/swh/model/validators.py b/swh/model/validators.py index 6cd7fc110dc13d4074338aa52eede6a76366b431..a2f9dbff2a672fd085eaa2ad14aaa43208b04314 100644 --- a/swh/model/validators.py +++ b/swh/model/validators.py @@ -3,8 +3,8 @@ # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information -from .exceptions import ValidationError, NON_FIELD_ERRORS from . import fields +from .exceptions import NON_FIELD_ERRORS, ValidationError from .hashutil import MultiHash, hash_to_bytes