Example #1
0
def resolvedirof(filename):
    filename = rpath.rabspath(filename)
    dirname = rpath.rabspath(os.path.join(filename, '..'))
    if os.path.islink(filename):
        try:
            link = _readlink_maybe(filename)
        except OSError:
            pass
        else:
            return resolvedirof(os.path.join(dirname, link))
    return dirname
Example #2
0
 def find_executable(self, executable):
     if os.sep in executable or (os.name == "nt" and ":" in executable):
         return rpath.rabspath(executable)
     path = os.environ.get("PATH")
     if path:
         for dir in path.split(os.pathsep):
             f = os.path.join(dir, executable)
             if os.path.isfile(f):
                 executable = f
                 break
     return rpath.rabspath(executable)
Example #3
0
def resolvedirof(filename):
    filename = rpath.rabspath(filename)
    dirname = rpath.rabspath(os.path.join(filename, '..'))
    if os.path.islink(filename):
        try:
            link = _readlink_maybe(filename)
        except OSError:
            pass
        else:
            return resolvedirof(os.path.join(dirname, link))
    return dirname
Example #4
0
 def find_executable(self, executable):
     if os.sep in executable or (os.name == "nt" and ":" in executable):
         return rpath.rabspath(executable)
     path = os.environ.get("PATH")
     if path:
         for dir in path.split(os.pathsep):
             f = os.path.join(dir, executable)
             if os.path.isfile(f):
                 executable = f
                 break
     return rpath.rabspath(executable)
Example #5
0
    def setup(self, executable):
        """
        Performs runtime setup.
        """
        path = rpath.rabspath(executable)
        # Fallback to a path relative to the compiled location.
        lib_path = self.base_lib_path
        while path:
            path = rpath.rabspath(os.path.join(path, os.path.pardir))
            if os.path.isdir(os.path.join(path, "lib-ruby")):
                lib_path = os.path.join(path, "lib-ruby")
                break

        self.send(self.w_load_path, self.newsymbol("unshift"), [self.newstr_fromstr(lib_path)])
Example #6
0
 def runtime_setup(self, executable, image_name):
     fullpath = rpath.rabspath(self.find_executable(executable))
     i = fullpath.rfind(os.path.sep) + 1
     assert i > 0
     self._executable_path.set(fullpath[:i])
     self._image_name.set(image_name)
     self.image_loaded.activate()
Example #7
0
def test_rabspath_absolute_nt():
    assert rpath._nt_rabspath('d:\\foo') == 'd:\\foo'
    assert rpath._nt_rabspath('d:\\foo\\bar\\..') == 'd:\\foo'
    assert rpath._nt_rabspath('d:\\foo\\bar\\..\\x') == 'd:\\foo\\x'
    curdrive = _ = rpath._nt_rsplitdrive(os.getcwd())
    assert len(curdrive) == 2 and curdrive[0][1] == ':'
    assert rpath.rabspath('\\foo') == '%s\\foo' % curdrive[0]
Example #8
0
def rpath_example():
    # normalize a pathname by collapsing redundant separators and up-level
    # references so that A//B, A/B/, A/./B and A/foo/../B all become A/B
    print rpath.rnormpath("///..//./foo/.//bar")    # "/foo/bar"

    # return a normalized absolutized version of the pathname path
    print rpath.rabspath('foo')                            # $(pwd)/foo

    # join two pathname components, inserting '/' as needed
    print rpath.rjoin("/foo", "bar" + rpath.sep + "foo")

    # return True if path is an absolute pathname
    print rpath.risabs('C:\\foo\\bar')              # 0

    # return True if path is an existing directory
    print rpath.risdir('_some_non_existant_file_')         # 0
Example #9
0
 def runtime_setup(self, executable, image_name):
     fullpath = rpath.rabspath(self.find_executable(executable))
     i = fullpath.rfind(os.path.sep) + 1
     assert i > 0
     self._executable_path.set(fullpath[:i])
     self._image_name.set(image_name)
     self.image_loaded.activate()
Example #10
0
def test_rabspath_absolute_nt():
    assert rpath._nt_rabspath('d:\\foo') == 'd:\\foo'
    assert rpath._nt_rabspath('d:\\foo\\bar\\..') == 'd:\\foo'
    assert rpath._nt_rabspath('d:\\foo\\bar\\..\\x') == 'd:\\foo\\x'
    curdrive = _ = rpath._nt_rsplitdrive(os.getcwd())
    assert len(curdrive) == 2 and curdrive[1] == ':'
    assert rpath.rabspath('\\foo') == '%s\\foo' % curdrive
Example #11
0
 def runtime_setup(self, argv, image_name):
     fullpath = rpath.rabspath(self.find_executable(argv[0]))
     i = fullpath.rfind(os.path.sep) + 1
     assert i > 0
     self._executable_path.set(fullpath[:i])
     self.set_system_attribute(SYSTEM_ATTRIBUTE_IMAGE_NAME_INDEX, image_name)
     self.image_loaded.activate()
     self.init_system_attributes(argv)
Example #12
0
 def setup(self, executable):
     """
     Performs runtime setup.
     """
     path = rpath.rabspath(self.find_executable(executable))
     # Fallback to a path relative to the compiled location.
     lib_path = self.base_lib_path
     kernel_path = os.path.join(os.path.join(lib_path, os.path.pardir), "lib-topaz")
     while True:
         par_path = rpath.rabspath(os.path.join(path, os.path.pardir))
         if par_path == path:
             break
         path = par_path
         if isdir(os.path.join(path, "lib-ruby")):
             lib_path = os.path.join(path, "lib-ruby")
             kernel_path = os.path.join(path, "lib-topaz")
             break
     self.send(self.w_load_path, "unshift", [self.newstr_fromstr(lib_path)])
     self.load_kernel(kernel_path)
Example #13
0
 def setup(self, executable):
     """
     Performs runtime setup.
     """
     path = rpath.rabspath(self.find_executable(executable))
     # Fallback to a path relative to the compiled location.
     lib_path = self.base_lib_path
     kernel_path = os.path.join(os.path.join(lib_path, os.path.pardir), "lib-topaz")
     while True:
         par_path = rpath.rabspath(os.path.join(path, os.path.pardir))
         if par_path == path:
             break
         path = par_path
         if isdir(os.path.join(path, "lib-ruby")):
             lib_path = os.path.join(path, "lib-ruby")
             kernel_path = os.path.join(path, "lib-topaz")
             break
     self.send(self.w_load_path, "unshift", [self.newstr_fromstr(lib_path)])
     self.load_kernel(kernel_path)
Example #14
0
def test_missing_current_dir(tmpdir):
    tmpdir1 = str(tmpdir) + '/temporary_removed'
    curdir1 = os.getcwd()
    try:
        os.mkdir(tmpdir1)
        os.chdir(tmpdir1)
        os.rmdir(tmpdir1)
        result = rpath.rabspath('.')
    finally:
        os.chdir(curdir1)
    assert result == '.'
Example #15
0
def test_missing_current_dir(tmpdir):
    tmpdir1 = str(tmpdir) + '/temporary_removed'
    curdir1 = os.getcwd()
    try:
        os.mkdir(tmpdir1)
        os.chdir(tmpdir1)
        os.rmdir(tmpdir1)
        result = rpath.rabspath('.')
    finally:
        os.chdir(curdir1)
    assert result == '.'
Example #16
0
def init_load_path(self_path):
    if not path.isfile(self_path):
        self_path = find_in_path(self_path)
        assert self_path is not None
    if path.islink(self_path):
        self_path = os.readlink(self_path)
    self_path = dirname(rpath.rabspath(self_path))

    # runtime is not loaded yet, so we have to do it manually
    LOAD_PATHS.set_root(Atom(EMPTY_VECTOR.conj(rt.wrap(self_path))))
    # just for run_load_stdlib (global variables can't be assigned to)
    load_path.set_root(rt.wrap(self_path))
Example #17
0
def read_file(filename):
	path = rpath.rabspath(filename)
	try:
		file = open_file_as_stream(path, mode="rb", buffering=0)
		try:
			data = file.readall()
		finally:
			file.close()
	except OSError as e:
		os.write(2, "%s -- %s (LoadError)\n" % (os.strerror(e.errno), path))
		return ("", False)
	return (data, True)
Example #18
0
def init_load_path(self_path):
    if not path.isfile(self_path):
        self_path = find_in_path(self_path)
        assert self_path is not None
    if path.islink(self_path):
        self_path = os.readlink(self_path)
    self_path = dirname(rpath.rabspath(self_path))

    # runtime is not loaded yet, so we have to do it manually
    LOAD_PATHS.set_root(Atom(EMPTY_VECTOR.conj(rt.wrap(self_path))))
    # just for run_load_stdlib (global variables can't be assigned to)
    load_path.set_root(rt.wrap(self_path))
Example #19
0
    def setup(self, executable):
        """
        Performs runtime setup.
        """
        path = rpath.rabspath(self.find_executable(executable))
        # Fallback to a path relative to the compiled location.
        lib_path = self.base_lib_path
        kernel_path = os.path.join(
            os.path.join(lib_path, os.path.pardir), "lib-topaz")
        while True:
            par_path = rpath.rabspath(os.path.join(path, os.path.pardir))
            if par_path == path:
                break
            path = par_path
            if isdir(os.path.join(path, "lib-ruby")):
                lib_path = os.path.join(path, "lib-ruby")
                kernel_path = os.path.join(path, "lib-topaz")
                break
        self.send(self.w_load_path, "unshift", [self.newstr_fromstr(lib_path)])
        self.load_kernel(kernel_path)

        self.set_const(
            self.w_object,
            "RUBY_ENGINE", self.newstr_fromstr(system.RUBY_ENGINE))
        self.set_const(
            self.w_object,
            "RUBY_VERSION", self.newstr_fromstr(system.RUBY_VERSION))
        self.set_const(
            self.w_object,
            "RUBY_PATCHLEVEL", self.newint(system.RUBY_PATCHLEVEL))
        self.set_const(
            self.w_object,
            "RUBY_PLATFORM", self.newstr_fromstr(system.RUBY_PLATFORM))
        self.set_const(
            self.w_object,
            "RUBY_DESCRIPTION", self.newstr_fromstr(system.RUBY_DESCRIPTION))
        self.set_const(
            self.w_object,
            "RUBY_REVISION", self.newstr_fromstr(system.RUBY_REVISION))
Example #20
0
def builtin_import(engine, payload):
    if not isinstance(payload, Str):
        raise Exception('unknown data type.')
    stack = engine.current_stack()
    executable_path = rabspath(engine.executable)
    pkg = rjoin(_dirname(_dirname(executable_path)), 'pkg')
    path = rjoin(pkg, '%s.ho' % payload.str_val)
    with open(path) as f:
        env = engine.run_module_code(f.read())
        stack = engine.current_stack()
        stack.namespace.update(env.namespace)
        stack.defs.update(env.defs)
    return null
Example #21
0
 def ensure_path(self):
     path = self.path
     if path:
         if os.path.exists(path):
             self.path = rpath.rabspath(path)
             return
         exedir = self.get_exedir()
         if not exedir:
             return
         path = rpath.rjoin(exedir, path)
         if os.path.exists(path):
             self.path = rpath.rabspath(path)
             return
     else:
         for filename in os.listdir(os.getcwd()):
             if filename.startswith("Squeak") and filename.endswith(".image"):
                 path = filename
                 break
     if path is None:
         from rsqueakvm.util.dialog import get_file
         path = get_file()
     self.path = rpath.rabspath(path)
Example #22
0
 def ensure_path(self):
     path = self.path
     if path:
         if os.path.exists(path):
             self.path = rpath.rabspath(path)
             return
         exedir = self.get_exedir()
         if not exedir:
             return
         path = rpath.rjoin(exedir, path)
         if os.path.exists(path):
             self.path = rpath.rabspath(path)
             return
     else:
         for filename in os.listdir(os.getcwd()):
             if filename.startswith("Squeak") and filename.endswith(
                     ".image"):
                 path = filename
                 break
     if path is None:
         from rsqueakvm.util.dialog import get_file
         path = get_file()
     self.path = rpath.rabspath(path)
Example #23
0
def init_load_path(self_path):
    if not path.isfile(self_path):
        self_path = find_in_path(self_path)
        assert self_path is not None
    if path.islink(self_path):
        self_path = os.readlink(self_path)
    self_path = dirname(rpath.rabspath(self_path))

    # runtime is not loaded yet, so we have to do it manually
    if build_config.USE_GLOBAL_LOADPATH:
        LOAD_PATHS.set_root(Atom(EMPTY_VECTOR.conj(rt.wrap(build_config.STDLIB_LOADPATH))))
        load_path.set_root(rt.wrap(build_config.STDLIB_LOADPATH))
    else:
        LOAD_PATHS.set_root(Atom(EMPTY_VECTOR.conj(rt.wrap(self_path))))
        # just for run_load_stdlib (global variables can't be assigned to)
        load_path.set_root(rt.wrap(self_path))
    add_env_to_load_path()
Example #24
0
def find_executable(executable):
    """
    Return the absolute path of the executable, by looking into PATH and
    the current directory.  If it cannot be found, return ''.
    """
    if (we_are_translated() and _WIN32
            and not executable.lower().endswith('.exe')):
        executable += '.exe'
    if os.sep in executable or (_WIN32 and ':' in executable):
        # the path is already more than just an executable name
        pass
    else:
        path = os.environ.get('PATH')
        if path:
            for dir in path.split(os.pathsep):
                fn = os.path.join(dir, executable)
                if _exists_and_is_executable(fn):
                    executable = fn
                    break
    executable = rpath.rabspath(executable)

    # 'sys.executable' should not end up being an non-existing file;
    # just use '' in this case. (CPython issue #7774)
    return executable if _exists_and_is_executable(executable) else ''
Example #25
0
def find_executable(executable):
    """
    Return the absolute path of the executable, by looking into PATH and
    the current directory.  If it cannot be found, return ''.
    """
    if (we_are_translated() and _WIN32 and
        not executable.lower().endswith('.exe')):
        executable += '.exe'
    if os.sep in executable or (_WIN32 and ':' in executable):
        # the path is already more than just an executable name
        pass
    else:
        path = os.environ.get('PATH')
        if path:
            for dir in path.split(os.pathsep):
                fn = os.path.join(dir, executable)
                if os.path.isfile(fn):
                    executable = fn
                    break
    executable = rpath.rabspath(executable)

    # 'sys.executable' should not end up being an non-existing file;
    # just use '' in this case. (CPython issue #7774)
    return executable if os.path.isfile(executable) else ''
Example #26
0
def test_rabspath_relative(tmpdir):
    tmpdir.chdir()
    assert rpath.rabspath('foo') == os.path.realpath(str(tmpdir.join('foo')))
def entry_point(argv):
    idx = 1
    path = None
    number = 0
    benchmark = None
    trace = False
    evented = True
    stringarg = ""
    code = None
    as_benchmark = False

    while idx < len(argv):
        arg = argv[idx]
        if arg in ["-h", "--help"]:
            _usage(argv)
            return 0
        elif arg in ["-j", "--jit"]:
            _arg_missing(argv, idx, arg)
            jitarg = argv[idx + 1]
            idx += 1
            jit.set_user_param(interpreter.Interpreter.jit_driver, jitarg)
        elif arg in ["-n", "--number"]:
            _arg_missing(argv, idx, arg)
            number = int(argv[idx + 1])
            idx += 1
        elif arg in ["-m", "--method"]:
            _arg_missing(argv, idx, arg)
            benchmark = argv[idx + 1]
            idx += 1
        elif arg in ["-t", "--trace"]:
            trace = True
        elif arg in ["-p", "--poll_events"]:
            evented = False
        elif arg in ["-a", "--arg"]:
            _arg_missing(argv, idx, arg)
            stringarg = argv[idx + 1]
            idx += 1
        elif arg in ["-r", "--run"]:
            _arg_missing(argv, idx, arg)
            code = argv[idx + 1]
            as_benchmark = False
            idx += 1
        elif arg in ["-b", "--benchmark"]:
            _arg_missing(argv, idx, arg)
            code = argv[idx + 1]
            as_benchmark = True
            idx += 1
        elif path is None:
            path = argv[idx]
        else:
            _usage(argv)
            return -1
        idx += 1

    if path is None:
        path = "Squeak.image"

    path = rpath.rabspath(path)
    try:
        f = open_file_as_stream(path, mode="rb", buffering=0)
    except OSError as e:
        os.write(2, "%s -- %s (LoadError)\n" % (os.strerror(e.errno), path))
        return 1
    try:
        imagedata = f.readall()
    finally:
        f.close()

    image_reader = squeakimage.reader_for_image(space, squeakimage.Stream(data=imagedata))
    image = create_image(space, image_reader)
    interp = interpreter.Interpreter(space, image, image_name=path, trace=trace, evented=evented)
    space.runtime_setup(argv[0])
    if benchmark is not None:
        return _run_benchmark(interp, number, benchmark, stringarg)
    elif code is not None:
        return _run_code(interp, code, as_benchmark=as_benchmark)
    else:
        _run_image(interp)
        return 0
def entry_point(argv):
    jit.set_param(None, "trace_limit", 1000000)
    # == Main execution parameters
    path = None
    selector = None
    code = ""
    number = 0
    have_number = False
    stringarg = None
    headless = True
    # == Other parameters
    poll = False
    interrupts = True
    trace = False
    trace_important = False

    space = prebuilt_space
    idx = 1
    try:
        while idx < len(argv):
            arg = argv[idx]
            idx += 1
            if arg in ["-h", "--help"]:
                _usage(argv)
                return 0
            elif arg in ["-j", "--jit"]:
                jitarg, idx = get_parameter(argv, idx, arg)
                jit.set_user_param(interpreter.Interpreter.jit_driver, jitarg)
            elif arg in ["-n", "--number"]:
                number, idx = get_int_parameter(argv, idx, arg)
                have_number = True
            elif arg in ["-m", "--method"]:
                selector, idx = get_parameter(argv, idx, arg)
            elif arg in ["-t", "--trace"]:
                trace = True
            elif arg in ["-T"]:
                trace_important = True
            elif arg in ["-s", "--safe-trace"]:
                space.omit_printing_raw_bytes.activate()
            elif arg in ["-p", "--poll"]:
                poll = True
            elif arg in ["-a", "--arg"]:
                stringarg, idx = get_parameter(argv, idx, arg)
            elif arg in ["-r", "--run"]:
                code, idx = get_parameter(argv, idx, arg)
            elif arg in ["-i", "--no-interrupts"]:
                interrupts = False
            elif arg in ["-P", "--process"]:
                headless = False
            elif arg in ["--hacks"]:
                space.run_spy_hacks.activate()
            elif arg in ["--use-plugins"]:
                space.use_plugins.activate()
            elif arg in ["-S"]:
                space.strategy_factory.no_specialized_storage.activate()
            elif arg in ["-u"]:
                from spyvm.plugins.vmdebugging import stop_ui_process
                stop_ui_process()
            elif arg in ["-l", "--storage-log"]:
                space.strategy_factory.logger.activate()
            elif arg in ["-L"]:
                space.strategy_factory.logger.activate(aggregate=True)
            elif path is None:
                path = arg
            else:
                _usage(argv)
                return -1

        if path is None:
            path = "Squeak.image"
        if code and selector:
            raise error.Exit("Cannot handle both -r and -m.")
    except error.Exit as e:
        print_error("Parameter error: %s" % e.msg)
        return 1

    path = rpath.rabspath(path)
    try:
        stream = squeakimage.Stream(filename=path)
    except OSError as e:
        print_error("%s -- %s (LoadError)" % (os.strerror(e.errno), path))
        return 1

    # Load & prepare image and environment
    image = squeakimage.ImageReader(space, stream).create_image()
    interp = interpreter.Interpreter(space, image,
                trace=trace, trace_important=trace_important,
                evented=not poll, interrupts=interrupts)
    space.runtime_setup(argv[0], path)
    interp.populate_remaining_special_objects()
    print_error("") # Line break after image-loading characters

    # Create context to be executed
    if code or selector:
        if not have_number:
            w_receiver = space.w_nil
        else:
            w_receiver = space.wrap_int(number)
        if code:
            selector = compile_code(interp, w_receiver, code)
        s_frame = create_context(interp, w_receiver, selector, stringarg)
        if headless:
            space.headless.activate()
            context = s_frame
        else:
            create_process(interp, s_frame)
            context = active_context(space)
    else:
        context = active_context(space)

    w_result = execute_context(interp, context)
    print result_string(w_result)
    return 0
Example #29
0
def test_rabspath_relative(tmpdir):
    tmpdir.chdir()
    assert rpath.rabspath('foo') == os.path.realpath(str(tmpdir.join('foo')))
Example #30
0
 def runtime_setup(self, executable):
     fullpath = rpath.rabspath(self.find_executable(executable))
     i = fullpath.rfind(os.path.sep) + 1
     assert i > 0
     self._executable_path[0] = fullpath[:i]
Example #31
0
def test_rabspath_absolute_posix():
    assert rpath.rabspath('/foo') == '/foo'
Example #32
0
def test_rabspath_absolute_nt():
    curdrive, _ = os.path.splitdrive(os.getcwd())
    assert rpath.rabspath('\\foo') == '%s\\foo' % curdrive
Example #33
0
 def runtime_setup(self, executable):
     fullpath = rpath.rabspath(self.find_executable(executable))
     i = fullpath.rfind(os.path.sep) + 1
     assert i > 0
     self._executable_path[0] = fullpath[:i]
def entry_point(argv):
    # == Main execution parameters
    path = None
    selector = None
    code = ""
    number = 0
    have_number = False
    stringarg = None
    headless = True
    # == Other parameters
    poll = False
    interrupts = True
    trace = False
    trace_important = False
    
    space = prebuilt_space
    idx = 1
    try:
        while idx < len(argv):
            arg = argv[idx]
            idx += 1
            if arg in ["-h", "--help"]:
                _usage(argv)
                return 0
            elif arg in ["-j", "--jit"]:
                jitarg, idx = get_parameter(argv, idx, arg)
                jit.set_user_param(interpreter.Interpreter.jit_driver, jitarg)
            elif arg in ["-n", "--number"]:
                number, idx = get_int_parameter(argv, idx, arg)
                have_number = True
            elif arg in ["-m", "--method"]:
                selector, idx = get_parameter(argv, idx, arg)
            elif arg in ["-t", "--trace"]:
                trace = True
            elif arg in ["-T"]:
                trace_important = True
            elif arg in ["-s", "--safe-trace"]:
                space.omit_printing_raw_bytes.activate()
            elif arg in ["-p", "--poll"]:
                poll = True
            elif arg in ["-a", "--arg"]:
                stringarg, idx = get_parameter(argv, idx, arg)
            elif arg in ["-r", "--run"]:
                code, idx = get_parameter(argv, idx, arg)
            elif arg in ["-i", "--no-interrupts"]:
                interrupts = False
            elif arg in ["-P", "--process"]:
                headless = False
            elif arg in ["--hacks"]:
                space.run_spy_hacks.activate()
            elif arg in ["-S"]:
                space.strategy_factory.no_specialized_storage.activate()
            elif arg in ["-u"]:
                from spyvm.plugins.vmdebugging import stop_ui_process
                stop_ui_process()
            elif arg in ["-l", "--storage-log"]:
                space.strategy_factory.logger.activate()
            elif arg in ["-L", "--storage-log-aggregate"]:
                space.strategy_factory.logger.activate(aggregate=True)
            elif path is None:
                path = arg
            else:
                _usage(argv)
                return -1
        
        if path is None:
            path = "Squeak.image"
        if code and selector:
            raise error.Exit("Cannot handle both -r and -m.")
    except error.Exit as e:
        print_error("Parameter error: %s" % e.msg)
        return 1
    
    path = rpath.rabspath(path)
    try:
        stream = squeakimage.Stream(filename=path)
    except OSError as e:
        print_error("%s -- %s (LoadError)" % (os.strerror(e.errno), path))
        return 1
    
    # Load & prepare image and environment
    image = squeakimage.ImageReader(space, stream).create_image()
    interp = interpreter.Interpreter(space, image,
                trace=trace, trace_important=trace_important,
                evented=not poll, interrupts=interrupts)
    space.runtime_setup(argv[0], path)
    print_error("") # Line break after image-loading characters
    
    # Create context to be executed
    if code or selector:
        if not have_number:
            w_receiver = space.w_nil
        else:
            w_receiver = space.wrap_int(number)
        if code:
            selector = compile_code(interp, w_receiver, code)
        s_frame = create_context(interp, w_receiver, selector, stringarg)
        if headless:
            space.headless.activate()
            context = s_frame
        else:
            create_process(interp, s_frame)
            context = active_context(space)
    else:
        context = active_context(space)
    
    w_result = execute_context(interp, context)
    print result_string(w_result)
    return 0
def entry_point(argv):
    idx = 1
    path = None
    number = 0
    benchmark = None
    trace = False
    evented = True
    stringarg = ""
    code = None
    as_benchmark = False

    while idx < len(argv):
        arg = argv[idx]
        if arg in ["-h", "--help"]:
            _usage(argv)
            return 0
        elif arg in ["-j", "--jit"]:
            _arg_missing(argv, idx, arg)
            jitarg = argv[idx + 1]
            idx += 1
            jit.set_user_param(interpreter.Interpreter.jit_driver, jitarg)
        elif arg in ["-n", "--number"]:
            _arg_missing(argv, idx, arg)
            number = int(argv[idx + 1])
            idx += 1
        elif arg in ["-m", "--method"]:
            _arg_missing(argv, idx, arg)
            benchmark = argv[idx + 1]
            idx += 1
        elif arg in ["-t", "--trace"]:
            trace = True
        elif arg in ["-p", "--poll_events"]:
            evented = False
        elif arg in ["-a", "--arg"]:
            _arg_missing(argv, idx, arg)
            stringarg = argv[idx + 1]
            idx += 1
        elif arg in ["-r", "--run"]:
            _arg_missing(argv, idx, arg)
            code = argv[idx + 1]
            as_benchmark = False
            idx += 1
        elif arg in ["-b", "--benchmark"]:
            _arg_missing(argv, idx, arg)
            code = argv[idx + 1]
            as_benchmark = True
            idx += 1
        elif path is None:
            path = argv[idx]
        else:
            _usage(argv)
            return -1
        idx += 1

    if path is None:
        path = "Squeak.image"

    path = rpath.rabspath(path)
    try:
        f = open_file_as_stream(path, mode="rb", buffering=0)
    except OSError as e:
        os.write(2, "%s -- %s (LoadError)\n" % (os.strerror(e.errno), path))
        return 1
    try:
        imagedata = f.readall()
    finally:
        f.close()

    image_reader = squeakimage.reader_for_image(
        space, squeakimage.Stream(data=imagedata))
    image = create_image(space, image_reader)
    interp = interpreter.Interpreter(space,
                                     image,
                                     image_name=path,
                                     trace=trace,
                                     evented=evented)
    space.runtime_setup(argv[0])
    if benchmark is not None:
        return _run_benchmark(interp, number, benchmark, stringarg)
    elif code is not None:
        return _run_code(interp, code, as_benchmark=as_benchmark)
    else:
        _run_image(interp)
        return 0