|
| 1 | +import os |
| 2 | + |
| 3 | +from subprocess import * |
| 4 | + |
| 5 | +from .popenwrapper import Popen |
| 6 | + |
| 7 | +# Static class that allows the type of a file to be checked. |
| 8 | +class FileType(object): |
| 9 | + # Provides int -> str map |
| 10 | + revMap = { } |
| 11 | + |
| 12 | + @classmethod |
| 13 | + def getFileType(cls, fileName): |
| 14 | + # This is a hacky way of determining |
| 15 | + # the type of file we are looking at. |
| 16 | + # Maybe we should use python-magic instead? |
| 17 | + |
| 18 | + fileP = Popen(['file',os.path.realpath(fileName)], stdout=PIPE) |
| 19 | + output = fileP.communicate()[0] |
| 20 | + output = output.decode() |
| 21 | + if 'ELF' in output and 'executable' in output: |
| 22 | + return cls.ELF_EXECUTABLE |
| 23 | + if 'Mach-O' in output and 'executable' in output: |
| 24 | + return cls.MACH_EXECUTABLE |
| 25 | + elif 'ELF' in output and 'shared' in output: |
| 26 | + return cls.ELF_SHARED |
| 27 | + elif 'Mach-O' in output and 'dynamically linked shared' in output: |
| 28 | + return cls.MACH_SHARED |
| 29 | + elif 'current ar archive' in output: |
| 30 | + return cls.ARCHIVE |
| 31 | + elif 'ELF' in output and 'relocatable' in output: |
| 32 | + return cls.ELF_OBJECT |
| 33 | + elif 'Mach-O' in output and 'object' in output: |
| 34 | + return cls.MACH_OBJECT |
| 35 | + else: |
| 36 | + return cls.UNKNOWN |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def init(cls): |
| 40 | + for (index, name) in enumerate(('UNKNOWN', |
| 41 | + 'ELF_EXECUTABLE', |
| 42 | + 'ELF_OBJECT', |
| 43 | + 'ELF_SHARED', |
| 44 | + 'MACH_EXECUTABLE', |
| 45 | + 'MACH_OBJECT', |
| 46 | + 'MACH_SHARED', |
| 47 | + 'ARCHIVE')): |
| 48 | + setattr(cls, name, index) |
| 49 | + cls.revMap[index] = name |
| 50 | + |
| 51 | +# Initialise FileType static class |
| 52 | +FileType.init() |
0 commit comments