Exemple #1
0
def execute(cmd,
            directory=os.getcwd(),
            capture=False,
            stdout_file=None,
            stderr_file=None,
            allow_fail=False):
    """A simple wrapper around executor."""
    from executor import ExternalCommand, ExternalCommandFailed
    try:
        command = ExternalCommand(cmd,
                                  directory=directory,
                                  capture=True,
                                  capture_stderr=True,
                                  stdout_file=stdout_file,
                                  stderr_file=stderr_file)

        command.start()
        if get_log_level() == 'DEBUG':
            logging.log(STDOUT, command.decoded_stdout)
            logging.log(STDERR, command.decoded_stderr)

        if capture:
            return command.decoded_stdout
        return True
    except ExternalCommandFailed as e:
        if allow_fail:
            logging.log(STDERR, e)
            sys.exit(e.returncode)
        else:
            return None
Exemple #2
0
class SubstrateTestEnv:
    def __init__(self, type_def, port, logLv = logging.DEBUG):
        self.type_def = type_def
        self.port = port
        self.logLv = logLv
        self.thread = None

        self.name = f'test_{self.type_def}_{self.port}'
        args = " --name {} --tmp -lruntime=debug --ws-port {}".format(self.name, self.port)
        if type_def != 'europa':
            args += ' --dev'
        self.cmd = self.type_def + args
        self.ec = ExternalCommand(self.cmd)

    @classmethod
    def create_canvas(cls, port=9944):
        return cls(type_def='canvas', port=port)

    @classmethod
    def create_europa(cls, port=9944):
        return cls(type_def='europa', port=port)

    def start_node(self):
        logging.info("run cmd {}".format(self.cmd))

        def loggerThread():
            self.ec.start()
            self.pid = self.ec.pid
            self.ec.wait()

        self.thread = threading.Thread(target=loggerThread, name=self.name)
        self.thread.start()

        time.sleep(3) # for startup

        logging.info("start_node {}".format(self.name))
        return

    def stop_node(self):
        self.ec.kill()
        return

    def url(self) -> str:
        return f'ws://127.0.0.1:{self.port}'

    def typ(self) -> str:
        if self.type_def == 'canvas':
            return 'canvas'

        if self.type_def == 'europa':
            return 'default'

    def types(self) -> dict:
        if self.type_def == 'canvas':
            return {'types': {"Address": "AccountId", "LookupSource": "AccountId"}}
        if self.type_def == 'europa':
            return {'types': {'LookupSource': 'MultiAddress'}}
Exemple #3
0
def execute(cmd, directory=os.getcwd(), capture=False, stdout_file=None,
            stderr_file=None):
    """A simple wrapper around executor."""
    command = ExternalCommand(
        cmd, directory=directory, capture=True, capture_stderr=True,
        stdout_file=stdout_file, stderr_file=stderr_file
    )

    command.start()
    if get_log_level() == 'DEBUG':
        logging.log(STDOUT, command.decoded_stdout)
        logging.log(STDERR, command.decoded_stderr)

    if capture:
        return command.decoded_stdout
Exemple #4
0
def execute(cmd,
            directory=os.getcwd(),
            capture_stdout=False,
            stdout_file=None,
            stderr_file=None,
            max_attempts=1,
            is_sra=False):
    """A simple wrapper around executor."""
    from executor import ExternalCommand, ExternalCommandFailed
    attempt = 0
    while attempt < max_attempts:
        attempt += 1
        try:
            command = ExternalCommand(cmd,
                                      directory=directory,
                                      capture=True,
                                      capture_stderr=True,
                                      stdout_file=stdout_file,
                                      stderr_file=stderr_file)

            command.start()
            if get_log_level() == 'DEBUG':
                logging.log(STDOUT, command.decoded_stdout)
                logging.log(STDERR, command.decoded_stderr)

            if capture_stdout:
                return command.decoded_stdout
            else:
                return command.returncode
        except ExternalCommandFailed as error:
            logging.error(f'"{cmd}" return exit code {command.returncode}')

            if is_sra and command.returncode == 3:
                # The FASTQ isn't on SRA for some reason, try to download from ENA
                error_msg = command.decoded_stderr.split("\n")[0]
                logging.error(error_msg)
                return 'SRA_NOT_FOUND'

            if attempt < max_attempts:
                logging.error(f'Retry execution ({attempt} of {max_attempts})')
                time.sleep(10)
            else:
                raise error
Exemple #5
0
    def run_cmd(cmdarray, workdir):
        """Run a command on the shell"""
        cmd = ExternalCommand(*cmdarray, capture=True, capture_stderr=True, async=True, shell=False, directory=workdir)
        cmd.start()
        last_out = ''
        last_err = ''
        while cmd.is_running:
            new_out = cmd.stdout.decode(cmd.encoding, 'ignore').replace(last_out, '')
            new_err = cmd.stderr.decode(cmd.encoding, 'ignore').replace(last_err, '')

            last_out += new_out
            last_err += new_err
            new_out = new_out.replace(u"\u2018", "'").replace(u"\u2019", "'")
            new_err = new_err.replace(u"\u2018", "'").replace(u"\u2019", "'")
            if new_out != '':
                print(new_out, end='')
            if new_err != '':
                print(new_err, end='')

        if cmd.returncode != 0:
            raise RuntimeError('Failure to run command')
        return cmd