def __init__(self, name): tpldir = TemplateLookup(directories=[Template.__TEMPLATE_DIR]) try: name = name.replace(' ', '_') self.tpl = template.Template(filename='%s/%s.tpl' % (Template.__TEMPLATE_DIR, name), lookup=tpldir) except IOError, e: self.tpl = template.Template( filename='%s/%s.tpl' % (Template.__TEMPLATE_DIR, Template.__DEFAULT), lookup=tpldir)
def run(self,data): stats = self.fr.after_strategies_dict["statistic"] jobdir = self.settings["JOBDIR"] htf = self.settings["REPORT_HTML_TEMPLATE"] f=open(htf,"r") data=f.read().decode("gbk") f.close() casef = "wget ftp://%s%s"%(socket.gethostname(),os.path.abspath(os.path.join(jobdir,"result.xls"))) t = template.Template(data, output_encoding="gbk") rf =os.path.join(jobdir,"report.html") f = open(rf,'w') f.write(t.render(STATS = stats,FTP=casef)) f.close() email_bin = self.settings['REPORT_EMAIL_BIN'] email_svr = self.settings['REPORT_EMAIL_SVR'] email_title = self.settings['REPORT_EMAIL_TITLE'] email_from = self.settings['REPORT_EMAIL_FROM'] email_to = " -t ".join(self.settings['REPORT_EMAIL_TO'].split()) log = os.path.join(jobdir,"email.log") err = os.path.join(jobdir,"email.err") cmd = "cat %s | %s -s %s -u %s -f %s -t %s >%s 2>%s "%(rf,email_bin,email_svr,email_title,email_from,email_to,log,err) print "[Strategy:%s] cmd:%s"%(self.name,cmd ) os.system(cmd)
def main(): # parse args parser = argparse.ArgumentParser(description="Scenario tests runner.") parser.add_argument('scenario_file', help="Path to scenario file.") args = parser.parse_args() scenario_file = args.scenario_file # parse config with open(scenario_file, 'r') as yaml_file: config = yaml.load(yaml_file) set_defaults(config) credentials = config['credentials'] network = config['network'] testcases = config['clusters'] # create testcase file test_template = mako_template.Template(filename=TEST_TEMPLATE_PATH) testcase_data = test_template.render(testcases=testcases, credentials=credentials, network=network) test_dir_path = tempfile.mkdtemp() print("The generated test file located at: %s" % test_dir_path) fileutils.write_to_tempfile(testcase_data, prefix='test_', suffix='.py', path=test_dir_path) # run tests os.environ['DISCOVER_DIRECTORY'] = test_dir_path return_code = os.system('bash tools/pretty_tox.sh') sys.exit(return_code)
def get_connection_info(self): return template.Template(messages.sessions.connection_info).render( url = self['url'], user = self['system_info']['results'].get('whoami', ''), host = self['system_info']['results'].get('hostname', ''), path = self['file_cd']['results'].get('cwd', '.') )
def htmlFooterTemplate(): return template.Template(''' <div style="margin-top: 10px; border-top: 1px solid #ccc;"></div> <% footerEscaped = filters.html_entities_escape(footer) %> <% footerBreaks = re.sub(r'\\r\\n?|\\n', '<br>', footerEscaped) %> <p style="margin: 20px; font-size: 11px; color: #999;">${footerBreaks}</p> ''')
def htmlTableTemplate(): return template.Template(''' % if len(results) > 0: <div style="margin:0"> <div style="overflow: auto; width: 100%;"> <table cellpadding="0" cellspacing="0" border="0" class="results" style="margin: 20px;"> <tbody> <% cols = [] %> <tr> % for key,val in results[0].items(): % if not key.startswith("_") or key == "_raw" or key == "_time": <% cols.append(key) %> <th style="text-align: left; padding: 4px 8px; border-bottom: 1px dotted #ccc;">${key|h}</th> % endif % endfor </tr> % for result in results: <tr valign="top"> % for col in cols: <td style="text-align: left; padding: 4px 8px; border-bottom: 1px dotted #ccc;">${result.get(col)|h}</td> % endfor </tr> % endfor </tbody> </table> </div> </div> % else: <div class="results" style="margin: 20px;">No results found.</div> % endif ''')
def buildPlainTextSSMetaData(): return template.Template(''' % if view_link and name and include_view_link: ${ssType.capitalize()} Title: ${name} ${ssType.capitalize()} Location: ${view_link} % endif % if ssquery and include_search: Search String: ${ssquery} % endif % if include_trigger and name and alert_type and ssType == "alert": % if alert_type == "number of events": Trigger: Saved Search [${name}]: ${alert_type} (${jobcount}) % else: Trigger: Saved Search [${name}]: ${alert_type} %endif % endif % if include_trigger_time and trigger_timeHMS and trigger_date and ssType == "alert": Trigger Time: ${trigger_timeHMS} on ${trigger_date}. % endif % if not include_inline: % if include_results_link: View results: ${results_link} % endif %endif ''')
def __init__(self, session): cmd.Cmd.__init__(self) self.session = session self.prompt = 'weevely> ' # Load all available modules self._load_modules() # Load history file self._load_history() # Set a nice intro self.intro = template.Template( messages.terminal.welcome_to_s ).render( path = self.session.get('path'), conn_info = session.get_connection_info(), version = messages.version, default_shell = self.session.get('default_shell') ) # Set default encoding utf8 reload(sys) sys.setdefaultencoding('utf8')
def GenerateResourceModule(base_dir, root_dir, api_name, api_version, discovery_doc_path, custom_resources): """Create resource.py file for given api and its discovery doc. Args: base_dir: str, Path of directory for the project. root_dir: str, Path of the resource file location within the project. api_name: str, name of the api. api_version: str, the version for the api. discovery_doc_path: str, file path to discovery doc. custom_resources: dict, dictionary of custom resource collections. Raises: WrongDiscoveryDoc: if discovery doc api name/version does not match. """ discovery_doc = resource_generator.DiscoveryDoc.FromJson( os.path.join(base_dir, root_dir, discovery_doc_path)) if discovery_doc.api_version != api_version: logging.warn( 'Discovery api version %s does not match %s, ' 'this client will be accessible via new alias.', discovery_doc.api_version, api_version) if discovery_doc.api_name != api_name: raise WrongDiscoveryDoc('api name {0}, expected {1}'.format( discovery_doc.api_name, api_name)) resource_collections = discovery_doc.GetResourceCollections(api_version) if custom_resources: # Check if this is redefining one of the existing collections. matched_resources = set([]) for collection in resource_collections: if collection.name in custom_resources: matched_resources.add(collection.name) custom_path = custom_resources[collection.name] if isinstance(custom_path, dict): collection.flat_paths.update(custom_path) elif isinstance(custom_path, basestring): collection.flat_paths[ resource_generator.DEFAULT_PATH_NAME] = custom_path # Remaining must be new custom resources. for collection_name in set( custom_resources.keys()) - matched_resources: collection_path = custom_resources[collection_name] collection_info = discovery_doc.MakeResourceCollection( collection_name, collection_path, api_version) resource_collections.append(collection_info) api_dir = os.path.join(base_dir, root_dir, api_name, api_version) if not os.path.exists(api_dir): os.makedirs(api_dir) resource_file_name = os.path.join(api_dir, 'resources.py') logging.debug('Generating resource module at %s', resource_file_name) if resource_collections: tpl = template.Template( filename=os.path.join(os.path.dirname(__file__), 'resources.tpl')) with open(resource_file_name, 'wb') as output_file: ctx = runtime.Context(output_file, collections=sorted(resource_collections), base_url=resource_collections[0].base_url) tpl.render_context(ctx)
def create_testcase_file(testcases, credentials, network, report, use_api_v2=False): # current directory, where to write reports, key files, etc, if required results_dir = os.getcwd() default_templ_dir = os.path.abspath(TEST_TEMPLATE_DIR) # create testcase file test_template = mako_template.Template(filename=TEST_TEMPLATE_PATH) testcase_data = test_template.render(testcases=testcases, credentials=credentials, network=network, report=report, results_dir=results_dir, default_templ_dir=default_templ_dir, use_api_v2=use_api_v2) test_dir_path = tempfile.mkdtemp() print("The generated test file located at: %s" % test_dir_path) fileutils.write_to_tempfile(testcase_data.encode("ASCII"), prefix='test_', suffix='.py', path=test_dir_path) # Copy both files as long as the old runner is supported shutil.copyfile(DEFAULT_STESTR_CONF, os.path.join(test_dir_path, '.stestr.conf')) return test_dir_path
def GenerateResourceModule(base_dir, root_dir, api_config): """Create resource.py file for each api with for given api_config. Args: base_dir: str, Path of directory for the project. root_dir: str, Path of the resource file location within the project. api_config: regeneration config for all apis. Raises: WrongDiscoveryDoc: if discovery doc api name/version does not match. """ tpl = template.Template( filename=os.path.join(os.path.dirname(__file__), 'resources.tpl')) for api_name, api_version_config in api_config.iteritems(): for api_version, api_config in api_version_config.iteritems(): discovery_doc = os.path.join(base_dir, root_dir, api_config['discovery_doc']) with open(discovery_doc, 'rU') as f: discovery = json.load(f) if discovery['version'] != api_version: logging.warn( 'Discovery api version %s does not match %s, ' 'this client will be accessible via new alias.', discovery['version'], api_version) if discovery['name'] != api_name: raise WrongDiscoveryDoc('api name {0}, expected {1}'.format( discovery['name'], api_name)) base_url = discovery['baseUrl'] try: resource_collections = _ExtractResources( api_name, api_version, base_url, discovery['resources']) except AmbiguousResourcePath as e: logging.warn(e) continue custom_resources = api_config.get('resources', {}) if custom_resources: for collection in resource_collections: if collection.name in custom_resources: custom_path = custom_resources[collection.name] if isinstance(custom_path, dict): collection.flat_paths.update(custom_path) elif isinstance(custom_path, basestring): collection.flat_paths[ _DEFAULT_PATH_NAME] = custom_path api_dir = os.path.join(base_dir, root_dir, api_name, api_version) if not os.path.exists(api_dir): os.makedirs(api_dir) resource_file_name = os.path.join(api_dir, 'resources.py') logging.debug('Generating resource module at %s', resource_file_name) if resource_collections: with open(resource_file_name, 'wb') as output_file: ctx = runtime.Context( output_file, collections=sorted(resource_collections), base_url=resource_collections[0].base_url) tpl.render_context(ctx)
def render(self, context): block = self.nodelist.render(context) #turn the context into a dict parameters = {} [parameters.update(parameter) for parameter in context] #render with Mako rendered = mako.Template(block, format_exceptions=True).render(**parameters) return rendered
def make_html(): tpl = template.Template(filename=TPL) result = tpl.render(repos=REPOS) print 'Write html...' with open(OUTPUT_HTML, 'w') as output: output.write(result.encode('utf8')) print 'Done..'
def do_GET(self): template_path = resource_loader.get_path_to_datafile( 'kmeans_labeler_template.html') template = mako_template.Template(open(template_path).read()) page = create_page(self.clusters, template) self.send_response(http_client.OK) self.send_header('Content-Type', 'text/html; charset=utf-8') self.end_headers() self.wfile.write(page)
def testCreatePage(self): num_clusters = 10 clusters = np.random.random((num_clusters, 12, 14)) template_src = resource_loader.get_path_to_datafile( 'kmeans_labeler_template.html') template = mako_template.Template(open(template_src).read()) html = kmeans_labeler_request_handler.create_page(clusters, template) self.assertEqual(len(re.findall('<img', html)), num_clusters) self.assertEqual(len(re.findall('<select', html)), num_clusters)
def run(self): # Check binary binary_path = spawn.find_executable(self.args['httpfs_binary']) if not binary_path: log.error(messages.module_file_mount.httpfs_s_not_found % self.args['httpfs_binary']) return # Generate PHP agent try: status = 0 agent = subprocess.check_output([binary_path, 'generate', 'php']) except subprocess.CalledProcessError as e: status = e.returncode agent = '' if status or not agent: log.error(messages.module_file_mount.error_generating_agent) return # Save temporary PHP agent, and upload it temp_file = tempfile.NamedTemporaryFile(suffix='.php', prefix='', delete=False) temp_file.write(agent) # Without this flush() uploads only a # portion of the file temp_file.flush() result = ModuleExec('file_upload2web', [temp_file.name, self.args['rpath']]).run() temp_file.close() if (not result or not result[0] or len(result[0]) != 2 or not result[0][0] or not result[0][1]): log.error(messages.module_file_mount.failed_agent_upload) return self.args.update({ 'agent_abs_path': result[0][0], 'agent_url': result[0][1] }) log.warn( template.Template( messages.module_file_mount.agent_installed_tutorial).render( **self.args)) if self.args['no_autoremove']: log.warn( messages.module_file_mount.httpfs_agent_manually_remove_s % (result[0][0])) else: log.warn(messages.module_file_mount.httpfs_agent_removed) atexit.register(ModuleExec('file_rm', [result[0][0]]).run)
def buildPlainTextError(): return template.Template(''' % if errors: ------------------------------------------------------------------------ % for error in errors: ${error} % endfor % endif ''')
def htmlErrorTemplate(): return template.Template(''' % if errors: <div style="border-top: 1px solid #ccc;"></div> % for error in errors: <p style="margin: 20px;">${error|h}</p> % endfor % endif ''')
def __init__(self, path): self.path = path logging.info('Parsing template %s', path) with open(path, 'rb') as fh: self._string = fh.read() # Load the template into mako lookup = mako_lookup.TemplateLookup() self._mako_template = mako_template.Template(self._string, lookup=lookup, format_exceptions=True) # print template.code self.module = self._mako_template.module
def htmlWrapperTemplate(): return template.Template(''' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body style="font-size: 14px; font-family: helvetica, arial, sans-serif; padding: 20px 0; margin: 0; color: #333;"> ${body} </body> </html> ''')
def _TemplateFileForCommandPath(command_template_filename, test=False): """Returns Mako template corresping to command_template_filename. Args: command_template_filename: name of file containing template (no path). test: if the template file should be a test file, defaults to False. """ if test: template_dir = 'test_templates' else: template_dir = 'command_templates' template_path = os.path.join(os.path.dirname(__file__), template_dir, command_template_filename) return template.Template(filename=template_path)
def plainRawTemplate(): return template.Template(''' % if len(results) > 0: % if results[0].get('_raw'): % for result in results: ${result.get("_raw", "")}\n % endfor % else: The results contain no "_raw" field. Please choose another inline format (csv or table). % endif % else: No results found. % endif ''')
def index_html(self, scripts, ver, path_map): """Get the main HTML page.""" def relpath(path): return os.path.relpath(path, 'build/') tmpl = template.Template(filename='static/index.mak') data = tmpl.render( relpath=relpath, scripts=scripts, css_data=self.index_css(), js_data=self.index_js(scripts, path_map), app_name='Robot Rampage', version=ver, ) return build.minify_html(self.config, data.encode('UTF-8'))
def plainResultsTemplate(): return template.Template(''' ------------------------------------------------------------------------ % if truncated: % if jobcount: Only the first ${resultscount} of ${jobcount} results are included below. % else: Search results in this email have been truncated. % endif % if include_results_link: View all results in Splunk: ${results_link} % endif % elif include_results_link: View results in Splunk: ${results_link} % endif ''')
def htmlRawTemplate(): return template.Template(''' % if len(results) > 0: % if results[0].get("_raw"): <div style="margin: 20px;" class="events"> % for result in results: <div class="event"style="border-bottom: 1px dotted #ccc; padding: 5px 0; font-family: monospace; word-break: break-all;">${result.get("_raw", "")|h}</div> % endfor </div> % else: <div> The results contain no "_raw" field. Please choose another inline format (csv or table).</div> % endif % else: <div class="results" style="margin: 20px;">No results found.</div> % endif ''')
def index_html(self, scripts, ver): with open('static/style.css', 'rb') as fp: css_data = build.minify_css(self.config, fp.read()) def relpath(path): return os.path.relpath(path, 'build/') tmpl = template.Template(filename='static/index.mak') data = tmpl.render( relpath=relpath, scripts=scripts, css_data=css_data.decode('UTF-8'), app_name='Chaos Tomb', version=ver, ) return build.minify_html(self.config, data.encode('UTF-8'))
def generate_build_file(self): # Collect all target names into a list targets = [] for model_path in self.model_paths: target_name = model_path.split('/')[-1].split('.')[0] targets.append(target_name) template_file_path = os.path.join(TEMPLATE_DIR, 'BUILD.mako') build_template = template.Template(filename=template_file_path) with open(self.output_dir + '/BUILD', 'w') as file_obj: key_values_in_template = { 'targets': targets, 'inputs': self.inputs, 'input_dtypes': self.input_types, 'output_dtype': self.output_type } file_obj.write(build_template.render(**key_values_in_template))
def __init__(self, definition: mako_tpl.Template, **kwargs: Any) -> None: super().__init__() try: if isinstance(definition, str): definition = mako_tpl.Template(definition) assert isinstance(definition, mako_tpl.Template) self.definition = definition except Exception as e: message = "Error in MakoTemplate" if self.definition_loc: message += f" created at {self.definition_loc[0]}:{self.definition_loc[1]}" try: message += f" (error likely around line {e.lineno}, column: {getattr(e, 'pos', '?')})" # type: ignore # assume Mako exception except Exception: message = f"{message}:\n---\n{definition}\n---\n" raise TemplateDefinitionError(message, definition=definition) from e
def htmlResultsTemplate(): return template.Template(''' <div style="margin-top: 10px; padding-top: 20px; border-top: 1px solid #ccc;"></div> % if truncated: %if jobcount: <p style="margin: 0 20px;">Only the first ${resultscount} of ${jobcount} results are included below. %else: <p style="margin: 0 20px;">Search results in this email have been truncated. %endif %if include_results_link: <a href="${results_link|h}" style=" text-decoration: none; margin: 0 0; color: #5379AF;">View all results</a> in Splunk.</p> %else: </p> %endif % elif include_results_link: <a href="${results_link|h}" style=" text-decoration: none; margin: 0 20px; color: #5379AF;">View results in Splunk</a> % endif ''')
def _get_minmax_kernel(self, ctx, dtype, mmc_dtype, prop_names, only_min, only_max, name, mmc_c_decl): tpl_args = ", ".join([ "%(dtype)s %(prop)s" % { 'dtype': dtype, 'prop': prop } for prop in prop_names ]) mmc_preamble = mmc_c_decl + minmax_tpl preamble = mkt.Template(text=mmc_preamble).render( args=tpl_args, prop_names=prop_names, dtype=name, only_min=only_min, only_max=only_max) knl_args = ", ".join([ "__global %(dtype)s *%(prop)s" % { 'dtype': dtype, 'prop': prop } for prop in prop_names ]) map_args = ", ".join([ "%(prop)s[i]" % { 'dtype': dtype, 'prop': prop } for prop in prop_names ]) from pyopencl.reduction import ReductionKernel knl = ReductionKernel(ctx, mmc_dtype, neutral="mmc_neutral()", reduce_expr="agg_mmc(a, b)", map_expr="mmc_from_scalar(%s)" % map_args, arguments=knl_args, preamble=preamble) return knl