OLD | NEW |
(Empty) | |
| 1 from os import path, listdir |
| 2 from hashlib import sha256, md5 |
| 3 from base64 import urlsafe_b64encode |
| 4 import re |
| 5 |
| 6 JS_DIR = path.normpath(path.join(__file__, "..", "..")) |
| 7 |
| 8 |
| 9 def js_files(): |
| 10 ''' |
| 11 Yield each file in the javascript directory |
| 12 ''' |
| 13 for f in listdir(JS_DIR): |
| 14 if path.isfile(f) and f.endswith(".js"): |
| 15 yield f |
| 16 |
| 17 |
| 18 def format_digest(digest): |
| 19 ''' |
| 20 URL-safe base64 encode a binary digest and strip any padding. |
| 21 ''' |
| 22 return urlsafe_b64encode(digest).rstrip("=") |
| 23 |
| 24 |
| 25 def sha256_uri(content): |
| 26 ''' |
| 27 Generate an encoded sha256 URI. |
| 28 ''' |
| 29 return "ni:///sha-256;%s" % format_digest(sha256(content).digest()) |
| 30 |
| 31 |
| 32 def md5_uri(content): |
| 33 ''' |
| 34 Generate an encoded md5 digest URI. |
| 35 ''' |
| 36 return "ni:///md5;%s" % format_digest(md5(content).digest()) |
| 37 |
| 38 |
| 39 def main(): |
| 40 for file in js_files(): |
| 41 base = path.splitext(path.basename(file))[0] |
| 42 var_name = re.sub(r"[^a-z]", "_", base) |
| 43 content = "%s=true;" % var_name |
| 44 with open(file, "w") as f: |
| 45 f.write(content) |
| 46 |
| 47 |
| 48 if __name__ == "__main__": |
| 49 main() |
OLD | NEW |