[game-data-packager] 01/03: Reduce circular imports by moving run_command_line to its own module

Simon McVittie smcv at debian.org
Sun Jan 24 15:01:46 UTC 2016


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 221759cb5dba573503f25f3277a85f6203466757
Author: Simon McVittie <smcv at debian.org>
Date:   Sun Jan 24 13:19:24 2016 +0000

    Reduce circular imports by moving run_command_line to its own module
---
 Makefile                           |   2 +-
 game_data_packager/__init__.py     | 218 --------------------------------
 game_data_packager/__main__.py     |  21 ----
 game_data_packager/command_line.py | 250 +++++++++++++++++++++++++++++++++++++
 run                                |   2 +-
 5 files changed, 252 insertions(+), 241 deletions(-)

diff --git a/Makefile b/Makefile
index 7a01b88..94fe7c0 100644
--- a/Makefile
+++ b/Makefile
@@ -175,7 +175,7 @@ manual-check:
 	install -d tmp/
 	for game in $(TEST_SUITE); do \
 	        GDP_MIRROR=$(GDP_MIRROR) GDP_UNINSTALLED=1 PYTHONPATH=. \
-		python3 -m game_data_packager -d ./tmp --no-compress $$game --no-search || exit $$?; \
+		python3 -m game_data_packager.command_line -d ./tmp --no-compress $$game --no-search || exit $$?; \
 	done
 	rm -fr tmp/
 
diff --git a/game_data_packager/__init__.py b/game_data_packager/__init__.py
index 9255528..65051e0 100644
--- a/game_data_packager/__init__.py
+++ b/game_data_packager/__init__.py
@@ -32,23 +32,13 @@ import yaml
 
 from .build import (HashedFile,
         PackagingTask)
-from .config import read_config
-from .gog import run_gog_meta_mode
 from .paths import (DATADIR, USE_VFS)
 from .util import ascii_safe
-from .steam import run_steam_meta_mode
 from .version import (DISTRO, FORMAT, GAME_PACKAGE_VERSION)
 
 logging.basicConfig()
 logger = logging.getLogger('game-data-packager')
 
-# For now, we're given these by the shell script wrapper.
-
-if os.environ.get('DEBUG') or os.environ.get('GDP_DEBUG'):
-    logging.getLogger().setLevel(logging.DEBUG)
-else:
-    logging.getLogger().setLevel(logging.INFO)
-
 MD5SUM_DIVIDER = re.compile(r' [ *]?')
 
 class WantedFile(HashedFile):
@@ -1372,211 +1362,3 @@ def load_game(progress, games, filename, content):
             raise
 
 load_game.counter = 0
-
-def run_command_line():
-    logger.debug('Arguments: %r', sys.argv)
-
-    # Don't set any defaults on this base parser, because it interferes
-    # with the ability to recognise the same argument either before or
-    # after the game name. Set them on the Namespace instead.
-    base_parser = argparse.ArgumentParser(prog='game-data-packager',
-            description='Package game files.',
-            add_help=False,
-            argument_default=argparse.SUPPRESS)
-
-    base_parser.add_argument('--package', '-p', action='append',
-            dest='packages', metavar='PACKAGE',
-            help='Produce this data package (may be repeated)')
-
-    base_parser.add_argument('--install-method', metavar='METHOD',
-            dest='install_method',
-            help='Use METHOD (apt, dpkg, gdebi, gdebi-gtk, gdebi-kde) ' +
-                'to install packages')
-
-    base_parser.add_argument('--gain-root-command', metavar='METHOD',
-            dest='gain_root_command',
-            help='Use METHOD (su, sudo, pkexec) to gain root if needed')
-
-    # This is about ability to audit, not freeness. We don't have an
-    # option to restrict g-d-p to dealing with Free Software, because
-    # that would rule out the vast majority of its packages: if a game's
-    # data is Free Software, we could put it in main or contrib and not need
-    # g-d-p at all.
-    base_parser.add_argument('--binary-executables', action='store_true',
-            help='allow installation of executable code that was not built ' +
-                'from public source code')
-
-    # Misc options
-    group = base_parser.add_mutually_exclusive_group()
-    group.add_argument('-i', '--install', action='store_true',
-            help='install the generated package')
-    group.add_argument('-n', '--no-install', action='store_false',
-            dest='install',
-            help='do not install the generated package (requires -d, default)')
-    base_parser.add_argument('-d', '--destination', metavar='OUTDIR',
-            help='write the generated .deb(s) to OUTDIR')
-
-    group = base_parser.add_mutually_exclusive_group()
-    group.add_argument('-z', '--compress', action='store_true',
-            help='compress generated .deb (default if -d is used)')
-    group.add_argument('--no-compress', action='store_false',
-            dest='compress',
-            help='do not compress generated .deb (default without -d)')
-
-    group = base_parser.add_mutually_exclusive_group()
-    group.add_argument('--download', action='store_true',
-            help='automatically download necessary files if possible ' +
-                '(default)')
-    group.add_argument('--no-download', action='store_false',
-            dest='download', help='do not download anything')
-    base_parser.add_argument('--save-downloads', metavar='DIR',
-            help='save downloaded files to DIR, and look for files there')
-
-    group = base_parser.add_mutually_exclusive_group()
-    group.add_argument('--verbose', action='store_true',
-            help='show output from external tools')
-    group.add_argument('--no-verbose', action='store_false',
-            dest='verbose', help='hide output from external '
-             'tools (default)')
-
-    class DebugAction(argparse.Action):
-        def __call__(self, parser, namespace, values, option_string=None):
-            logging.getLogger().setLevel(logging.DEBUG)
-
-    base_parser.add_argument('--debug', action=DebugAction, nargs=0,
-            help='show debug messages')
-
-    class DumbParser(argparse.ArgumentParser):
-        def error(self, message):
-            pass
-
-    dumb_parser = DumbParser(parents=(base_parser,),add_help=False)
-    dumb_parser.add_argument('game', type=str, nargs='?')
-    dumb_parser.add_argument('paths', type=str, nargs='*')
-    dumb_parser.add_argument('-h', '--help', action='store_true', dest='h')
-    g = dumb_parser.parse_args().game
-    zip = os.path.join(DATADIR, 'vfs.zip')
-    if g is None:
-        games = load_games()
-    elif '-h' in sys.argv or '--help' in sys.argv:
-        games = load_games()
-    elif os.path.isfile(os.path.join(DATADIR, '%s.json' % g)):
-        games = load_games(game=g)
-    elif not os.path.isfile(zip):
-        games = load_games()
-    else:
-        with zipfile.ZipFile(zip, 'r') as zf:
-            if '%s.json' % g in zf.namelist():
-                games = load_games(game=g)
-            else:
-                games = load_games()
-
-    parser = argparse.ArgumentParser(prog='game-data-packager',
-            description='Package game files.', parents=(base_parser,),
-            epilog='Run "game-data-packager GAME --help" to see ' +
-                'game-specific arguments.')
-
-    game_parsers = parser.add_subparsers(dest='shortname',
-            title='supported games', metavar='GAME')
-
-    for g in sorted(games.keys()):
-        games[g].add_parser(game_parsers, base_parser)
-
-    # GOG meta-mode
-    gog_parser = game_parsers.add_parser('gog',
-        help='Package all your GOG.com games at once',
-        description='Automatically package all your GOG.com games',
-        formatter_class=argparse.RawDescriptionHelpFormatter,
-        parents=(base_parser,))
-    group = gog_parser.add_mutually_exclusive_group()
-    group.add_argument('--all', action='store_true', default=False,
-                       help='show all GOG.com games')
-    group.add_argument('--new', action='store_true', default=False,
-                       help='show all new GOG.com games')
-
-    # Steam meta-mode
-    steam_parser = game_parsers.add_parser('steam',
-        help='Package all Steam games at once',
-        description='Automatically package all your Steam games',
-        formatter_class=argparse.RawDescriptionHelpFormatter,
-        parents=(base_parser,))
-    group = steam_parser.add_mutually_exclusive_group()
-    group.add_argument('--all', action='store_true', default=False,
-                       help='package all Steam games')
-    group.add_argument('--new', action='store_true', default=False,
-                       help='package all new Steam games')
-
-    config = read_config()
-    parsed = argparse.Namespace(
-            binary_executables=False,
-            compress=None,
-            destination=None,
-            download=True,
-            verbose=False,
-            install=False,
-            install_method='',
-            gain_root_command='',
-            packages=[],
-            save_downloads=None,
-            shortname=None,
-    )
-    if config['install']:
-        logger.debug('obeying INSTALL=yes in configuration')
-        parsed.install = True
-    if config['preserve']:
-        logger.debug('obeying PRESERVE=yes in configuration')
-        parsed.destination = '.'
-    if config['verbose']:
-        logger.debug('obeying VERBOSE=yes in configuration')
-        parsed.verbose = True
-    if config['install_method']:
-        logger.debug('obeying INSTALL_METHOD=%r in configuration',
-                config['install_method'])
-        parsed.install_method = config['install_method']
-    if config['gain_root_command']:
-        logger.debug('obeying GAIN_ROOT_COMMAND=%r in configuration',
-                config['gain_root_command'])
-        parsed.gain_root_command = config['gain_root_command']
-
-    parser.parse_args(namespace=parsed)
-    logger.debug('parsed command-line arguments into: %r', parsed)
-
-    if parsed.destination is None and not parsed.install:
-        logger.error('At least one of --install or --destination is required')
-        sys.exit(2)
-
-    if parsed.shortname is None:
-        parser.print_help()
-        sys.exit(0)
-
-    for arg, path in (('--save-downloads', parsed.save_downloads),
-                      ('--destination', parsed.destination)):
-        if path is None:
-            continue
-        elif not os.path.isdir(path):
-            logger.error('argument "%s" to %s does not exist', path, arg)
-            sys.exit(2)
-        elif not os.access(path, os.W_OK | os.X_OK):
-            logger.error('argument "%s" to %s is not writable', path, arg)
-            sys.exit(2)
-
-    if parsed.shortname == 'steam':
-        run_steam_meta_mode(parsed, games)
-        return
-    elif parsed.shortname == 'gog':
-        run_gog_meta_mode(parsed, games)
-        return
-    elif parsed.shortname in games:
-        game = games[parsed.shortname]
-    else:
-        parsed.package = parsed.shortname
-        for game in games.values():
-            if parsed.shortname in game.packages:
-                break
-            if parsed.shortname in game.aliases:
-                break
-        else:
-            raise AssertionError('could not find %s' % parsed.shortname)
-
-    with game.construct_task() as task:
-        task.run_command_line(parsed)
diff --git a/game_data_packager/__main__.py b/game_data_packager/__main__.py
deleted file mode 100644
index 881fe7e..0000000
--- a/game_data_packager/__main__.py
+++ /dev/null
@@ -1,21 +0,0 @@
-#!/usr/bin/python3
-# encoding=utf-8
-#
-# Copyright © 2014 Simon McVittie <smcv at debian.org>
-#
-# 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.
-
-from . import run_command_line
-
-if __name__ == '__main__':
-    run_command_line()
diff --git a/game_data_packager/command_line.py b/game_data_packager/command_line.py
new file mode 100644
index 0000000..ae55bd1
--- /dev/null
+++ b/game_data_packager/command_line.py
@@ -0,0 +1,250 @@
+#!/usr/bin/python3
+# encoding=utf-8
+#
+# Copyright © 2014-2016 Simon McVittie <smcv at debian.org>
+# Copyright © 2015-2016 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 argparse
+import logging
+import os
+import sys
+import zipfile
+
+from . import (load_games)
+from .config import (read_config)
+from .gog import (run_gog_meta_mode)
+from .paths import (DATADIR)
+from .steam import (run_steam_meta_mode)
+
+logging.basicConfig()
+logger = logging.getLogger(__name__)
+
+# For now, we're given these by the shell script wrapper.
+
+if os.environ.get('DEBUG') or os.environ.get('GDP_DEBUG'):
+    logging.getLogger().setLevel(logging.DEBUG)
+else:
+    logging.getLogger().setLevel(logging.INFO)
+
+def run_command_line():
+    logger.debug('Arguments: %r', sys.argv)
+
+    # Don't set any defaults on this base parser, because it interferes
+    # with the ability to recognise the same argument either before or
+    # after the game name. Set them on the Namespace instead.
+    base_parser = argparse.ArgumentParser(prog='game-data-packager',
+            description='Package game files.',
+            add_help=False,
+            argument_default=argparse.SUPPRESS)
+
+    base_parser.add_argument('--package', '-p', action='append',
+            dest='packages', metavar='PACKAGE',
+            help='Produce this data package (may be repeated)')
+
+    base_parser.add_argument('--install-method', metavar='METHOD',
+            dest='install_method',
+            help='Use METHOD (apt, dpkg, gdebi, gdebi-gtk, gdebi-kde) ' +
+                'to install packages')
+
+    base_parser.add_argument('--gain-root-command', metavar='METHOD',
+            dest='gain_root_command',
+            help='Use METHOD (su, sudo, pkexec) to gain root if needed')
+
+    # This is about ability to audit, not freeness. We don't have an
+    # option to restrict g-d-p to dealing with Free Software, because
+    # that would rule out the vast majority of its packages: if a game's
+    # data is Free Software, we could put it in main or contrib and not need
+    # g-d-p at all.
+    base_parser.add_argument('--binary-executables', action='store_true',
+            help='allow installation of executable code that was not built ' +
+                'from public source code')
+
+    # Misc options
+    group = base_parser.add_mutually_exclusive_group()
+    group.add_argument('-i', '--install', action='store_true',
+            help='install the generated package')
+    group.add_argument('-n', '--no-install', action='store_false',
+            dest='install',
+            help='do not install the generated package (requires -d, default)')
+    base_parser.add_argument('-d', '--destination', metavar='OUTDIR',
+            help='write the generated .deb(s) to OUTDIR')
+
+    group = base_parser.add_mutually_exclusive_group()
+    group.add_argument('-z', '--compress', action='store_true',
+            help='compress generated .deb (default if -d is used)')
+    group.add_argument('--no-compress', action='store_false',
+            dest='compress',
+            help='do not compress generated .deb (default without -d)')
+
+    group = base_parser.add_mutually_exclusive_group()
+    group.add_argument('--download', action='store_true',
+            help='automatically download necessary files if possible ' +
+                '(default)')
+    group.add_argument('--no-download', action='store_false',
+            dest='download', help='do not download anything')
+    base_parser.add_argument('--save-downloads', metavar='DIR',
+            help='save downloaded files to DIR, and look for files there')
+
+    group = base_parser.add_mutually_exclusive_group()
+    group.add_argument('--verbose', action='store_true',
+            help='show output from external tools')
+    group.add_argument('--no-verbose', action='store_false',
+            dest='verbose', help='hide output from external '
+             'tools (default)')
+
+    class DebugAction(argparse.Action):
+        def __call__(self, parser, namespace, values, option_string=None):
+            logging.getLogger().setLevel(logging.DEBUG)
+
+    base_parser.add_argument('--debug', action=DebugAction, nargs=0,
+            help='show debug messages')
+
+    class DumbParser(argparse.ArgumentParser):
+        def error(self, message):
+            pass
+
+    dumb_parser = DumbParser(parents=(base_parser,),add_help=False)
+    dumb_parser.add_argument('game', type=str, nargs='?')
+    dumb_parser.add_argument('paths', type=str, nargs='*')
+    dumb_parser.add_argument('-h', '--help', action='store_true', dest='h')
+    g = dumb_parser.parse_args().game
+    zip = os.path.join(DATADIR, 'vfs.zip')
+    if g is None:
+        games = load_games()
+    elif '-h' in sys.argv or '--help' in sys.argv:
+        games = load_games()
+    elif os.path.isfile(os.path.join(DATADIR, '%s.json' % g)):
+        games = load_games(game=g)
+    elif not os.path.isfile(zip):
+        games = load_games()
+    else:
+        with zipfile.ZipFile(zip, 'r') as zf:
+            if '%s.json' % g in zf.namelist():
+                games = load_games(game=g)
+            else:
+                games = load_games()
+
+    parser = argparse.ArgumentParser(prog='game-data-packager',
+            description='Package game files.', parents=(base_parser,),
+            epilog='Run "game-data-packager GAME --help" to see ' +
+                'game-specific arguments.')
+
+    game_parsers = parser.add_subparsers(dest='shortname',
+            title='supported games', metavar='GAME')
+
+    for g in sorted(games.keys()):
+        games[g].add_parser(game_parsers, base_parser)
+
+    # GOG meta-mode
+    gog_parser = game_parsers.add_parser('gog',
+        help='Package all your GOG.com games at once',
+        description='Automatically package all your GOG.com games',
+        formatter_class=argparse.RawDescriptionHelpFormatter,
+        parents=(base_parser,))
+    group = gog_parser.add_mutually_exclusive_group()
+    group.add_argument('--all', action='store_true', default=False,
+                       help='show all GOG.com games')
+    group.add_argument('--new', action='store_true', default=False,
+                       help='show all new GOG.com games')
+
+    # Steam meta-mode
+    steam_parser = game_parsers.add_parser('steam',
+        help='Package all Steam games at once',
+        description='Automatically package all your Steam games',
+        formatter_class=argparse.RawDescriptionHelpFormatter,
+        parents=(base_parser,))
+    group = steam_parser.add_mutually_exclusive_group()
+    group.add_argument('--all', action='store_true', default=False,
+                       help='package all Steam games')
+    group.add_argument('--new', action='store_true', default=False,
+                       help='package all new Steam games')
+
+    config = read_config()
+    parsed = argparse.Namespace(
+            binary_executables=False,
+            compress=None,
+            destination=None,
+            download=True,
+            verbose=False,
+            install=False,
+            install_method='',
+            gain_root_command='',
+            packages=[],
+            save_downloads=None,
+            shortname=None,
+    )
+    if config['install']:
+        logger.debug('obeying INSTALL=yes in configuration')
+        parsed.install = True
+    if config['preserve']:
+        logger.debug('obeying PRESERVE=yes in configuration')
+        parsed.destination = '.'
+    if config['verbose']:
+        logger.debug('obeying VERBOSE=yes in configuration')
+        parsed.verbose = True
+    if config['install_method']:
+        logger.debug('obeying INSTALL_METHOD=%r in configuration',
+                config['install_method'])
+        parsed.install_method = config['install_method']
+    if config['gain_root_command']:
+        logger.debug('obeying GAIN_ROOT_COMMAND=%r in configuration',
+                config['gain_root_command'])
+        parsed.gain_root_command = config['gain_root_command']
+
+    parser.parse_args(namespace=parsed)
+    logger.debug('parsed command-line arguments into: %r', parsed)
+
+    if parsed.destination is None and not parsed.install:
+        logger.error('At least one of --install or --destination is required')
+        sys.exit(2)
+
+    if parsed.shortname is None:
+        parser.print_help()
+        sys.exit(0)
+
+    for arg, path in (('--save-downloads', parsed.save_downloads),
+                      ('--destination', parsed.destination)):
+        if path is None:
+            continue
+        elif not os.path.isdir(path):
+            logger.error('argument "%s" to %s does not exist', path, arg)
+            sys.exit(2)
+        elif not os.access(path, os.W_OK | os.X_OK):
+            logger.error('argument "%s" to %s is not writable', path, arg)
+            sys.exit(2)
+
+    if parsed.shortname == 'steam':
+        run_steam_meta_mode(parsed, games)
+        return
+    elif parsed.shortname == 'gog':
+        run_gog_meta_mode(parsed, games)
+        return
+    elif parsed.shortname in games:
+        game = games[parsed.shortname]
+    else:
+        parsed.package = parsed.shortname
+        for game in games.values():
+            if parsed.shortname in game.packages:
+                break
+            if parsed.shortname in game.aliases:
+                break
+        else:
+            raise AssertionError('could not find %s' % parsed.shortname)
+
+    with game.construct_task() as task:
+        task.run_command_line(parsed)
+
+if __name__ == '__main__':
+    run_command_line()
diff --git a/run b/run
index c1d02b6..77a62bb 100755
--- a/run
+++ b/run
@@ -24,6 +24,6 @@ case "$1" in
 		exec python3 -m game_data_packager.make_template "$@"
 		;;
 	*)
-		exec python3 -m game_data_packager "$@"
+		exec python3 -m game_data_packager.command_line "$@"
 		;;
 esac

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