Skip to content

Commit c4f429e

Browse files
committed
Add farchive init command for creating new archives
- Add init command that creates a new empty archive - Update tests to use init instead of Farchive(db).close() workaround - Users must now run 'farchive init db' before 'farchive store' etc. This provides a clear workflow: farchive init new.db && farchive store new.db loc/a file.txt
1 parent e291481 commit c4f429e

3 files changed

Lines changed: 49 additions & 11 deletions

File tree

src/farchive/_cli.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1537,11 +1537,24 @@ def _cmd_schema(args: argparse.Namespace) -> None:
15371537
print("Note: archive will be auto-migrated on next write.", file=sys.stderr)
15381538
elif current > SCHEMA_VERSION:
15391539
print(
1540-
"Warning: archive schema is newer than library. Upgrade farchive.",
1540+
"ERROR: archive schema is newer than library supports.",
15411541
file=sys.stderr,
15421542
)
15431543

15441544

1545+
def _cmd_init(args: argparse.Namespace) -> None:
1546+
"""Initialize a new archive."""
1547+
if os.path.isfile(args.db):
1548+
print(f"Archive already exists: {args.db}", file=sys.stderr)
1549+
sys.exit(1)
1550+
if _is_valid_db(args.db):
1551+
print(f"Archive already exists (valid): {args.db}", file=sys.stderr)
1552+
sys.exit(1)
1553+
with Farchive(args.db) as fa:
1554+
pass
1555+
print(f"Created new archive: {args.db}", file=sys.stderr)
1556+
1557+
15451558
def main(argv: list[str] | None = None) -> None:
15461559
parser = argparse.ArgumentParser(
15471560
description="Content-addressed archive with observation history.",
@@ -1555,6 +1568,10 @@ def main(argv: list[str] | None = None) -> None:
15551568
"-v", "--verbose", action="store_true", help="Show all storage classes"
15561569
)
15571570

1571+
# init
1572+
p = sub.add_parser("init", help="Initialize a new archive")
1573+
p.add_argument("db", help="DB path")
1574+
15581575
# history LOCATOR
15591576
p = sub.add_parser("history", help="Show span history for a locator")
15601577
p.add_argument("db", help="DB path")
@@ -1766,6 +1783,7 @@ def main(argv: list[str] | None = None) -> None:
17661783
sys.exit(1)
17671784

17681785
cmds = {
1786+
"init": _cmd_init,
17691787
"stats": _cmd_stats,
17701788
"history": _cmd_history,
17711789
"locators": _cmd_locators,

tests/test_cli_cat.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ class TestStore:
108108

109109
def test_store_from_file(self, tmp_path):
110110
db = tmp_path / "test.db"
111-
Farchive(db).close() # Create empty db
111+
subprocess.run(
112+
[sys.executable, "-m", "farchive._cli", "init", str(db)], check=True
113+
)
112114
content = b"hello from file"
113115
f = tmp_path / "input.txt"
114116
f.write_bytes(content)
@@ -123,7 +125,9 @@ def test_store_from_file(self, tmp_path):
123125

124126
def test_store_from_stdin(self, tmp_path):
125127
db = tmp_path / "test.db"
126-
Farchive(db).close() # Create empty db
128+
subprocess.run(
129+
[sys.executable, "-m", "farchive._cli", "init", str(db)], check=True
130+
)
127131
content = b"hello from stdin"
128132

129133
result = subprocess.run(
@@ -148,7 +152,9 @@ def test_store_from_stdin(self, tmp_path):
148152

149153
def test_store_with_storage_class(self, tmp_path):
150154
db = tmp_path / "test.db"
151-
Farchive(db).close() # Create empty db
155+
subprocess.run(
156+
[sys.executable, "-m", "farchive._cli", "init", str(db)], check=True
157+
)
152158
f = tmp_path / "page.html"
153159
f.write_bytes(b"<html></html>")
154160

@@ -173,7 +179,9 @@ def test_store_with_storage_class(self, tmp_path):
173179

174180
def test_store_json_output(self, tmp_path):
175181
db = tmp_path / "test.db"
176-
Farchive(db).close() # Create empty db
182+
subprocess.run(
183+
[sys.executable, "-m", "farchive._cli", "init", str(db)], check=True
184+
)
177185
f = tmp_path / "data.txt"
178186
f.write_bytes(b"data")
179187

@@ -194,14 +202,18 @@ def test_store_json_output(self, tmp_path):
194202

195203
def test_store_file_not_found(self, tmp_path):
196204
db = tmp_path / "test.db"
197-
Farchive(db).close() # Create empty db
205+
subprocess.run(
206+
[sys.executable, "-m", "farchive._cli", "init", str(db)], check=True
207+
)
198208
result = _run(["store", str(db), "loc/a", "nonexistent.txt"])
199209
assert result.returncode != 0
200210
assert b"File not found" in result.stderr
201211

202212
def test_store_metadata(self, tmp_path):
203213
db = tmp_path / "test.db"
204-
Farchive(db).close() # Create empty db
214+
subprocess.run(
215+
[sys.executable, "-m", "farchive._cli", "init", str(db)], check=True
216+
)
205217
f = tmp_path / "data.txt"
206218
f.write_bytes(b"data")
207219

tests/test_cli_import.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ class TestPutBlob:
2727

2828
def test_put_blob_from_file(self, tmp_path):
2929
db = tmp_path / "test.db"
30-
Farchive(db).close() # Create empty db
30+
subprocess.run(
31+
[sys.executable, "-m", "farchive._cli", "init", str(db)], check=True
32+
)
3133
f = tmp_path / "data.bin"
3234
f.write_bytes(b"hello blob")
3335

@@ -41,7 +43,9 @@ def test_put_blob_from_file(self, tmp_path):
4143

4244
def test_put_blob_from_stdin(self, tmp_path):
4345
db = tmp_path / "test.db"
44-
Farchive(db).close() # Create empty db
46+
subprocess.run(
47+
[sys.executable, "-m", "farchive._cli", "init", str(db)], check=True
48+
)
4549
result = subprocess.run(
4650
[sys.executable, "-m", "farchive._cli", "put-blob", str(db), "-"],
4751
input=b"stdin blob",
@@ -56,7 +60,9 @@ def test_put_blob_from_stdin(self, tmp_path):
5660

5761
def test_put_blob_json(self, tmp_path):
5862
db = tmp_path / "test.db"
59-
Farchive(db).close() # Create empty db
63+
subprocess.run(
64+
[sys.executable, "-m", "farchive._cli", "init", str(db)], check=True
65+
)
6066
f = tmp_path / "data.bin"
6167
f.write_bytes(b"data")
6268

@@ -68,7 +74,9 @@ def test_put_blob_json(self, tmp_path):
6874

6975
def test_put_blob_no_locator(self, tmp_path):
7076
db = tmp_path / "test.db"
71-
Farchive(db).close() # Create empty db
77+
subprocess.run(
78+
[sys.executable, "-m", "farchive._cli", "init", str(db)], check=True
79+
)
7280
f = tmp_path / "data.bin"
7381
f.write_bytes(b"data")
7482

0 commit comments

Comments
 (0)