diff options
author | Milo Casagrande <milo@ubuntu.com> | 2013-06-04 10:17:22 +0200 |
---|---|---|
committer | Milo Casagrande <milo@ubuntu.com> | 2013-06-04 10:17:22 +0200 |
commit | 69dc0a67073f98cf1286b42798bc0181adaa4f94 (patch) | |
tree | 874e4d10e05c87d6e0fcbdc19242e308d72f966e /linaro_image_tools/hwpack | |
parent | 680224c5223762066b7efa9d08756f77ac65385e (diff) |
Moved PackageUnpacker into its own file/class.
Diffstat (limited to 'linaro_image_tools/hwpack')
-rw-r--r-- | linaro_image_tools/hwpack/builder.py | 40 | ||||
-rw-r--r-- | linaro_image_tools/hwpack/package_unpacker.py | 66 |
2 files changed, 67 insertions, 39 deletions
diff --git a/linaro_image_tools/hwpack/builder.py b/linaro_image_tools/hwpack/builder.py index 45644f5..9be8750 100644 --- a/linaro_image_tools/hwpack/builder.py +++ b/linaro_image_tools/hwpack/builder.py @@ -22,7 +22,6 @@ import logging import errno import subprocess -import tempfile import os import shutil from glob import iglob @@ -39,6 +38,7 @@ from linaro_image_tools.hwpack.packages import ( LocalArchiveMaker, PackageFetcher, ) +from linaro_image_tools.hwpack.package_unpacker import PackageUnpacker from linaro_image_tools.hwpack.hwpack_fields import ( PACKAGE_FIELD, @@ -59,44 +59,6 @@ class ConfigFileMissing(Exception): "No such config file: '%s'" % self.filename) -class PackageUnpacker(object): - def __enter__(self): - self.tempdir = tempfile.mkdtemp() - return self - - def __exit__(self, type, value, traceback): - if self.tempdir is not None and os.path.exists(self.tempdir): - shutil.rmtree(self.tempdir) - - def get_path(self, package_file_name, file_name=''): - """Get package or file path in unpacker tmp dir.""" - package_dir = os.path.basename(package_file_name) - return os.path.join(self.tempdir, package_dir, file_name) - - def unpack_package(self, package_file_name): - # We could extract only a single file, but since dpkg will pipe - # the entire package through tar anyway we might as well extract all. - unpack_dir = self.get_path(package_file_name) - if not os.path.isdir(unpack_dir): - os.mkdir(unpack_dir) - p = cmd_runner.run(["tar", "-C", unpack_dir, "-xf", "-"], - stdin=subprocess.PIPE) - cmd_runner.run(["dpkg", "--fsys-tarfile", package_file_name], - stdout=p.stdin).communicate() - p.communicate() - - def get_file(self, package, file): - # File path passed here must not be absolute, or file from - # real filesystem will be referenced. - assert file and file[0] != '/' - self.unpack_package(package) - logger.debug("Unpacked package %s." % package) - temp_file = self.get_path(package, file) - assert os.path.exists(temp_file), "The file '%s' was " \ - "not found in the package '%s'." % (file, package) - return temp_file - - class HardwarePackBuilder(object): def __init__(self, config_path, version, local_debs, out_name=None): diff --git a/linaro_image_tools/hwpack/package_unpacker.py b/linaro_image_tools/hwpack/package_unpacker.py new file mode 100644 index 0000000..b976abf --- /dev/null +++ b/linaro_image_tools/hwpack/package_unpacker.py @@ -0,0 +1,66 @@ +# Copyright (C) 2010, 2011, 2013 Linaro +# +# This file is part of Linaro Image Tools. +# +# Linaro Image Tools is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# Linaro Image Tools is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Linaro Image Tools; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +# USA. + +import logging +import os +import tempfile + +from subprocess import PIPE +from shutil import rmtree + +from linaro_image_tools import cmd_runner + +logger = logging.getLogger(__name__) + + +class PackageUnpacker(object): + def __enter__(self): + self.tempdir = tempfile.mkdtemp() + return self + + def __exit__(self, type, value, traceback): + if self.tempdir is not None and os.path.exists(self.tempdir): + rmtree(self.tempdir) + + def get_path(self, package_file_name, file_name=''): + """Get package or file path in unpacker tmp dir.""" + package_dir = os.path.basename(package_file_name) + return os.path.join(self.tempdir, package_dir, file_name) + + def unpack_package(self, package_file_name): + # We could extract only a single file, but since dpkg will pipe + # the entire package through tar anyway we might as well extract all. + unpack_dir = self.get_path(package_file_name) + if not os.path.isdir(unpack_dir): + os.mkdir(unpack_dir) + p = cmd_runner.run(["tar", "-C", unpack_dir, "-xf", "-"], stdin=PIPE) + cmd_runner.run(["dpkg", "--fsys-tarfile", package_file_name], + stdout=p.stdin).communicate() + p.communicate() + + def get_file(self, package, file): + # File path passed here must not be absolute, or file from + # real filesystem will be referenced. + assert file and file[0] != '/' + self.unpack_package(package) + logger.debug("Unpacked package %s." % package) + temp_file = self.get_path(package, file) + assert os.path.exists(temp_file), "The file '%s' was " \ + "not found in the package '%s'." % (file, package) + return temp_file |