Skip to content

Commit

Permalink
scripts: waifulib: zip: add support for passing compression level on …
Browse files Browse the repository at this point in the history
…Python 3.7. Exclude PNG files from compression
  • Loading branch information
a1batross committed Jan 23, 2025
1 parent c86c455 commit 3e22ee6
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions scripts/waifulib/zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# encoding: utf-8

from waflib import TaskGen, Task, Logs, Utils
import zipfile
import zipfile, sys

class ziparchive(Task.Task):
color = 'YELLOW'
Expand All @@ -17,15 +17,25 @@ def keyword(self):

def run(self):
outfile = self.outputs[0].abspath()
comp = zipfile.ZIP_STORED if self.compresslevel == 0 else zipfile.ZIP_DEFLATED
kwargs = {}
kwargs['mode'] = 'w'
kwargs['compression'] = zipfile.ZIP_STORED if self.compresslevel == 0 else zipfile.ZIP_DEFLATED

with zipfile.ZipFile(outfile, mode='w', compression=comp) as zf:
if sys.hexversion >= 0x3070000:
kwargs['compresslevel'] = self.compresslevel

with zipfile.ZipFile(outfile, **kwargs) as zf:
for src in self.inputs:
infile = src.path_from(src.ctx.launch_node())
arcfile = src.path_from(self.relative_to)

# TODO: pass excluded list from TaskGen
Logs.debug('%s: %s <- %s as %s', self.__class__.__name__, outfile, infile, arcfile)
zf.write(infile, arcfile)

if infile.endswith('.png'):
zf.write(infile, arcfile, zipfile.ZIP_STORED)
else:
zf.write(infile, arcfile)

@TaskGen.feature('zip')
def create_zip_archive(self):
Expand Down

0 comments on commit 3e22ee6

Please sign in to comment.