Skip to content

Commit 899d755

Browse files
author
Filip Richtarik
committed
fix
1 parent 99df03a commit 899d755

9 files changed

Lines changed: 41 additions & 101 deletions

File tree

.flake8

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[flake8]
2+
max-line-length = 90
3+
extend-ignore=E203

.github/workflows/test.yml

Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -40,65 +40,15 @@ jobs:
4040
key: ${{ runner.os }}-${{ matrix.python-version }}-tox-${{ hashFiles('setup.py') }}-${{ hashFiles('setup.cfg') }} }}
4141
- name: Test with tox
4242
run: tox
43+
- name: Commit and Push to Pull Request
44+
if: matrix.python-version == 3.8
45+
run: |
46+
if [ -n "$(git status -s)" ]; then
47+
git add .
48+
git commit -m "✨ ⭐ Automated commit has been added to your pull request to fix formatting! ⭐ ✨"
49+
git push origin ${{ github.head_ref }}
50+
fi
4351
- uses: codecov/codecov-action@v4
4452
with:
4553
token: ${{ secrets.CODECOV_TOKEN }}
4654
verbose: true
47-
48-
# Always run this last as it can push new changes and actions will not rerun.
49-
# not working on pull requests from forks
50-
pre-commit:
51-
if: false
52-
permissions:
53-
contents: write
54-
pull-requests: write
55-
needs: [tests]
56-
runs-on: ubuntu-latest
57-
steps:
58-
- name: Checkout
59-
uses: actions/checkout@v4
60-
with:
61-
ref: ${{ github.head_ref }}
62-
63-
- name: Setup Python
64-
uses: actions/setup-python@v5
65-
with:
66-
python-version: "3.8"
67-
68-
- name: Install PreCommit
69-
run: pip install pre-commit
70-
71-
- uses: actions/cache@v4
72-
with:
73-
path: ~/.cache/pre-commit
74-
key: ${{ runner.os }}-pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}
75-
restore-keys: |
76-
${{ runner.os }}-pre-commit-
77-
78-
- name: PreCommit
79-
id: pre-commit
80-
run: |
81-
if pre-commit run --show-diff-on-failure --color=always --all-files; then
82-
echo "failed=0" >> $GITHUB_OUTPUT
83-
else
84-
echo "failed=1" >> $GITHUB_OUTPUT
85-
fi
86-
if [ -n "$(git status -s)" ]; then
87-
echo "dirty=1" >> $GITHUB_OUTPUT
88-
else
89-
echo "dirty=0" >> $GITHUB_OUTPUT
90-
fi
91-
92-
# Run a second time to verify that everything has indeed been fixed.
93-
- name: PreCommit verify
94-
if: steps.pre-commit.outputs.failed == 1
95-
run: |
96-
pre-commit run --show-diff-on-failure --color=always --all-files
97-
98-
- name: Commit and Push to Pull Request
99-
if: steps.pre-commit.outputs.dirty == 1
100-
run: |
101-
git add .
102-
git status
103-
git commit -m "✨ ⭐ Automated commit has been added to your pull request to fix formatting! ⭐ ✨"
104-
git push origin ${{ github.head_ref }}

.pre-commit-config.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ repos:
1818
rev: 7.0.0
1919
hooks:
2020
- id: flake8
21-
args: ["--max-line-length=90", "--extend-ignore=E203,W503"]
21+
- repo: https://github.com/astral-sh/ruff-pre-commit
22+
rev: v0.3.7
23+
hooks:
24+
- id: ruff
25+
- id: ruff-format
2226
- repo: https://github.com/asottile/pyupgrade
2327
rev: v3.15.2
2428
hooks:

benchmark/run.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ def require_test_cases():
6060

6161

6262
def time_shell(cmd, runs=1, repetitions=1, setup=None):
63-
time_cmd = "time for i in {{1..{rep}}}; do {cmd}; done".format(
64-
cmd=cmd, rep=repetitions
65-
)
63+
time_cmd = f"time for i in {{1..{repetitions}}}; do {cmd}; done"
6664
if setup is not None:
6765
time_cmd = f"{setup}; {time_cmd}"
6866

@@ -97,8 +95,8 @@ def get_reference_shell_cmd(dirpath, algorithm):
9795
else:
9896
raise ValueError("only md5 and sha supported")
9997

100-
return "find {dir} -type f -print0 | sort -z | xargs -0 {alg} | {alg}".format(
101-
dir=dirpath, alg=algorithm
98+
return (
99+
f"find {dirpath} -type f -print0 | sort -z | xargs -0 {algorithm} | {algorithm}"
102100
)
103101

104102

src/dirhash/__init__.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env python
2-
"""dirhash - a python library (and CLI) for hashing of file system directories.
3-
"""
2+
"""dirhash - a python library (and CLI) for hashing of file system directories."""
43

54
import hashlib
65
import os
@@ -458,9 +457,8 @@ def __init__(self, entry_properties=("name", "data"), allow_cyclic_links=False):
458457
entry_properties = set(entry_properties)
459458
if not entry_properties.issubset(self.EntryProperties.options):
460459
raise ValueError(
461-
"entry properties {} not supported".format(
462-
entry_properties - self.EntryProperties.options
463-
)
460+
f"entry properties {entry_properties - self.EntryProperties.options} "
461+
"not supported"
464462
)
465463
if not (
466464
self.EntryProperties.NAME in entry_properties
@@ -476,8 +474,7 @@ def __init__(self, entry_properties=("name", "data"), allow_cyclic_links=False):
476474

477475
if not isinstance(allow_cyclic_links, bool):
478476
raise ValueError(
479-
"allow_cyclic_link must be a boolean, "
480-
"got {}".format(allow_cyclic_links)
477+
f"allow_cyclic_link must be a boolean, got {allow_cyclic_links}"
481478
)
482479
self.allow_cyclic_links = allow_cyclic_links
483480

src/dirhash/_version.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,8 @@ def git_pieces_from_vcs(
381381
if verbose:
382382
fmt = "tag '%s' doesn't start with prefix '%s'"
383383
print(fmt % (full_tag, tag_prefix))
384-
pieces["error"] = "tag '{}' doesn't start with prefix '{}'".format(
385-
full_tag,
386-
tag_prefix,
384+
pieces["error"] = (
385+
f"tag '{full_tag}' doesn't start with prefix '{tag_prefix}'"
387386
)
388387
return pieces
389388
pieces["closest-tag"] = full_tag[len(tag_prefix) :]

src/dirhash/cli.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env python
2-
"""Get hash for the content and/or structure of a directory.
3-
"""
2+
"""Get hash for the content and/or structure of a directory."""
43

54
import argparse
65
import sys
@@ -39,14 +38,13 @@ def get_kwargs(args):
3938
choices=dirhash.algorithms_available,
4039
default="md5",
4140
help=(
42-
'Hashing algorithm to use, by default "md5". Always available: {}. '
43-
"Additionally available on current platform: {}. Note that the same "
44-
"algorithm may appear multiple times in this set under different names "
45-
"(thanks to OpenSSL) "
46-
"[https://docs.python.org/2/library/hashlib.html]".format(
47-
sorted(dirhash.algorithms_guaranteed),
48-
sorted(dirhash.algorithms_available - dirhash.algorithms_guaranteed),
49-
)
41+
"Hashing algorithm to use, by default 'md5'. "
42+
f"Always available: {sorted(dirhash.algorithms_guaranteed)}. "
43+
f"Additionally available on current platform: "
44+
f"{sorted(dirhash.algorithms_available - dirhash.algorithms_guaranteed)}. "
45+
"Note that the same algorithm may appear multiple times in this set "
46+
"under different names (thanks to OpenSSL) "
47+
"[https://docs.python.org/2/library/hashlib.html]."
5048
),
5149
metavar="",
5250
)
@@ -129,11 +127,12 @@ def get_kwargs(args):
129127
dest="entry_properties",
130128
default=["data", "name"],
131129
help=(
132-
"List of file/directory properties to include in the hash. Available "
133-
"properties are: {} and at least one of name and data must be "
134-
"included. Default is [data name] which means that both the name/paths"
135-
" and content (actual data) of files and directories will be included"
136-
).format(list(dirhash.Protocol.EntryProperties.options)),
130+
"List of file/directory properties to include in the hash. "
131+
f"Available properties are: {list(dirhash.Protocol.EntryProperties.options)} "
132+
"and at least one of name and data must be included. "
133+
"Default is [data name] which means that both the name/paths "
134+
"and content (actual data) of files and directories will be included"
135+
),
137136
metavar="",
138137
)
139138
protocol_options.add_argument(

tests/test_dirhash.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323

2424
class TestGetHasherFactory:
25-
2625
def test_get_guaranteed(self):
2726
algorithm_and_hasher_factory = [
2827
("md5", hashlib.md5),
@@ -59,14 +58,12 @@ def test_not_available(self):
5958
_get_hasher_factory("not available")
6059

6160
def test_bypass_hasher_factory(self):
62-
6361
# test standard hasher
6462
hasher_factory = _get_hasher_factory(hashlib.sha256)
6563
assert hasher_factory is hashlib.sha256
6664

6765
# test raise on custom hasher with bad interface
6866
class IncompleteMockHasher:
69-
7067
def __init__(self, *args, **kwargs):
7168
pass
7269

@@ -78,7 +75,6 @@ def update(self, *args, **kwargs):
7875

7976
# test custom hasher with ok interface
8077
class MockHasher(IncompleteMockHasher):
81-
8278
def hexdigest(self):
8379
return ""
8480

@@ -87,7 +83,6 @@ def hexdigest(self):
8783

8884

8985
class TestGetMatchPatterns:
90-
9186
def test_default_match_all(self):
9287
ms = get_match_patterns()
9388
assert ms == ["*"]
@@ -139,7 +134,6 @@ def test_ignore_extensions(self):
139134

140135

141136
class TempDirTest:
142-
143137
def setup_method(self):
144138
self.dir = tempfile.mkdtemp()
145139

@@ -441,7 +435,6 @@ def dirhash_mp_comp(*args, **kwargs):
441435

442436

443437
class TestDirhash(TempDirTest):
444-
445438
def test_guaranteed_algorithms(self):
446439
self.mkdirs("root/d1/d11")
447440
self.mkdirs("root/d2")
@@ -545,7 +538,6 @@ def test_symlinked_dir(self):
545538
assert root1_linked_dirs_true == root2
546539

547540
def test_cache_used_for_symlinks(self):
548-
549541
self.mkdirs("root/dir")
550542
self.mkfile("root/file", "< one chunk content")
551543
for i in range(10):
@@ -667,7 +659,6 @@ def test_raise_on_not_at_least_one_of_name_and_data(self):
667659
)
668660

669661
def test_multiproc_speedup(self):
670-
671662
self.mkdirs("root/dir")
672663
num_files = 10
673664
for i in range(num_files):
@@ -809,7 +800,6 @@ def hexdigest(self):
809800

810801

811802
class IdentityHasher:
812-
813803
def __init__(self, initial_data=b""):
814804
self.datas = [initial_data.decode("utf-8")]
815805

@@ -821,7 +811,6 @@ def hexdigest(self):
821811

822812

823813
class TestProtocol:
824-
825814
def test_raise_for_invalid_entry_properties(self):
826815
with pytest.raises(ValueError):
827816
Protocol(entry_properties=["not-valid"])

tox.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
envlist = py{38,39,310,311,312}
33

44
[testenv]
5+
allowlist_externals = bash
56
deps =
67
pre-commit
78
pytest
89
pytest-cov
910
commands =
10-
pre-commit run --all-files
11+
bash -ec 'if [[ ${envpython} == *"py38"* ]]; then pre-commit run --all-files; fi' {posargs}
1112
pytest --cov=dirhash --cov-report=xml --cov-report=term-missing --cov-config=.coveragerc tests/ {posargs}
1213

1314
[gh-actions]

0 commit comments

Comments
 (0)