|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +''' |
| 4 | +Copyright 2022 Veloman Yunkan <[email protected]> |
| 5 | +
|
| 6 | +This program is free software; you can redistribute it and/or modify |
| 7 | +it under the terms of the GNU General Public License as published by |
| 8 | +the Free Software Foundation; either version 3 of the License, or any |
| 9 | +later version. |
| 10 | +
|
| 11 | +This program is distributed in the hope that it will be useful, but |
| 12 | +WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | +General Public License for more details. |
| 15 | +
|
| 16 | +You should have received a copy of the GNU General Public License |
| 17 | +along with this program; if not, write to the Free Software |
| 18 | +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 19 | +02110-1301, USA. |
| 20 | +''' |
| 21 | + |
| 22 | +import argparse |
| 23 | +import hashlib |
| 24 | +import os.path |
| 25 | +import re |
| 26 | + |
| 27 | +def read_resource_file(resource_file_path): |
| 28 | + with open(resource_file_path, 'r') as f: |
| 29 | + return [line.strip() for line in f] |
| 30 | + |
| 31 | +def list_resources(resource_file_path): |
| 32 | + for resource_path in read_resource_file(resource_file_path): |
| 33 | + print(resource_path) |
| 34 | + |
| 35 | +def get_resource_revision(base_dir, resource_path): |
| 36 | + with open(os.path.join(base_dir, resource_path), 'rb') as f: |
| 37 | + return hashlib.sha1(f.read()).hexdigest()[:8] |
| 38 | + |
| 39 | +resource_revisions = {} |
| 40 | + |
| 41 | +def fill_resource_revisions(resource_file_path): |
| 42 | + base_dir = os.path.dirname(os.path.realpath(resource_file_path)) |
| 43 | + for resource in read_resource_file(resource_file_path): |
| 44 | + resource_revisions[resource] = get_resource_revision(base_dir, resource) |
| 45 | + |
| 46 | +src_or_href_pattern=r'(src|href)="\{\{root\}\}/(skin/[^"]+)"' |
| 47 | +def add_resource_revision(resource_matchobj): |
| 48 | + attr = resource_matchobj.group(1) |
| 49 | + resource = resource_matchobj.group(2) |
| 50 | + if resource in resource_revisions: |
| 51 | + uniqueid = '?' + resource_revisions[resource] |
| 52 | + else: |
| 53 | + uniqueid = '' |
| 54 | + return attr + r'="{{root}}/' + resource + uniqueid + '"' |
| 55 | + |
| 56 | +def preprocess_template(srcpath, dstpath): |
| 57 | + with open(srcpath, 'r') as source: |
| 58 | + with open(dstpath, 'w') as target: |
| 59 | + for line in source: |
| 60 | + line = re.sub(src_or_href_pattern, add_resource_revision, line) |
| 61 | + print(line, end='', file=target) |
| 62 | + |
| 63 | +def symlink_resource(src, resource_path): |
| 64 | + if os.path.exists(resource_path): |
| 65 | + if os.path.islink(resource_path) and os.readlink(resource_path) == src: |
| 66 | + return |
| 67 | + os.remove(resource_path) |
| 68 | + os.symlink(src, resource_path) |
| 69 | + |
| 70 | +def preprocess_resource(srcdir, resource_path, outdir): |
| 71 | + resource_dir = os.path.dirname(resource_path) |
| 72 | + if resource_dir != '': |
| 73 | + os.makedirs(os.path.join(outdir, resource_dir), exist_ok=True) |
| 74 | + srcpath = os.path.join(srcdir, resource_path) |
| 75 | + outpath = os.path.join(outdir, resource_path) |
| 76 | + if resource_path.startswith('templates/'): |
| 77 | + preprocess_template(srcpath, outpath) |
| 78 | + else: |
| 79 | + symlink_resource(srcpath, outpath) |
| 80 | + |
| 81 | +def copy_file(src_path, dst_path): |
| 82 | + with open(src_path, 'rb') as src: |
| 83 | + with open(dst_path, 'wb') as dst: |
| 84 | + dst.write(src.read()) |
| 85 | + |
| 86 | +def preprocess_resources(resource_file_path, outdir): |
| 87 | + base_dir = os.path.dirname(os.path.realpath(resource_file_path)) |
| 88 | + resource_filename = os.path.basename(resource_file_path) |
| 89 | + for resource in read_resource_file(resource_file_path): |
| 90 | + preprocess_resource(base_dir, resource, outdir) |
| 91 | + copy_file(resource_file_path, os.path.join(outdir, resource_filename)) |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + parser = argparse.ArgumentParser() |
| 95 | + commands = parser.add_mutually_exclusive_group() |
| 96 | + commands.add_argument('--list-all', action='store_true') |
| 97 | + commands.add_argument('--preprocess', action='store_true') |
| 98 | + parser.add_argument('--outdir') |
| 99 | + parser.add_argument('resource_file') |
| 100 | + args = parser.parse_args() |
| 101 | + |
| 102 | + if args.list_all: |
| 103 | + list_resources(args.resource_file) |
| 104 | + elif args.preprocess: |
| 105 | + fill_resource_revisions(args.resource_file) |
| 106 | + preprocess_resources(args.resource_file, args.outdir) |
0 commit comments