11import argparse
22import shutil
33import tomlkit
4+ import tomllib
45import dataclasses
56import zipfile
67import json
78import typing as t
9+ import git
810import scratchattach .editor # this is slow but easier
911from pathlib import Path
1012from 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
3034def 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
6778def 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