Skip to content

Commit 9c6140d

Browse files
pkgutil -> inspect + Path
As per elicn's comments. TODO: If Qiling allows 3.9 standard Libs, move everything to importlib.resources.files
1 parent 719b98a commit 9c6140d

File tree

4 files changed

+33
-27
lines changed

4 files changed

+33
-27
lines changed

qiling/arch/evm/analysis/signatures.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import pkgutil
1+
import inspect
2+
from pathlib import Path
23
import re
34
import logging
45
import json
@@ -92,8 +93,9 @@ def analysis_func_sign(insns:list, engine_num=1):
9293
class signatures_engine_1:
9394
@staticmethod
9495
def find_signature(sign):
95-
data = pkgutil.get_data(__package__, 'signatures.json').decode()
96-
data = json.load(data)
96+
path = Path(inspect.getfile(inspect.getframe())).parent / 'signatures.json'
97+
with path.open('r') as data_file:
98+
data = json.load(data_file)
9799

98100
list_name = [name for name, hexa in data.items() if hexa == sign]
99101

qiling/debugger/gdb/gdb.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
# documentation: according to https://sourceware.org/gdb/current/onlinedocs/gdb/Remote-Protocol.html#Remote-Protocol
88

99
import struct, os, socket
10-
import pkgutil
10+
import inspect
11+
from pathlib import Path
1112
from binascii import unhexlify
1213
from typing import Iterator, Literal
1314

@@ -482,15 +483,14 @@ def handle_q(subcmd):
482483
else:
483484
self.send("PacketSize=47ff;QPassSignals+;QProgramSignals+;QStartupWithShell+;QEnvironmentHexEncoded+;QEnvironmentReset+;QEnvironmentUnset+;QSetWorkingDir+;QCatchSyscalls+;qXfer:libraries-svr4:read+;augmented-libraries-svr4-read+;qXfer:auxv:read+;qXfer:siginfo:read+;qXfer:siginfo:write+;qXfer:features:read+;QStartNoAckMode+;qXfer:osdata:read+;multiprocess+;fork-events+;vfork-events+;exec-events+;QNonStop+;QDisableRandomization+;qXfer:threads:read+;ConditionalTracepoints+;TraceStateVariables+;TracepointSource+;DisconnectedTracing+;FastTracepoints+;StaticTracepoints+;InstallInTrace+;qXfer:statictrace:read+;qXfer:traceframe-info:read+;EnableDisableTracepoints+;QTBuffer:size+;tracenz+;ConditionalBreakpoints+;BreakpointCommands+;QAgent+;Qbtrace:bts+;Qbtrace-conf:bts:size+;Qbtrace:pt+;Qbtrace-conf:pt:size+;Qbtrace:off+;qXfer:btrace:read+;qXfer:btrace-conf:read+;swbreak+;hwbreak+;qXfer:exec-file:read+;vContSupported+;QThreadEvents+;no-resumed+")
484485
elif subcmd.startswith('Xfer:features:read'):
485-
if self.ql.os.type is not QL_OS.WINDOWS:
486-
try:
487-
xfercmd_file = subcmd.split(':')[3]
488-
xml_folder = self.ql.arch.type.name.lower()
489-
file_contents = pkgutil.get_data(__package__, f"xml/{xml_folder}/{xfercmd_file}").decode()
490-
self.send("l%s" % file_contents)
491-
except:
492-
self.ql.log.info("gdb> Platform is not supported by xml or xml file not found: %s\n" % (xfercmd_file))
493-
self.send("l")
486+
xfercmd_file = subcmd.split(':')[3]
487+
xfercmd_abspath = Path(inspect.getfile(inspect.currentframe())).parent
488+
xml_folder = self.ql.arch.type.name.lower()
489+
xfercmd_file = xfercmd_abspath / 'xml' / xml_folder / xfercmd_file
490+
491+
if xfercmd_file.exists() and self.ql.os.type is not QL_OS.WINDOWS:
492+
with xfercmd_file.open('r') as f:
493+
file_contents = f.read()
494494
else:
495495
self.ql.log.info("gdb> Platform is not supported by xml or xml file not found: %s\n" % (xfercmd_file))
496496
self.send("l")

qiling/os/uefi/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import csv
22
from typing import Mapping
3-
import pkgutil
3+
import inspect
4+
from pathlib import Path
45

56
def __init_guids_db() -> Mapping[str, str]:
67
"""Initialize GUIDs dictionary from a local database.
78
"""
89

9-
guids_file = pkgutil.get_data(__package__, 'guids.csv').decode()
10-
guids_reader = csv.reader(guids_file.splitlines())
10+
csv_path = Path(inspect.getfile(inspect.currentframe())).parent / 'guids.csv'
1111

12-
return dict(tuple(entry) for entry in guids_reader)
12+
with csv_path.open('r') as guids_file:
13+
guids_reader = csv.reader(guids_file)
14+
15+
return dict(tuple(entry) for entry in guids_reader)
1316

1417
guids_db = __init_guids_db()

qiling/utils.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"""
1010

1111
from functools import partial
12-
import importlib, pkgutil, os
12+
from pathlib import Path
13+
import importlib, inspect, os
1314

1415
from configparser import ConfigParser
1516
from types import ModuleType
@@ -415,19 +416,19 @@ def profile_setup(ostype: QL_OS, filename: Optional[str]):
415416
config = {}
416417

417418
else:
419+
qiling_home = Path(inspect.getfile(inspect.currentframe())).parent
420+
os_profile = qiling_home / 'profiles' / f'{ostype.name.lower()}.ql'
421+
422+
profiles = [os_profile]
423+
424+
if filename:
425+
profiles.append(filename)
426+
418427
# patch 'getint' to convert integers of all bases
419428
int_converter = partial(int, base=0)
420429

421430
config = ConfigParser(converters={'int': int_converter})
422-
423-
try:
424-
os_profile = pkgutil.get_data(__package__, f'profiles/{ostype.name.lower()}.ql').decode()
425-
config.read_string(os_profile)
426-
except FileNotFoundError as e:
427-
pass
428-
429-
if filename:
430-
config.read(filename)
431+
config.read(profiles)
431432

432433
return config
433434

0 commit comments

Comments
 (0)