Commit 5f9159dc authored by Emmanuel Papillon's avatar Emmanuel Papillon
Browse files

* added script utility : script_directory() / run_script()

parent 2bba7336
import importlib
import os.path
import sys
from bliss import current_session
from bliss.config.settings import SimpleSetting
SCRIPT_HOME = SimpleSetting("%s:script_home"%current_session.name, default_value=".")
__all__ = [ "script_directory", "run_script" ]
def script_directory(new_dir=None):
if new_dir is not None:
if not os.path.isdir(new_dir):
raise RuntimeError(f"Invalid directory [{new_dir}]")
SCRIPT_HOME.set(new_dir)
else:
return SCRIPT_HOME.get()
def run_script(scriptname):
script_dir = os.path.abspath(SCRIPT_HOME.get())
(filename, fileext) = os.path.splitext(scriptname)
if not fileext:
fileext = ".py"
filepath = os.path.join(script_dir, "".join((filename, fileext)))
if not os.path.isfile(filepath):
raise RuntimeError(f"Cannot find [{filepath}] !!")
try:
script = open(filepath).read()
except:
raise RuntimeError(f"Failed to read [{filepath}] !!")
print(f"Running [{filepath}] ...")
globals_dict = current_session.env_dict.copy()
exec(script, globals_dict)
for k in globals_dict.keys():
if k.startswith("_"):
continue
current_session.env_dict[k] = globals_dict[k]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment