commit cf45099e2196a239feedebbb2f2c6ec2f1989894 Author: Tomas Touceda chiiph@gentoo.org Date: Wed Jun 22 08:11:18 2011 -0300
Improve ThpDB, implement ThpPackage
ThpDB's insert, delete and exists seems to work as expected by now. --- lib/thandy/packagesys/PackageSystem.py | 2 + lib/thandy/packagesys/ThpPackages.py | 75 +++++++++++++++++++++++++------ 2 files changed, 62 insertions(+), 15 deletions(-)
diff --git a/lib/thandy/packagesys/PackageSystem.py b/lib/thandy/packagesys/PackageSystem.py index ccf5532..d1060d0 100644 --- a/lib/thandy/packagesys/PackageSystem.py +++ b/lib/thandy/packagesys/PackageSystem.py @@ -70,6 +70,7 @@ def getChecker(relPath, extra, defaultFormat, package): k,v=extra['registry_ent'] return thandy.packagesys.ExePackages.RegistryChecker(k,v) elif checkType == 'thp': + print "Getting thp checker" import thandy.packagesys.ThpPackages return thandy.packagesys.ThpPackages.ThpChecker( extra['thp_name'], extra['thp_version']) @@ -108,6 +109,7 @@ def getInstaller(relPath, extra, defaultFormat, package): installer = thandy.packagesys.ExePackages.CommandInstaller( relPath, extra['cmd_install'], extra.get('cmd_remove')) elif installType == 'thp': + print "Getting thp installer" import thandy.packagesys.ThpPackages installer = thandy.packagesys.ThpPackages.ThpInstaller( relPath) diff --git a/lib/thandy/packagesys/ThpPackages.py b/lib/thandy/packagesys/ThpPackages.py index ae86128..b87c7b4 100644 --- a/lib/thandy/packagesys/ThpPackages.py +++ b/lib/thandy/packagesys/ThpPackages.py @@ -2,6 +2,9 @@
import logging import os +import zipfile +import tempfile +import time
import thandy.util import thandy.packagesys.PackageSystem as PS @@ -11,19 +14,19 @@ json = thandy.util.importJSON()
class ThpDB(object): def __init__(self): - self._thp_root = os.environ.get("THP_INSTALL_ROOT") - if self._thp_root is None: - raise Exception("There is no THP_INSTALL_ROOT variable set") + self._thp_db_root = os.environ.get("THP_DB_ROOT") + if self._thp_db_root is None: + raise Exception("There is no THP_DB_ROOT variable set")
def insert(self, pkg): - thandy.util.replaceFile(os.path.join(self._thp_root, - pkg['package_name']), - pkg) + thandy.util.replaceFile(os.path.join(self._thp_db_root, "pkg-status", + pkg['package_name'])+".json", + json.dumps(pkg))
def delete(self, pkg): try: - os.unlink(os.path.join(self._thp_root, - pkg['package_name'])) + os.unlink(os.path.join(self._thp_db_root, "pkg-status", + pkg['package_name'])+".json") except Exception as e: print e
@@ -31,7 +34,7 @@ class ThpDB(object): self.insert(pkg)
def exists(self, name): - fname = os.path.join(self._thp_root, name) + fname = os.path.join(self._thp_db_root, "pkg-status", name+".json") fexists = os.path.exists(fname)
version = -1 @@ -39,7 +42,17 @@ class ThpDB(object): contents = open(fname, "r").read() metadata = json.loads(contents) version = metadata['package_version'] - return exists, version + return fexists, version + + def statusInProgress(self, pkg): + thandy.util.replaceFile(os.path.join(self._thp_db_root, "pkg-status", + pkg['package_name']+".status"), + json.dumps({ "status" : "IN-PROGRESS" })) + + def statusInstalled(self, pkg): + thandy.util.replaceFile(os.path.join(self._thp_db_root, "pkg-status", + pkg['package_name']+".status"), + json.dumps({ "status" : "INSTALLED" }))
class ThpChecker(PS.Checker): def __init__(self, name, version): @@ -53,7 +66,7 @@ class ThpChecker(PS.Checker):
def getInstalledVersions(self): versions = [] - (exists, version) = self._db.exists(self._name): + (exists, version) = self._db.exists(self._name)
if exists: versions.append(version) @@ -64,7 +77,7 @@ class ThpChecker(PS.Checker): return self._version in self.getInstalledVersions()
class ThpInstaller(PS.Installer): - def __init__(self, relPath, installCommand, removeCommand=None): + def __init__(self, relPath): PS.Installer.__init__(self, relPath) self._db = ThpDB()
@@ -72,13 +85,45 @@ class ThpInstaller(PS.Installer): return "ThpInstaller(%r)" %(self._relPath)
def install(self): + print "Running thp installer", self._cacheRoot, self._relPath self._thp_root = os.environ.get("THP_INSTALL_ROOT") if self._thp_root is None: raise Exception("There is no THP_INSTALL_ROOT variable set")
+ pkg = ThpPackage(os.path.join(self._cacheRoot, self._relPath[1:])) + self._db.insert(pkg.getAll()) +# self._db.delete(pkg.getAll()) + print self._db.exists(pkg.get("package_name")) + # shutil.copytree()
def remove(self): - if self._removeCommand: - raise thandy.RemoveNotSupported() - self._runCommand(self._removeCommand) + print "Running thp remover" + +class ThpPackage(object): + def __init__(self, thp_path): + self._thp_path = thp_path + self._metadata = None + + self._process() + + def __repr__(self): + print "ThpPackage(%s)" % self._thp_path + + def _process(self): + tmpPath = tempfile.mkdtemp(suffix=str(time.time()), + prefix="thp") + + thpFile = zipfile.ZipFile(self._thp_path) + thpFile.extractall(tmpPath) + contents = open(os.path.join(tmpPath, "meta", "package.json")).read() + self._metadata = json.loads(contents) + + thandy.util.deltree(tmpPath) + + def get(self, key): + if self._metadata: + return self._metadata.get(key) + + def getAll(self): + return self._metadata