From 0ed848a9ea7a543192cef1ca78695a9046ee4e98 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Thu, 23 Jun 2011 21:35:32 -0500 Subject: pactest: create packages in memory This is similar to what was just done for the sync databases. Move a few pieces around so we never need to actually write out the filesystem to create a package, and simply stream the tarfile out from the data we've collected. Once again, a few newline addition hacks and other things have to be left in place in order not to break everything; this time however most of the assumptions are in pactest and not libalpm. Signed-off-by: Dan McGee --- test/pacman/pmdb.py | 3 +- test/pacman/pmpkg.py | 57 ++++++++++++++++++++++++------------ test/pacman/pmtest.py | 9 ++---- test/pacman/util.py | 80 ++++++++++++++++++++++++++++++--------------------- 4 files changed, 89 insertions(+), 60 deletions(-) diff --git a/test/pacman/pmdb.py b/test/pacman/pmdb.py index 06bc9a6f..90b817ac 100644 --- a/test/pacman/pmdb.py +++ b/test/pacman/pmdb.py @@ -229,8 +229,7 @@ class pmdb(object): path = os.path.join(self.dbdir, pkg.fullname()) util.mkdir(path) for name, data in entry.iteritems(): - filename = os.path.join(path, name) - util.mkfile(filename, data) + util.mkfile(path, name, data) if self.dbfile: tar = tarfile.open(self.dbfile, "w:gz") diff --git a/test/pacman/pmpkg.py b/test/pacman/pmpkg.py index 64c4f5ef..8e6d7c43 100644 --- a/test/pacman/pmpkg.py +++ b/test/pacman/pmpkg.py @@ -20,6 +20,7 @@ import os import tempfile import stat import shutil +from StringIO import StringIO import tarfile import util @@ -66,6 +67,7 @@ class pmpkg(object): "pre_upgrade": "", "post_upgrade": "", } + self.path = None def __str__(self): s = ["%s" % self.fullname()] @@ -106,16 +108,7 @@ class pmpkg(object): A package archive is generated in the location 'path', based on the data from the object. """ - self.path = os.path.join(path, self.filename()) - - curdir = os.getcwd() - tmpdir = tempfile.mkdtemp() - os.chdir(tmpdir) - - # Generate package file system - for f in self.files: - util.mkfile(f, f) - self.size += os.lstat(self.parse_filename(f))[stat.ST_SIZE] + archive_files = [] # .PKGINFO data = ["pkgname = %s" % self.name] @@ -143,23 +136,51 @@ class pmpkg(object): data.append("provides = %s" % i) for i in self.backup: data.append("backup = %s" % i) - util.mkfile(".PKGINFO", "\n".join(data)) + data.append("\n") + archive_files.append((".PKGINFO", "\n".join(data))) # .INSTALL if any(self.install.values()): - util.mkfile(".INSTALL", self.installfile()) + archive_files.append((".INSTALL", self.installfile())) - # safely create the dir + self.path = os.path.join(path, self.filename()) util.mkdir(os.path.dirname(self.path)) - # Generate package archive + # Generate package metadata tar = tarfile.open(self.path, "w:gz") - for i in os.listdir("."): - tar.add(i) + for name, data in archive_files: + info = tarfile.TarInfo(name) + info.size = len(data) + tar.addfile(info, StringIO(data)) + + # Generate package file system + for name in self.files: + fileinfo = util.getfileinfo(name) + info = tarfile.TarInfo(fileinfo["filename"]) + if fileinfo["hasperms"]: + info.mode = fileinfo["perms"] + if fileinfo["isdir"]: + info.type = tarfile.DIRTYPE + tar.addfile(info) + elif fileinfo["islink"]: + info.type = tarfile.SYMTYPE + info.linkname = fileinfo["link"] + tar.addfile(info) + else: + # TODO wow what a hack, adding a newline to match mkfile? + filedata = name + "\n" + info.size = len(filedata) + tar.addfile(info, StringIO(filedata)) + tar.close() - os.chdir(curdir) - shutil.rmtree(tmpdir) + def install_package(self, root): + """Install the package in the given root.""" + for f in self.files: + util.mkfile(root, f, f) + path = os.path.join(root, f) + if os.path.isfile(path): + os.utime(path, (355, 355)) def full_filelist(self): """Generate a list of package files. diff --git a/test/pacman/pmtest.py b/test/pacman/pmtest.py index 3f78ff92..e38d3a66 100644 --- a/test/pacman/pmtest.py +++ b/test/pacman/pmtest.py @@ -156,16 +156,11 @@ class pmtest(object): vprint(" Populating file system") for pkg in self.db["local"].pkgs: vprint("\tinstalling %s" % pkg.fullname()) - for f in pkg.files: - vprint("\t%s" % f) - path = os.path.join(self.root, f) - util.mkfile(path, f) - if os.path.isfile(path): - os.utime(path, (355, 355)) + pkg.install_package(self.root) for f in self.filesystem: vprint("\t%s" % f) + util.mkfile(self.root, f, f) path = os.path.join(self.root, f) - util.mkfile(path, f) if os.path.isfile(path): os.utime(path, (355, 355)) diff --git a/test/pacman/util.py b/test/pacman/util.py index 0cf0eabe..dbe416f2 100644 --- a/test/pacman/util.py +++ b/test/pacman/util.py @@ -47,49 +47,63 @@ def vprint(msg): # Methods to generate files # -def mkfile(name, data = ""): - isdir = 0 - islink = 0 - setperms = 0 - filename = name - link = "" - perms = "" - +def getfileinfo(filename): + data = { + 'changed': False, + 'isdir': False, + 'islink': False, + 'link': None, + 'hasperms': False, + 'perms': None, + } if filename[-1] == "*": + data["changed"] = True filename = filename.rstrip("*") if filename.find(" -> ") != -1: - islink = 1 filename, link = filename.split(" -> ") + data["islink"] = True + data["link"] = link elif filename.find("|") != -1: - setperms = 1 filename, perms = filename.split("|") + data["hasperms"] = True + data["perms"] = int(perms, 8) if filename[-1] == "/": - isdir = 1 + data["isdir"] = True - if isdir: - path = filename - else: - path = os.path.dirname(filename) - if path and not os.path.isdir(path): - os.makedirs(path, 0755) + data["filename"] = filename + return data + +def mkfile(base, name, data=""): + info = getfileinfo(name) + filename = info["filename"] - if isdir: + path = os.path.join(base, filename) + if info["isdir"]: + if not os.path.isdir(path): + os.makedirs(path, 0755) return - if islink: - curdir = os.getcwd() - if path: - os.chdir(path) - os.symlink(link, os.path.basename(filename)) - os.chdir(curdir) + + dir_path = os.path.dirname(path) + if dir_path and not os.path.isdir(dir_path): + os.makedirs(dir_path, 0755) + + if info["islink"]: + os.symlink(info["link"], path) else: - fd = file(filename, "w") - if data: - fd.write(data) - if data[-1] != "\n": - fd.write("\n") - fd.close() - if setperms: - os.chmod(filename, int(perms, 8)) + writedata(path, data) + + if info["perms"]: + os.chmod(path, info["perms"]) + +def writedata(filename, data): + if isinstance(data, list): + data = "\n".join(data) + fd = file(filename, "w") + if data: + fd.write(data) + if data[-1] != "\n": + fd.write("\n") + fd.close() def mkcfgfile(filename, root, option, db): # Options @@ -111,7 +125,7 @@ def mkcfgfile(filename, root, option, db): for optkey, optval in value.option.iteritems(): data.extend(["%s = %s" % (optkey, j) for j in optval]) - mkfile(os.path.join(root, filename), "\n".join(data)) + mkfile(root, filename, "\n".join(data)) # -- cgit v1.2.3-70-g09d2