Exemple #1
0
    def run(self, args):
        configurationModulePath = Path(args.configuration)
        configuration = SourceFileLoader(
            'configuration', str(configurationModulePath)).load_module()
        if not configuration.COMPETITION_TYPE in CliToolCompetition.AVAILABLE_COMPETITIONS:
            print(
                "Competition type " + configuration.COMPETITION_TYPE +
                " is not supported. Please choose from " +
                str(sorted(CliToolCompetition.AVAILABLE_COMPETITIONS.keys())) +
                ".")
            sys.exit(1)

        competitionClass = CliToolCompetition.AVAILABLE_COMPETITIONS.get(
            configuration.COMPETITION_TYPE)
        competitionInstance = competitionClass.configure(
            configurationModulePath,
            verbose=args.verbose,
            silent=args.silent,
            colored=args.colored,
            outputDir=Path(args.output) if args.output else None)
        self.competitionInstance = competitionInstance

        if args.liveplot:
            competitionInstance.addResultCallback(self.drawCallback)

        competitionInstance.run()

        if args.finalplot:
            self.draw(competitionInstance.results())
Exemple #2
0
def import_module_from_module_path(path):
    """import module from module_path

    :type path: str
    :param path: path to import module
    :return module for
    :rtype module
    """
    return SourceFileLoader('', path).load_module()
Exemple #3
0
def import_object(modname, objpath, objtype='', attrgetter=safe_getattr, warningiserror=False):
    # type: (str, List[unicode], str, Callable[[Any, unicode], Any], bool) -> Any
    if objpath:
        logger.debug('[autodoc] from %s import %s', modname, '.'.join(objpath))
    else:
        logger.debug('[autodoc] import %s', modname)

    try:
        module = import_module(modname, warningiserror=warningiserror)
        import os
        if hasattr(module, '__file__') and os.path.isfile('{}i'.format(module.__file__)):
            try:
                from importlib._bootstrap import spec_from_loader
                from importlib._bootstrap_external import SourceFileLoader
                from importlib._bootstrap import module_from_spec
                spec = spec_from_loader(modname, SourceFileLoader(modname, '{}i'.format(module.__file__)))
                module = module_from_spec(spec)
                spec.loader.exec_module(module)
            except BaseException as e:
                raise e
        logger.debug('[autodoc] => %r', module)
        obj = module
        parent = None
        object_name = None
        for attrname in objpath:
            parent = obj
            logger.debug('[autodoc] getattr(_, %r)', attrname)
            obj = attrgetter(obj, attrname)
            logger.debug('[autodoc] => %r', obj)
            object_name = attrname
        return [module, parent, object_name, obj]
    except (AttributeError, ImportError) as exc:
        if objpath:
            errmsg = ('autodoc: failed to import %s %r from module %r' %
                      (objtype, '.'.join(objpath), modname))
        else:
            errmsg = 'autodoc: failed to import %s %r' % (objtype, modname)

        if isinstance(exc, ImportError):
            # import_module() raises ImportError having real exception obj and
            # traceback
            real_exc, traceback_msg = exc.args
            if isinstance(real_exc, SystemExit):
                errmsg += ('; the module executes module level statement '
                           'and it might call sys.exit().')
            elif isinstance(real_exc, ImportError) and real_exc.args:
                errmsg += '; the following exception was raised:\n%s' % real_exc.args[0]
            else:
                errmsg += '; the following exception was raised:\n%s' % traceback_msg
        else:
            errmsg += '; the following exception was raised:\n%s' % traceback.format_exc()

        if PY2:
            errmsg = errmsg.decode('utf-8')  # type: ignore
        logger.debug(errmsg)
        raise ImportError(errmsg)
Exemple #4
0
 def import_module(modname, warningiserror=False):
     module = original_import_module(modname, warningiserror)
     if hasattr(module, '__file__') and os.path.isfile('{}i'.format(
             module.__file__)):
         # merge external spec into the imported module
         from importlib._bootstrap import spec_from_loader
         from importlib._bootstrap_external import SourceFileLoader
         from importlib._bootstrap import module_from_spec
         spec = spec_from_loader(
             modname,
             SourceFileLoader(modname, '{}i'.format(module.__file__)))
         # print(spec.loader.get_code(modname).co_names)
         module = module_from_spec(spec)
         spec.loader.exec_module(module)
Exemple #5
0
def import_class_from_module_path(path, class_name):
    """import class from module path

    :type path: str
    :type class_name: str
    :param path: path of module
    :param class_name: class name to load class from module

    :return: class

    :raise FileNotFoundError
    if path not found
    :raise AttributeError, ImportError
    """
    try:
        module_ = SourceFileLoader('', path).load_module()
        return getattr(module_, class_name)
    except FileNotFoundError:
        raise FileNotFoundError("%s not found" % path)
    except AttributeError:
        raise AttributeError("%s class not found in %s" % (class_name, path))
Exemple #6
0
def render(resource):
    if resource is None:
        return DEFAULT_LAYOUT
    if resource.startswith(DASH_UPLOAD_RESULTS_FLAG):
        loads = json.loads(str(homepage.layout[INVISIBLE_ID].children))
        return render_layout(
            add_dash(get_upload_dash(*loads), UPLOAD_RESULT_URL_PART))
    dir_path = os.path.join(get_directory(), unquote(resource))
    full_path = os.path.join(dir_path, unquote(resource) + ".py")
    with open(full_path) as f:
        if 'Dash' not in f.read():
            return error_layout("Этот файл не содержит объект Dash")
    try:
        dash_module = SourceFileLoader(resource[:-3], full_path).load_module()
    except ImportError as ie:
        return render(resource) if pip_install(ie.__str__().split(
            "'")[1]) == 0 else error_layout("Невозможно загрузить зависимости")
    except:
        error = traceback.format_exc().split("call_with_frames_removed\n",
                                             1)[1].replace(
                                                 get_directory() + "/", "")
        return error_layout(error)
    else:
        return render_layout(add_dash(dash_module, resource))
Exemple #7
0
def load_class_from_source_path(module_path, class_name):
    from importlib._bootstrap_external import SourceFileLoader

    module_ = SourceFileLoader('', module_path).load_module()
    return getattr(module_, class_name)
 def load_input_shape_from_external_file(self, filepath):
     model_file = SourceFileLoader('conf', filepath).load_module()
     return model_file.input_shape
Exemple #9
0
    def start(self, until=0):
        if self._running:
            return 10

        if until > 0:
            self.end_ptr = utils.parse_ptr(until)
            if self.end_ptr == 0:
                # invalid end pointer
                return 1
        if self.context is None:
            err = self.setup()
            if err > 0:
                return 200 + err

        # calculate the start address
        address = self._current_instruction
        if address == 0:
            if self.uc._arch == unicorn.UC_ARCH_ARM:
                address = self.uc.reg_read(unicorn.arm_const.UC_ARM_REG_PC)
            elif self.uc._arch == unicorn.UC_ARCH_ARM64:
                address = self.uc.reg_read(unicorn.arm64_const.UC_ARM64_REG_PC)
            else:
                # unsupported arch
                return 2

        if until > 0:
            self.log_to_ui('[*] start emulation from %s to %s' %
                           (hex(address), hex(self.end_ptr)))
        else:
            self.log_to_ui('[*] stepping %s' % hex(address))
        self.dwarf.get_bus().emit('emulator_start')

        if self.thumb:
            address = address | 1

        # invalidate prefs before start
        self.invalida_configurations()

        # load callbacks if needed
        if self.callbacks_path is not None and self.callbacks_path != '':
            try:
                spec = spec_from_loader(
                    "callbacks",
                    SourceFileLoader("callbacks", self.callbacks_path))
                self.callbacks = module_from_spec(spec)
                spec.loader.exec_module(self.callbacks)
            except Exception as e:
                self.log_to_ui('[*] failed to load callbacks: %s' % str(e))
                # reset callbacks path
                self.dwarf.get_prefs().put(prefs.EMULATOR_CALLBACKS_PATH, '')
                self.callbacks_path = ''
                self.callbacks = None
        else:
            self.callbacks = None

        # until is 0 (i.e we are stepping)
        if until == 0:
            self.stepping = [True, False]
            self.end_ptr = address + (self.dwarf.pointer_size * 2)
        else:
            self.stepping = [False, False]
        Thread(target=self.__start, args=(address, self.end_ptr)).start()
        return 0
Exemple #10
0
    def emulate(self,
                until=0,
                step_mode=STEP_MODE_NONE,
                user_arch=None,
                user_mode=None,
                cs_arch=None,
                cs_mode=None):
        if self.isRunning():
            raise self.EmulatorAlreadyRunningError()

        if isinstance(until, str):
            try:
                until = int(until, 16)
            except ValueError:
                until = 0

        if until and isinstance(until, int):
            self.end_ptr = utils.parse_ptr(until)
            if self.end_ptr == 0:
                # invalid end pointer
                raise self.EmulatorSetupFailedError('Invalid EndPtr')

        if self.context is None:
            err = self.setup(user_arch=user_arch,
                             user_mode=user_mode,
                             cs_arch=cs_arch,
                             cs_mode=cs_mode)
            if err > 0:
                # make sure context is None if setup failed for any reason. we want a clean setup later
                self.context = None

                err_msg = 'unhandled error'
                if err == self.ERR_INVALID_TID:
                    err_msg = 'invalid thread id'
                elif err == self.ERR_INVALID_CONTEXT:
                    err_msg = 'invalid context'
                raise self.EmulatorSetupFailedError('Setup failed: %s' %
                                                    err_msg)

        # calculate the start address
        address = self._next_instruction
        if address == 0:
            if self.uc._arch == unicorn.UC_ARCH_ARM:
                address = self.uc.reg_read(unicorn.arm_const.UC_ARM_REG_PC)
            elif self.uc._arch == unicorn.UC_ARCH_ARM64:
                address = self.uc.reg_read(unicorn.arm64_const.UC_ARM64_REG_PC)
            elif self.uc._arch == unicorn.UC_ARCH_X86 and self.uc._mode == unicorn.UC_MODE_32:
                address = self.uc.reg_read(unicorn.x86_const.UC_X86_REG_EIP)
            elif self.uc._arch == unicorn.UC_ARCH_X86 and self.uc._mode == unicorn.UC_MODE_64:
                address = self.uc.reg_read(unicorn.x86_const.UC_X86_REG_RIP)
            else:
                raise self.EmulatorSetupFailedError('Unsupported arch')

        if until > 0:
            self.log_to_ui('[*] start emulation from %s to %s' %
                           (hex(address), hex(self.end_ptr)))
        else:
            if step_mode == STEP_MODE_NONE or step_mode == STEP_MODE_SINGLE:
                self.log_to_ui('[*] stepping %s' % hex(address))
            elif step_mode == STEP_MODE_FUNCTION:
                self.log_to_ui('[*] stepping to next function call')
            elif step_mode == STEP_MODE_JUMP:
                self.log_to_ui('[*] stepping to next jump')
        self.onEmulatorStart.emit()

        # invalidate prefs before start
        self.invalida_configurations()

        # load callbacks if needed
        if self.callbacks_path is not None and self.callbacks_path != '':
            try:
                spec = spec_from_loader(
                    "callbacks",
                    SourceFileLoader("callbacks", self.callbacks_path))
                self.callbacks = module_from_spec(spec)
                spec.loader.exec_module(self.callbacks)
            except Exception as e:
                self.log_to_ui('[*] failed to load callbacks: %s' % str(e))
                # reset callbacks path
                self._prefs.put(prefs.EMULATOR_CALLBACKS_PATH, '')
                self.callbacks_path = ''
                self.callbacks = None
        else:
            self.callbacks = None

        # until is 0 (i.e we are stepping)
        if until == 0 and step_mode == STEP_MODE_NONE:
            self.step_mode = STEP_MODE_SINGLE
        else:
            self.step_mode = step_mode

        self._start_address = address
        if self.thumb:
            if self._start_address % 2 == 0:
                self._start_address = self._start_address | 1
        else:
            if self._start_address % 2 != 0:
                self._start_address -= 1
        self._end_address = self.end_ptr
        self._setup_done = True
        self.start()
Exemple #11
0
    def emulate(self, until=0):
        if self.isRunning():
            raise self.EmulatorAlreadyRunningError()

        if isinstance(until, str):
            try:
                until = int(until, 16)
            except ValueError:
                until = 0

        if until and isinstance(until, int):
            self.end_ptr = utils.parse_ptr(until)
            if self.end_ptr == 0:
                # invalid end pointer
                raise self.EmulatorSetupFailedError('Invalid EndPtr')

        if self.context is None:
            if not self.setup():
                raise self.EmulatorSetupFailedError('Setup failed')

        # calculate the start address
        address = self._current_instruction
        if address == 0:
            if self.uc._arch == unicorn.UC_ARCH_ARM:
                address = self.uc.reg_read(unicorn.arm_const.UC_ARM_REG_PC)
            elif self.uc._arch == unicorn.UC_ARCH_ARM64:
                address = self.uc.reg_read(unicorn.arm64_const.UC_ARM64_REG_PC)
            elif self.uc._arch == unicorn.UC_ARCH_X86 and self.uc._mode == unicorn.UC_MODE_32:
                address = self.uc.reg_read(unicorn.x86_const.UC_X86_REG_EIP)
            elif self.uc._arch == unicorn.UC_ARCH_X86 and self.uc._mode == unicorn.UC_MODE_64:
                address = self.uc.reg_read(unicorn.x86_const.UC_X86_REG_RIP)
            else:
                raise self.EmulatorSetupFailedError('Unsupported arch')

        if until > 0:
            self.log_to_ui('[*] start emulation from %s to %s' %
                           (hex(address), hex(self.end_ptr)))
        else:
            self.log_to_ui('[*] stepping %s' % hex(address))
        self.onEmulatorStart.emit()

        if self.thumb:
            address = address | 1

        # invalidate prefs before start
        self.invalida_configurations()

        # load callbacks if needed
        if self.callbacks_path is not None and self.callbacks_path != '':
            try:
                spec = spec_from_loader(
                    "callbacks",
                    SourceFileLoader("callbacks", self.callbacks_path))
                self.callbacks = module_from_spec(spec)
                spec.loader.exec_module(self.callbacks)
            except Exception as e:
                self.log_to_ui('[*] failed to load callbacks: %s' % str(e))
                # reset callbacks path
                self._prefs.put(prefs.EMULATOR_CALLBACKS_PATH, '')
                self.callbacks_path = ''
                self.callbacks = None
        else:
            self.callbacks = None

        # until is 0 (i.e we are stepping)
        if until == 0:
            self.stepping = [True, False]
            # self.end_ptr = address + (self.dwarf.pointer_size * 2) stupid
        else:
            self.stepping = [False, False]

        self._start_address = address
        self._end_address = self.end_ptr
        self._setup_done = True
        self.start()
 def get_data(self, path: str):
     data = SourceFileLoader.get_data(self, path)
     if check_if_use_moshmosh_sys(data):
         return perform_extension(data, self.path)
     return data
 def __init__(self, loader: SourceFileLoader):
     SourceFileLoader.__init__(self, loader.name, loader.path)