diff --git a/src/pisi-build b/src/pisi-build index dc68428d..0412dcaf 100755 --- a/src/pisi-build +++ b/src/pisi-build @@ -46,8 +46,11 @@ def main(): else: pspec = args[0] + # What we need to do first is create a context with our specfile + ctx = pisi.config.Context(pspec) + # don't do the real job here. this is just a CLI! - pb = PisiBuild(pspec) + pb = PisiBuild(ctx) pb.build() diff --git a/src/pisi/archive.py b/src/pisi/archive.py index 9d4f6310..5a4a4659 100644 --- a/src/pisi/archive.py +++ b/src/pisi/archive.py @@ -9,14 +9,13 @@ import tarfile import zipfile #pisi modules -import config import util class ArchiveBase(object): - def __init__(self, type, fileName): - self.type = type - self.fileName = fileName - self.filePath = config.archives_dir() + '/' + self.fileName + def __init__(self, ctx): + self.type = ctx.spec.source.archiveType + self.fileName = os.path.basename(ctx.spec.source.archiveUri) + self.filePath = ctx.archives_dir() + '/' + self.fileName def unpack(self, targetDir): self.targetDir = targetDir @@ -27,8 +26,8 @@ class ArchiveBase(object): os.makedirs(self.targetDir) class ArchiveTarFile(ArchiveBase): - def __init__(self, type, fileName): - super(ArchiveTarFile, self).__init__(type, fileName) + def __init__(self, ctx): + super(ArchiveTarFile, self).__init__(ctx) def unpack(self, targetDir): super(ArchiveTarFile, self).unpack(targetDir) @@ -49,8 +48,8 @@ class ArchiveTarFile(ArchiveBase): tar.close() class ArchiveZip(ArchiveBase): - def __init__(self, type, fileName): - super(ArchiveZip, self).__init__(type, fileName) + def __init__(self, ctx): + super(ArchiveZip, self).__init__(ctx) def unpack(self, targetDir): super(ArchiveZip, self).unpack(targetDir) @@ -86,7 +85,7 @@ class ArchiveZip(ArchiveBase): class Archive: """Unpack magic for Archive files...""" - def __init__(self, type, fileName): + def __init__(self, ctx): """accepted archive types: targz, tarbz2, zip, tar""" @@ -97,7 +96,8 @@ class Archive: 'zip': ArchiveZip } - self.archive = actions.get(type)(type, fileName) + type = ctx.spec.source.archiveType + self.archive = actions.get(type)(ctx) def unpack(self, targetDir): self.archive.unpack(targetDir) diff --git a/src/pisi/build.py b/src/pisi/build.py index b6f57692..a9e5cd87 100644 --- a/src/pisi/build.py +++ b/src/pisi/build.py @@ -5,13 +5,11 @@ # python standard library import os -from specfile import SpecFile from fetcher import Fetcher from archive import Archive # import pisipackage import util -import config import ui class PisiBuildError(Exception): @@ -28,17 +26,11 @@ def displayProgress(pd): class PisiBuild: """PisiBuild class, provides the package build and creation routines""" - def __init__(self, pspecfile): - self.pspecfile = pspecfile - spec = SpecFile() - spec.read(pspecfile) - spec.verify() # check pspec integrity - - self.work_dir = config.build_work_dir(spec.source.name, - spec.source.version, - spec.source.release) + def __init__(self, context): + self.ctx = context + self.work_dir = self.ctx.build_work_dir() - self.spec = spec + self.spec = self.ctx.spec def build(self): ui.info("Building PISI source package: %s\n" % self.spec.source.name) @@ -46,23 +38,23 @@ class PisiBuild: ui.info("Fetching source from: %s\n" % self.spec.source.archiveUri) self.fetchArchive(displayProgress) ui.info("Source archive is stored: %s/%s\n" - %(config.archives_dir(), self.spec.source.archiveName)) + %(self.ctx.archives_dir(), self.spec.source.archiveName)) self.solveBuildDependencies() ui.info("Unpacking archive...") - targetDir = self.unpackArchive() - ui.info(" unpacked (%s)\n" % targetDir) + self.unpackArchive() + ui.info(" unpacked (%s)\n" % self.ctx.build_work_dir()) # applyPatches() - self.actionScript = open( os.path.dirname( self.pspecfile ) + '/' + 'actions' ).read() + self.actionScript = open( os.path.dirname( self.ctx.pspecfile ) + '/' + 'actions' ).read() # FIXME: It's wrong to assume that unpacked archive # will create a name-version top-level directory. # Archive module should give the exact location. # (from the assumption is evil dept.) - os.chdir( config.build_work_dir( self.spec.source.name, self.spec.source.version, self.spec.source.release ) + "/" + self.spec.source.name + "-" + self.spec.source.version) + os.chdir( self.ctx.build_work_dir() + "/" + self.spec.source.name + "-" + self.spec.source.version) locals = globals = {} try: @@ -79,10 +71,9 @@ class PisiBuild: self.installSource( locals ) def fetchArchive(self, percentHook=None): - """fetch an archive and store to config.archives_dir() + """fetch an archive and store to ctx.archives_dir() using fether.Fetcher""" - fetch = Fetcher(self.spec.source.archiveUri, - self.spec.source.archiveName) + fetch = Fetcher(self.ctx) # check if source already cached destpath = fetch.filedest + "/" + fetch.filename @@ -104,10 +95,7 @@ class PisiBuild: pass def unpackArchive(self): - type = self.spec.source.archiveType - fileName = self.spec.source.archiveName - - archive = Archive(type, fileName) + archive = Archive(self.ctx) archive.unpack(self.work_dir) def applyPatches(self): diff --git a/src/pisi/config.py b/src/pisi/config.py index e1a9222a..16cd7c12 100644 --- a/src/pisi/config.py +++ b/src/pisi/config.py @@ -1,40 +1,96 @@ # -*- coding: utf-8 -*- # PISI configuration (static and dynamic) -lib_dir_suffix = "/var/lib/pisi" -db_dir_suffix = "/var/db/pisi" -archives_dir_suffix = "/var/cache/pisi/archives" -tmp_dir_suffix = "/var/tmp/pisi" +from specfile import SpecFile -# directory suffixes for build -# I'm not comfortable with these and accompanying functions, -# but could find a cleaner way for now. -build_work_dir_suffix = "/work" -build_install_dir_suffix = "/install" +class Constants: + """Pisi constants""" + class __const: + """Constant members implementation""" + class ConstError(TypeError): + pass + + def __setattr__(self, name, value): + if self.__dict__.has_key(name): + raise self.ConstError, "Can't rebind constant: %s" % name + # Binding an attribute once to a const is available + self.__dict__[name] = value + + def __delattr__(self, name): + if self.__dict__.has_key(name): + raise self.ConstError, "Can't unbind constant: %s" % name + # we don't have an attribute by this name + raise NameError, name + + c = __const() + + def __init__(self): + self.c.lib_dir_suffix = "/var/lib/pisi" + self.c.db_dir_suffix = "/var/db/pisi" + self.c.archives_dir_suffix = "/var/cache/pisi/archives" + self.c.tmp_dir_suffix = "/var/tmp/pisi" + + # directory suffixes for build + self.c.build_work_dir_suffix = "/work" + self.c.build_install_dir_suffix = "/install" + + def __getattr__(self, attr): + return getattr(self.c, attr) + + def __setattr__(self, attr, value): + return setattr(self.c, attr, value) -destdir = '' # install default to root by default +class Context(object): + """Config/Context Singleton""" + class __impl: + def __init__(self): + self.const = Constants() + # self.c.destdir = '' # install default to root by default + self.const.destdir = './tmp' # only for ALPHA + # the idea is that destdir can be set with --destdir=... -destdir = './tmp' # only for ALPHA + def _specFile(self, pspecfile): + self.pspecfile = pspecfile + spec = SpecFile() + spec.read(pspecfile) + spec.verify() # check pspec integrity -# the idea is that destdir can be set with --destdir=... + self.spec = spec -def lib_dir(): - return destdir + lib_dir_suffix + def lib_dir(self): + return self.const.destdir + self.const.lib_dir_suffix -def db_dir(): - return destdir + db_dir_suffix + def db_dir(self): + return self.const.destdir + self.const.db_dir_suffix -def archives_dir(): - return destdir + archives_dir_suffix + def archives_dir(self): + return self.const.destdir + self.const.archives_dir_suffix + + def tmp_dir(self): + return self.const.destdir + self.const.tmp_dir_suffix + + def build_work_dir(self): + packageDir = self.spec.source.name + '-' \ + + self.spec.source.version + '-' + self.spec.source.release -def tmp_dir(): - return destdir + tmp_dir_suffix + return self.const.destdir + self.const.tmp_dir_suffix \ + + '/' + packageDir + self.const.build_work_dir_suffix -def build_work_dir(packageName, version, release): - packageDir = packageName + '-' + version + '-' + release - return destdir + tmp_dir_suffix + '/' + packageDir + build_work_dir_suffix + def build_install_dir(self): + packageDir = self.spec.source.name + '-' \ + + self.spec.source.version + '-' + self.spec.source.release -def build_install_dir(packageName, version, release): - packageDir = packageName + '-' + version + '-' + release - return destdir + tmp_dir_suffix + '/' + packageDir + build_install_dir_suffix + return self.const.destdir + self.const.tmp_dir_suffix \ + + '/' + packageDir + self.const.build_install_dir_suffix + + __instance = __impl() + + def __init__(self, pspecfile): + self.__instance._specFile(pspecfile) + + def __getattr__(self, attr): + return getattr(self.__instance, attr) + + def __setattr__(self, attr, value): + return setattr(self.__instance, attr, value) diff --git a/src/pisi/fetcher.py b/src/pisi/fetcher.py index a073dbf6..572598c7 100644 --- a/src/pisi/fetcher.py +++ b/src/pisi/fetcher.py @@ -11,7 +11,6 @@ from sys import exit from sys import stderr # pisi modules -import config import util class FetchError (Exception): @@ -19,9 +18,9 @@ class FetchError (Exception): class Fetcher: """Yet another Pisi tool for fetching files from various sources..""" - def __init__(self, uri, fileName): - self.uri = uri - self.filedest = config.archives_dir() + def __init__(self, ctx): + self.uri = ctx.spec.source.archiveUri + self.filedest = ctx.archives_dir() util.check_dir(self.filedest) self.scheme = "file" self.netloc = "" @@ -33,7 +32,7 @@ class Fetcher: from string import split u = urlparse.urlparse(self.uri) self.scheme, self.netloc, self.filepath = u[0], u[1], u[2] - self.filename = fileName + self.filename = os.path.basename(self.uri) def fetch (self): """Return value: Fetched file's full path..""" diff --git a/src/samples/sandbox.pspec b/src/samples/sandbox.pspec new file mode 100644 index 00000000..5660d106 --- /dev/null +++ b/src/samples/sandbox.pspec @@ -0,0 +1,33 @@ + + + + + + + + sandbox + http://cekirdek.uludag.org.tr/~meren/ + + Meral + meren@uludag.org.tr + + As-Is + + http://cekirdek.uludag.org.tr/~meren/sandbox.zip + + + + 06/15/2005 + 0.1 + 1 + + + + + + sandbox + Bla + temprorary:zip + + diff --git a/src/unittests/archivetests.py b/src/unittests/archivetests.py index 14121672..6c10cb41 100644 --- a/src/unittests/archivetests.py +++ b/src/unittests/archivetests.py @@ -4,24 +4,21 @@ from os.path import exists as pathexists from os.path import basename, islink from pisi import archive -from pisi import specfile from pisi import fetcher from pisi import util from pisi import config class ArchiveFileTestCase(unittest.TestCase): - def setUp(self): - self.spec = specfile.SpecFile() - self.spec.read("samples/popt.pspec") - +# def setUp(self): +# pass + def testUnpackTar(self): - targetDir = config.build_work_dir(self.spec.source.name, - self.spec.source.version, - self.spec.source.release) - achv = archive.Archive(self.spec.source.archiveType, - self.spec.source.archiveName) + ctx = config.Context("samples/popt.pspec") + + targetDir = ctx.build_work_dir() + achv = archive.Archive(ctx) - assert self.spec.source.archiveType == "targz" + assert ctx.spec.source.archiveType == "targz" # unpacking is trivial with Archive() achv.unpack(targetDir) @@ -38,16 +35,15 @@ class ArchiveFileTestCase(unittest.TestCase): "171545adab7b51ebf6ec5575d3000a95") def testUnpackZip(self): - # first, we need to fetch a zip file - uri = "http://cekirdek.uludag.org.tr/~meren/sandbox.zip" - filename = basename(uri) - fetch = fetcher.Fetcher(uri, filename) + ctx = config.Context("samples/sandbox.pspec") + fetch = fetcher.Fetcher(ctx) fetch.fetch() - # imaginary name, version and release for our test zip file - targetDir = config.build_work_dir("sandbox", "0.1", "1") + targetDir = ctx.build_work_dir() - achv = archive.Archive("zip", filename) + assert ctx.spec.source.archiveType == "zip" + + achv = archive.Archive(ctx) achv.unpack(targetDir) assert pathexists(targetDir + "/sandbox") diff --git a/src/unittests/fetchertests.py b/src/unittests/fetchertests.py index 5b586c93..cddd1d2b 100644 --- a/src/unittests/fetchertests.py +++ b/src/unittests/fetchertests.py @@ -3,23 +3,21 @@ import unittest import os from pisi import fetcher -from pisi import specfile from pisi import util +from pisi import config class FetcherTestCase(unittest.TestCase): def setUp(self): - self.spec = specfile.SpecFile() - self.spec.read("samples/popt.pspec") + self.ctx = config.Context("samples/popt.pspec") - self.fetch = fetcher.Fetcher(self.spec.source.archiveUri, - self.spec.source.archiveName) + self.fetch = fetcher.Fetcher(self.ctx) def testFetch(self): self.fetch.fetch() destpath = self.fetch.filedest + "/" + self.fetch.filename if os.access(destpath, os.R_OK): self.assertEqual(util.md5_file(destpath), - self.spec.source.archiveMD5) + self.ctx.spec.source.archiveMD5) suite = unittest.makeSuite(FetcherTestCase)