]>
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 # XXX this is totally ugly and needs to be done right!
27 class Packager(object):
28 ARCHIVE_FILES
= ("info", "filelist", "data.img")
30 def __init__(self
, pakfire
, pkg
, env
):
31 self
.pakfire
= pakfire
37 # Store meta information
39 "package_format" : PACKAGE_FORMAT
,
40 "package_type" : "binary",
41 "package_uuid" : uuid
.uuid4(),
42 "payload_comp" : None,
44 self
.info
.update(self
.pkg
.info
)
45 self
.info
.update(self
.pakfire
.distro
.info
)
46 self
.info
.update(self
.env
.info
)
48 ### Create temporary files
49 # Create temp directory to where we extract all files again and
50 # gather some information about them like requirements and provides.
51 self
.tempdir
= self
.env
.chrootPath("tmp", "%s_data" % self
.pkg
.friendly_name
)
52 if not os
.path
.exists(self
.tempdir
):
53 os
.makedirs(self
.tempdir
)
55 # Create files that have the archive data
56 self
.archive_files
= {}
57 for i
in self
.ARCHIVE_FILES
:
58 self
.archive_files
[i
] = \
59 self
.env
.chrootPath("tmp", "%s_%s" % (self
.pkg
.friendly_name
, i
))
62 logging
.debug("Packaging %s" % self
.pkg
.friendly_name
)
64 # Create the tarball and add all data to it.
67 e
= self
.env
.do("/usr/lib/buildsystem-tools/dependency-tracker %s" % \
68 self
.tempdir
[len(self
.env
.chrootPath()):], returnOutput
=True,
71 for line
in e
.splitlines():
72 m
= re
.match(r
"^(\w+)=(.*)$", line
)
78 if not key
in ("requires", "provides"):
84 self
.info
[key
] = " ".join(sorted(val
))
88 # Create the outer tarball.
89 resultdir
= os
.path
.join(self
.env
.chrootPath("result", self
.pkg
.arch
))
90 if not os
.path
.exists(resultdir
):
91 os
.makedirs(resultdir
)
93 filename
= os
.path
.join(resultdir
, self
.pkg
.filename
)
95 tar
= tarfile
.TarFile(filename
, mode
="w", format
=tarfile
.PAX_FORMAT
)
97 for i
in self
.ARCHIVE_FILES
:
98 tar
.add(self
.archive_files
[i
], arcname
=i
)
102 def create_tarball(self
, compress
="xz"):
103 tar
= InnerTarFile(self
.archive_files
["data.img"], mode
="w")
108 for pattern
in self
.pkg
.file_patterns
:
109 # Check if we are running in include or exclude mode.
110 if pattern
.startswith("!"):
113 # Strip the ! charater
114 pattern
= pattern
[1:]
119 if pattern
.startswith("/"):
120 pattern
= pattern
[1:]
121 pattern
= self
.env
.chrootPath(self
.env
.buildroot
, pattern
)
123 # Recognize the type of the pattern. Patterns could be a glob
124 # pattern that is expanded here or just a directory which will
125 # be included recursively.
126 if "*" in pattern
or "?" in pattern
:
127 files
+= glob
.glob(pattern
)
129 elif os
.path
.exists(pattern
):
130 # Add directories recursively...
131 if os
.path
.isdir(pattern
):
132 for dir, subdirs
, _files
in os
.walk(pattern
):
134 file = os
.path
.join(dir, file)
137 # all other files are just added.
139 files
.append(pattern
)
142 for file in includes
:
143 # Skip if file is already in the file set or
144 # marked to be excluded from this archive.
145 if file in excludes
or file in files
:
152 filelist
= open(self
.archive_files
["filelist"], mode
="w")
154 for file_real
in files
:
155 file_tar
= file_real
[len(self
.env
.chrootPath(self
.env
.buildroot
)) + 1:]
156 file_tmp
= os
.path
.join(self
.tempdir
, file_tar
)
158 if file_tar
in ORPHAN_DIRECTORIES
and not os
.listdir(file_real
):
159 logging
.debug("Found an orphaned directory: %s" % file_tar
)
163 tar
.add(file_real
, arcname
=file_tar
)
165 # Record the packaged file to the filelist.
166 filelist
.write("/%s\n" % file_tar
)
168 # "Copy" the file to the tmp path for later investigation.
169 if os
.path
.isdir(file_real
):
172 file_dir
= os
.path
.dirname(file_tmp
)
174 if not os
.path
.exists(file_dir
):
175 os
.makedirs(file_dir
)
177 if os
.path
.isfile(file_real
):
178 os
.link(file_real
, file_tmp
)
180 elif os
.path
.islink(file_real
):
181 # Dead symlinks cannot be copied by shutil.
182 os
.symlink(os
.readlink(file_real
), file_tmp
)
184 elif os
.path
.isdir(file_real
):
185 if not os
.path
.exists(file_tmp
):
186 os
.makedirs(file_tmp
)
189 shutil
.copy2(file_real
, file_tmp
)
191 # Unlink the file and remove empty directories.
192 if not os
.path
.isdir(file_real
):
195 elif os
.path
.isdir(file_real
) and not os
.listdir(file_real
):
198 # Dump all files that are in the archive.
201 # Write all data to disk.
205 # compress the tarball here
207 # Save algorithm to metadata.
208 self
.info
["payload_comp"] = compress
210 logging
.debug("Compressing package with %s algorithm." % compress
or "no")
212 # Compress file (in place).
213 pakfire
.compress
.compress(self
.archive_files
["data.img"],
214 algo
=compress
, progress
=True)
216 # Calc hashsum of the payload of the package.
217 self
.info
["payload_hash1"] = util
.calc_hash1(self
.archive_files
["data.img"])
219 def create_info(self
):
220 f
= open(self
.archive_files
["info"], "w")
221 f
.write(BINARY_PACKAGE_META
% self
.info
)