[game-data-packager] 09/17: gdp.unpack.auto: add makeself content-sniffing, from make-template

Simon McVittie smcv at debian.org
Wed Dec 30 22:57:21 UTC 2015


This is an automated email from the git hooks/post-receive script.

smcv pushed a commit to branch master
in repository game-data-packager.

commit 461af9bb68b4535177a69c595369121a6b2185c3
Author: Simon McVittie <smcv at debian.org>
Date:   Wed Dec 30 22:28:26 2015 +0000

    gdp.unpack.auto: add makeself content-sniffing, from make-template
---
 game_data_packager/unpack/auto.py | 48 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/game_data_packager/unpack/auto.py b/game_data_packager/unpack/auto.py
index c0199db..373c9cf 100644
--- a/game_data_packager/unpack/auto.py
+++ b/game_data_packager/unpack/auto.py
@@ -2,6 +2,7 @@
 # encoding=utf-8
 #
 # Copyright © 2015 Simon McVittie <smcv at debian.org>
+#           © 2015 Alexandre Detiste <alexandre at detiste.be>
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -20,6 +21,46 @@ import zipfile
 
 from . import (TarUnpacker, ZipUnpacker)
 
+def get_makeself_offset(archive, reader=None):
+    """Sniff Makeself archives, returning the offset to the embedded
+    tar file, or 0 if not a makeself archive.
+
+    http://megastep.org/makeself/
+    """
+    skip = 0
+    line_number = 0
+    has_makeself = False
+    trailer = None
+
+    SHEBANG = bytes('/bin/sh', 'ascii')
+    HEADER_V1 = bytes('# This script was generated using Makeself 1.', 'ascii')
+    HEADER_V2 = bytes('# This script was generated using Makeself 2.', 'ascii')
+    TRAILER_V1 = bytes('END_OF_STUB', 'ascii')
+    TRAILER_V2 = bytes('eval $finish; exit $res', 'ascii')
+
+    if reader is None:
+        reader = open(archive, 'rb')
+
+    for line in reader:
+        line_number += 1
+        skip += len(line)
+
+        if line_number == 1 and SHEBANG not in line:
+            return 0
+        elif has_makeself:
+            if trailer in line:
+                return skip
+        elif HEADER_V1 in line:
+            has_makeself = True
+            trailer = TRAILER_V1
+        elif HEADER_V2 in line:
+            has_makeself = True
+            trailer = TRAILER_V2
+        elif line_number > 3:
+            return 0
+
+    return 0
+
 def automatic_unpacker(archive, reader=None):
     if reader is None:
         is_plain_file = True
@@ -28,6 +69,13 @@ def automatic_unpacker(archive, reader=None):
         is_plain_file = False
 
     if reader.seekable():
+        skip = get_makeself_offset(archive, reader)
+
+        if skip > 0:
+            reader.seek(0)
+            return TarUnpacker(archive, reader=reader, skip=skip,
+                    compression='gz')
+
         if zipfile.is_zipfile(reader):
             return ZipUnpacker(reader)
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/game-data-packager.git



More information about the Pkg-games-commits mailing list