def check_requires(data): requires = get_poc_requires(data) requires = [ i.strip().strip('"').strip("'") for i in requires.split(',') ] if requires else [''] if requires[0]: poc_name = get_poc_name(data) info_msg = 'PoC script "{0}" requires "{1}" to be installed'.format( poc_name, ','.join(requires)) logger.info(info_msg) try: for r in requires: if ":" in r: rows = r.split(":") if len(rows) == 2: r, module = rows else: module = rows[-1] r = ''.join(rows[:-1]) __import__(module) else: __import__(r) except ImportError: err_msg = 'try install with "python -m pip install {0}"'.format( r) logger.error(err_msg) raise SystemExit
def check_requires(data): requires = get_poc_requires(data) requires = [ i.strip().strip('"').strip("'") for i in requires.split(',') ] if requires else [''] if requires[0]: poc_name = get_poc_name(data) info_msg = 'PoC script "{0}" requires "{1}" to be installed'.format( poc_name, ', '.join(requires)) logger.info(info_msg) try: for r in requires: r = r.replace(' ', '') install_name, import_name = (r.split(':') + [''])[0:2] t = re.split('>|<|=|~', install_name) if len(t) > 1: install_name = t[0] if not import_name: import_name = install_name m = __import__(import_name) ver = m.__version__ if hasattr(m, '__version__') else '' logger.info(f'{install_name} {ver} has been installed') except ImportError: err_msg = f'{install_name} not found, try install with "python -m pip install {install_name}"' logger.error(err_msg) raise SystemExit
def command_list(self, *args, **kwargs): # 展现所有可用的poc search_result = [] tb = prettytable.PrettyTable(["Index", "Path", "Name"]) index = 0 for tmp_module in self.main_modules_dirs: found = os.path.join(paths.POCSUITE_ROOT_PATH, tmp_module + ".py") with open(found, encoding='utf-8') as f: code = f.read() name = get_poc_name(code) tb.add_row([str(index), tmp_module, name]) search_result.append(tmp_module) index += 1 data_to_stdout("\n" + tb.get_string() + "\n") self.last_search = search_result