Example #1
0
def print_file(new_file):
    print("Printing {}".format(new_file.path))
    print("Waiting three seconds to ensure the file has been written")
    time.sleep(3)
    try:
        run_process(["selphy", new_file.path], timeout=30)
    except OSError:
        traceback.print_exc()
        print("Sleeping two seconds after file printing error")
        time.sleep(2)
Example #2
0
def input_output_test(f_name, in_str):
    completed = run_process([sys.executable, '-B', f_name],
                            input=in_str,
                            stdout=PIPE,
                            stderr=PIPE,
                            universal_newlines=True)
    return completed.returncode, completed.stdout
Example #3
0
    def _run(self,
             args_list,
             title: str,
             cwd: Union[Path, str] = None,
             exception: Type[BaseException] = None,
             executable: str = None,
             shell: bool = False):

        print_command(cmd=args_list, at=self.at, title=title)

        cp = run_process(args_list,
                         cwd=cwd,
                         encoding=sys.stdout.encoding,
                         stdout=PIPE,
                         stderr=STDOUT,
                         executable=executable,
                         shell=shell,
                         universal_newlines=True)

        output = cp.stdout.rstrip()
        if output:
            print(output)

        if cp.returncode != 0:
            cpe = CalledProcessError(cp.returncode, args_list, cp.stdout,
                                     cp.stderr)
            if exception is None:
                raise cpe
            else:
                raise exception(cpe)

        return cp
Example #4
0
def processText(text):
    print("transforming text", file=stderr)
    inputb = bytearray(text, "utf-8")

    # call our external module via stdio
    resp = run_process(["./reverseTextLines2.py"],
                       shell=True,
                       input=inputb,
                       capture_output=True)

    return resp.stdout.decode("utf-8")
Example #5
0
def evaluate_submission(result):
    setup_django()
    from demograder.models import ResultDependency
    # create temporary directory
    with TemporaryDirectory() as temp_dir:
        # copy dglib library
        tmp_dglib = copyfile(DGLIB, join_path(temp_dir, basename(DGLIB)))
        # copy the submission script
        tmp_script = copyfile(result.project.script.name, join_path(temp_dir, basename(result.project.script.name)))
        # copy the submission
        tmp_uploads = []
        for upload in result.submission.uploads():
            tmp_uploads.append(copyfile(upload.file.name, join_path(temp_dir, basename(upload.file.name))))
        # copy all dependency files
        tmp_args = defaultdict(list)
        for result_dependency in ResultDependency.objects.filter(result=result):
            keyword = result_dependency.project_dependency.keyword
            for upstream_submission in result_dependency.producer.uploads():
                filepath = upstream_submission.file.name
                tmp_args[keyword].append(copyfile(filepath, join_path(temp_dir, basename(filepath))))
        timeout = result.project.timeout
        try:
            args = ['--_script', tmp_script, '--_uploads', ','.join(tmp_uploads)]
            for key, files in tmp_args.items():
                args.extend(('--{}'.format(key), ','.join(files)))
            # FIXME switch user
            args = [sys.executable, tmp_dglib] + args
            print(args)
            completed_process = run_process(args, timeout=timeout, stderr=PIPE, stdout=PIPE)
            stdout = completed_process.stdout.decode('utf-8')
            stderr = completed_process.stderr.decode('utf-8')
            return_code = completed_process.returncode
        except TimeoutExpired as e:
            stdout = e.stdout.decode('utf-8')
            stdout += '\n\n' +  dedent('''
            The program failed to complete within {}
            seconds and was terminated.
            '''.format(timeout)).strip()
            stderr = e.stderr.decode('utf-8')
            return_code = 1
    # update Result
    result.stdout = stdout.strip()
    result.stderr = stderr.strip()
    result.return_code = return_code
    result.save()
Example #6
0
def doc():
    from shutil import rmtree
    from sys import stderr, stdout
    from os.path import abspath, join
    from subprocess import run as run_process

    print('Building document...')
    source_dir = './doc'
    output_dir = './doc/build/html'

    rmtree(output_dir, True)
    status = run_process(
        ['sphinx-build', '-bhtml', '-E', '-j2', '-q', source_dir, output_dir],
        stderr=stderr,
        stdout=stdout)

    if status.returncode != 0:
        print('Building document failed.')
    else:
        target_file = abspath(join(output_dir, 'index.html'))
        print('The document has been written to: ' + target_file)
Example #7
0
def input_output_test(in_str, out_str):
    template = dedent('''
    INPUT
    -----
    {}

    EXPECTED OUTPUT
    ---------------
    {}

    ACTUAL OUTPUT
    -------------
    {}
    ''').strip()
    submission = sys.argv[1]
    input_text = dedent(in_str).strip()
    expected_output = dedent(out_str).strip()
    completed = run_process([sys.executable, submission], input=input_text, stdout=PIPE, stderr=PIPE, universal_newlines=True)
    actual_output = (completed.stdout.strip() + '\n' + completed.stderr.strip()).strip()
    transcript = template.format(input_text, expected_output, actual_output)
    print_result(transcript, completed.returncode == 0 and not is_different(expected_output, actual_output), should_exit=True)
Example #8
0
def test(recreate=False):
    """
    Runs tests in docker environment
    pass --recreate to recreate docker environment
    """

    from subprocess import run as run_process

    if recreate:
        recreate_commands = ('docker-compose down', 'docker-compose build',
                             'docker-compose up -d', 'docker-compose ps')

        run_process(" && ".join(recreate_commands), shell=True)

    # small hook to assure that contatiners built
    time.sleep(3)

    run_process('docker-compose exec api pytest', shell=True)

    if recreate:
        run_process('docker-compose down', shell=True)
Example #9
0
def preview(expr,
            output='png',
            viewer=None,
            euler=True,
            packages=(),
            filename=None,
            outputbuffer=None,
            preamble=None,
            dvioptions=None,
            outputTexFile=None,
            **latex_settings):
    r"""
    View expression or LaTeX markup in PNG, DVI, PostScript or PDF form.

    If the expr argument is an expression, it will be exported to LaTeX and
    then compiled using the available TeX distribution.  The first argument,
    'expr', may also be a LaTeX string.  The function will then run the
    appropriate viewer for the given output format or use the user defined
    one. By default png output is generated.

    By default pretty Euler fonts are used for typesetting (they were used to
    typeset the well known "Concrete Mathematics" book). For that to work, you
    need the 'eulervm.sty' LaTeX style (in Debian/Ubuntu, install the
    texlive-fonts-extra package). If you prefer default AMS fonts or your
    system lacks 'eulervm' LaTeX package then unset the 'euler' keyword
    argument.

    To use viewer auto-detection, lets say for 'png' output, issue

    >>> from sympy import symbols, preview, Symbol
    >>> x, y = symbols("x,y")

    >>> preview(x + y, output='png')

    This will choose 'pyglet' by default. To select a different one, do

    >>> preview(x + y, output='png', viewer='gimp')

    The 'png' format is considered special. For all other formats the rules
    are slightly different. As an example we will take 'dvi' output format. If
    you would run

    >>> preview(x + y, output='dvi')

    then 'view' will look for available 'dvi' viewers on your system
    (predefined in the function, so it will try evince, first, then kdvi and
    xdvi). If nothing is found you will need to set the viewer explicitly.

    >>> preview(x + y, output='dvi', viewer='superior-dvi-viewer')

    This will skip auto-detection and will run user specified
    'superior-dvi-viewer'. If 'view' fails to find it on your system it will
    gracefully raise an exception.

    You may also enter 'file' for the viewer argument. Doing so will cause
    this function to return a file object in read-only mode, if 'filename'
    is unset. However, if it was set, then 'preview' writes the genereted
    file to this filename instead.

    There is also support for writing to a StringIO like object, which needs
    to be passed to the 'outputbuffer' argument.

    >>> from StringIO import StringIO
    >>> obj = StringIO()
    >>> preview(x + y, output='png', viewer='StringIO',
    ...         outputbuffer=obj)

    The LaTeX preamble can be customized by setting the 'preamble' keyword
    argument. This can be used, e.g., to set a different font size, use a
    custom documentclass or import certain set of LaTeX packages.

    >>> preamble = "\\documentclass[10pt]{article}\n" \
    ...            "\\usepackage{amsmath,amsfonts}\\begin{document}"
    >>> preview(x + y, output='png', preamble=preamble)

    If the value of 'output' is different from 'dvi' then command line
    options can be set ('dvioptions' argument) for the execution of the
    'dvi'+output conversion tool. These options have to be in the form of a
    list of strings (see subprocess.Popen).

    Additional keyword args will be passed to the latex call, e.g., the
    symbol_names flag.

    >>> phidd = Symbol('phidd')
    >>> preview(phidd, symbol_names={phidd:r'\ddot{\varphi}'})

    For post-processing the generated TeX File can be written to a file by
    passing the desired filename to the 'outputTexFile' keyword
    argument. To write the TeX code to a file named
    "sample.tex" and run the default png viewer to display the resulting
    bitmap, do

    >>> preview(x+y, outputTexFile="sample.tex")


    """
    special = ['pyglet']

    if viewer is None:
        if output == "png":
            viewer = "pyglet"
        else:
            # sorted in order from most pretty to most ugly
            # very discussable, but indeed 'gv' looks awful :)
            # TODO add candidates for windows to list
            candidates = {
                "dvi": ["evince", "okular", "kdvi", "xdvi"],
                "ps": ["evince", "okular", "gsview", "gv"],
                "pdf": ["evince", "okular", "kpdf", "acroread", "xpdf", "gv"],
            }

            try:
                for candidate in candidates[output]:
                    path = find_executable(candidate)
                    if path is not None:
                        viewer = path
                        break
                else:
                    raise SystemError(
                        "No viewers found for '%s' output format." % output)
            except KeyError:
                raise SystemError("Invalid output format: %s" % output)
    else:
        if viewer == "file":
            if filename is None:
                SymPyDeprecationWarning(
                    feature="Using viewer=\"file\" without a "
                    "specified filename ",
                    last_supported_version="0.7.3",
                    use_instead="viewer=\"file\" and filename=\"desiredname\"")
        elif viewer == "StringIO":
            if outputbuffer is None:
                raise ValueError("outputbuffer has to be a StringIO "
                                 "compatible object if viewer=\"StringIO\"")
        elif viewer not in special and not find_executable(viewer):
            raise SystemError("Unrecognized viewer: %s" % viewer)

    if preamble is None:
        actual_packages = packages + ("amsmath", "amsfonts")
        if euler:
            actual_packages += ("euler", )
        package_includes = "\n" + "\n".join(
            ["\\usepackage{%s}" % p for p in actual_packages])

        preamble = r"""\documentclass[12pt]{article}
\pagestyle{empty}
%s

\begin{document}
""" % (package_includes)
    else:
        if len(packages) > 0:
            raise ValueError("The \"packages\" keyword must not be set if a "
                             "custom LaTeX preamble was specified")
    latex_main = preamble + '\n%s\n\n' + r"\end{document}"

    if isinstance(expr, str):
        latex_string = expr
    else:
        latex_string = latex(expr, mode='inline', **latex_settings)

    try:
        workdir = tempfile.mkdtemp()

        with open(join(workdir, 'texput.tex'), 'w') as fh:
            fh.write(latex_main % latex_string)

        if outputTexFile is not None:
            shutil.copyfile(join(workdir, 'texput.tex'), outputTexFile)

        try:
            run_process([
                'latex', '-halt-on-error', '-interaction=nonstopmode',
                'texput.tex'
            ],
                        cwd=workdir,
                        stderr=STDOUT)
        except CalledProcessError, e:
            raise RuntimeError(
                "latex exited abnormally with the following output:\n%s" %
                e.output)

        if output != "dvi":
            defaultoptions = {
                "ps": [],
                "pdf": [],
                "png": ["-T", "tight", "-z", "9", "--truecolor"]
            }

            commandend = {
                "ps": ["-o", "texput.ps", "texput.dvi"],
                "pdf": ["texput.dvi", "texput.pdf"],
                "png": ["-o", "texput.png", "texput.dvi"]
            }

            cmd = ["dvi" + output]
            try:
                if dvioptions is not None:
                    cmd.extend(dvioptions)
                else:
                    cmd.extend(defaultoptions[output])
                cmd.extend(commandend[output])
            except KeyError:
                raise SystemError("Invalid output format: %s" % output)

            try:
                run_process(cmd, cwd=workdir, stderr=STDOUT)
            except CalledProcessError, e:
                raise RuntimeError(
                    "%s exited abnormally with the following output:\n%s" %
                    (cmd[0], e.output))
Example #10
0
                def on_key_press(symbol, modifiers):
                    if symbol in [key.Q, key.ESCAPE]:
                        on_close()

                win.on_key_press = on_key_press

                def on_expose():
                    gl.glClearColor(1.0, 1.0, 1.0, 1.0)
                    gl.glClear(gl.GL_COLOR_BUFFER_BIT)

                    img.blit((win.width - img.width) / 2,
                             (win.height - img.height) / 2)

                win.on_expose = on_expose

                while not win.has_exit:
                    win.dispatch_events()
                    win.flip()
            except KeyboardInterrupt:
                pass

            win.close()
        else:
            run_process([viewer, src], cwd=workdir, stderr=STDOUT)
    finally:
        try:
            shutil.rmtree(workdir)  # delete directory
        except OSError, e:
            if e.errno != 2:  # code 2 - no such file or directory
                raise
Example #11
0
def preview(expr, output='png', viewer=None, euler=True, packages=(),
            filename=None, outputbuffer=None, preamble=None, dvioptions=None,
            outputTexFile=None, **latex_settings):
    r"""
    View expression or LaTeX markup in PNG, DVI, PostScript or PDF form.

    If the expr argument is an expression, it will be exported to LaTeX and
    then compiled using the available TeX distribution.  The first argument,
    'expr', may also be a LaTeX string.  The function will then run the
    appropriate viewer for the given output format or use the user defined
    one. By default png output is generated.

    By default pretty Euler fonts are used for typesetting (they were used to
    typeset the well known "Concrete Mathematics" book). For that to work, you
    need the 'eulervm.sty' LaTeX style (in Debian/Ubuntu, install the
    texlive-fonts-extra package). If you prefer default AMS fonts or your
    system lacks 'eulervm' LaTeX package then unset the 'euler' keyword
    argument.

    To use viewer auto-detection, lets say for 'png' output, issue

    >>> from sympy import symbols, preview, Symbol
    >>> x, y = symbols("x,y")

    >>> preview(x + y, output='png')

    This will choose 'pyglet' by default. To select a different one, do

    >>> preview(x + y, output='png', viewer='gimp')

    The 'png' format is considered special. For all other formats the rules
    are slightly different. As an example we will take 'dvi' output format. If
    you would run

    >>> preview(x + y, output='dvi')

    then 'view' will look for available 'dvi' viewers on your system
    (predefined in the function, so it will try evince, first, then kdvi and
    xdvi). If nothing is found you will need to set the viewer explicitly.

    >>> preview(x + y, output='dvi', viewer='superior-dvi-viewer')

    This will skip auto-detection and will run user specified
    'superior-dvi-viewer'. If 'view' fails to find it on your system it will
    gracefully raise an exception.

    You may also enter 'file' for the viewer argument. Doing so will cause
    this function to return a file object in read-only mode, if 'filename'
    is unset. However, if it was set, then 'preview' writes the genereted
    file to this filename instead.

    There is also support for writing to a StringIO like object, which needs
    to be passed to the 'outputbuffer' argument.

    >>> from StringIO import StringIO
    >>> obj = StringIO()
    >>> preview(x + y, output='png', viewer='StringIO',
    ...         outputbuffer=obj)

    The LaTeX preamble can be customized by setting the 'preamble' keyword
    argument. This can be used, e.g., to set a different font size, use a
    custom documentclass or import certain set of LaTeX packages.

    >>> preamble = "\\documentclass[10pt]{article}\n" \
    ...            "\\usepackage{amsmath,amsfonts}\\begin{document}"
    >>> preview(x + y, output='png', preamble=preamble)

    If the value of 'output' is different from 'dvi' then command line
    options can be set ('dvioptions' argument) for the execution of the
    'dvi'+output conversion tool. These options have to be in the form of a
    list of strings (see subprocess.Popen).

    Additional keyword args will be passed to the latex call, e.g., the
    symbol_names flag.

    >>> phidd = Symbol('phidd')
    >>> preview(phidd, symbol_names={phidd:r'\ddot{\varphi}'})

    For post-processing the generated TeX File can be written to a file by
    passing the desired filename to the 'outputTexFile' keyword
    argument. To write the TeX code to a file named
    "sample.tex" and run the default png viewer to display the resulting
    bitmap, do

    >>> preview(x+y, outputTexFile="sample.tex")


    """
    special = [ 'pyglet' ]

    if viewer is None:
        if output == "png":
            viewer = "pyglet"
        else:
            # sorted in order from most pretty to most ugly
            # very discussable, but indeed 'gv' looks awful :)
            # TODO add candidates for windows to list
            candidates = {
                "dvi": [ "evince", "okular", "kdvi", "xdvi" ],
                "ps": [ "evince", "okular", "gsview", "gv" ],
                "pdf": [ "evince", "okular", "kpdf", "acroread", "xpdf", "gv" ],
            }

            try:
                for candidate in candidates[output]:
                    path = find_executable(candidate)
                    if path is not None:
                        viewer = path
                        break
                else:
                    raise SystemError(
                        "No viewers found for '%s' output format." % output)
            except KeyError:
                raise SystemError("Invalid output format: %s" % output)
    else:
        if viewer == "file":
            if filename is None:
                SymPyDeprecationWarning(feature="Using viewer=\"file\" without a "
                    "specified filename ", last_supported_version="0.7.3",
                    use_instead="viewer=\"file\" and filename=\"desiredname\"")
        elif viewer == "StringIO":
            if outputbuffer is None:
                raise ValueError("outputbuffer has to be a StringIO "
                                 "compatible object if viewer=\"StringIO\"")
        elif viewer not in special and not find_executable(viewer):
            raise SystemError("Unrecognized viewer: %s" % viewer)


    if preamble is None:
        actual_packages = packages + ("amsmath", "amsfonts")
        if euler:
            actual_packages += ("euler",)
        package_includes = "\n" + "\n".join(["\\usepackage{%s}" % p
                                             for p in actual_packages])

        preamble = r"""\documentclass[12pt]{article}
\pagestyle{empty}
%s

\begin{document}
""" % (package_includes)
    else:
        if len(packages) > 0:
            raise ValueError("The \"packages\" keyword must not be set if a "
                             "custom LaTeX preamble was specified")
    latex_main = preamble + '\n%s\n\n' + r"\end{document}"

    if isinstance(expr, str):
        latex_string = expr
    else:
        latex_string = latex(expr, mode='inline', **latex_settings)

    try:
        workdir = tempfile.mkdtemp()

        with open(join(workdir, 'texput.tex'), 'w') as fh:
            fh.write(latex_main % latex_string)

        if outputTexFile is not None:
            shutil.copyfile(join(workdir, 'texput.tex'), outputTexFile)

        try:
            run_process(['latex', '-halt-on-error', '-interaction=nonstopmode',
                         'texput.tex'], cwd=workdir, stderr=STDOUT)
        except CalledProcessError, e:
            raise RuntimeError(
                "latex exited abnormally with the following output:\n%s" %
                e.output)

        if output != "dvi":
            defaultoptions = {
                "ps": [],
                "pdf": [],
                "png": ["-T", "tight", "-z", "9", "--truecolor"]
            }

            commandend = {
                "ps": ["-o", "texput.ps", "texput.dvi"],
                "pdf": ["texput.dvi", "texput.pdf"],
                "png": ["-o", "texput.png", "texput.dvi"]
            }

            cmd = ["dvi" + output]
            try:
                if dvioptions is not None:
                    cmd.extend(dvioptions)
                else:
                    cmd.extend(defaultoptions[output])
                cmd.extend(commandend[output])
            except KeyError:
                raise SystemError("Invalid output format: %s" % output)

            try:
                run_process(cmd, cwd=workdir, stderr=STDOUT)
            except CalledProcessError, e:
                raise RuntimeError(
                    "%s exited abnormally with the following output:\n%s" %
                    (cmd[0], e.output))
Example #12
0
                        on_close()

                win.on_key_press = on_key_press

                def on_expose():
                    gl.glClearColor(1.0, 1.0, 1.0, 1.0)
                    gl.glClear(gl.GL_COLOR_BUFFER_BIT)

                    img.blit(
                        (win.width - img.width) / 2,
                        (win.height - img.height) / 2
                    )

                win.on_expose = on_expose

                while not win.has_exit:
                    win.dispatch_events()
                    win.flip()
            except KeyboardInterrupt:
                pass

            win.close()
        else:
            run_process([viewer, src], cwd=workdir, stderr=STDOUT)
    finally:
        try:
            shutil.rmtree(workdir) # delete directory
        except OSError, e:
            if e.errno != 2: # code 2 - no such file or directory
                raise