The apt_inst extension provides access to functions for working with locally available Debian packages (.deb files) and tar files.
Call the function referenced by func for each member of the tar file chunk which is contained in the AR file referenced by the file object file.
An example would be:
debExtract(open("package.deb"), my_callback, "data.tar.gz")
See Example: Emulating dpkg –contents for a more detailed example.
Call the function func for each member of the tar file file.
Comp is a string determining the compressor used. Possible options are “lzma”, “bzip2” and “gzip”.
Both of these functions expect a callback with the signature (what, name, link, mode, uid, gid, size, mtime, major, minor).
The parameter what describes the type of the member. It can be ‘FILE’, ‘DIR’, or ‘HARDLINK’.
The parameter name refers to the name of the member. In case of links, link refers to the target of the link.
Extract the archive referenced by the file object file into the directory specified by rootdir.
See Example: Emulating dpkg –extract for an example.
Warning
If the directory given by rootdir does not exist, the package is extracted into the current directory.
Return the indicated file from the control tar. The default is ‘control’.
If you want to print the control file of a given package, you could do something like:
print debExtractControl(open("package.deb"))
Returns: | The contents of the file, as str. |
---|
Here is a code snippet which emulates dpkg -x. It can be run as tool pkg.deb outdir.
#!/usr/bin/python
"""Emulate dpkg --extract package.deb outdir"""
import os
import sys
import apt_inst
def main():
"""Main function."""
if len(sys.argv) < 3:
print >> sys.stderr, "Usage:", __file__, "package.deb outdir"
sys.exit(1)
if not os.path.exists(sys.argv[2]):
print >> sys.stderr, "The directory %s does not exist" % sys.argv[2]
sys.exit(1)
fobj = open(sys.argv[1])
try:
apt_inst.debExtractArchive(fobj, sys.argv[2])
finally:
fobj.close()
if __name__ == "__main__":
main()
#!/usr/bin/python
"""Emulate dpkg --contents"""
import grp
import pwd
import stat
import sys
import time
import apt_inst
def format_mode(what, mode):
"""Return the symbolic mode"""
s_mode = dict(DIR="d", HARDLINK="h", FILE="-").get(what)
s_mode += ((mode & stat.S_IRUSR) and "r" or "-")
s_mode += ((mode & stat.S_IWUSR) and "w" or "-")
s_mode += ((mode & stat.S_IXUSR) and (mode & stat.S_ISUID and "s" or "x")
or (mode & stat.S_ISUID and "S" or "-"))
s_mode += ((mode & stat.S_IRGRP) and "r" or "-")
s_mode += ((mode & stat.S_IWGRP) and "w" or "-")
s_mode += ((mode & stat.S_IXGRP) and (mode & stat.S_ISGID and "s" or "x")
or (mode & stat.S_ISGID and "S" or "-"))
s_mode += ((mode & stat.S_IROTH) and "r" or "-")
s_mode += ((mode & stat.S_IWOTH) and "w" or "-")
s_mode += ((mode & stat.S_IXOTH) and "x" or "-")
return s_mode
def callback(what, name, link, mode, uid, gid, size, mtime, major, minor):
"""callback for debExtract"""
s_mode = format_mode(what, mode)
s_owner = "%s/%s" % (pwd.getpwuid(uid)[0], grp.getgrgid(gid)[0])
s_size = "%9d" % size
s_time = time.strftime("%Y-%m-%d %H:%M", time.localtime(mtime))
s_name = name.startswith(".") and name or ("./" + name)
if link:
s_name += " link to %s" % link
print s_mode, s_owner, s_size, s_time, s_name
def main():
"""Main function"""
if len(sys.argv) < 2:
print >> sys.stderr, "need filename argumnet"
sys.exit(1)
fobj = open(sys.argv[1])
try:
apt_inst.debExtract(fobj, callback, "data.tar.gz")
finally:
fobj.close()
if __name__ == "__main__":
main()
#!/usr/bin/python
"""Emulate dpkg --info package.deb control-file"""
import sys
from apt_inst import debExtractControl
def main():
"""Main function."""
if len(sys.argv) < 3:
print >> sys.stderr, 'Usage: tool file.deb control-file'
sys.exit(0)
fobj = open(sys.argv[1])
try:
print debExtractControl(fobj, sys.argv[2])
finally:
fobj.close()
if __name__ == '__main__':
main()