Bladeren bron

fixed some cache bug

Tobias Simetsreiter 5 jaren geleden
bovenliggende
commit
aef9aa0d62
1 gewijzigde bestanden met toevoegingen van 29 en 18 verwijderingen
  1. 29 18
      ttsselfhoster.py

+ 29 - 18
ttsselfhoster.py

@@ -16,24 +16,25 @@ def main():
         if type(val)==str and url_regex.match(val):
             print(it, file=sys.stderr)
             if args.server_dir:
-                sha_url, sha_f = cache_file(val, args.server_dir, force=args.no_cache)
+                sha_f = cache_file(val, args.server_dir, force=args.no_cache)
                 print(sha_f, file=sys.stderr)
             if args.url and sha_f:
                 newurl = args.url + "/blocks/" + sha_f
                 dict_set(js, it[:-1], newurl)
-    if args.output_file == "-":
+    if args.output_file == None and args.server_dir != None:
+        args.output_file = os.path.join(args.server_dir , "output.json")
+    elif args.output_file == "-":
+        args.output_file = "/dev/stdout"
+    with open(args.output_file, "w") as fd:
         json.dump(js, sys.stdout)
-    else:
-        with open(args.output_file, "w") as fd:
-            json.dump(js, sys.stdout)
 
 
 def parser():
     import argparse
     p = argparse.ArgumentParser()
     p.add_argument("-f","--input_file", required=True)
-    p.add_argument("-o","--output_file", default="-")
-    p.add_argument("-s","--server_dir", default=None)
+    p.add_argument("-o","--output_file", default=None)
+    p.add_argument("-s","--server_dir", default="repo")
     p.add_argument("-n","--no_cache", action="store_true")
     p.add_argument("-u","--url", default=None)
     return p
@@ -46,36 +47,44 @@ def dict_set(dic , path, val):
     last_dic[p] = val
 
 
-def cache_file(url, cache_dir, force=True):
+def cache_file(url, cache_dir, force=False):
     import os
     import hashlib
-    blocks_dir = os.path.join(cache_dir,"blocks")
     sha_url = hashlib.sha512()
     sha_url.update(url.encode())
     sha_url = sha_url.hexdigest()
     sha_dir = os.path.join(cache_dir, "sha")
     sha_file = os.path.join(sha_dir, sha_url)
 
-    if not os.path.isdir(blocks_dir):
-        os.makedirs(blocks_dir)
     if not os.path.isdir(sha_dir):
         os.makedirs(sha_dir)
-    cachefile = os.path.join(blocks_dir, sha_url)
-    if force or (not os.path.isfile(cachefile)):
-        sha_req = get_file(url, cachefile, sha_file)
+    sha_f = None
+    if force or (not os.path.isfile(sha_file)):
+        sha_f = get_file(url, cache_dir, sha_file)
+    else:
+        try:
+            sha_f = sha_file.open(sha_file).read()
+        except:
+            sha_f = None
 
-    return (sha_url, sha_req)
+    return sha_f
 
-def get_file(url, cachefile, sha_file):
+def get_file(url, cache_dir, sha_file):
     from urllib.request import urlopen
     from urllib.error import HTTPError
     import hashlib
     import sys
     import os
+    import uuid
+    blocks_dir = os.path.join(cache_dir,"blocks")
+    if not os.path.isdir(blocks_dir):
+        os.makedirs(blocks_dir)
+
+    tmpfile = os.path.join(blocks_dir, uuid.uuid4().hex)
     sha_f = hashlib.sha512()
     try:
         with urlopen(url) as req:
-            with open(cachefile,"wb") as fd:
+            with open(tmpfile,"wb") as fd:
                 BUF = True
                 while BUF:
                     BUF = req.read()
@@ -84,12 +93,14 @@ def get_file(url, cachefile, sha_file):
     except HTTPError as ex:
         print(ex, file=sys.stderr)
         try:
-            os.unlink(cachefile)
+            os.unlink(tmpfile)
             os.unlink(sha_file)
         except:
             pass
         return None
     sha_f = sha_f.hexdigest()
+    cachefile = os.path.join(blocks_dir, sha_f)
+    os.rename(tmpfile, cachefile)
     with open(sha_file, "w") as fd:
         fd.write(sha_f)
     return sha_f