Skip to content
This repository was archived by the owner on Feb 26, 2026. It is now read-only.

Commit fde7827

Browse files
committed
feat: add assets
1 parent de085f0 commit fde7827

1 file changed

Lines changed: 46 additions & 2 deletions

File tree

src/sb2git/__init__.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import argparse
22
import shutil
33
import tomlkit
4+
import tomllib
45
import dataclasses
56
import zipfile
67
import json
78
import typing as t
9+
import git
810
import scratchattach.editor # this is slow but easier
911
from pathlib import Path
1012
from slugify import slugify
@@ -25,6 +27,8 @@ class ArgNamespace(argparse.Namespace):
2527
path: str
2628
output: str
2729
sort_by: t.Literal["mtime", "name", "size"]
30+
author: str
31+
email: str
2832

2933

3034
def main() -> None:
@@ -49,6 +53,13 @@ def main() -> None:
4953
if build := command.add_parser(
5054
"build", help="Build a sb2git input directory into an output directory"
5155
):
56+
build.add_argument("path", nargs="?", default=".", type=str)
57+
build.add_argument(
58+
"--author", type=str, help="name for git author", required=True
59+
)
60+
build.add_argument(
61+
"--email", type=str, help="email for git author", required=True
62+
)
5263
build.add_argument(
5364
"-o", "--output", type=str, default="build", help="Output directory"
5465
)
@@ -61,7 +72,7 @@ def run(args: ArgNamespace):
6172
case "init":
6273
init(args)
6374
case "build":
64-
...
75+
build(args)
6576

6677

6778
def init(args: ArgNamespace):
@@ -158,4 +169,37 @@ def sortfunc(f: Path):
158169
tomlkit.dump(content, f)
159170

160171

161-
def build(args: ArgNamespace): ...
172+
def build(args: ArgNamespace):
173+
# setup filesystem
174+
path = Path(args.path)
175+
config = path / "sb2git.toml"
176+
assets_in = path / "assets"
177+
assert path.exists() and path.is_dir()
178+
assert assets_in.exists() and assets_in.is_dir()
179+
180+
output = path / args.output
181+
print(f"Building into {output}")
182+
183+
if output.exists():
184+
if input(f"Overwrite {output}? (y/N): ") != "y":
185+
return
186+
shutil.rmtree(output)
187+
output.mkdir()
188+
189+
assets_out = output / "assets"
190+
assets_out.mkdir()
191+
192+
repo = git.Repo.init(output)
193+
actor = git.Actor(args.author, args.email)
194+
# load stuff
195+
data = tomllib.load(config.open("rb"))
196+
197+
# put stuff in the repo
198+
for asset in data["assets"]:
199+
fn: str = asset["md5"] + "." + asset["ext"]
200+
fp = assets_in / fn
201+
(assets_out / f"{asset["chosen_name"]}.{asset["ext"]}").write_bytes(fp.read_bytes())
202+
203+
repo.index.add(["assets"])
204+
repo.index.commit("chore: add assets")
205+
print(repo)

0 commit comments

Comments
 (0)