Skip to content

Commit d830c55

Browse files
committed
Ensure idempotency close #20
1 parent d79e684 commit d830c55

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

pathtraits/cli.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def main():
3434
def batch(path, db_path, exclude_regex, verbose):
3535
"""
3636
Update database once, searches for all directories recursively.
37+
Deletes existing database to ensure its synced with sidecar metadata files.
3738
"""
3839
scan.batch(path, db_path, exclude_regex, verbose)
3940

@@ -86,15 +87,20 @@ def query(query_str, db_path):
8687

8788

8889
@main.command()
89-
@click.argument("path", required=True, type=click.Path(exists=True, file_okay=False, dir_okay=True))
90+
@click.argument(
91+
"path", required=True, type=click.Path(exists=True, file_okay=False, dir_okay=True)
92+
)
9093
@click.option(
91-
"--needed-until", "-nu",
94+
"--needed-until",
95+
"-nu",
9296
default=None,
9397
help="Optional account termination date (YYYY-MM-DD)",
9498
)
9599
@click.option(
96-
"--overwrite", "-o",
97-
is_flag=True, default=False,
100+
"--overwrite",
101+
"-o",
102+
is_flag=True,
103+
default=False,
98104
help="Overwrite existing metadata.",
99105
)
100106
@click.option("-v", "--verbose", is_flag=True, default=False)
@@ -104,5 +110,6 @@ def create(path, needed_until, overwrite, verbose):
104110
"""
105111
_create.generate_metadata(path, needed_until, overwrite, verbose)
106112

113+
107114
if __name__ == "__main__":
108115
main()

pathtraits/scan.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def scan_meta_yml(path, pathpairs=[], exclude_regex=None):
5050
def batch(path, db_path, exclude_regex, verbose):
5151
"""
5252
Update database once, searches for all directories recursively.
53+
Deletes existing database to ensure its synced with sidecar metadata files.
5354
5455
:param path: path to scan in batch mode recursively
5556
:param db_path: path to the database
@@ -60,6 +61,11 @@ def batch(path, db_path, exclude_regex, verbose):
6061

6162
if db_path is None:
6263
db_path = path + "/.pathtraits.db"
64+
65+
# database is just a cache, deleting specific rows is very slow.
66+
if os.path.exists(db_path):
67+
os.remove(db_path)
68+
6369
db = TraitsDB(db_path)
6470
if exclude_regex is not None:
6571
exclude_regex = re.compile(exclude_regex)

test/test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import pathtraits.db
1111
import pathtraits.scan
1212
import pathtraits.access
13+
import hashlib
1314

1415

1516
class TestMain(unittest.TestCase):
@@ -79,6 +80,17 @@ def test_query(self):
7980
)
8081
self.assertEqual(len(q2), 1)
8182

83+
def test_idempotency_batch(self):
84+
db_path = tempfile.mkstemp()[1]
85+
pathtraits.scan.batch("test/example", db_path, "", False)
86+
hash1 = hashlib.file_digest(open(db_path, "rb"), "sha256").hexdigest()
87+
88+
pathtraits.scan.batch("test/example", db_path, "", False)
89+
hash2 = hashlib.file_digest(open(db_path, "rb"), "sha256").hexdigest()
90+
91+
os.remove(db_path)
92+
self.assertEqual(hash1, hash2)
93+
8294

8395
if __name__ == "__main__":
8496
unittest.main()

0 commit comments

Comments
 (0)