[game-data-packager] 01/01: fedora: implement PackageCache & install_packages, split util.py

Alexandre Detiste detiste-guest at moszumanska.debian.org
Mon Nov 2 00:47:17 UTC 2015


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

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

commit e6c0656a1ad23a99ca67935927d73d1bbdb1c621
Author: Alexandre Detiste <alexandre.detiste at gmail.com>
Date:   Mon Nov 2 01:46:10 2015 +0100

    fedora: implement PackageCache & install_packages, split util.py
---
 debian/rules                   |   1 +
 game_data_packager/util.py     | 110 ++++---------------------------------
 game_data_packager/util_deb.py | 121 +++++++++++++++++++++++++++++++++++++++++
 game_data_packager/util_rpm.py |  64 ++++++++++++++++++++++
 4 files changed, 198 insertions(+), 98 deletions(-)

diff --git a/debian/rules b/debian/rules
index 2f6485b..5fdb907 100755
--- a/debian/rules
+++ b/debian/rules
@@ -39,6 +39,7 @@ override_dh_install:
 		touch debian/game-data-packager/usr/share/games/game-data-packager/is-ubuntu-derived; \
 	fi
 	install -D -m755 runtime/doom2-masterlevels.py debian/game-data-packager/usr/games/doom2-masterlevels
+	rm -f debian/game-data-packager/usr/share/games/game-data-packager/game_data_packager/util_rpm.py
 
 override_dh_installdocs:
 	dh_installdocs -XTODO
diff --git a/game_data_packager/util.py b/game_data_packager/util.py
index 3b2e33b..a311c73 100644
--- a/game_data_packager/util.py
+++ b/game_data_packager/util.py
@@ -25,10 +25,8 @@ import stat
 import subprocess
 import sys
 
-from debian.debian_support import Version
-
 from .paths import DATADIR
-from .version import GAME_PACKAGE_VERSION
+from .version import (GAME_PACKAGE_VERSION, FORMAT)
 
 logger = logging.getLogger('game-data-packager.util')
 
@@ -93,72 +91,6 @@ def copy_with_substitutions(from_, to, **kwargs):
             line = line.replace(k, v)
         to.write(line)
 
-class PackageCache:
-    installed = None
-    available = None
-
-    def is_installed(self, package):
-        # FIXME: this shouldn't be hard-coded
-        if package == 'doom-engine':
-            return (self.is_installed('chocolate-doom')
-                 or self.is_installed('prboom-plus')
-                 or self.is_installed('doomsday'))
-        if package == 'boom-engine':
-            return (self.is_installed('prboom-plus')
-                 or self.is_installed('doomsday'))
-        if package == 'heretic-engine':
-            return (self.is_installed('chocolate-heretic')
-                 or self.is_installed('doomsday'))
-        if package == 'hexen-engine':
-            return (self.is_installed('chocolate-hexen')
-                 or self.is_installed('doomsday'))
-
-        if os.path.isdir(os.path.join('/usr/share/doc', package)):
-            return True
-
-        if self.installed is None:
-            cache = set()
-            proc = subprocess.Popen(['dpkg-query', '--show',
-                        '--showformat', '${Package}\\n'],
-                    universal_newlines=True,
-                    stdout=subprocess.PIPE)
-            for line in proc.stdout:
-                cache.add(line.rstrip())
-            self.installed = cache
-
-        return package in self.installed
-
-    def is_available(self, package):
-        if self.available is None:
-            cache = set()
-            proc = subprocess.Popen(['apt-cache', 'pkgnames'],
-                    universal_newlines=True,
-                    stdout=subprocess.PIPE)
-            for line in proc.stdout:
-                cache.add(line.rstrip())
-            self.available = cache
-
-        return package in self.available
-
-    def current_version(self, package):
-        # 'dpkg-query: no packages found matching $package'
-        # will leak on stderr if called with an unknown package,
-        # but that should never happen
-        try:
-            return check_output(['dpkg-query', '--show',
-              '--showformat', '${Version}', package], universal_newlines=True)
-        except subprocess.CalledProcessError:
-            return
-
-    def available_version(self, package):
-        current_ver = check_output(['apt-cache', 'madison', package],
-                                    universal_newlines=True)
-        current_ver = current_ver.splitlines()[0]
-        current_ver = current_ver.split('|')[1].strip()
-        return current_ver
-
-PACKAGE_CACHE = PackageCache()
-
 def prefered_langs():
     if prefered_langs.langs is not None:
         return prefered_langs.langs
@@ -257,35 +189,6 @@ def run_as_root(argv, gain_root=''):
                 gain_root)
         subprocess.call([gain_root] + list(argv))
 
-def install_packages(debs, method, gain_root='su'):
-    """Install one or more packages (a list of filenames)."""
-
-    if method and method not in (
-            'apt', 'dpkg',
-            'gdebi', 'gdebi-gtk', 'gdebi-kde',
-            ):
-        logger.warning(('Unknown installation method %r, using apt or dpkg ' +
-            'instead') % method)
-        method = None
-
-    if not method:
-        apt_ver = PACKAGE_CACHE.current_version('apt')
-        if Version(apt_ver.strip()) >= Version('1.1~0'):
-            method = 'apt'
-        else:
-            method = 'dpkg'
-
-    if method == 'apt':
-        run_as_root(['apt-get', 'install', '--install-recommends'] + list(debs),
-                gain_root)
-    elif method == 'dpkg':
-        run_as_root(['dpkg', '-i'] + list(debs), gain_root)
-    elif method == 'gdebi':
-        run_as_root(['gdebi'] + list(debs), gain_root)
-    else:
-        # gdebi-gtk etc.
-        subprocess.call([method] + list(debs))
-
 def check_call(command, *args, **kwargs):
     """Like subprocess.check_call, but log what we will do first."""
     logger.debug('%r', command)
@@ -309,3 +212,14 @@ def recursive_utime(directory, orig_time):
         for fn in filenames:
             full = os.path.join(dirpath, fn)
             os.utime(full, orig_time)
+
+
+# loaded at the end to avoid failed cyclic loads
+if FORMAT == 'deb':
+    from .util_deb import (PACKAGE_CACHE, install_packages)
+else:
+    from .util_rpm import (PACKAGE_CACHE, install_packages)
+
+# pyflakes
+PACKAGE_CACHE
+install_packages
diff --git a/game_data_packager/util_deb.py b/game_data_packager/util_deb.py
new file mode 100644
index 0000000..8da06eb
--- /dev/null
+++ b/game_data_packager/util_deb.py
@@ -0,0 +1,121 @@
+#!/usr/bin/python3
+# encoding=utf-8
+#
+# Copyright © 2014-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
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+#
+# You can find the GPL license text on a Debian system under
+# /usr/share/common-licenses/GPL-2.
+
+import logging
+import os
+import subprocess
+
+from .util import (run_as_root, check_output)
+from debian.debian_support import Version
+
+logger = logging.getLogger('game-data-packager.util_deb')
+
+class PackageCache:
+    installed = None
+    available = None
+
+    def is_installed(self, package):
+        # FIXME: this shouldn't be hard-coded
+        if package == 'doom-engine':
+            return (self.is_installed('chocolate-doom')
+                 or self.is_installed('prboom-plus')
+                 or self.is_installed('doomsday'))
+        if package == 'boom-engine':
+            return (self.is_installed('prboom-plus')
+                 or self.is_installed('doomsday'))
+        if package == 'heretic-engine':
+            return (self.is_installed('chocolate-heretic')
+                 or self.is_installed('doomsday'))
+        if package == 'hexen-engine':
+            return (self.is_installed('chocolate-hexen')
+                 or self.is_installed('doomsday'))
+
+        if os.path.isdir(os.path.join('/usr/share/doc', package)):
+            return True
+
+        if self.installed is None:
+            cache = set()
+            proc = subprocess.Popen(['dpkg-query', '--show',
+                        '--showformat', '${Package}\\n'],
+                    universal_newlines=True,
+                    stdout=subprocess.PIPE)
+            for line in proc.stdout:
+                cache.add(line.rstrip())
+            self.installed = cache
+
+        return package in self.installed
+
+    def is_available(self, package):
+        if self.available is None:
+            cache = set()
+            proc = subprocess.Popen(['apt-cache', 'pkgnames'],
+                    universal_newlines=True,
+                    stdout=subprocess.PIPE)
+            for line in proc.stdout:
+                cache.add(line.rstrip())
+            self.available = cache
+
+        return package in self.available
+
+    def current_version(self, package):
+        # 'dpkg-query: no packages found matching $package'
+        # will leak on stderr if called with an unknown package,
+        # but that should never happen
+        try:
+            return check_output(['dpkg-query', '--show',
+              '--showformat', '${Version}', package], universal_newlines=True)
+        except subprocess.CalledProcessError:
+            return
+
+    def available_version(self, package):
+        current_ver = check_output(['apt-cache', 'madison', package],
+                                    universal_newlines=True)
+        current_ver = current_ver.splitlines()[0]
+        current_ver = current_ver.split('|')[1].strip()
+        return current_ver
+
+PACKAGE_CACHE = PackageCache()
+
+def install_packages(debs, method, gain_root='su'):
+    """Install one or more packages (a list of filenames)."""
+
+    if method and method not in (
+            'apt', 'dpkg',
+            'gdebi', 'gdebi-gtk', 'gdebi-kde',
+            ):
+        logger.warning(('Unknown installation method %r, using apt or dpkg ' +
+            'instead') % method)
+        method = None
+
+    if not method:
+        apt_ver = PACKAGE_CACHE.current_version('apt')
+        if Version(apt_ver.strip()) >= Version('1.1~0'):
+            method = 'apt'
+        else:
+            method = 'dpkg'
+
+    if method == 'apt':
+        run_as_root(['apt-get', 'install', '--install-recommends'] + list(debs),
+                gain_root)
+    elif method == 'dpkg':
+        run_as_root(['dpkg', '-i'] + list(debs), gain_root)
+    elif method == 'gdebi':
+        run_as_root(['gdebi'] + list(debs), gain_root)
+    else:
+        # gdebi-gtk etc.
+        subprocess.call([method] + list(debs))
diff --git a/game_data_packager/util_rpm.py b/game_data_packager/util_rpm.py
new file mode 100644
index 0000000..6c2becb
--- /dev/null
+++ b/game_data_packager/util_rpm.py
@@ -0,0 +1,64 @@
+#!/usr/bin/python3
+# encoding=utf-8
+#
+# Copyright © 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
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+import logging
+import subprocess
+
+from .util import (run_as_root, check_output)
+
+logger = logging.getLogger('game-data-packager.util_rpm')
+
+class PackageCache:
+    available = None
+
+    def is_installed(self, package):
+        return 0 == subprocess.call(['rpm', '-q', package],
+                                    stdout=subprocess.DEVNULL,
+                                    stderr=subprocess.DEVNULL)
+
+    def is_available(self, package):
+        if self.available is None:
+            cache = set()
+            proc = subprocess.Popen(['dnf', 'list'],
+                    universal_newlines=True,
+                    stdout=subprocess.PIPE)
+            for line in proc.stdout:
+                if '.' in line:
+                    cache.add(line.split('.')[0])
+            self.available = cache
+
+        return package in self.available
+
+    def current_version(self, package):
+        try:
+            return check_output(['rpm', '-q',
+              '--qf', '%{VERSION}', package], universal_newlines=True)
+        except subprocess.CalledProcessError:
+            return
+
+    def available_version(self, package):
+        proc = subprocess.Popen(['dnf', 'list', package],
+                                 universal_newlines=True,
+                                 stderr=subprocess.DEVNULL,
+                                 stdout=subprocess.PIPE)
+        # keep only last line
+        for line in proc.stdout:
+            pass
+        return line.split()[1]
+
+PACKAGE_CACHE = PackageCache()
+
+def install_packages(debs, method, gain_root='su'):
+    """Install one or more packages (a list of filenames)."""
+    run_as_root(['rpm', '-ivh'] + list(debs), gain_root)

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