Esempio n. 1
0
    def isystem(self, cmd, **kwargs):

        if OS == "windows":
            kwargs["shell"] = True

            if isinstance(cmd, (tuple, list)):
                cmd = " ".join(cmd)
        else:
            if not isinstance(cmd, (tuple, list)):
                cmd = pyloco_shlex.split(cmd)

        kwargs.pop("stdout", None)
        kwargs.pop("stderr", None)

        p = subprocess.Popen(cmd, stderr=subprocess.PIPE)

        while True:

            out = p.stderr.read(1)
            # if out == '' and p.poll() is not None:
            if out == '' or p.poll() is not None:
                break

            if out != '':
                if PY3:
                    sys.stdout.write(out.decode("utf-8"))

                else:
                    sys.stdout.write(out)

                sys.stdout.flush()

        retval = p.wait()

        return retval
Esempio n. 2
0
def main(argv=None, manager=None):
    """run a task from command-line
        Handling pyloco options
    """

    if argv is None:
        argv = sys.argv[1:]
    elif isinstance(argv, str):
        argv = pyloco_shlex.split(argv)

    if manager is None:
        manager = PylocoManager

    if not argv:
        print("usage: " + manager._usage_.format(manager=manager._name_))
        print(manager._help_help_.format(manager=manager._name_))
        return 0

    num_pipes = argv.count("--")

    if num_pipes > 0:

        idx = argv.index("--")
        targv, sargv = argv[:idx], argv[idx + 1:]

        if targv:
            if targv[0] == "input":
                ret, _ = perform(GroupInputCmdTask,
                                 argv=targv[1:],
                                 subargv=sargv,
                                 manager=manager)

            elif targv[0] in mgmt_tasks:
                ret, _ = perform(targv[0],
                                 argv=targv[1:],
                                 subargv=sargv,
                                 manager=manager)

            elif targv[0].startswith("-"):
                ret, _ = perform(GroupCmdTask,
                                 argv=targv,
                                 subargv=sargv,
                                 manager=manager)

            else:
                ret, _ = perform(GroupCmdTask, subargv=argv, manager=manager)
        else:
            ret, _ = perform(GroupCmdTask, subargv=sargv, manager=manager)

    elif argv[0].startswith("-"):
        ret, _ = perform(OptionTask, argv=argv, manager=manager)

    else:
        task = argv.pop(0)
        ret, _ = perform(task, argv=argv, manager=manager)

    return ret
Esempio n. 3
0
    def append_argument(self, path, arg):

        if isinstance(arg, str):
            arg = pyloco_shlex.split(arg)

        if path in self.departure_data:
            self.departure_argument[path].extend(arg)

        else:
            self.departure_argument[path] = arg
Esempio n. 4
0
    def system(self, cmd, **kwargs):

        if OS == "windows":
            kwargs["shell"] = True
            if isinstance(cmd, (tuple, list)):
                cmd = " ".join(cmd)
        else:
            if not isinstance(cmd, (tuple, list)):
                cmd = pyloco_shlex.split(cmd)

        tmpout = False
        tmperr = False

        stdout = kwargs.pop("stdout", None)
        stderr = kwargs.pop("stderr", None)

        if stdout is None:
            stdout = tempfile.TemporaryFile()
            tmpout = True

        if stderr is None:
            stderr = tempfile.TemporaryFile()
            tmperr = True

        popen = subprocess.Popen(cmd, stdout=stdout, stderr=stderr, **kwargs)

        retval = popen.wait()

        if tmpout:
            stdout.seek(0)
            out = strdecode(stdout.read())
            stdout.close()

        else:
            out = stdout

        if tmperr:
            stderr.seek(0)
            err = strdecode(stderr.read())
            stderr.close()

        else:
            err = stderr

        return retval, out, err
Esempio n. 5
0
def perform(task,
            argv=None,
            subargv=None,
            parent=None,
            forward=None,
            shared=None,
            return_directory=None,
            manager=None):

    multiprocessing.freeze_support()

    out = -1, None

    if forward is None:
        forward = {}

    if isinstance(argv, str):
        argv = pyloco_shlex.split(argv)

    elif argv is None:
        argv = []

    if isinstance(subargv, str):
        subargv = pyloco_shlex.split(subargv)

    elif subargv is None:
        subargv = []

    run_profile, run_trace = _extract_option(["--profile", "--trace"], argv,
                                             subargv)

    run_debug = True if ("--debug" in argv + subargv) else False

    if parent is None:
        if manager is None:
            parent = PylocoManager(shared=shared)

        else:
            parent = manager(shared=shared)

    if not task:
        if subargv:
            task = GroupCmdTask(parent)

        else:
            task = OptionTask(parent)

    elif isinstance(task, str):

        orgtask = task

        if task == "input" and subargv:
            task_class = GroupInputCmdTask
        else:
            try:
                task_class, argv, subargv, objs = load_taskclass(
                    task, argv, subargv)
            except:
                sys.stderr.write("ERROR: Task '%s' load failure. Please "
                                 "check if '%s' is available.\n" %
                                 (orgtask, orgtask))
                return out

        if task_class is None:
            sys.stderr.write("ERROR: Task '%s' is not loaded. "
                             "Please check task-path.\n" % orgtask)
            return out

        task = task_class(parent)

        if task is None:
            sys.stderr.write("ERROR: Task '%s' is not created.\n" % orgtask)
            return out

        task._env.update(objs)

    elif type(task) == type(Task) and issubclass(task, Task):

        task = task(parent)

    else:
        sys.stderr.write("ERROR: Task is not found. Please check task-path.\n")
        return out

    if isinstance(task, Task):

        if run_profile:

            try:
                import cProfile as prof

            except ImportError:
                import profile as prof

            prof.runctx('task.run(argv, subargv=subargv, forward=forward)',
                        globals(),
                        locals(),
                        sort=1)
        else:
            if run_trace:
                sys.excepthook = _excepthook
                out = task.run(argv, subargv=subargv, forward=forward)
            elif sys.excepthook == _excepthook:
                out = task.run(argv, subargv=subargv, forward=forward)

            elif run_debug:
                out = task.run(argv, subargv=subargv, forward=forward)
            else:
                try:
                    out = task.run(argv, subargv=subargv, forward=forward)

                except UsageError as err:
                    print("USAGE ERROR: " + str(err))

                except InternalError as err:
                    print("INTERNAL ERROR: " + str(err))

                except TypeCheckError as err:
                    print("TYPE MISMATCH: " + str(err))

                except NotImplementedError as err:
                    print("NOT IMPLEMENTED: " + str(err))

                except NormalExit:
                    return 0, None

                except SystemExit as err:
                    if err.code != 0:
                        raise SystemExit(err.code)

                    out = 0, None

                except (KeyboardInterrupt, EOFError):
                    print('[Interrupted.]')

                #except Exception as err:
                #    print("ERROR: " + str(err))

    if return_directory:
        os.chdir(return_directory)

    return out