[game-data-packager] 43/51: make_template: Consolidate file/group collection

Simon McVittie smcv at debian.org
Fri Dec 29 01:23:38 UTC 2017


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 c007f5ce5bb06c46089fd97e6006c93a94719e89
Author: Simon McVittie <smcv at debian.org>
Date:   Thu Dec 28 21:35:58 2017 +0000

    make_template: Consolidate file/group collection
    
    Signed-off-by: Simon McVittie <smcv at debian.org>
---
 game_data_packager/make_template.py | 242 ++++++++++++++++++------------------
 1 file changed, 119 insertions(+), 123 deletions(-)

diff --git a/game_data_packager/make_template.py b/game_data_packager/make_template.py
index 42f790e..f89c957 100644
--- a/game_data_packager/make_template.py
+++ b/game_data_packager/make_template.py
@@ -70,6 +70,13 @@ def guess_lang(string):
             if lang in arg:
                 return 'en'
 
+def is_probably_optional(file):
+    file = file.split('?')[0]
+    name, ext = os.path.splitext(file.lower())
+    if ext in ('cfg', 'cmd', 'com', 'drv', 'ico', 'ini'):
+        return True
+    return False
+
 def is_license(file):
     file = file.split('?')[0]
     name, ext = os.path.splitext(file.lower())
@@ -144,6 +151,7 @@ class Template:
         self.plugin = None
         self.strip_paths = strip_paths
         self.has_dosbox = False
+        self.preexisting_files = set(self.game.files.keys())
 
     def new_group(self, stem):
         i = 0
@@ -168,9 +176,9 @@ class Template:
         if lower:
             out_name = out_name.lower()
 
-        return self.add_file(path, out_name=out_name,
-                group=self.game.ensure_group('archives'),
-                unpack=unpack)
+        result = self.add_file(path, out_name=out_name, unpack=unpack)
+        self.game.ensure_group('archives').group_members.add(result.name)
+        return result
 
     def add_unpacker(self, path, result, unpacker):
         if result.unpack is None:
@@ -179,36 +187,84 @@ class Template:
         if isinstance(unpacker, TarUnpacker):
             result.unpack['skip'] = unpacker.skip
 
-        main_group = self.new_group('contents of %s' % result.name)
-        opt_group = self.new_group('contents of %s - optional' % result.name)
-        doc_group = self.new_group('contents of %s - documentation' % result.name)
-        doc_group.doc = True
-        license_group = self.new_group('contents of %s - licenses' % result.name)
-        license_group.license = True
+        contents = []
 
         for entry in unpacker:
             if entry.is_regular_file and entry.is_extractable:
                 try:
-                    self.add_file(entry.name, opened=unpacker.open(entry),
-                        size=entry.size, parent_unpacker=unpacker,
-                        main_group=main_group,
-                        opt_group=opt_group,
-                        doc_group=doc_group,
-                        license_group=license_group)
+                    provided = self.add_file(
+                        entry.name,
+                        opened=unpacker.open(entry),
+                        size=entry.size,
+                        parent_unpacker=unpacker)
                 except NotImplementedError as e:
                     logger.warning("Can't decompress %s from %s: %s" %
                                    (entry.name, result.name, e))
+                else:
+                    contents.append(provided)
 
-        result.provides = set()
+        self.reconcile_groups(contents,
+            stem=result.name, provider=result)
+
+    def reconcile_groups(self, files,
+            stem=None, provider=None, package=None):
+        remaining = set()
+        groups = {}
+        optional = set()
 
-        for g in (main_group, opt_group, doc_group, license_group):
-            if g.group_members:
-                result.provides.add(g.name)
+        for f in files:
+            remaining.add(f.name)
 
-    def add_file(self, path, out_name=None, group=None, opened=None, size=None,
-            lang=None, parent_unpacker=None,
-            opt_group=None, doc_group=None, license_group=None,
-            unpack=False):
+        if remaining:
+            main_group = self.new_group('contents of %s' % stem)
+            opt_group = self.new_group('contents of %s - optional' % stem)
+            optional.add(opt_group.name)
+            doc_group = self.new_group('contents of %s - documentation' % stem)
+            doc_group.doc = True
+            license_group = self.new_group('contents of %s - licenses' % stem)
+            license_group.license = True
+            # only from .deb
+            abs_group = self.new_group('contents of %s - absolute paths' % stem)
+            abs_group.install_to = '.'
+            optional.add(abs_group.name)
+
+            for g in (
+                    main_group, opt_group, doc_group, license_group, abs_group):
+                groups[g.name] = g
+
+            for n in remaining:
+                f = self.game.files[n]
+
+                if f.install_to == '.':
+                    abs_group.group_members.add(n)
+                elif f.license and f.doc:
+                    license_group.group_members.add(n)
+                    doc_group.group_members.add(n)
+                elif f.license:
+                    license_group.group_members.add(n)
+                elif f.doc:
+                    doc_group.group_members.add(n)
+                elif f.ignorable or is_probably_optional(f.name):
+                    opt_group.group_members.add(n)
+                else:
+                    main_group.group_members.add(n)
+
+        if provider:
+            provider.provides = set()
+
+            for name, g in groups.items():
+                if g.group_members:
+                    provider.provides.add(name)
+
+        if package:
+            for name, g in groups.items():
+                if g.license or g.doc or g.ignorable or name in optional:
+                    package.optional.add(name)
+                else:
+                    package.install.add(name)
+
+    def add_file(self, path, out_name=None, opened=None, size=None,
+            lang=None, parent_unpacker=None, unpack=False):
 
         if out_name is None:
             out_name = path
@@ -326,18 +382,6 @@ class Template:
 
         ext = path.lower().split('.')[-1]
 
-        if group is None:
-            group = self.game.ensure_group('miscellaneous')
-
-        if opt_group is None:
-            opt_group = group
-
-        if license_group is None:
-            license_group = group
-
-        if doc_group is None:
-            doc_group = group
-
         if result is not existing and ignorable:
             result.ignorable = True
 
@@ -347,23 +391,6 @@ class Template:
         if result is not existing and is_doc(path):
             result.doc = True
 
-        if result.license and result.doc:
-            license_group.group_members.add(result.name)
-            doc_group.group_members.add(result.name)
-        elif result.license:
-            license_group.group_members.add(result.name)
-        elif result.doc:
-            doc_group.group_members.add(result.name)
-        elif (
-                # most of the times these files are not needed
-                ext in ('cfg', 'cmd', 'com', 'drv', 'ico', 'ini') or
-                result.ignorable):
-            opt_group.group_members.add(result.name)
-        elif unpacker is not None or ext in ('umod', 'zip'):
-            self.game.ensure_group('archives').group_members.add(result.name)
-        else:
-            group.group_members.add(result.name)
-
         if unpacker is not None:
             with unpacker:
                 self.add_unpacker(path, result, unpacker)
@@ -371,23 +398,11 @@ class Template:
         return result
 
     def add_one_dir(self, destdir, lower=False, game=None, lang=None,
-            main_group=None, opt_group=None, doc_group=None,
-            license_group=None):
+            group_stem=None):
         basename = os.path.basename(os.path.abspath(destdir))
 
-        if main_group is None:
-            main_group = self.new_group('contents of %s' % basename)
-
-        if opt_group is None:
-            opt_group = self.new_group('contents of %s - optional' % basename)
-
-        if doc_group is None:
-            doc_group = self.new_group('contents of %s - documentation' % basename)
-            doc_group.doc = True
-
-        if license_group is None:
-            license_group = self.new_group('contents of %s - licenses' % basename)
-            license_group.license = True
+        if group_stem is None:
+            group_stem = basename
 
         if destdir.startswith('/usr/local') or destdir.startswith('/opt/'):
             self.game.try_repack_from.append(destdir)
@@ -439,6 +454,8 @@ class Template:
         if steam > 0:
             package.steam = steam_dict
 
+        contents = []
+
         for dirpath, dirnames, filenames in os.walk(destdir):
             if self.is_scummvm(dirpath) or is_runtime(dirpath):
                 continue
@@ -457,11 +474,8 @@ class Template:
                 elif os.path.islink(path):
                     package.symlinks[path] = os.path.realpath(path)
                 elif os.path.isfile(path):
-                    self.add_file(path, out_name=out_name, lang=lang,
-                            group=main_group,
-                            doc_group=doc_group,
-                            license_group=license_group,
-                            opt_group=opt_group)
+                    contents.append(
+                        self.add_file(path, out_name=out_name, lang=lang))
                 else:
                     logger.warning('ignoring unknown file type at %s' % path)
 
@@ -471,12 +485,7 @@ class Template:
         if self.plugin != 'scummvm_common':
             package.install_to = '$assets/' + game
 
-        if main_group.group_members:
-            package.install.add(main_group.name)
-
-        for g in (opt_group, doc_group, license_group):
-            if g.group_members:
-                package.optional.add(g.name)
+        self.reconcile_groups(contents, group_stem, package)
 
     def add_one_gog_sh(self,archive):
         self.add_archive(archive, unpack=True)
@@ -519,26 +528,13 @@ class Template:
                  cwd=tmp)
         self.game.longname = log.split('\n')[0].split('"')[1]
 
-        main_group = self.new_group('contents of %s' % result.name)
-        opt_group = self.new_group('contents of %s - optional' % result.name)
-        doc_group = self.new_group('contents of %s - documentation' % result.name)
-        doc_group.doc = True
-        license_group = self.new_group('contents of %s - licenses' % result.name)
-        license_group.license = True
-
         self.add_one_dir(os.path.join(tmp, 'app'), game=game,
-                lang=guess_lang(exe), lower=lower,
-                main_group=main_group, opt_group=opt_group, doc_group=doc_group,
-                license_group=license_group)
+                lang=guess_lang(exe), lower=lower, group_stem=result.name)
         rm_rf(tmp)
 
         result.unpack = dict(format='innoextract')
         result.provides = set()
 
-        for g in (main_group, opt_group, doc_group, license_group):
-            if g.group_members:
-                result.provides.add(g.name)
-
     def add_one_deb(self,deb,lower):
         if not ON_DEBIAN or not which('dpkg-deb'):
             exit('.deb analysis is only implemented on Debian.')
@@ -546,14 +542,6 @@ class Template:
         control = None
 
         result = self.add_archive(deb, unpack=False)
-        main_group = self.new_group('contents of %s' % result.name)
-        ignorable_group = self.new_group('contents of %s - ignorable' % result.name)
-        abs_group = self.new_group('contents of %s - unusual install path' % result.name)
-        abs_group.install_to = '.'
-        doc_group = self.new_group('contents of %s - documentation' % result.name)
-        doc_group.doc = True
-        license_group = self.new_group('contents of %s - licenses' % result.name)
-        license_group.license = True
 
         version = None
         with subprocess.Popen(['dpkg-deb', '--ctrl-tarfile', deb],
@@ -589,12 +577,6 @@ class Template:
             logger.error('Could not find DEBIAN/control')
 
         result.unpack = dict(format='deb')
-        result.provides = set()
-
-        for g in (main_group, ignorable_group, abs_group, doc_group,
-                license_group):
-            if g.group_members:
-                result.provides.add(g.name)
 
         package_name = control['package']
         i = 0
@@ -610,6 +592,7 @@ class Template:
             package.version = version
 
         install_to = None
+        contents = []
 
         with DpkgDebUnpacker(deb) as unpacker:
             for entry in unpacker:
@@ -649,27 +632,30 @@ class Template:
                 target = entry.get_symbolic_link_target()
 
                 if entry.is_regular_file:
-                    group = main_group
+                    abs_path = False
+                    doc = False
+                    ignorable = False
+                    license = False
 
                     if name.startswith('usr/share/doc/'):
                         name = name[len('usr/share/doc/'):]
                         name = name.split('/', 1)[1]
-                        group = doc_group
+                        doc = True
 
                     name_l = name.lower()
                     basename_l = os.path.basename(name_l)
 
                     if os.path.splitext(name_l)[1] in ('.exe', '.bat'):
-                        group = ignorable_group
+                        ignorable = True
                     elif 'support/gog' in name_l or os.path.basename(name_l) in (
                                                     'start.sh', 'uninstall.sh'):
-                        group = ignorable_group
+                        ignorable = True
                     elif name.startswith('opt/') and is_license(name):
                         name = basename_l
-                        group = license_group
+                        license = True
                     elif name.startswith('opt/') and is_doc(name):
-                        group = doc_group
-                    elif (group is main_group and
+                        doc = True
+                    elif (not doc and
                             install_to is not None and
                             name.startswith(install_to + '/')):
                         name = name[len(install_to) + 1:]
@@ -677,16 +663,30 @@ class Template:
                             name = name.lower()
                         if self.game.gog and name.startswith('data/'):
                             name = name[len('data/'):]
-                    else:
-                        group = abs_group
+                    elif not doc:
+                        abs_path = True
 
-                    result = self.add_file(
+                    provided = self.add_file(
                         deb + '//data.tar.*//' + name,
                         out_name=name,
-                        group=group,
                         opened=unpacker.open(entry),
                         size=entry.size,
                         parent_unpacker=unpacker)
+
+                    if provided.name not in self.preexisting_files:
+                        if doc:
+                            provided.doc = True
+
+                        if license:
+                            provided.license = True
+
+                        if ignorable:
+                            provided.ignorable = True
+
+                        if abs_path:
+                            provided.install_to = '.'
+
+                    contents.append(provided)
                 elif entry.is_directory:
                     pass
                 elif target is not None:
@@ -700,12 +700,8 @@ class Template:
             package.install_to = os.path.join('/',
                 install_to).replace('/usr/share/games/', '$assets/')
 
-        if main_group.group_members:
-            package.install.add(main_group.name)
-
-        for g in (abs_group, doc_group, ignorable_group, license_group):
-            if g.group_members:
-                package.optional.add(g.name)
+        self.reconcile_groups(contents, stem=result.name,
+            package=package, provider=result)
 
     def print_yaml(self):
         print('---')

-- 
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