def run(event, context): load_dependency('pydot') channel = event.get('channel') data = event.get('dot') #slack_message("in dot to svg: {0}".format(event), [], channel) log_to_elk("in dot to svg: {0}".format(event)) import dot_parser try: (fd, tmp_file) = tempfile.mkstemp('dot)') dot_static = '/tmp/lambdas-dependencies/pydot/dot_static' Process.run("chmod", ['+x', dot_static]) data = data.replace('<', '<').replace( '>', '>' ) # this solved a really nasty bug caused by the fact that Slack will html encode the < and > # graph = pydot.graph_from_dot_data(data).pop() # <from pydot> use code below (instead of above) to get a better error message from dot parser graphparser = dot_parser.graph_definition() graphparser.parseWithTabs() tokens = graphparser.parseString(data) graph = list(tokens).pop() # </from pydot> graph.write_svg(tmp_file, prog=dot_static) svg_raw = Files.contents(tmp_file) return base64.b64encode(svg_raw.encode()).decode() except Exception as error: slack_message("[dot_to_svg] Error: {0} ".format(error), [], channel) return None
def run(event, context): load_dependency('plantuml') dot_static = '/tmp/lambdas-dependencies/plantuml/dot_static' plantuml_jar = '/tmp/lambdas-dependencies/plantuml/plantuml.jar' Process.run("chmod", ['+x', dot_static]) Process.run("chmod", ['+x', plantuml_jar]) os.environ['PLANTUML_LIMIT_SIZE'] = str( 4096 * 4) # set max with to 4 times the default (16,384) os.environ['GRAPHVIZ_DOT'] = dot_static (fd, puml_file) = tempfile.mkstemp('.puml') png_file = puml_file.replace(".puml", ".png") code = event.get('puml') Files.write(puml_file, code) subprocess.run([ 'java', '-jar', plantuml_jar, '-Xmx2512m', '-tpng', '-o', '/tmp', puml_file ], stdout=subprocess.PIPE, stderr=subprocess.PIPE) if os.path.exists(png_file): with open(png_file, "rb") as image_file: png = base64.b64encode(image_file.read()).decode() else: png = None return {"png_base64": png}
def test_run__ls(self): result = Process.run('ls') #assert result == {'runParams': ['ls'], 'stderr': '', 'stdout': 'Test_Process.py\naws\n'} Dev.pprint(result) result = Process.run('ls', ['-la', '..']) #assert '-rw-r--r--@ 1 dinis staff 6148 Oct 29 11:59 .DS_Store\n' in result['stdout'] Dev.pprint(result)
async def set_up_browser(): from pyppeteer import launch # import pyppeteer dependency Process.run("chmod", ['+x', path_headless_shell ]) # set the privs of path_headless_shell to execute self._browser = await launch( executablePath= path_headless_shell, # lauch chrome (i.e. headless_shell) args=['--no-sandbox', '--single-process' ]) # two key settings or the requests will not work
def lambda_status(team_id, channel, params): text = "Here are the current status of the `graph` lambda function" attachments = [{ 'title': 'Processes', 'text': Process.run("ps", ["-A"]).get('stdout') }, { 'title': 'Temp Files', 'text': Process.run("ls", ["-ls", '/tmp']).get('stdout') }] return text, attachments
def build_step__create_image(self, no_cache, show_log): params = ['build'] if no_cache: params.append('--no-cache') params += ['-t', 'sync-server', '.'] Dev.pprint(params) result = Process.run('docker', params, cwd=self.path_docker_folder) if show_log: Dev.pprint(result['stderr']) Dev.pprint(result['stdout']) return result
async def take_screenshot(): from pyppeteer import launch # import pyppeteer dependency Process.run("chmod", ['+x', path_headless_shell ]) # set the privs of path_headless_shell to execute browser = await launch( executablePath= path_headless_shell, # lauch chrome (i.e. headless_shell) args=['--no-sandbox', '--single-process' ]) # two key settings or the requests will not work page = await browser.newPage( ) # typical pyppeteer code, where we create a new Page object await page.goto(target_url) # - open an url #await page.waitFor(2 * 1000); await page.screenshot({ 'path': path_page_screenshot, 'fullPage': True }) # - take a screenshot of the page loaded and save it #await page.pdf({'path': path_page_screenshot}); await browser.close()
def cleanup_chrome_processes_and_tmp_files(): # remote temp files for file in Files.find('/tmp/core.headless_shell.*'): pid = file.split('.')[-1] Process.run('pkill',['-TERM','-P',str(pid)]) # this doesn't seem to be working since the "headless_shell <defunct>" is still there Files.delete(file)
def exec_in_image(self, cmd, cmd_params=[]): params = ['run', '--rm', 'sync-server', cmd] + cmd_params result = Process.run('docker', params, cwd=self.path_docker_folder) return result #['stdout'].strip() # assumes that command executes ok