pactest: move filelist/backup generation into package object
[pacman-ng.git] / test / pacman / pmpkg.py
blob43edd8f0094937a07c961afad918f761a4512d49
1 #! /usr/bin/python
3 # Copyright (c) 2006 by Aurelien Foret <orelien@chez.com>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 import os
20 import tempfile
21 import stat
22 import shutil
23 import tarfile
25 import util
27 class pmpkg(object):
28 """Package object.
30 Object holding data from an Arch Linux package.
31 """
33 def __init__(self, name, version = "1.0-1"):
34 self.path = "" #the path of the generated package
35 # desc
36 self.name = name
37 self.version = version
38 self.desc = ""
39 self.groups = []
40 self.url = ""
41 self.license = []
42 self.arch = ""
43 self.builddate = ""
44 self.installdate = ""
45 self.packager = ""
46 self.size = 0
47 self.csize = 0
48 self.isize = 0
49 self.reason = 0
50 self.md5sum = "" # sync only
51 self.pgpsig = "" # sync only
52 self.replaces = []
53 self.depends = []
54 self.optdepends = []
55 self.conflicts = []
56 self.provides = []
57 # files
58 self.files = []
59 self.backup = []
60 # install
61 self.install = {
62 "pre_install": "",
63 "post_install": "",
64 "pre_remove": "",
65 "post_remove": "",
66 "pre_upgrade": "",
67 "post_upgrade": "",
70 def __str__(self):
71 s = ["%s" % self.fullname()]
72 s.append("description: %s" % self.desc)
73 s.append("url: %s" % self.url)
74 s.append("files: %s" % " ".join(self.files))
75 s.append("reason: %d" % self.reason)
76 return "\n".join(s)
78 def fullname(self):
79 """Long name of a package.
81 Returns a string formatted as follows: "pkgname-pkgver".
82 """
83 return "%s-%s" % (self.name, self.version)
85 def filename(self):
86 """File name of a package, including its extension.
88 Returns a string formatted as follows: "pkgname-pkgver.PKG_EXT_PKG".
89 """
90 return "%s%s" % (self.fullname(), util.PM_EXT_PKG)
92 @staticmethod
93 def parse_filename(name):
94 filename = name
95 if filename[-1] == "*":
96 filename = filename.rstrip("*")
97 if filename.find(" -> ") != -1:
98 filename, extra = filename.split(" -> ")
99 elif filename.find("|") != -1:
100 filename, extra = filename.split("|")
101 return filename
103 def makepkg(self, path):
104 """Creates an Arch Linux package archive.
106 A package archive is generated in the location 'path', based on the data
107 from the object.
109 self.path = os.path.join(path, self.filename())
111 curdir = os.getcwd()
112 tmpdir = tempfile.mkdtemp()
113 os.chdir(tmpdir)
115 # Generate package file system
116 for f in self.files:
117 util.mkfile(f, f)
118 self.size += os.lstat(self.parse_filename(f))[stat.ST_SIZE]
120 # .PKGINFO
121 data = ["pkgname = %s" % self.name]
122 data.append("pkgver = %s" % self.version)
123 data.append("pkgdesc = %s" % self.desc)
124 data.append("url = %s" % self.url)
125 data.append("builddate = %s" % self.builddate)
126 data.append("packager = %s" % self.packager)
127 data.append("size = %s" % self.size)
128 if self.arch:
129 data.append("arch = %s" % self.arch)
130 for i in self.license:
131 data.append("license = %s" % i)
132 for i in self.replaces:
133 data.append("replaces = %s" % i)
134 for i in self.groups:
135 data.append("group = %s" % i)
136 for i in self.depends:
137 data.append("depend = %s" % i)
138 for i in self.optdepends:
139 data.append("optdepend = %s" % i)
140 for i in self.conflicts:
141 data.append("conflict = %s" % i)
142 for i in self.provides:
143 data.append("provides = %s" % i)
144 for i in self.backup:
145 data.append("backup = %s" % i)
146 util.mkfile(".PKGINFO", "\n".join(data))
148 # .INSTALL
149 if any(self.install.values()):
150 util.mkinstallfile(".INSTALL", self.install)
152 # safely create the dir
153 util.mkdir(os.path.dirname(self.path))
155 # Generate package archive
156 tar = tarfile.open(self.path, "w:gz")
157 for i in os.listdir("."):
158 tar.add(i)
159 tar.close()
161 os.chdir(curdir)
162 shutil.rmtree(tmpdir)
164 def full_filelist(self):
165 """Generate a list of package files.
167 Each path is decomposed to generate the list of all directories leading
168 to the file.
170 Example with 'usr/local/bin/dummy':
171 The resulting list will be:
172 usr/
173 usr/local/
174 usr/local/bin/
175 usr/local/bin/dummy
177 file_set = set()
178 for name in self.files:
179 name = self.parse_filename(name)
180 file_set.add(name)
181 while "/" in name:
182 name, tmp = name.rsplit("/", 1)
183 file_set.add(name + "/")
184 return sorted(file_set)
186 def local_backup_entries(self):
187 return ["%s\t%s" % (self.parse_filename(i), util.mkmd5sum(i)) for i in self.backup]
189 # vim: set ts=4 sw=4 et: