예제 #1
0
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('&lt;', '<').replace('&gt;','>')  # 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
예제 #2
0
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)
예제 #3
0
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}
예제 #4
0
    async def get_screenshot():  # async method to run request

        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)
        # To Remove
        #await page.waitForNavigation(); not working

        if doc_type and doc_type == 'pdf':
            await page.pdf({'path': path_page_screenshot})
        else:
            await page.screenshot({
                'path': path_page_screenshot
            })  #   - take a screenshot of the page loaded and save it

        await browser.close()  #   - close the browser
 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
예제 #6
0
 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',
             #'--disable-dev-shm-usage'
         ])  # two key settings or the requests will not work
예제 #7
0
def run(event, context):
    port  = event.get('port')
    count = event.get('count')
    if port and count:
        for i in range(0,count):
            subprocess.Popen(["python", "-m", "http.server", str(port+i),"&"], cwd='/tmp')

    return Process.run('ps',['-aux']).get('stdout')
예제 #8
0
 def docker_exec(self, params=None, cwd=None):
     if params is None: params = []
     executable = 'docker'
     result = Process.run(executable, params, cwd)
     if result.get('stderr') != '':
         return {
             'ok': False,
             'console': result.get('stdout').strip().split('\n'),
             'error': result.get('stderr').split('\n')
         }
     return {
         'ok': True,
         'console': result.get('stdout').strip().split('\n')
     }
예제 #9
0
def hook(os_path, model, contents_manager):
    git_dir = os.environ.get('CODEBUILD_SRC_DIR')
    if git_dir is None:
        git_dir = '/home/jovyan/work'
    Process.run('git', cwd=git_dir, params=['add', '-A'])
    git_status = Process.run('git', cwd=git_dir,
                             params=['status']).get('stdout')
    Process.run('git',
                cwd=git_dir,
                params=['commit', '-m', 'auto saved (changes in notebook)'])
    Process.run('git',
                cwd=git_dir,
                params=['pull', '--no-edit', 'origin', 'master'])
    git_result = Process.run('git',
                             cwd=git_dir,
                             params=['push', 'origin', 'master']).get('stderr')
    if 'rejected' in git_result:
        log_message(
            ':red_circle: on `{0}`, saved to git\n```{1}```\n{2}'.format(
                os.environ['repo_name'], git_status, git_result))
예제 #10
0
def list_notebooks():
    return Process.run('find', ["-type", "f", "-name", "*.ipynb"],
                       cwd='../../..').get('stdout').split('\n')