dex.py 1.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# -*- coding: utf-8 -*-
#
# diffoscope: in-depth comparison of files, archives, and directories
#
# Copyright © 2015 Reiner Herrmann <reiner@reiner-h.de>
#
# diffoscope 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 3 of the License, or
# (at your option) any later version.
#
# diffoscope 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
18
# along with diffoscope.  If not, see <https://www.gnu.org/licenses/>.
19 20 21

import re
import os.path
22
import logging
23
import subprocess
Chris Lamb's avatar
Chris Lamb committed
24

25
from diffoscope.tools import tool_required
26

27
from .utils.file import File
28
from .utils.archive import Archive
29

30 31
logger = logging.getLogger(__name__)

32 33 34 35 36 37

class DexContainer(Archive):
    @property
    def path(self):
        return self._path

38
    def open_archive(self):
39 40 41
        return self

    def close_archive(self):
42
        pass
43 44

    def get_member_names(self):
45
        return [self.get_compressed_content_name('.dex') + '.jar']
46 47 48 49 50

    @tool_required('enjarify')
    def extract(self, member_name, dest_dir):
        dest_path = os.path.join(dest_dir, member_name)
        logger.debug('dex extracting to %s', dest_path)
51
        subprocess.check_call(['enjarify', '-o', dest_path, self.source.path],
Mattia Rizzolo's avatar
Mattia Rizzolo committed
52
                              shell=False, stderr=None, stdout=subprocess.PIPE)
53 54
        return dest_path

Mattia Rizzolo's avatar
Mattia Rizzolo committed
55

56
class DexFile(File):
57
    DESCRIPTION = "Dalvik .dex files"
58
    FILE_TYPE_RE = re.compile(r'^Dalvik dex file .*\b')
59
    CONTAINER_CLASS = DexContainer