Best Python code snippet using radish
test_modules.py
Source:test_modules.py
...9 """10 Test SpiderFoot module loading11 """12 @staticmethod13 def load_modules(sf):14 # Go through each module in the modules directory with a .py extension15 sfModules = dict()16 mod_dir = sf.myPath() + '/modules/'17 for filename in os.listdir(mod_dir):18 if not filename.startswith("sfp_"):19 continue20 if not filename.endswith(".py"):21 continue22 modName = filename.split('.')[0]23 # Load and instantiate the module24 sfModules[modName] = dict()25 mod = __import__('modules.' + modName, globals(), locals(), [modName])26 sfModules[modName]['object'] = getattr(mod, modName)()27 mod_dict = sfModules[modName]['object'].asdict()28 sfModules[modName].update(mod_dict)29 return sfModules30 def test_module_use_cases_are_valid(self):31 sf = SpiderFoot(self.default_options)32 valid_use_cases = ["Footprint", "Passive", "Investigate"]33 sfModules = self.load_modules(sf)34 for module in sfModules:35 m = sfModules[module]36 for group in m.get('group'):37 self.assertIn(group, valid_use_cases)38 def test_module_labels_are_valid(self):39 sf = SpiderFoot(self.default_options)40 valid_labels = ["errorprone", "tor", "slow", "invasive", "apikey", "tool"]41 sfModules = self.load_modules(sf)42 for module in sfModules:43 m = sfModules[module]44 for label in m.get('labels'):45 self.assertIn(label, valid_labels)46 def test_module_categories_are_valid(self):47 sf = SpiderFoot(self.default_options)48 valid_categories = ["Content Analysis", "Crawling and Scanning", "DNS",49 "Leaks, Dumps and Breaches", "Passive DNS",50 "Public Registries", "Real World", "Reputation Systems",51 "Search Engines", "Secondary Networks", "Social Media"]52 sfModules = self.load_modules(sf)53 for module in sfModules:54 m = sfModules[module]55 self.assertIsInstance(m.get('cats'), list)56 self.assertTrue(len(m.get('cats')) <= 1)57 if module in ["sfp__stor_db", "sfp__stor_stdout"]:58 continue59 for cat in m.get('cats', list()):60 self.assertIn(cat, valid_categories)61 def test_modules_with_api_key_have_apiKeyInstructions(self):62 sf = SpiderFoot(self.default_options)63 sfModules = self.load_modules(sf)64 for module in sfModules:65 m = sfModules[module]66 self.assertTrue(m.get('meta'))67 self.assertIsInstance(m.get('meta'), dict)68 meta = m.get('meta')69 if 'apikey' in m.get('labels'):70 self.assertIn('dataSource', meta)71 self.assertIsInstance(meta.get('dataSource').get('apiKeyInstructions'), list)72 self.assertTrue(meta.get('dataSource').get('apiKeyInstructions'))73 def test_modules_with_api_key_options_have_apikey_label(self):74 sf = SpiderFoot(self.default_options)75 sfModules = self.load_modules(sf)76 for module in sfModules:77 m = sfModules[module]78 for opt in m.get('opts'):79 if "api_key" in opt:80 self.assertIn("apikey", m.get('labels'))81 def test_modules_with_invasive_flag_are_not_in_passive_use_case(self):82 sf = SpiderFoot(self.default_options)83 sfModules = self.load_modules(sf)84 for module in sfModules:85 m = sfModules[module]86 if "Passive" in m.get('group'):87 self.assertNotIn("invasive", m.get('labels', list()))88 def test_module_watched_events_are_valid(self):89 sf = SpiderFoot(self.default_options)90 sf.dbh = SpiderFootDb(self.default_options, False)91 valid_events = []92 for event in sf.dbh.eventTypes():93 valid_events.append(event[1])94 sfModules = self.load_modules(sf)95 for module in sfModules:96 m = sfModules[module]97 for watched_event in m.get('consumes'):98 if watched_event == '*':99 continue100 self.assertIn(watched_event, valid_events)101 def test_module_produced_events_are_valid(self):102 sf = SpiderFoot(self.default_options)103 sf.dbh = SpiderFootDb(self.default_options, False)104 valid_events = []105 for event in sf.dbh.eventTypes():106 valid_events.append(event[1])107 sfModules = self.load_modules(sf)108 for module in sfModules:109 m = sfModules[module]110 provides = m.get('provides')111 if not provides:112 continue113 for produced_event in provides:114 self.assertIn(produced_event, valid_events)115 def test_each_module_option_has_a_description(self):116 sf = SpiderFoot(self.default_options)117 sfModules = self.load_modules(sf)118 for module in sfModules:119 m = sfModules[module]120 if module in ["sfp__stor_db", "sfp__stor_stdout"]:121 continue122 # check len(options) == len(option descriptions)123 if m.get('opts'):124 self.assertEqual(f"{module} opts: {len(m.get('opts').keys())}", f"{module} opts: {len(m.get('optdescs').keys())}")125 def test_required_module_properties_are_present_and_valid(self):126 sf = SpiderFoot(self.default_options)127 sfModules = self.load_modules(sf)128 for module in sfModules:129 m = sfModules[module]130 self.assertTrue(m.get('object'))131 self.assertTrue(m.get('name'))132 self.assertTrue(m.get('meta'))133 self.assertTrue(m.get('descr'))134 self.assertTrue(m.get('consumes'))135 self.assertIsInstance(m.get('cats'), list)136 self.assertIsInstance(m.get('labels'), list)137 self.assertIsInstance(m.get('provides'), list)138 self.assertIsInstance(m.get('consumes'), list)139 self.assertIsInstance(m.get('meta'), dict)140 # output modules do not have use cases, categories or produced events141 if module in ["sfp__stor_db", "sfp__stor_stdout"]:...
build.py
Source:build.py
1from autotest_lib.client.virt import kvm_installer2class BrewInstaller:3 """4 Class that handles installing KVM from the brew build service.5 It uses yum to install and remove packages.6 """7 def __init__(self, test, params):8 """9 Class constructor. Sets default paths, and sets up class attributes10 @param test: kvm test object11 @param params: Dictionary with test arguments12 """13 default_pkg_list = 'kmod-kvm kvm kvm-qemu-img kvm-tools'14 default_qemu_bin_paths = '/usr/bin/qemu-kvm /usr/bin/qemu-img'15 default_base_url = 'http://download.devel.redhat.com/brewroot/'\16 'packages/'17 default_release = 'kvm-83-142.el5'18 default_extra_modules = None19 # Checking if all required dependencies are available20 os_dep.command("rpm")21 os_dep.command("yum")22 self.pkg_list = params.get("pkg_list", default_pkg_list).split()23 self.base_url = params.get("base_pgk_url", default_base_url)24 self.qemu_bin_paths = params.get("qemu_bin_paths",25 default_qemu_bin_paths).split()26 self.release = params.get('release',default_release)27 # Are we going to load modules?28 load_modules = params.get('load_modules')29 if not load_modules:30 self.load_modules = True31 elif load_modules == 'yes':32 self.load_modules = True33 elif load_modules == 'no':34 self.load_modules = False35 self.extra_modules = params.get("extra_modules", default_extra_modules)36 self.srcdir = test.srcdir37 self.test_bindir = test.bindir38 def __clean_previous_installs(self):39 """40 Remove all rpms previously installed.41 """42 kill_qemu_processes()43 removable_packages = ""44 for pkg in self.pkg_list:45 removable_packages += " %s" % pkg46 utils.system("yum remove -y %s" % removable_packages)47 def __get_packages(self):48 """49 Downloads the packages using wget from brew source.50 """51 if not os.path.isdir(self.srcdir):52 os.makedirs(self.srcdir)53 os.chdir(self.srcdir)54 old_rpm_list = glob.glob("*.rpm")55 for rpm in old_rpm_list:56 os.remove(rpm)57 arch = utils.get_arch()58 built_list = self.release.split('-')59 for i in built_list:60 self.base_url = self.base_url + i + '/'61 self.base_url = self.base_url + arch + '/'62 for i in self.pkg_list:63 pkg_url = self.base_url + i + '-' + '-'.join(built_list[1:]) + \64 '.' + arch + '.rpm'65 os.system("wget -q " + pkg_url)66 def __install_packages(self):67 """68 Install all relevant packages that was just downloaded.69 """70 os.chdir(self.srcdir)71 installable_packages = ""72 rpm_list = glob.glob("*.rpm")73 for rpm in rpm_list:74 installable_packages += " %s" % rpm75 utils.system("yum install --nogpgcheck -y %s" % installable_packages)76 def install(self):77 self.__clean_previous_installs()78 self.__get_packages()79 self.__install_packages()80 create_symlinks(test_bindir=self.test_bindir,81 bin_list=self.qemu_bin_paths)82 if self.load_modules:83 load_kvm_modules(load_stock=True, extra_modules=self.extra_modules)84def run_build(test, params, env):85 """86 Installs KVM using the selected install mode. Most install methods will87 take kvm source code, build it and install it to a given location.88 @param test: kvm test object.89 @param params: Dictionary with test parameters.90 @param env: Test environment.91 """92 srcdir = params.get("srcdir", test.srcdir)93 params["srcdir"] = srcdir94 try:95 installer_object = kvm_installer.make_installer(params)96 installer_object.set_install_params(test, params)97 installer_object.install()98 env.register_installer(installer_object)99 except Exception, e:100 # if the build/install fails, don't allow other tests101 # to get a installer.102 msg = "KVM install failed: %s" % (e)103 env.register_installer(kvm_installer.FailedInstaller(msg))...
adapter.py
Source:adapter.py
1#!/usr/bin/python2# Generates a JS adapter module from an IDL file3import argparse4import os5import sys6import traceback7import itl_parser8arg_parser = argparse.ArgumentParser()9arg_parser.add_argument('itl_in')10arg_parser.add_argument('-o', dest='js_out', default=None)11arg_parser.add_argument('--node', dest='do_node', action='store_true')12arg_parser.add_argument('--name', dest='component_name', default=None)13args = arg_parser.parse_args(sys.argv[1:])14# TODO: actually re-support node scripts15itl_path = args.itl_in16itl_filename = os.path.basename(itl_path)17basename, _ = os.path.splitext(itl_filename)18if args.js_out:19 js_path = os.path.abspath(args.js_out)20 outpath = os.path.dirname(js_path)21else:22 outpath = os.path.abspath('out')23 js_path = os.path.join(outpath, basename + '.js')24if args.component_name:25 component_name = args.component_name26else:27 component_name = basename + 'Component'28srcpath = os.path.dirname(__file__)29def main():30 contents = open(itl_path).read()31 component = itl_parser.parse(contents)32 ensure_path(outpath)33 write_js_module(component)34def ensure_path(path):35 try:36 os.makedirs(path)37 except:38 pass39# NodeJS wrapper module40def write_js_module(component):41 def escape(s):42 return s.replace('\\', '/')43 def function(func, n_indent, is_internal=False):44 global num_locals45 ret = ''46 params = ', '.join(['x' + str(i) for i in range(len(func.params))])47 if is_internal:48 decl = 'function {}'.format(func.name)49 else:50 decl = '"{}": function'.format(func.exname)51 ret += tab * n_indent + '{}({}) {{\n'.format(decl, params)52 for i in range(len(func.body)):53 sexpr = func.body[i]54 ret += tab * (n_indent + 1)55 if func.results and i == len(func.body) - 1:56 ret += 'return ' + sexpr.as_js()57 else:58 ret += sexpr.as_js()59 ret += ';\n'60 if is_internal:61 ret += tab * n_indent + '};\n'62 else:63 ret += tab * n_indent + '},\n'64 return ret65 # Paths and setup66 template_path = os.path.join(srcpath, 'wrapper_module_template.js')67 js_str = open(template_path).read()68 tab = ' '69 js_str = js_str.replace('/**COMPONENT_NAME**/', component_name)70 module_names = ''71 load_modules = '' 72 for mod in component.modules:73 name = mod.name74 path = mod.path75 module_names += tab * 2 + 'let {};\n'.format(name)76 load_modules += tab * 2 + '{} = await loadModule("{}", {{\n'.format(name, path)77 for imp, funcs in mod.imports.items():78 load_modules += tab * 3 + imp + ': {\n'79 for func in funcs:80 load_modules += function(func, n_indent=4)81 load_modules += tab * 3 + '},\n'82 load_modules += tab * 2 + '});\n'83 # call any static constructors84 # TODO: configure this; have a start function in ITL to call these85 load_modules += tab * 2 + '{}._initialize();\n'.format(name)86 js_str = js_str.replace('/**MODULE_NAMES**/', module_names)87 js_str = js_str.replace('/**LOAD_MODULES**/', load_modules)88 component_functions = ''89 for ty in component.types.values():90 component_functions += ty.js_decls(n_indent=2)91 for func in component.funcs:92 component_functions += function(func, n_indent=2, is_internal=True)93 js_str = js_str.replace('/**COMPONENT_FUNCTIONS**/\n', component_functions)94 exports = ''95 for func in component.exports:96 exports += function(func, n_indent=3)97 js_str = js_str.replace('/**EXPORTS**/\n', exports)98 open(js_path, 'w').write(js_str)99 print('Wrote JS module', js_path)100if __name__ == '__main__':101 try:102 main()103 except Exception as e:104 trace = traceback.format_exc(e)105 print(trace)...
addons.py
Source:addons.py
...7 module = modules[app]8 if hasattr(module, value):9 dest.extend(getattr(module, value)())10 return dest11def load_modules():12 modules = {}13 for app in settings.INSTALLED_APPS:14 j_app = app.startswith('juntagrico') and app != 'juntagrico'15 if j_app and util.find_spec(app + '.juntagricoapp') is not None:16 modules[app] = importlib.import_module(app + '.juntagricoapp')17 return modules18def set_cache(key, value):19 cache.set(key, value)20def get_cache(key):21 return cache.get(key, [])22def set_admin_menus(admin_menus):23 set_cache('admin_menus', admin_menus)24def set_user_menus(user_menus):25 set_cache('user_menus', user_menus)26def get_admin_menus():27 return get_cache('admin_menus')28def get_user_menus():29 return get_cache('user_menus')30def get_member_inlines():31 return load_config('member_inlines', [], load_modules())32def get_job_inlines():33 return load_config('job_inlines', [], load_modules())34def get_jobextra_inlines():35 return load_config('jobextra_inlines', [], load_modules())36def get_jobextratype_inlines():37 return load_config('jobextratype_inlines', [], load_modules())38def get_subsize_inlines():39 return load_config('subsize_inlines', [], load_modules())40def get_subtype_inlines():41 return load_config('subtype_inlines', [], load_modules())42def get_area_inlines():43 return load_config('area_inlines', [], load_modules())44def get_assignment_inlines():45 return load_config('assignment_inlines', [], load_modules())46def get_share_inlines():47 return load_config('share_inlines', [], load_modules())48def get_depot_inlines():49 return load_config('depot_inlines', [], load_modules())50def get_extrasub_inlines():51 return load_config('extrasub_inlines', [], load_modules())52def get_extrasubcat_inlines():53 return load_config('extrasubcat_inlines', [], load_modules())54def get_extrasubtype_inlines():55 return load_config('extrasubtype_inlines', [], load_modules())56def get_delivery_inlines():57 return load_config('delivery_inlines', [], load_modules())58def get_jobtype_inlines():59 return load_config('jobtype_inlines', [], load_modules())60def get_otjob_inlines():61 return load_config('otjob_inlines', [], load_modules())62def get_subscription_inlines():...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!