Authentication hadisesine bir parça düzenleme.
This commit is contained in:
@@ -17,8 +17,10 @@ class PisiCLI(object):
|
||||
parser = OptionParser(usage=usage,version="%prog " + pisi.__version__)
|
||||
|
||||
parser.add_option("-D", "--destdir", action="store")
|
||||
parser.add_option("-U", "--username", action="store")
|
||||
parser.add_option("-P", "--password", action="store")
|
||||
parser.add_option("-u", "--username", action="store")
|
||||
parser.add_option("-p", "--password", action="store")
|
||||
parser.add_option("-P", action="store_true", dest="getpass", default=False,
|
||||
help="Get password from the command line")
|
||||
parser.add_option("-v", "--verbose", action="store_true",
|
||||
dest="verbose", default=False,
|
||||
help="detailed output")
|
||||
@@ -37,12 +39,28 @@ class PisiCLI(object):
|
||||
print usage
|
||||
sys.exit(1)
|
||||
|
||||
self.commands = {
|
||||
"help": self.help,
|
||||
"install": self.install,
|
||||
"build": self.build,
|
||||
"updateindex": self.updateindex
|
||||
}
|
||||
self.authInfo = None
|
||||
self.checkAuthInfo()
|
||||
|
||||
def checkAuthInfo(self):
|
||||
username = self.options.username
|
||||
password = self.options.password
|
||||
if not username and not password:
|
||||
return # No authentication
|
||||
elif username and password:
|
||||
self.authInfo = (username, password)
|
||||
return
|
||||
|
||||
# TODO: yapılandırma dosyasını da kontrol et. Belki orada
|
||||
# tanımlanmıştır?
|
||||
|
||||
|
||||
if username and self.options.getpass:
|
||||
from getpass import getpass
|
||||
password = getpass("Password: ")
|
||||
self.authInfo = (username, password)
|
||||
return
|
||||
|
||||
|
||||
# FIX: her komut için ayrı help
|
||||
def help(self, command=""):
|
||||
@@ -62,10 +80,8 @@ class PisiCLI(object):
|
||||
if not self.args:
|
||||
self.help("build")
|
||||
|
||||
username = self.options.username
|
||||
password = self.options.password
|
||||
for arg in self.args:
|
||||
buildhelper.build(arg, username, password)
|
||||
buildhelper.build(arg, self.authInfo)
|
||||
|
||||
def updateindex(self):
|
||||
"""Update the repos db with the given index file (pisi-index.xml)"""
|
||||
@@ -76,12 +92,19 @@ class PisiCLI(object):
|
||||
indexhelper.updateindex(indexfile)
|
||||
|
||||
|
||||
def run(self):
|
||||
for key in self.commands.keys():
|
||||
def runCommand(self):
|
||||
commands = {
|
||||
"help": self.help,
|
||||
"install": self.install,
|
||||
"build": self.build,
|
||||
"updateindex": self.updateindex
|
||||
}
|
||||
|
||||
for key in commands.keys():
|
||||
if key == self.cmd:
|
||||
self.commands[key]()
|
||||
commands[key]()
|
||||
break
|
||||
|
||||
if __name__ == "__main__":
|
||||
cli = PisiCLI()
|
||||
cli.run()
|
||||
cli.runCommand()
|
||||
|
||||
+16
-17
@@ -11,16 +11,17 @@ from pisi.purl import PUrl
|
||||
from pisi.fetcher import fetchUrl
|
||||
|
||||
class RemoteSource(object):
|
||||
def __init__(self, url, username, password):
|
||||
def __init__(self, url, authInfo=None):
|
||||
self.url = url
|
||||
self.username = username
|
||||
self.password = password
|
||||
if authInfo:
|
||||
self.url.setAuthInfo(authInfo)
|
||||
self.location = dirname(self.url.uri)
|
||||
|
||||
pkgname = basename(dirname(self.url.path()))
|
||||
self.dest = join(config.tmp_dir(), pkgname)
|
||||
|
||||
self.fetch_pspec()
|
||||
# fetch pspec file
|
||||
self.fetch()
|
||||
pspec = join(self.dest, self.url.filename())
|
||||
self.ctx = BuildContext(pspec)
|
||||
|
||||
@@ -28,40 +29,38 @@ class RemoteSource(object):
|
||||
self.fetch_patches()
|
||||
self.fetch_additionalFiles()
|
||||
|
||||
def fetch_pspec(self):
|
||||
self.fetchFile(self.url.uri)
|
||||
|
||||
def fetch_actionsfile(self):
|
||||
actionsuri = join(self.location, const.actions_file)
|
||||
self.fetchFile(actionsuri)
|
||||
self.url.uri = actionsuri
|
||||
self.fetch()
|
||||
|
||||
def fetch_patches(self):
|
||||
spec = self.ctx.spec
|
||||
for patch in spec.source.patches:
|
||||
patchuri = join(self.location,
|
||||
const.files_dir, patch.filename)
|
||||
self.fetchFile(patchuri, const.files_dir)
|
||||
self.url.uri = patchuri
|
||||
self.fetch(const.files_dir)
|
||||
|
||||
def fetch_additionalFiles(self):
|
||||
spec = self.ctx.spec
|
||||
for afile in spec.source.additionalFiles:
|
||||
afileuri = join(self.location,
|
||||
const.files_dir, patch.filename)
|
||||
self.fetchFile(afileuri, const.files_dir)
|
||||
self.url.uri = afileuri
|
||||
self.fetch(const.files_dir)
|
||||
|
||||
def fetchFile(self, uri, appendDest=""):
|
||||
ui.info("Fetching %s\n" % uri)
|
||||
def fetch(self, appendDest=""):
|
||||
ui.info("Fetching %s\n" % self.url.uri)
|
||||
dest = join(self.dest, appendDest)
|
||||
fetchUrl(uri, dest,
|
||||
username=self.username,
|
||||
password=self.password)
|
||||
fetchUrl(self.url, dest)
|
||||
|
||||
|
||||
def build(pspecfile, username=None, password=None):
|
||||
def build(pspecfile, authInfo=None):
|
||||
# What we need to do first is create a context with our specfile
|
||||
url = PUrl(pspecfile)
|
||||
if url.isRemoteFile():
|
||||
rs = RemoteSource(url, username, password)
|
||||
rs = RemoteSource(url, authInfo)
|
||||
ctx = rs.ctx
|
||||
else:
|
||||
ctx = BuildContext(url.uri)
|
||||
|
||||
+5
-16
@@ -24,11 +24,9 @@ def displayProgress(pd):
|
||||
(pd['filename'], pd['percent'], pd['rate'], pd['symbol'])
|
||||
ui.info(out)
|
||||
|
||||
def fetchUrl(url, dest, username=None, password=None, percentHook=None):
|
||||
def fetchUrl(url, dest, percentHook=None):
|
||||
fetch = Fetcher(url, dest)
|
||||
fetch.percentHook = percentHook
|
||||
if username and password:
|
||||
fetch.setAuthInfo(username, password)
|
||||
fetch.fetch()
|
||||
if percentHook:
|
||||
ui.info('\n')
|
||||
@@ -47,8 +45,6 @@ class Fetcher:
|
||||
self.percent = 0
|
||||
self.rate = 0.0
|
||||
self.percentHook = None
|
||||
self.username = ''
|
||||
self.passwd = ''
|
||||
|
||||
def fetch (self):
|
||||
"""Return value: Fetched file's full path.."""
|
||||
@@ -135,18 +131,11 @@ class Fetcher:
|
||||
dest = open(os.path.join(self.filedest, self.url.filename()) , "w")
|
||||
self.doGrab(fileObj, dest, totalsize)
|
||||
|
||||
def setAuthInfo(self, username='', passwd=''):
|
||||
if self.url.isLocalFile():
|
||||
self.err('No auth info needed for local files')
|
||||
self.username = username
|
||||
self.passwd = passwd
|
||||
|
||||
def formatRequest(self, request):
|
||||
if self.username:
|
||||
request.add_header('Authorization', 'Basic %s' %
|
||||
(encodestring('%s:%s' %
|
||||
(self.username, self.passwd))))
|
||||
|
||||
authinfo = self.url.authInfo()
|
||||
if authinfo:
|
||||
enc = encodestring("%s:%s" % authinfo)
|
||||
request.add_header('Authorization', 'Basic %s' % enc)
|
||||
return request
|
||||
|
||||
def err (self, error):
|
||||
|
||||
@@ -21,6 +21,8 @@ class PUrl(object):
|
||||
self.__fragment = None
|
||||
self.__uri = None
|
||||
|
||||
self.__authinfo = None
|
||||
|
||||
def getUri(self):
|
||||
if self.__uri:
|
||||
return self.__uri
|
||||
@@ -48,6 +50,14 @@ class PUrl(object):
|
||||
def isRemoteFile(self):
|
||||
return not self.isLocalFile()
|
||||
|
||||
def setAuthInfo(self, authTuple):
|
||||
if not isinstance(authTuple, tuple):
|
||||
raise Exception, "setAuthInfo needs a tuple (user, pass)"
|
||||
self.__authinfo = authTuple
|
||||
|
||||
def authInfo(self):
|
||||
return self.__authinfo
|
||||
|
||||
def scheme(self):
|
||||
return self.__scheme
|
||||
|
||||
|
||||
Reference in New Issue
Block a user