From f9d85f1349f9441da0485e5ec2db7dfcab1e86b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Metin?= Date: Tue, 28 Jun 2005 08:56:36 +0000 Subject: [PATCH] =?UTF-8?q?PUrl=20kullanmaya=20ba=C5=9Flad=C4=B1k.=20A?= =?UTF-8?q?=C5=9Fa=C4=9F=C4=B1daki=20komutu=20deneyin...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ./pisi-install http://cekirdek.uludag.org.tr/popt-libs-1.7-3.pisi --- pisi/archive.py | 3 ++- pisi/fetcher.py | 62 +++++++++++++++++++++++++------------------ pisi/package.py | 13 ++++++++- pisi/sourcearchive.py | 57 +++++++++++++++++---------------------- 4 files changed, 74 insertions(+), 61 deletions(-) diff --git a/pisi/archive.py b/pisi/archive.py index 79a5d19d..be33c946 100644 --- a/pisi/archive.py +++ b/pisi/archive.py @@ -71,7 +71,8 @@ class ArchiveZip(ArchiveBase): def __init__(self, filepath, type="zip", mode='r'): super(ArchiveZip, self).__init__(filepath, type) - self.zip = zipfile.ZipFile(filepath, mode) + + self.zip = zipfile.ZipFile(self.filePath, mode) def close(self): """Close the zip archive.""" diff --git a/pisi/fetcher.py b/pisi/fetcher.py index 57983bcd..37d1d8a9 100644 --- a/pisi/fetcher.py +++ b/pisi/fetcher.py @@ -6,40 +6,50 @@ # knows what. # python standard library modules -import urlparse import urllib2 import os # pisi modules import util from config import config +from purl import PUrl +from ui import ui class FetchError (Exception): pass +# helper functions +def displayProgress(pd): + out = '\r%-30.30s %3d%% %12.2f %s' % \ + (pd['filename'], pd['percent'], pd['rate'], pd['symbol']) + ui.info(out) + +def fetchUrl(url, dest, percentHook=None): + fetch = Fetcher(url, dest) + fetch.percentHook = percentHook + fetch.fetch() + if percentHook: + ui.info('\n') + + class Fetcher: """Fetcher can fetch a file from various sources using various protocols.""" - def __init__(self, source): - self.uri = source.archiveUri - self.filedest = config.archives_dir() + def __init__(self, url, dest): + if not isinstance(url, PUrl): + url = PUrl(url) + + self.url = url + self.filedest = dest util.check_dir(self.filedest) - self.scheme = "file" - self.netloc = "" - self.filepath = "" - self.filename = "" self.percent = 0 self.rate = 0.0 self.percentHook = None - from string import split - u = urlparse.urlparse(self.uri) - self.scheme, self.netloc, self.filepath = u[0], u[1], u[2] - self.filename = os.path.basename(self.uri) def fetch (self): """Return value: Fetched file's full path..""" - if self.filename == "": + if not self.url.filename(): self.err("filename error") if os.access(self.filedest, os.W_OK) == False: @@ -47,13 +57,12 @@ class Fetcher: scheme_err = lambda: self.err("unexpected scheme") - handlers = { - 'file': self.fetchLocalFile, - 'http': self.fetchRemoteFile, - 'ftp' : self.fetchRemoteFile - }; handlers.get(self.scheme, scheme_err)() + if self.url.isLocalFile(): + self.fetchLocalFile() + else: + self.fetchRemoteFile() - return self.filedest + "/" + self.filename + return self.filedest + "/" + self.url.filename() def doGrab(self, file, dest, totalsize): symbols = [' B/s', 'KB/s', 'MB/s', 'GB/s'] @@ -81,7 +90,7 @@ class Fetcher: if p.update(size): self.percent = p.percent if self.percentHook != None: - retval = {'filename': self.filename, + retval = {'filename': self.url.filename(), 'percent' : self.percent, 'rate': self.rate, 'symbol': symbol} @@ -92,13 +101,14 @@ class Fetcher: def fetchLocalFile (self): from shutil import copyfile + url = self.url - if os.access(self.filepath, os.F_OK) == False: + if os.access(url.path(), os.F_OK) == False: self.err("no such file or no perm to read") - dest = open(self.filedest + "/" + self.filename , "w") - totalsize = os.path.getsize(self.filepath) - file = open(self.filepath) + dest = open(self.filedest + "/" + url.filename() , "w") + totalsize = os.path.getsize(url.path()) + file = open(url.path()) self.doGrab(file, dest, totalsize) @@ -106,7 +116,7 @@ class Fetcher: from httplib import HTTPException try: - file = urllib2.urlopen(self.uri) + file = urllib2.urlopen(self.url.uri) headers = file.info() except ValueError, e: @@ -122,7 +132,7 @@ class Fetcher: self.err('file not found') else: totalsize = int(headers['Content-Length']) - dest = open(self.filedest + "/" + self.filename , "w") + dest = open(self.filedest + "/" + self.url.filename() , "w") self.doGrab(file, dest, totalsize) diff --git a/pisi/package.py b/pisi/package.py index 031900bc..a1f88b57 100644 --- a/pisi/package.py +++ b/pisi/package.py @@ -5,13 +5,24 @@ import archive from constants import const from config import config +from purl import PUrl class Package: """PISI Package Class provides access to a pisi package (.pisi file).""" def __init__(self, packagefn, mode='r'): - self.impl = archive.ArchiveZip(packagefn, 'zip', mode) self.filename = packagefn + url = PUrl(packagefn) + + if url.isRemoteFile(): + from os import getcwd + from fetcher import fetchUrl, displayProgress + # TODO: belki Constants.packages_dir() gibi bir yere + # indirmek daha iyi olur. + fetchUrl(url, getcwd(), displayProgress) + self.filename = url.filename() + + self.impl = archive.ArchiveZip(self.filename, 'zip', mode) def add_to_package(self, fn): """Add a file or directory to package""" diff --git a/pisi/sourcearchive.py b/pisi/sourcearchive.py index 887b2c41..e321c1c2 100644 --- a/pisi/sourcearchive.py +++ b/pisi/sourcearchive.py @@ -1,17 +1,22 @@ +# -*- coding: utf-8 -*- # python standard library # Authors: Baris Metin -import os +from os.path import join +from os import access, R_OK import sys # pisi modules -from fetcher import Fetcher from archive import Archive -import util +from purl import PUrl from ui import ui +from config import config +from fetcher import fetchUrl, displayProgress import context +import util + class SourceArchiveError(Exception): pass @@ -21,36 +26,22 @@ class SourceArchive: and unpacking a source archive""" def __init__(self, ctx): self.ctx = ctx - self.fileName = os.path.basename(self.ctx.spec.source.archiveUri) - self.filePath = os.path.join(self.ctx.archives_dir(), self.fileName) + self.url = PUrl(self.ctx.spec.source.archiveUri) + self.dest = join(config.archives_dir(), self.url.filename()) + + def fetch(self): + if not self.isCached(): + fetchUrl(self.url, config.archives_dir(), displayProgress) + + def isCached(self): + if not access(self.dest, R_OK): + return False + + # check hash + if util.sha1_file(self.dest) == self.ctx.spec.source.archiveSHA1: + ui.info('%s [cached]\n' % self.ctx.spec.source.archiveName) + return True def unpack(self): - archive = Archive(self.filePath, self.ctx.spec.source.archiveType) + archive = Archive(self.dest, self.ctx.spec.source.archiveType) archive.unpack(self.ctx.pkg_work_dir()) - - def displayProgress(pd): - out = '\r%-30.30s %3d%% %12.2f %s' % \ - (pd['filename'], pd['percent'], pd['rate'], pd['symbol']) - ui.info(out) - - def fetch(self, percentHook=displayProgress): - """fetch an archive and store to ctx.archives_dir() - using fetcher.Fetcher""" - fetch = Fetcher(self.ctx.spec.source) - - # check if source already cached - destpath = fetch.filedest + "/" + fetch.filename - if os.access(destpath, os.R_OK): - if util.sha1_file(destpath) == self.ctx.spec.source.archiveSHA1: - ui.info('%s [cached]\n' % self.ctx.spec.source.archiveName) - return - - if percentHook: - fetch.percentHook = percentHook - - fetch.fetch() - - # FIXME: What a ugly hack! We should really find a cleaner way for output. - if percentHook: - ui.info('\n') - pass