aboutsummaryrefslogtreecommitdiff
path: root/scripts/zstd.py
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2020-03-28 20:01:52 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2020-03-28 20:12:45 +0200
commit6fd3190c8dfad44e1a4460ea70edc0dc2dccfe42 (patch)
tree0c72aa919e6678d309dd4bedf347ab17abe2a0df /scripts/zstd.py
parentAdd namespace to format (diff)
downloadrcc-6fd3190c8dfad44e1a4460ea70edc0dc2dccfe42.tar.xz
Change rcc command line
rcc: - add - and Zstd modes - fix Zstd dictionary train
Diffstat (limited to 'scripts/zstd.py')
-rw-r--r--scripts/zstd.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/scripts/zstd.py b/scripts/zstd.py
new file mode 100644
index 0000000..f85e7da
--- /dev/null
+++ b/scripts/zstd.py
@@ -0,0 +1,41 @@
+import subprocess
+from rcc_format import *
+
+def zstd(filelist, args):
+ if args.train is not None:
+ train(filelist, args.train, args.binary, args.dsize)
+ return
+
+ write_header(args.output, args.namespace)
+
+ for f in filelist:
+ with open(f.path, 'rb') as contents:
+ write_item(args.output, f.variable, compress(contents, args.binary, args.level, dictionary=args.dict))
+
+ write_entries(args.output, filelist)
+ if args.dict is not None:
+ write_item(args.output, 'dict', args.dict.read())
+ print("constexpr auto dictionary = std::span(dict, dict_len);", file=args.output)
+
+ print("constexpr auto compression = embed::Zstd;", file=args.output)
+
+ write_footer(args.output, args.namespace)
+
+def train(filelist, output, zstd_bin, maxdict):
+ cmd = [ zstd_bin, '--train', '--maxdict=' + str(maxdict), '-o', output.name ]
+
+ for f in filelist:
+ cmd.append(f.path)
+
+ subprocess.run(cmd)
+
+def compress(file, zstd_bin, level, dictionary=None):
+ cmd = [ zstd_bin, '--compress', '--stdout', '-' + str(level) ]
+
+ if dictionary is not None:
+ cmd.append('-D')
+ cmd.append(dictionary.name)
+
+ cmd.append(file.name)
+ return subprocess.run(cmd, capture_output=True).stdout
+