[game-data-packager] 13/39: Give the Python code a full command-line parser
Simon McVittie
smcv at debian.org
Sun Jan 11 01:52:22 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 5d7d5ca6990b768253a764fd05065f329bd9abde
Author: Simon McVittie <smcv at debian.org>
Date: Sat Jan 10 19:09:59 2015 +0000
Give the Python code a full command-line parser
This is a step towards getting rid of the shell script layer.
---
lib/game_data_packager/__init__.py | 63 ++++++++++++++++++++++++++++++++++----
lib/game_data_packager/__main__.py | 12 ++------
2 files changed, 59 insertions(+), 16 deletions(-)
diff --git a/lib/game_data_packager/__init__.py b/lib/game_data_packager/__init__.py
index b5cfedf..7f40ee5 100644
--- a/lib/game_data_packager/__init__.py
+++ b/lib/game_data_packager/__init__.py
@@ -19,7 +19,9 @@
"""
import argparse
+import glob
import hashlib
+import importlib
import io
import logging
import os
@@ -1063,15 +1065,15 @@ class GameData(object):
return True
- def run_command_line(self, argv, outdir=''):
- parser = argparse.ArgumentParser(description='Package game files.',
- prog='game-data-packager ' + self.shortname)
+ def add_parser(self, parsers):
+ parser = parsers.add_parser(self.shortname,
+ help=self.longname)
parser.add_argument('--repack', action='store_true',
help='Locate installed game files automatically')
parser.add_argument('paths', nargs='*',
metavar='DIRECTORY|FILE')
- args = parser.parse_args(argv)
+ def run_command_line(self, args, outdir=''):
if args.repack:
can_repack = False
absent = set()
@@ -1100,7 +1102,8 @@ class GameData(object):
possible = set()
for package in self.packages.values():
- if argv and argv[0] in self.packages and package.name != argv[0]:
+ if (args.shortname in self.packages and
+ package.name != args.shortname):
continue
if self.fill_gaps(package, log=False):
@@ -1164,7 +1167,7 @@ class GameData(object):
raise SystemExit(1)
for package in ready:
- destdir = os.path.join(os.environ['WORKDIR'],
+ destdir = os.path.join(self.get_workdir(),
'%s.deb.d' % package.name)
if not self.fill_dest_dir(package, destdir):
raise SystemExit(1)
@@ -1200,3 +1203,51 @@ class GameData(object):
rm_rf(destdir)
rm_rf(os.path.join(self.get_workdir(), 'tmp'))
+
+def run_command_line():
+ datadir = os.environ['DATADIR']
+ etcdir = os.environ['ETCDIR']
+ workdir = os.environ['WORKDIR']
+
+ parser = argparse.ArgumentParser(prog='game-data-packager',
+ description='Package game files.')
+
+ games = {}
+
+ for yamlfile in glob.glob(os.path.join(datadir, '*.yaml')):
+ g = os.path.basename(yamlfile)
+ g = g[:len(g) - 5]
+
+ games[g] = GameData(g, datadir=datadir, etcdir=etcdir,
+ workdir=workdir)
+
+ game_parsers = parser.add_subparsers(dest='shortname',
+ title='supported games', metavar='GAME')
+
+ for g in sorted(games.keys()):
+ games[g].add_parser(game_parsers)
+
+ # Misc options
+ parser.add_argument('-i', '--install', action='store_true',
+ help='install the generated package')
+ parser.add_argument('-n', '--no-install', action='store_false',
+ dest='install',
+ help='do not install the generated package (requires -d, default)')
+ parser.add_argument('-d', '--destination', metavar='OUTDIR',
+ help='write the generated .deb(s) to OUTDIR')
+ parser.add_argument('-z', '--compress', action='store_true',
+ default=None,
+ help='compress generated .deb (default unless -i is used)')
+ parser.add_argument('--no-compress', action='store_false',
+ dest='compress',
+ help='do not compress generated .deb (default with -i)')
+
+ parsed = parser.parse_args()
+
+ if parsed.shortname is None:
+ parser.print_help()
+ sys.exit(0)
+
+ with games[parsed.shortname] as game:
+ parsed = parser.parse_args()
+ game.run_command_line(parsed)
diff --git a/lib/game_data_packager/__main__.py b/lib/game_data_packager/__main__.py
index 2ccdd19..824739c 100644
--- a/lib/game_data_packager/__main__.py
+++ b/lib/game_data_packager/__main__.py
@@ -15,15 +15,7 @@
# You can find the GPL license text on a Debian system under
# /usr/share/common-licenses/GPL-2.
-import os
-import sys
-
-from . import GameData
+from . import run_command_line
if __name__ == '__main__':
- with GameData(sys.argv[1],
- datadir=os.environ['DATADIR'],
- workdir=os.environ['WORKDIR'],
- etcdir=os.environ['ETCDIR'],
- ) as game:
- game.run_command_line(sys.argv[2:], outdir=os.environ['OUTDIR'])
+ run_command_line()
--
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