Example #1
0
def subopen(args, stdin=None, shell=False, cwd=None, env=None):
    temp = None
    pid = None
    close_fds = True
    rstrip = True
    try:
        if stdin is not None and isstring(stdin):
            temp = tempfile()
            write(temp, stdin)
            stdin = open(temp)
        args = tolist(args)
        for i, arg in enumerate(args):
            if not isinstance(arg, int):
                if PY3:
                    args[i] = str(arg)  # python3
                else:
                    args[i] = __builtin__.unicode(arg)  # python2
        if not env:
            env = os.environ.copy()
        process = subprocess.Popen(args,
                                   stdin=stdin,
                                   stderr=subprocess.PIPE,
                                   stdout=subprocess.PIPE,
                                   close_fds=close_fds,
                                   shell=shell,
                                   cwd=cwd,
                                   env=env,
                                   universal_newlines=False  # ,
                                   # startupinfo=startupinfo#,
                                   # creationflags=creationflags
                                   )
        pid = process.pid
        stdoutdata, stderrdata = process.communicate()
        if PY3:
            # python3. process.communicate returns bytes
            stdoutdata = str(stdoutdata, "utf-8")  # stdoutdata
            stderrdata = str(stderrdata, "utf-8")  # stderrdata
        # rstrip
        if rstrip and stdoutdata:
            stdoutdata = stdoutdata.rstrip()
        if rstrip and stderrdata:
            stderrdata = stderrdata.rstrip()
        returncode = process.returncode
        return (returncode, stdoutdata, stderrdata)
        # raise CalledProcessError(returncode,args,err)
        # if returncode!=0:
        # raise CalledProcessError(returncode,args,stderrdata)
        # return stdoutdata
    finally:
        try:
            if pid:
                os.killpg(pid, signal.SIGTERM)
        except Exception:
            pass
            # print(format_exc())
        rm(temp)
Example #2
0
def ismatch(string, pattern):
    if string == pattern:
        return True
    if pattern is None:
        return False
    for s in list(set(tolist(string))):
        if s == pattern:
            return True
        if not isstring(s):
            s = str(s)
        for p in tolist(pattern):
            if s == p:
                return True
            if isregex(p):
                return bool(p.match(s))
            if inspect.isfunction(p):
                return p(s)
            if fnmatch.fnmatch(s, str(p)):
                return True
    return False
def load_module(name, path=None):
    """imp.load_module replacement
    todo: isfile, isdir
    """
    if name:
        name = str(name)
    asname = name
    if name in sys.modules.keys():  # already loaded
        return sys.modules[name]
    if path:
        if isinstance(path, list):
            pass
            # modpath = path
        else:
            path = str(path)
            assert_exists(path)
            if os.path.isdir(path):
                path = [path]
            if not isinstance(path, list) and os.path.isfile(path):
                asname = os.path.basename(path).replace(".py", "")
                path = [os.path.dirname(path)]
    else:
        path = [os.getcwd()]
    # path = [] # python2 default
    # path = None # python3

    try:
        t = imp.find_module(asname, path)
    except Exception:
        return
    warnings.simplefilter("ignore", RuntimeWarning)
    warnings.simplefilter("ignore", ImportWarning)
    syspath = sys.path
    if path:
        sys.path += tolist(path)
    module = imp.load_module(name, *t)
    sys.path = syspath
    return module