]>
git.ipfire.org Git - pakfire.git/blob - pakfire/packages/packager.py
17 import pakfire
.compress
20 from pakfire
.constants
import *
21 from pakfire
.i18n
import _
23 from file import InnerTarFile
25 class Packager(object):
26 ARCHIVE_FILES
= ("info", "filelist", "data.img")
28 def __init__(self
, pakfire
, pkg
, env
):
29 self
.pakfire
= pakfire
37 # Store meta information
39 "package_format" : PACKAGE_FORMAT
,
40 "package_type" : self
.type,
41 "package_uuid" : uuid
.uuid4(),
47 self
.info
.update(self
.pkg
.info
)
48 self
.info
["groups"] = " ".join(self
.info
["groups"])
49 self
.info
.update(self
.pakfire
.distro
.info
)
50 self
.info
.update(self
.env
.info
)
52 ### Create temporary files
53 # Create temp directory to where we extract all files again and
54 # gather some information about them like requirements and provides.
55 self
.tempdir
= self
.env
.chrootPath("tmp", "%s_data" % self
.pkg
.friendly_name
)
56 if not os
.path
.exists(self
.tempdir
):
57 os
.makedirs(self
.tempdir
)
59 # Create files that have the archive data
60 self
.archive_files
= {}
61 for i
in self
.ARCHIVE_FILES
:
62 self
.archive_files
[i
] = \
63 self
.env
.chrootPath("tmp", "%s_%s" % (self
.pkg
.friendly_name
, i
))
66 logging
.debug("Packaging %s" % self
.pkg
.friendly_name
)
68 # Create the tarball and add all data to it.
71 if self
.type == "binary":
72 e
= self
.env
.do("/usr/lib/buildsystem-tools/dependency-tracker %s" % \
73 self
.tempdir
[len(self
.env
.chrootPath()):], returnOutput
=True,
76 for line
in e
.splitlines():
77 m
= re
.match(r
"^(\w+)=(.*)$", line
)
83 if not key
in ("requires", "provides"):
89 self
.info
[key
] = " ".join(sorted(val
))
93 # Create the outer tarball.
94 resultdir
= os
.path
.join(self
.env
.chrootPath("result", self
.pkg
.arch
))
95 if not os
.path
.exists(resultdir
):
96 os
.makedirs(resultdir
)
98 filename
= os
.path
.join(resultdir
, self
.pkg
.filename
)
100 tar
= tarfile
.TarFile(filename
, mode
="w", format
=tarfile
.PAX_FORMAT
)
102 for i
in self
.ARCHIVE_FILES
:
103 tar
.add(self
.archive_files
[i
], arcname
=i
)
107 def create_tarball(self
, compress
=None):
108 tar
= InnerTarFile(self
.archive_files
["data.img"], mode
="w")
110 prefix
= self
.env
.buildroot
111 if self
.type == "source":
114 if not compress
and self
.type == "binary":
120 for pattern
in self
.pkg
.file_patterns
:
121 # Check if we are running in include or exclude mode.
122 if pattern
.startswith("!"):
125 # Strip the ! charater
126 pattern
= pattern
[1:]
131 if pattern
.startswith("/"):
132 pattern
= pattern
[1:]
133 pattern
= self
.env
.chrootPath(prefix
, pattern
)
135 # Recognize the type of the pattern. Patterns could be a glob
136 # pattern that is expanded here or just a directory which will
137 # be included recursively.
138 if "*" in pattern
or "?" in pattern
:
139 files
+= glob
.glob(pattern
)
141 elif os
.path
.exists(pattern
):
142 # Add directories recursively...
143 if os
.path
.isdir(pattern
):
144 for dir, subdirs
, _files
in os
.walk(pattern
):
146 file = os
.path
.join(dir, file)
149 # all other files are just added.
151 files
.append(pattern
)
154 for file in includes
:
155 # Skip if file is already in the file set or
156 # marked to be excluded from this archive.
157 if file in excludes
or file in files
:
164 filelist
= open(self
.archive_files
["filelist"], mode
="w")
166 for file_real
in files
:
167 file_tar
= file_real
[len(self
.env
.chrootPath(prefix
)) + 1:]
168 file_tmp
= os
.path
.join(self
.tempdir
, file_tar
)
170 if file_tar
in ORPHAN_DIRECTORIES
and not os
.listdir(file_real
):
171 logging
.debug("Found an orphaned directory: %s" % file_tar
)
175 tar
.add(file_real
, arcname
=file_tar
)
177 # Record the packaged file to the filelist.
178 filelist
.write("/%s\n" % file_tar
)
180 # "Copy" the file to the tmp path for later investigation.
181 if os
.path
.isdir(file_real
):
184 file_dir
= os
.path
.dirname(file_tmp
)
186 if not os
.path
.exists(file_dir
):
187 os
.makedirs(file_dir
)
189 if os
.path
.isfile(file_real
):
190 os
.link(file_real
, file_tmp
)
192 elif os
.path
.islink(file_real
):
193 # Dead symlinks cannot be copied by shutil.
194 os
.symlink(os
.readlink(file_real
), file_tmp
)
196 elif os
.path
.isdir(file_real
):
197 if not os
.path
.exists(file_tmp
):
198 os
.makedirs(file_tmp
)
201 shutil
.copy2(file_real
, file_tmp
)
203 # Unlink the file and remove empty directories.
205 if not os
.path
.isdir(file_real
):
208 elif os
.path
.isdir(file_real
) and not os
.listdir(file_real
):
211 # Dump all files that are in the archive.
214 # Write all data to disk.
218 # compress the tarball here
220 # Save algorithm to metadata.
221 self
.info
["payload_comp"] = compress
223 logging
.debug("Compressing package with %s algorithm." % compress
or "no")
225 # Compress file (in place).
226 pakfire
.compress
.compress(self
.archive_files
["data.img"],
227 algo
=compress
, progress
=True)
229 # Calc hashsum of the payload of the package.
230 self
.info
["payload_hash1"] = util
.calc_hash1(self
.archive_files
["data.img"])
232 def create_info(self
):
233 f
= open(self
.archive_files
["info"], "w")
234 f
.write(BINARY_PACKAGE_META
% self
.info
)
239 raise NotImplementedError
242 class BinaryPackager(Packager
):
248 class SourcePackager(Packager
):
249 def __init__(self
, *args
, **kwargs
):
250 Packager
.__init
__(self
, *args
, **kwargs
)