1
# jhbuild - a build script for GNOME 1.x and 2.x
2
# Copyright (C) 2007 Alberto Ruiz <aruiz@gnome.org>
4
# unpack.py: helper functions for unpacking compressed packages
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
from jhbuild.utils.cmds import has_command
25
from jhbuild.errors import CommandError
28
def unpack_tar_file(localfile, target_directory):
29
pkg = tarfile.open(localfile, 'r|*')
30
pkg.extractall(target_directory)
34
def unpack_zip_file(localfile, target_directory):
35
# Attributes are stored in ZIP files in a host-dependent way.
36
# The zipinfo.create_system value describes the host OS.
39
# * 11 (undefined, seems to be UNIX)
41
# Reference: http://www.opennet.ru/docs/formats/zip.txt
43
def attr_check_symlink(host, attr):
46
return attr == 0xA1ED0000
48
def attr_to_file_perm(host, attr):
61
def attr_to_dir_perm(host, attr):
63
# attr & 16 should be true (this is directory bit)
76
if not os.path.isdir(dir):
79
pkg = zipfile.ZipFile(localfile, 'r')
80
for pkg_fileinfo in pkg.filelist:
81
pkg_file = pkg_fileinfo.filename
82
attr = pkg_fileinfo.external_attr
83
chost = pkg_fileinfo.create_system
86
if attr_check_symlink(chost, attr):
87
# TODO: support symlinks in zipfiles
91
if pkg_file.endswith('/'):
92
dir = os.path.join(target_directory, pkg_file)
94
os.chmod(dir, attr_to_dir_perm(chost, attr))
99
dir = os.path.dirname(pkg_file)
100
dir = os.path.join(target_directory, dir)
103
data = pkg.read(pkg_file)
104
file = open(os.path.join(target_directory, pkg_file), 'wb')
108
os.chmod(os.path.join(target_directory, pkg_file), attr_to_file_perm(chost, attr))
111
def unpack_archive(buildscript, localfile, target_directory):
112
ext = os.path.splitext(localfile)[-1]
113
if ext == '.lzma' and has_command('lzcat') and has_command('tar'):
114
buildscript.execute('lzcat -d "%s" | tar xf -' % localfile,
115
cwd = target_directory)
116
if ext == '.bz2' and has_command('bunzip2') and has_command('tar'):
117
buildscript.execute('bunzip2 -dc "%s" | tar xf -' % localfile,
118
cwd = target_directory)
119
elif ext in ('.gz', '.tgz') and has_command('gunzip') and has_command('tar'):
120
buildscript.execute('gunzip -dc "%s" | tar xf -' % localfile,
121
cwd = target_directory)
122
elif ext == '.zip' and has_command('unzip'):
123
buildscript.execute('unzip "%s"' % localfile,
124
cwd = target_directory)
127
if tarfile.is_tarfile(localfile):
128
unpack_tar_file(localfile, target_directory)
129
elif zipfile.is_zipfile(localfile):
130
unpack_zip_file(localfile, target_directory)
132
raise CommandError(_('Failed to unpack %s (unknown archive type)') % localfile)
134
raise CommandError(_('Failed to unpack %s') % localfile)