Skip to content

Commit d71014a

Browse files
authored
Only create default config file in interactive mode (#4218)
This fixes macOS unit tests, and makes a bit more sense.
1 parent 51c0754 commit d71014a

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

scapy/main.py

+24-21
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,18 @@
6464
]
6565

6666

67-
def _probe_config_file(*cf, default=None):
68-
# type: (str, Optional[str]) -> Union[str, None]
67+
def _probe_config_file(*cf):
68+
# type: (str) -> Union[str, None]
6969
path = pathlib.Path(os.path.expanduser("~"))
7070
if not path.exists():
7171
# ~ folder doesn't exist. Unsalvageable
7272
return None
73-
cf_path = path.joinpath(*cf)
74-
if not cf_path.exists():
75-
if default is not None:
76-
# We have a default ! set it
77-
cf_path.parent.mkdir(parents=True, exist_ok=True)
78-
with cf_path.open("w") as fd:
79-
fd.write(default)
80-
return str(cf_path.resolve())
81-
return None
82-
return str(cf_path.resolve())
73+
return str(path.joinpath(*cf).resolve())
8374

8475

8576
def _read_config_file(cf, _globals=globals(), _locals=locals(),
86-
interactive=True):
87-
# type: (str, Dict[str, Any], Dict[str, Any], bool) -> None
77+
interactive=True, default=None):
78+
# type: (str, Dict[str, Any], Dict[str, Any], bool, Optional[str]) -> None
8879
"""Read a config file: execute a python file while loading scapy, that
8980
may contain some pre-configured values.
9081
@@ -93,11 +84,13 @@ def _read_config_file(cf, _globals=globals(), _locals=locals(),
9384
function. Otherwise, vars are only available from inside the scapy
9485
console.
9586
96-
params:
97-
- _globals: the globals() vars
98-
- _locals: the locals() vars
99-
- interactive: specified whether or not errors should be printed
87+
Parameters:
88+
89+
:param _globals: the globals() vars
90+
:param _locals: the locals() vars
91+
:param interactive: specified whether or not errors should be printed
10092
using the scapy console or raised.
93+
:param default: if provided, set a default value for the config file
10194
10295
ex, content of a config.py file:
10396
'conf.verb = 42\n'
@@ -107,6 +100,16 @@ def _read_config_file(cf, _globals=globals(), _locals=locals(),
107100
2
108101
109102
"""
103+
cf_path = pathlib.Path(cf)
104+
if not cf_path.exists():
105+
log_loading.debug("Config file [%s] does not exist.", cf)
106+
if default is None:
107+
return
108+
# We have a default ! set it
109+
cf_path.parent.mkdir(parents=True, exist_ok=True)
110+
with cf_path.open("w") as fd:
111+
fd.write(default)
112+
log_loading.debug("Config file [%s] created with default.", cf)
110113
log_loading.debug("Loading config file [%s]", cf)
111114
try:
112115
with open(cf) as cfgf:
@@ -151,8 +154,7 @@ def _validate_local(k):
151154
# conf.use_pcap = True
152155
""".strip()
153156

154-
DEFAULT_PRESTART_FILE = _probe_config_file(".config", "scapy", "prestart.py",
155-
default=DEFAULT_PRESTART)
157+
DEFAULT_PRESTART_FILE = _probe_config_file(".config", "scapy", "prestart.py")
156158
DEFAULT_STARTUP_FILE = _probe_config_file(".config", "scapy", "startup.py")
157159

158160

@@ -718,7 +720,8 @@ def interact(mydict=None, argv=None, mybanner=None, loglevel=logging.INFO):
718720
_read_config_file(
719721
PRESTART_FILE,
720722
interactive=True,
721-
_locals=_scapy_prestart_builtins()
723+
_locals=_scapy_prestart_builtins(),
724+
default=DEFAULT_PRESTART,
722725
)
723726

724727
SESSION = init_session(session_name, mydict=mydict, ret=True)

test/regression.uts

+7-4
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,8 @@ assert len(conf.temp_files) == 0
651651
import mock, sys
652652
from scapy.main import interact
653653

654-
from scapy.main import DEFAULT_PRESTART_FILE
654+
from scapy.main import DEFAULT_PRESTART_FILE, DEFAULT_PRESTART, _read_config_file
655+
_read_config_file(DEFAULT_PRESTART_FILE, _locals=globals(), default=DEFAULT_PRESTART)
655656
# By now .config/scapy/startup.py should have been created
656657
with open(DEFAULT_PRESTART_FILE, "r") as fd:
657658
OLD_DEFAULT_PRESTART = fd.read()
@@ -691,7 +692,8 @@ interact_emulator(extra_args=["-d"]) # Extended
691692
import sys
692693
import mock
693694

694-
from scapy.main import DEFAULT_PRESTART_FILE
695+
from scapy.main import DEFAULT_PRESTART_FILE, DEFAULT_PRESTART, _read_config_file
696+
_read_config_file(DEFAULT_PRESTART_FILE, _locals=globals(), default=DEFAULT_PRESTART)
695697
# By now .config/scapy/startup.py should have been created
696698
with open(DEFAULT_PRESTART_FILE, "w+") as fd:
697699
fd.write("conf.interactive_shell = 'ptpython'")
@@ -1093,6 +1095,7 @@ assert "NameError" in ret[0]
10931095

10941096
cmds = """log_runtime.info(hex_bytes("446166742050756e6b"))\n"""
10951097
ret = autorun_get_text_interactive_session(cmds)
1098+
ret
10961099
assert "Daft Punk" in ret[0]
10971100

10981101
= Test utility TEX functions
@@ -1122,8 +1125,8 @@ conf.verb = saved_conf_verb
11221125

11231126
= Test config file functions failures
11241127

1125-
from scapy.main import _probe_config_file
1126-
assert _probe_config_file("filethatdoesnotexistnorwillever.tsppajfsrdrr") is None
1128+
from scapy.main import _read_config_file, _probe_config_file
1129+
assert _read_config_file(_probe_config_file("filethatdoesnotexistnorwillever.tsppajfsrdrr")) is None
11271130

11281131
= Test CacheInstance repr
11291132

0 commit comments

Comments
 (0)