diff --git a/scapy/main.py b/scapy/main.py index 0b33007b0f2..2cb176ceee1 100644 --- a/scapy/main.py +++ b/scapy/main.py @@ -64,27 +64,18 @@ ] -def _probe_config_file(*cf, default=None): - # type: (str, Optional[str]) -> Union[str, None] +def _probe_config_file(*cf): + # type: (str) -> Union[str, None] path = pathlib.Path(os.path.expanduser("~")) if not path.exists(): # ~ folder doesn't exist. Unsalvageable return None - cf_path = path.joinpath(*cf) - if not cf_path.exists(): - if default is not None: - # We have a default ! set it - cf_path.parent.mkdir(parents=True, exist_ok=True) - with cf_path.open("w") as fd: - fd.write(default) - return str(cf_path.resolve()) - return None - return str(cf_path.resolve()) + return str(path.joinpath(*cf).resolve()) def _read_config_file(cf, _globals=globals(), _locals=locals(), - interactive=True): - # type: (str, Dict[str, Any], Dict[str, Any], bool) -> None + interactive=True, default=None): + # type: (str, Dict[str, Any], Dict[str, Any], bool, Optional[str]) -> None """Read a config file: execute a python file while loading scapy, that may contain some pre-configured values. @@ -93,11 +84,13 @@ def _read_config_file(cf, _globals=globals(), _locals=locals(), function. Otherwise, vars are only available from inside the scapy console. - params: - - _globals: the globals() vars - - _locals: the locals() vars - - interactive: specified whether or not errors should be printed + Parameters: + + :param _globals: the globals() vars + :param _locals: the locals() vars + :param interactive: specified whether or not errors should be printed using the scapy console or raised. + :param default: if provided, set a default value for the config file ex, content of a config.py file: 'conf.verb = 42\n' @@ -107,6 +100,16 @@ def _read_config_file(cf, _globals=globals(), _locals=locals(), 2 """ + cf_path = pathlib.Path(cf) + if not cf_path.exists(): + log_loading.debug("Config file [%s] does not exist.", cf) + if default is None: + return + # We have a default ! set it + cf_path.parent.mkdir(parents=True, exist_ok=True) + with cf_path.open("w") as fd: + fd.write(default) + log_loading.debug("Config file [%s] created with default.", cf) log_loading.debug("Loading config file [%s]", cf) try: with open(cf) as cfgf: @@ -151,8 +154,7 @@ def _validate_local(k): # conf.use_pcap = True """.strip() -DEFAULT_PRESTART_FILE = _probe_config_file(".config", "scapy", "prestart.py", - default=DEFAULT_PRESTART) +DEFAULT_PRESTART_FILE = _probe_config_file(".config", "scapy", "prestart.py") DEFAULT_STARTUP_FILE = _probe_config_file(".config", "scapy", "startup.py") @@ -718,7 +720,8 @@ def interact(mydict=None, argv=None, mybanner=None, loglevel=logging.INFO): _read_config_file( PRESTART_FILE, interactive=True, - _locals=_scapy_prestart_builtins() + _locals=_scapy_prestart_builtins(), + default=DEFAULT_PRESTART, ) SESSION = init_session(session_name, mydict=mydict, ret=True) diff --git a/test/regression.uts b/test/regression.uts index 9a09899d972..3d0e3bd92b5 100644 --- a/test/regression.uts +++ b/test/regression.uts @@ -651,7 +651,8 @@ assert len(conf.temp_files) == 0 import mock, sys from scapy.main import interact -from scapy.main import DEFAULT_PRESTART_FILE +from scapy.main import DEFAULT_PRESTART_FILE, DEFAULT_PRESTART, _read_config_file +_read_config_file(DEFAULT_PRESTART_FILE, _locals=globals(), default=DEFAULT_PRESTART) # By now .config/scapy/startup.py should have been created with open(DEFAULT_PRESTART_FILE, "r") as fd: OLD_DEFAULT_PRESTART = fd.read() @@ -691,7 +692,8 @@ interact_emulator(extra_args=["-d"]) # Extended import sys import mock -from scapy.main import DEFAULT_PRESTART_FILE +from scapy.main import DEFAULT_PRESTART_FILE, DEFAULT_PRESTART, _read_config_file +_read_config_file(DEFAULT_PRESTART_FILE, _locals=globals(), default=DEFAULT_PRESTART) # By now .config/scapy/startup.py should have been created with open(DEFAULT_PRESTART_FILE, "w+") as fd: fd.write("conf.interactive_shell = 'ptpython'") @@ -1093,6 +1095,7 @@ assert "NameError" in ret[0] cmds = """log_runtime.info(hex_bytes("446166742050756e6b"))\n""" ret = autorun_get_text_interactive_session(cmds) +ret assert "Daft Punk" in ret[0] = Test utility TEX functions @@ -1122,8 +1125,8 @@ conf.verb = saved_conf_verb = Test config file functions failures -from scapy.main import _probe_config_file -assert _probe_config_file("filethatdoesnotexistnorwillever.tsppajfsrdrr") is None +from scapy.main import _read_config_file, _probe_config_file +assert _read_config_file(_probe_config_file("filethatdoesnotexistnorwillever.tsppajfsrdrr")) is None = Test CacheInstance repr