コード例 #1
0
ファイル: TestCore.py プロジェクト: trootodon/pashmak
    def run_script(self, script_content: str, read_inputs=[], args=[], want_argv=False, stop_after_error=True) -> dict:
        ''' Runs a script and returns result '''
        script_operations = parser.parse(script_content)
        prog = program.Program(is_test=self.is_test, args=args)
        prog.stop_after_error = stop_after_error
        prog.read_data = read_inputs
        prog.set_operations(script_operations)
        prog.start()

        out = {}
        out['vars'] = prog.variables
        out['output'] = prog.output
        out['runtime_error'] = prog.runtime_error
        out['mem'] = prog.mem
        try:
            out['exit_code'] = prog.exit_code
        except:
            out['exit_code'] = 0

        del out['vars']['argc']

        if not want_argv:
            del out['vars']['argv']
        del out['vars']['pashmakinfo']

        return out
コード例 #2
0
    def run_script(self,
                   script_content: str,
                   read_inputs=None,
                   args=None,
                   want_argv=False,
                   stop_after_error=True) -> dict:
        ''' Runs a script and returns result '''
        if read_inputs is None:
            read_inputs = []
        if args is None:
            args = []
        script_commands = parser.parse(script_content, filepath='<test>')
        prog = program.Program(is_test=self.is_test, args=args)
        prog.stop_after_error = stop_after_error
        prog.read_data = read_inputs
        prog.set_commands(script_commands)
        prog.start()

        out = {}
        out['vars'] = prog.frames[-1]['vars']
        out['output'] = prog.output
        out['runtime_error'] = prog.runtime_error
        out['mem'] = prog.mem
        try:
            out['exit_code'] = prog.exit_code
        except:
            out['exit_code'] = 0

        del out['vars']['argc']

        if not want_argv:
            del out['vars']['argv']
        try:
            del out['vars']['__file__']
        except:
            pass
        try:
            del out['vars']['__dir__']
        except:
            pass
        try:
            del out['vars']['__ismain__']
        except:
            pass

        return out
コード例 #3
0
        print(version.version)
        sys.exit(0)

    if sys.argv[1] == '-r':
        if len(sys.argv) <= 2:
            print(sys.argv[0] +
                  ': `-r` option requires code as argument: -r [code...]')
            sys.exit(1)
        script_commands = parser.parse(sys.argv[2], filepath='-')
        filename = '-r'
    else:
        filename = sys.argv[1]

        if sys.argv[1] == '-':
            script_content = ''
            for line in sys.stdin.readlines():
                script_content += line
            script_commands = parser.parse(script_content, filepath=filename)
        elif not os.path.isfile(filename):
            print(sys.argv[0] + ': file "' + filename + '" not found')
            sys.exit(1)
        else:
            # read content of file and parse it with the parser
            script_commands = jit.load(filename, code_location=filename)

    # make pashmak program object
    prog = program.Program(args=sys.argv[1:])
    prog.main_filename = filename
    prog.set_commands(script_commands)
    prog.start()
コード例 #4
0
def main():
    """ The main entry point """
    # set signal handler
    signal.signal(signal.SIGINT, signal_handler)

    # set the python path
    try:
        py_path = os.environ['PYTHONPATH'].split(';') if os.name.lower(
        ) == "nt" else os.environ['PYTHONPATH'].split(':')
        py_path = [p for p in py_path if p != '']
        sys.path = [*py_path, *sys.path]
    except KeyError:
        pass

    # validate arguments
    if len(sys.argv) <= 1:
        print(sys.argv[0] +
              ': script file name is required: pashmak [filename]')
        sys.exit(1)

    if sys.argv[1] == '--info':
        print('Pashmak Version: ' + version.version)
        print('Python Version: ', end='')
        print(sys.version.replace('\n', ''))
        sys.exit(0)

    if sys.argv[1] == '--version' or sys.argv[1] == '-v':
        print(version.version)
        sys.exit(0)

    is_module_run = False
    if sys.argv[1][0] == '@':
        module_name = sys.argv[1]
        is_module_run = module_name
        sys.argv[1] = '-r'
        sys.argv.insert(2, 'import_run ' + module_name)

    if sys.argv[1] == '-r':
        if len(sys.argv) <= 2:
            print(sys.argv[0] +
                  ': `-r` option requires code as argument: -r [code...]')
            sys.exit(1)
        script_commands = parser.parse(sys.argv[2], filepath='-')
        filename = '-r'
    else:
        filename = sys.argv[1]

        if sys.argv[1] == '-':
            filename = '<stdin>'
            script_content = ''.join(sys.stdin.readlines())
            script_commands = parser.parse(script_content, filepath=filename)
        elif not os.path.isfile(filename):
            print(sys.argv[0] + ': file "' + filename + '" not found')
            sys.exit(1)
        else:
            # read content of file and parse it with the parser
            script_commands = jit.load(filename,
                                       code_location=filename,
                                       ismain_default=True)

    # make pashmak program object
    if is_module_run:
        sys.argv[2] = is_module_run
        prog = program.Program(args=sys.argv[2:])
    else:
        prog = program.Program(args=sys.argv[1:])
    prog.main_filename = filename
    prog.set_commands(script_commands)
    prog.start()