Skip to content
Permalink
Browse files

use subprocess.call

  • Loading branch information...
sdpython committed May 21, 2015
1 parent a09ae45 commit 0c7129d3bb56e233bf2b685b4dd646856bd5986c
@@ -40,12 +40,33 @@ def test_call_setup_hook(self):
init, "pyquickhelper", fLOG=fLOG, function_name="______")
fLOG(err)
fLOG(out)
assert err == "no ______"
assert err == "no ______" or "linux" in out

out, err = call_setup_hook(init, "pyquickhelper", fLOG=fLOG)
fLOG(err)
fLOG(out)
assert len(err) == 0

def test_call_setup_hook_call(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
init = os.path.join(
os.path.split(__file__)[0],
"..",
"..")
out, err = call_setup_hook(
init, "pyquickhelper", fLOG=fLOG, function_name="______", force_call=True)
fLOG(err)
fLOG(out)
assert err == "no ______" or "linux" in out

out, err = call_setup_hook(
init, "pyquickhelper", fLOG=fLOG, force_call=True)
fLOG(err)
fLOG(out)
assert len(err) == 0

if __name__ == "__main__":
unittest.main()
@@ -40,7 +40,7 @@ def check():
return True


def _setup_hook():
def _setup_hook(use_print=False):
"""
if this function is added to the module,
the help automation and unit tests call it first before
@@ -49,6 +49,8 @@ def _setup_hook():
"""
# we can check many things, needed module
# any others things before unit tests are started
if use_print:
print("Success: _setup_hook")
pass


@@ -5,12 +5,15 @@
.. versionadded:: 1.1
"""
import os
from ..loghelper import run_cmd, noLOG
import sys
import subprocess
from ..loghelper import noLOG, run_cmd
from ..loghelper.flog import get_interpreter_path


def call_setup_hook(folder, module_name, fLOG=noLOG, must_be=False,
function_name="_setup_hook", use_print=False):
def call_setup_hook(folder, module_name, fLOG=noLOG, must_be=False,
function_name="_setup_hook", use_print=False,
force_call=False):
"""
calls function @see fn _setup_hook for a specific module,
it is called in a separate process
@@ -21,6 +24,7 @@ def call_setup_hook(folder, module_name, fLOG=noLOG, must_be=False,
@param must_be raises an exception if @see fn _setup_hook is not found
@param function_name function to call by default
@param use_print use print to display information
@param force_call use *subprocess.call* instead of @see fn run_cmd
@return stdout, stderr
The function expects to find file ``__init__.py`` in
@@ -44,15 +48,33 @@ def call_setup_hook(folder, module_name, fLOG=noLOG, must_be=False,
print("CMD:\n", cmd)

fLOG("~~~~~~~~~ calls _setup_hook from", module_name)
out, err = run_cmd(cmd, wait=True, fLOG=fLOG, log_error=False)
if not force_call and sys.platform.startswith("win"):
out, err = run_cmd(cmd, wait=True, fLOG=fLOG, log_error=False)
exit = 0
else:
if use_print:
print("subprocess.call", cmd)
exit = subprocess.call(cmd)
out = "linux"
err = ""

if exit != 0:
init = os.path.join(src, module_name, "__init__.py")
with open(init, "r") as f:
content = f.read()
if 'def {0}'.format(function_name) not in init:
exit = 0
err = "ImportError: cannot import name '{0}'".format(
function_name)
fLOG("~~~~~~~~~ end of call _setup_hook")

if use_print:
print("OUT:\n", out)
print("ERR:\n", err)

def error():
mes = "**CMD:\n{3}\n**CODE:\n{0}\n**OUT:\n{1}\n**ERR:\n{2}".format(code.replace(";", "\n"),
out, err, cmd)
mes = "**CMD:\n{3}\n**CODE:\n{0}\n**OUT:\n{1}\n**ERR:\n{2}\nexit={4}".format(code.replace(";", "\n"),
out, err, cmd, exit)
return mes

if not must_be and "ImportError: cannot import name '{0}'".format(function_name) in err:
@@ -62,4 +84,6 @@ def error():
raise Exception(error())
if "ImportError: No module named" in err:
raise Exception(error())
if exit != 0:
raise Exception(error())
return out, err

0 comments on commit 0c7129d

Please sign in to comment.
You can’t perform that action at this time.