Merge lp:~milo/linaro-image-tools/bug1081747 into lp:linaro-image-tools/11.11

Proposed by Milo Casagrande
Status: Merged
Approved by: James Tunnicliffe
Approved revision: 626
Merged at revision: 625
Proposed branch: lp:~milo/linaro-image-tools/bug1081747
Merge into: lp:linaro-image-tools/11.11
Diff against target: 164 lines (+69/-41)
4 files modified
linaro_image_tools/hwpack/builder.py (+1/-39)
linaro_image_tools/hwpack/handler.py (+1/-1)
linaro_image_tools/hwpack/package_unpacker.py (+66/-0)
linaro_image_tools/hwpack/tests/test_builder.py (+1/-1)
To merge this branch: bzr merge lp:~milo/linaro-image-tools/bug1081747
Reviewer Review Type Date Requested Status
James Tunnicliffe (community) Approve
Review via email: mp+167265@code.launchpad.net

Description of the change

Refactored out the PackageUnpacker class in order to avoid unrelated imports
so that linaro-android-media-create can be run (from source) also under non
Debian-based system.

To post a comment you must log in.
Revision history for this message
James Tunnicliffe (dooferlad) wrote :

Looks good to me.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'linaro_image_tools/hwpack/builder.py'
--- linaro_image_tools/hwpack/builder.py 2013-02-18 13:05:58 +0000
+++ linaro_image_tools/hwpack/builder.py 2013-06-04 13:05:38 +0000
@@ -22,7 +22,6 @@
22import logging22import logging
23import errno23import errno
24import subprocess24import subprocess
25import tempfile
26import os25import os
27import shutil26import shutil
28from glob import iglob27from glob import iglob
@@ -39,6 +38,7 @@
39 LocalArchiveMaker,38 LocalArchiveMaker,
40 PackageFetcher,39 PackageFetcher,
41)40)
41from linaro_image_tools.hwpack.package_unpacker import PackageUnpacker
4242
43from linaro_image_tools.hwpack.hwpack_fields import (43from linaro_image_tools.hwpack.hwpack_fields import (
44 PACKAGE_FIELD,44 PACKAGE_FIELD,
@@ -59,44 +59,6 @@
59 "No such config file: '%s'" % self.filename)59 "No such config file: '%s'" % self.filename)
6060
6161
62class PackageUnpacker(object):
63 def __enter__(self):
64 self.tempdir = tempfile.mkdtemp()
65 return self
66
67 def __exit__(self, type, value, traceback):
68 if self.tempdir is not None and os.path.exists(self.tempdir):
69 shutil.rmtree(self.tempdir)
70
71 def get_path(self, package_file_name, file_name=''):
72 """Get package or file path in unpacker tmp dir."""
73 package_dir = os.path.basename(package_file_name)
74 return os.path.join(self.tempdir, package_dir, file_name)
75
76 def unpack_package(self, package_file_name):
77 # We could extract only a single file, but since dpkg will pipe
78 # the entire package through tar anyway we might as well extract all.
79 unpack_dir = self.get_path(package_file_name)
80 if not os.path.isdir(unpack_dir):
81 os.mkdir(unpack_dir)
82 p = cmd_runner.run(["tar", "-C", unpack_dir, "-xf", "-"],
83 stdin=subprocess.PIPE)
84 cmd_runner.run(["dpkg", "--fsys-tarfile", package_file_name],
85 stdout=p.stdin).communicate()
86 p.communicate()
87
88 def get_file(self, package, file):
89 # File path passed here must not be absolute, or file from
90 # real filesystem will be referenced.
91 assert file and file[0] != '/'
92 self.unpack_package(package)
93 logger.debug("Unpacked package %s." % package)
94 temp_file = self.get_path(package, file)
95 assert os.path.exists(temp_file), "The file '%s' was " \
96 "not found in the package '%s'." % (file, package)
97 return temp_file
98
99
100class HardwarePackBuilder(object):62class HardwarePackBuilder(object):
10163
102 def __init__(self, config_path, version, local_debs, out_name=None):64 def __init__(self, config_path, version, local_debs, out_name=None):
10365
=== modified file 'linaro_image_tools/hwpack/handler.py'
--- linaro_image_tools/hwpack/handler.py 2013-02-18 13:05:58 +0000
+++ linaro_image_tools/hwpack/handler.py 2013-06-04 13:05:38 +0000
@@ -27,7 +27,7 @@
27import tempfile27import tempfile
2828
29from linaro_image_tools.hwpack.config import Config29from linaro_image_tools.hwpack.config import Config
30from linaro_image_tools.hwpack.builder import PackageUnpacker30from linaro_image_tools.hwpack.package_unpacker import PackageUnpacker
31from linaro_image_tools.utils import DEFAULT_LOGGER_NAME31from linaro_image_tools.utils import DEFAULT_LOGGER_NAME
3232
3333
3434
=== added file 'linaro_image_tools/hwpack/package_unpacker.py'
--- linaro_image_tools/hwpack/package_unpacker.py 1970-01-01 00:00:00 +0000
+++ linaro_image_tools/hwpack/package_unpacker.py 2013-06-04 13:05:38 +0000
@@ -0,0 +1,66 @@
1# Copyright (C) 2010, 2011, 2013 Linaro
2#
3# This file is part of Linaro Image Tools.
4#
5# Linaro Image Tools is free software; you can redistribute it and/or
6# modify it under the terms of the GNU General Public License
7# as published by the Free Software Foundation; either version 2
8# of the License, or (at your option) any later version.
9#
10# Linaro Image Tools is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with Linaro Image Tools; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18# USA.
19
20import logging
21import os
22import tempfile
23
24from subprocess import PIPE
25from shutil import rmtree
26
27from linaro_image_tools import cmd_runner
28
29logger = logging.getLogger(__name__)
30
31
32class PackageUnpacker(object):
33 def __enter__(self):
34 self.tempdir = tempfile.mkdtemp()
35 return self
36
37 def __exit__(self, type, value, traceback):
38 if self.tempdir is not None and os.path.exists(self.tempdir):
39 rmtree(self.tempdir)
40
41 def get_path(self, package_file_name, file_name=''):
42 """Get package or file path in unpacker tmp dir."""
43 package_dir = os.path.basename(package_file_name)
44 return os.path.join(self.tempdir, package_dir, file_name)
45
46 def unpack_package(self, package_file_name):
47 # We could extract only a single file, but since dpkg will pipe
48 # the entire package through tar anyway we might as well extract all.
49 unpack_dir = self.get_path(package_file_name)
50 if not os.path.isdir(unpack_dir):
51 os.mkdir(unpack_dir)
52 p = cmd_runner.run(["tar", "-C", unpack_dir, "-xf", "-"], stdin=PIPE)
53 cmd_runner.run(["dpkg", "--fsys-tarfile", package_file_name],
54 stdout=p.stdin).communicate()
55 p.communicate()
56
57 def get_file(self, package, file):
58 # File path passed here must not be absolute, or file from
59 # real filesystem will be referenced.
60 assert file and file[0] != '/'
61 self.unpack_package(package)
62 logger.debug("Unpacked package %s." % package)
63 temp_file = self.get_path(package, file)
64 assert os.path.exists(temp_file), "The file '%s' was " \
65 "not found in the package '%s'." % (file, package)
66 return temp_file
067
=== modified file 'linaro_image_tools/hwpack/tests/test_builder.py'
--- linaro_image_tools/hwpack/tests/test_builder.py 2013-02-17 13:53:41 +0000
+++ linaro_image_tools/hwpack/tests/test_builder.py 2013-06-04 13:05:38 +0000
@@ -27,10 +27,10 @@
2727
28from linaro_image_tools.hwpack.builder import (28from linaro_image_tools.hwpack.builder import (
29 ConfigFileMissing,29 ConfigFileMissing,
30 PackageUnpacker,
31 HardwarePackBuilder,30 HardwarePackBuilder,
32 logger as builder_logger,31 logger as builder_logger,
33)32)
33from linaro_image_tools.hwpack.package_unpacker import PackageUnpacker
34from linaro_image_tools.hwpack.config import HwpackConfigError34from linaro_image_tools.hwpack.config import HwpackConfigError
35from linaro_image_tools.hwpack.hardwarepack import Metadata35from linaro_image_tools.hwpack.hardwarepack import Metadata
36from linaro_image_tools.hwpack.packages import (36from linaro_image_tools.hwpack.packages import (

Subscribers

People subscribed via source and target branches