ソースを参照

make it work with bazel paths

Getty Ritter 2 年 前
コミット
17c2ca89b3
2 ファイル変更39 行追加7 行削除
  1. 13 0
      BUILD.bazel
  2. 26 7
      main.py

+ 13 - 0
BUILD.bazel

@@ -11,3 +11,16 @@ py_binary(
         requirement("PyYAML"),
     ]
 )
+
+filegroup(
+    name = "data",
+    srcs=glob(["quips/*", "quotes/*", "works/**/*", "works.json"]),
+)
+
+genrule(
+    name = "out",
+    srcs = [":data"] + glob(["templates/*.mustache", "static/*"], exclude=["*~"]),
+    tools = [":main"],
+    outs = ["output"],
+    cmd = "$(location :main) $(location output) $(locations :data)",
+)

+ 26 - 7
main.py

@@ -6,6 +6,8 @@ import os
 import markdown
 import pystache
 import shutil
+import sys
+import tempfile
 import yaml
 
 class Datum:
@@ -39,16 +41,19 @@ class Work:
     contents: str
 
 class Path:
-    DATADIR=os.getenv('DATADIR', '/home/gdritter/projects/lib-data')
-    OUTDIR=os.getenv('OUTDIR', '/tmp/lib')
+    OUTDIR=tempfile.TemporaryDirectory()
 
     @classmethod
     def data(cls, *paths):
-        return os.path.join(cls.DATADIR, *paths)
+        tgt = os.path.join(*paths)
+        for path in sys.argv[2:]:
+            if path.endswith(os.path.join(tgt)):
+                return path
+        raise Exception(f"Could not find {tgt}")
 
     @classmethod
     def out(cls, *paths):
-        return os.path.join(cls.OUTDIR, *paths)
+        return os.path.join(cls.OUTDIR.name, *paths)
 
     @classmethod
     def write(cls, *paths):
@@ -63,7 +68,15 @@ class Path:
 
     @classmethod
     def list(cls, *paths):
-        return os.listdir(cls.data(*paths))
+        stuff = []
+        tgt = f'{os.path.join(*paths)}/'
+        for path in sys.argv[2:]:
+            if tgt in path:
+                print(f'{tgt} in {path}')
+                chunks = path.split('/')
+                idx = chunks.index(paths[-1])
+                stuff.append(chunks[idx +1])
+        return stuff
 
 class Template:
     renderer = pystache.Renderer(search_dirs="templates")
@@ -79,6 +92,8 @@ class Template:
 
 
 def main():
+    out_file = sys.argv[1]
+
     year = datetime.datetime.now().year
     std_copy = f'© Getty Ritter {year}'
     no_copy = 'all rights reversed'
@@ -143,10 +158,10 @@ def main():
         }))
 
     # create each category page
-    for slug in os.listdir(Path.data('works')):
+    for slug in Path.list('works'):
         # we need to know what works exist in the category
         works = []
-        for work in os.listdir(Path.data('works', slug)):
+        for work in Path.list('works', slug):
             # grab the metadata for this work
             with open(Path.data('works', slug, work, 'metadata.yaml')) as f:
                 meta = yaml.safe_load(f)
@@ -198,6 +213,10 @@ def main():
     shutil.copy('static/main.css', Path.out('static', 'main.css'))
     shutil.copy('static/icon.png', Path.out('static', 'icon.png'))
 
+    print(f'writing to {out_file}')
+    shutil.make_archive('output', 'zip', Path.OUTDIR.name)
+    shutil.move('output.zip', out_file)
+    Path.OUTDIR.cleanup()
 
 if __name__ == '__main__':
     main()