biraz şekle soktum. komutların her birini sınıf yapmak getattr magiclerinden kurtardı bizi.

This commit is contained in:
Barış Metin
2005-07-12 21:09:25 +00:00
parent 1ee07261c6
commit f4e7b5ec6f
+182 -157
View File
@@ -6,6 +6,8 @@ from optparse import OptionParser
import pisi import pisi
from pisi.config import config from pisi.config import config
# globals
usage = """%prog <command> [options] [arguments] usage = """%prog <command> [options] [arguments]
where <command> is one of: where <command> is one of:
@@ -23,100 +25,63 @@ Use \"%prog help <command>\" for help on a specific subcommand.
PISI Package Manager PISI Package Manager
""" """
class ParserError: # helper functions
def __init__(self, msg): def cmdObject(cmd, fail=False):
self.msg = msg commands = {"help": Help,
"build": Build,
"info": Info,
"install": Install,
"remove": Remove,
"index": Index,
"updatedb": UpdateDB}
class Parser(OptionParser): if commands.has_key(cmd):
obj = commands[cmd]()
return obj
def __init__(self, usage, version): if fail:
OptionParser.__init__(self, usage=usage, version=version) print "Unrecognized command: ", cmd
sys.exit(1)
else:
return None
def error(self, msg): def commonopts(parser):
raise ParserError(msg) p = parser
p.add_option("-D", "--destdir", action="store")
# eheh, burada ne yaptigim anlasilamamis galiba p.add_option("-u", "--username", action="store")
# sunlari yazin: p.add_option("-p", "--password", action="store")
# pisi-cli --test remove x p.add_option("-P", action="store_true", dest="getpass", default=False,
# pisi-cli --test build x help="Get password from the command line")
# diger turlu olmaz bu. dikkat. per command optionlar p.add_option("-v", "--verbose", action="store_true",
# svn log'unda anlatmistim. simdi bir daha yazmam gerekti bazi dest="verbose", default=False,
# seyleri bastan calistirabilmek icin. bir ise yaramasa yapmazdim. help="detailed output")
p.add_option("-d", "--debug", action="store_true",
class PisiCLI(object): default=True, help="show debugging information")
p.add_option("-n", "--dry-run", action="store_true", default=False,
commands = ["help", "build", "info", "install", help = "do not perform any action, just show what\
"remove", "index", "updatedb"] would be done")
return p
def commonopts(self):
p = self.parser
p.add_option("-D", "--destdir", action="store")
p.add_option("-u", "--username", action="store")
p.add_option("-p", "--password", action="store")
p.add_option("-P", action="store_true", dest="getpass", default=False,
help="Get password from the command line")
p.add_option("-v", "--verbose", action="store_true",
dest="verbose", default=False,
help="detailed output")
p.add_option("-d", "--debug", action="store_true",
default=True, help="show debugging information")
p.add_option("-n", "--dry-run", action="store_true", default=False,
help = "do not perform any action, just show what\
would be done")
######## start commands #########
class Command(object):
"""generic help string for any command"""
def __init__(self): def __init__(self):
# first construct a parser for common options
# this is really dummy
self.parser = Parser(usage=usage, version="%prog " + pisi.__version__)
#self.parser.allow_interspersed_args = False
self.commonopts()
self.command = ""
try:
(options, args) = self.parser.parse_args()
#if len(parser.rargs)==0:
# self.die()
self.command = args[0]
except ParserError:
# fully expected :) let's see if we got an argument
if len(self.parser.rargs)==0:
self.die()
self.command = self.parser.rargs[0]
#print '*', self.command
# now for the real parser THIS IS ABSOLUTELY NECESSARY # now for the real parser THIS IS ABSOLUTELY NECESSARY
self.parser = OptionParser(usage=usage, self.parser = OptionParser(usage=usage,
version="%prog " + pisi.__version__) version="%prog " + pisi.__version__)
#self.parser.allow_interspersed_args = False #self.parser.allow_interspersed_args = False
self.commonopts() self.options()
self.add_subcommand_opts() self.parsr = commonopts(self.parser)
(self.options, args) = self.parser.parse_args() (self.options, args) = self.parser.parse_args()
self.args = args[1:] self.args = args[1:]
self.authInfo = None
self.checkAuthInfo() self.checkAuthInfo()
def die(self): def options(self):
print usage """This is a fall back function. If the implementer module provides an
sys.exit(1) options function it will be called"""
pass
def add_subcommand_opts(self):
if self.command in self.commands:
f = self.__getattribute__(self.command + '_opts')
f()
else:
print 'Unrecognized command:', self.command
self.die()
def runCommand(self):
if self.command in self.commands:
f = self.__getattribute__(self.command)
f()
else:
print 'Unrecognized command:', self.command
self.die()
def checkAuthInfo(self): def checkAuthInfo(self):
username = self.options.username username = self.options.username
@@ -124,7 +89,7 @@ class PisiCLI(object):
if not username and not password: if not username and not password:
if config.username and config.password: if config.username and config.password:
self.authInfo = (config.username, config.password) self.authInfo = (config.username, config.password)
return return
elif username and password: elif username and password:
self.authInfo = (username, password) self.authInfo = (username, password)
return return
@@ -133,37 +98,87 @@ class PisiCLI(object):
from getpass import getpass from getpass import getpass
password = getpass("Password: ") password = getpass("Password: ")
self.authInfo = (username, password) self.authInfo = (username, password)
return else:
self.authInfo = None
# FIX: her komut için ayrı help def help(self):
print getattr(self, "__doc__")
help_help = """help: display help class Help(Command):
usage: help """prints usage"""
help <command>""" def __init__(self):
super(Help, self).__init__()
def help_opts(self):
pass
def help(self, command=""): def run(self, args=None):
if not self.args: if not args:
print usage print usage
return return
for arg in args:
obj = cmdObject(arg, True)
obj.help()
class Build(Command):
"""build: compile PISI package using a pspec.xml file"""
def __init__(self):
super(Build, self).__init__()
if command !="": def run(self):
print self.__getattribute__(command + '_help')
else:
print self.__getattribute__(self.args[0] + '_help')
info_help = """info: display information about a package
usage: info <package> ...
"""
def info_opts(self):
pass
def info(self):
if not self.args: if not self.args:
self.help("info") self.help()
return
from pisi.cli import buildhelper
for arg in self.args:
buildhelper.build(arg, self.authInfo)
class Install(Command):
"""install: install PISI packages"""
def __init__(self):
super(Install, self).__init__()
def options(self):
self.parser.add_option("", "--test", action="store_true",
default=True, help="xxxx")
def run(self):
if not self.args:
self.help()
return
from pisi.cli import installhelper
for arg in self.args:
installhelper.install(arg)
class Remove(Command):
"""remove: remove PISI packages"""
def __init__(self):
super(Remove, self).__init__()
def options(self):
self.parser.add_option("", "--test", action="store_true",
default=True, help="xxxx")
def run(self):
if not self.args:
self.help()
return
for arg in self.args:
pisi.install.remove(arg)
class Info(Command):
"""info: display information about a package
usage: info <package> ..."""
def __init__(self):
super(Info, self).__init__()
def run(self):
if not self.args:
self.help()
return
for arg in self.args: for arg in self.args:
self.printinfo(arg) self.printinfo(arg)
@@ -173,16 +188,17 @@ usage: info <package> ...
metadata, files = pisi.install.get_pkg_info(arg) metadata, files = pisi.install.get_pkg_info(arg)
print metadata.package print metadata.package
index_help = """index: Index PISI files in a given directory""" class Index(Command):
"""index: Index PISI files in a given directory"""
def __init__(self):
super(Index, self).__init__()
def index_opts(self): def run(self):
pass
def index(self):
from pisi.cli import indexhelper
if not self.args: if not self.args:
self.help("index") self.help()
return
from pisi.cli import indexhelper
if len(self.args)==1: if len(self.args)==1:
indexhelper.index(self.args[0]) indexhelper.index(self.args[0])
elif len(self.args)==0: elif len(self.args)==0:
@@ -191,54 +207,63 @@ usage: info <package> ...
print 'Indexing only a single directory supported' print 'Indexing only a single directory supported'
self.die() self.die()
install_help = """install: install PISI packages""" class UpdateDB(Command):
"""updatedb: update source and package databases"""
def __init__(self):
super(UpdateDB, self).__init__()
def install_opts(self): def run(self):
self.parser.add_option("", "--test", action="store_true",
default=True, help="xxxx")
def install(self):
from pisi.cli import installhelper
if not self.args:
self.help("install")
for arg in self.args:
installhelper.install(arg)
build_help = """build: compile PISI packages"""
def build_opts(self):
pass
def build(self):
from pisi.cli import buildhelper
if not self.args:
self.help("build")
for arg in self.args:
buildhelper.build(arg, self.authInfo)
remove_help = """remove: remove PISI packages"""
def remove_opts(self):
self.parser.add_option("", "--test", action="store_true",
default=True, help="xxxx")
def remove(self):
if not self.args:
self.help("install")
for arg in self.args:
pisi.install.remove(arg)
updatedb_help = """updatedb: update source and package databases"""
def updatedb_opts(self):
pass
def updatedb(self):
"""Update the repos db with the given index file (pisi-index.xml)"""
if len(self.args) != 1: if len(self.args) != 1:
self.help("updatedb") self.help()
return
indexfile = self.args[0] indexfile = self.args[0]
indexhelper.updatedb(indexfile) indexhelper.updatedb(indexfile)
######## end commands #########
class ParserError:
def __init__(self, msg):
self.msg = msg
class Parser(OptionParser):
def __init__(self, usage, version):
OptionParser.__init__(self, usage=usage, version=version)
def error(self, msg):
raise ParserError(msg)
class PisiCLI(object):
def __init__(self):
# first construct a parser for common options
# this is really dummy
self.parser = Parser(usage=usage, version="%prog " + pisi.__version__)
#self.parser.allow_interspersed_args = False
self.parser = commonopts(self.parser)
cmd = ""
try:
(options, args) = self.parser.parse_args()
#if len(parser.rargs)==0:
# self.die()
cmd = args[0]
except IndexError:
self.die()
except ParserError:
# fully expected :) let's see if we got an argument
if len(self.parser.rargs)==0:
self.die()
cmd = self.parser.rargs[0]
#print '*', cmd
self.command = cmdObject(cmd)
if not self.command:
print "Unrecognized command: ", cmd
self.die()
def die(self):
print usage
sys.exit(1)
def runCommand(self):
self.command.run()