Exemple #1
0
def decode():
    _test_ready()
    try:
        _argv.pop(0)
        target = _argv.pop(0)
        if len(_argv) > 0:
            raise IndexError()
    except IndexError:
        print('"unembedmath <filename>" requires exactly one filename.',
              file=_stderr)
        exit(1)
    target = _path.join(_path.curdir, target)
    if not _path.exists(target):
        print('{} file not found'.format(repr(target)), file=_stderr)
        exit(1)
    with _TD() as cd:
        dest = _path.join(cd, 'temp.md')
        _run([
            'pandoc', target, '-f', 'markdown', '-t', 'markdown', '-s', '-o',
            dest, '--filter', '_embedmath_decoding_filter'
        ],
             timeout=30,
             text=True)
        with open(dest, 'rt') as fp:
            print(fp.read())
    exit(0)
Exemple #2
0
def install_packages(packages_dir, quiet=False):
    """
    Install packages.

    Args:
        packages_dir (str): Directory containing packages.
        quiet (bool): Hide packages manager output.
    """
    # Get packages
    packages_dir = _realpath(packages_dir)
    packages = [
        _join(packages_dir, package_file)
        for package_file in _listdir(packages_dir)
        if (_splitext(package_file)[-1].lower() in (".deb", ".rpm") and
            # Skip "-dev"/"-devel" packages
            "-dev" not in package_file)
    ]

    # Run command
    if quiet:
        run_kwargs = dict(stdout=_PIPE, stderr=_PIPE, universal_newlines=True)
    else:
        run_kwargs = dict()

    _run(detect_package_manager() + " ".join(packages),
         shell=True,
         **run_kwargs).check_returncode()
Exemple #3
0
def _toggle_on(slave: str, master: str) -> None:
    # run `xinput` to reattach the slave and master keyboard ids
    # send notification, including on image
    _run(["xinput", "reattach", slave, master], check=True)
    _run(
        ["notify-send", "-i", _ON, "Enabling Keyboard...", "Connected"],
        check=True,
    )
Exemple #4
0
def _toggle_off(slave: str) -> None:
    # run `xinput` to detach the slave device
    # send notification, including off image
    _run(["xinput", "float", slave], check=True)
    _run(
        ["notify-send", "-i", _OFF, "Disabling Keyboard...", "Disconnected"],
        check=True,
    )
Exemple #5
0
def call(command, check=True, pipe_stdout=False, **run_kwargs):
    """
    Call command in subprocess.

    Args:
        command (iterable of str): Command
        run_kwargs: subprocess.run keyword arguments.
        check (bool): If True, Check return code for error.
        pipe_stdout (bool): If True, redirect stdout into a pipe, this allow to
            hide outputs from sys.stdout and permit to retrieve stdout as
            "result.stdout".

    Returns:
        subprocess.CompletedProcess: Utility call result.
    """
    if pipe_stdout:
        run_kwargs.setdefault('stdout', _PIPE)

    result = _run(command, universal_newlines=True, stderr=_PIPE, **run_kwargs)

    if check and result.returncode:
        raise _RuntimeException(
            (result.stderr or result.stdout
             or 'See stdout for more information.').strip())

    return result
Exemple #6
0
def _tput(command: list[str]) -> None:
    """Shorthand for tput calls"""

    waited_commands = [
        "clear",
        "smcup",
        "cup",
    ]

    command.insert(0, "tput")
    str_command = [str(c) for c in command]

    if command[1] in waited_commands:
        _run(str_command, check=True)
        return

    _Popen(str_command)
Exemple #7
0
def _xinput_list() -> str:
    # return the output of `xinput list`
    # if `FileNotFoundError` is raised, catch it and return a more
    # user-friendly `CommandNotFoundError`
    try:
        proc = _run(["xinput", "list"], capture_output=True, check=True)
        return proc.stdout.decode()
    except FileNotFoundError as err:
        raise CommandNotFoundError("xinput: command not found...") from err
Exemple #8
0
def _has_dvisvgm():
    try:
        out = _run(['dvisvgm', '--version'],
                   timeout=1,
                   capture_output=True,
                   text=True)
        return out.stdout.split('\n')[0]
    except FileNotFoundError:
        return False
Exemple #9
0
 def view(self, latex='lualatex'):
     with open(self._wdirname / 'Figure_{}.tikz'.format(self.index), 'w') as f:
         self.write(f)
     verbosity = '-silent'
     rv = _run(['latexmk', "-{}".format(latex), "-pv", verbosity, "-jobname=Figure_{}".format(self.index),
                self.viewdir / 'viewtemplate.tex'], cwd=self._wdir.name)
     if rv.returncode != 0:
         with open(self._wdirname / 'Figure_{}.log'.format(self.index)) as f:
             print(f.read())
Exemple #10
0
    def run(self, clt, show=True, output_dir="."):
        if show:
            self.show(clt)
        oneline = self._join_args(clt)
        #if self.test:
        #    oneline = oneline

        CP = _run(oneline.split())
        if self.test:
            return CP
Exemple #11
0
 def save(self, filename, latex='lualatex'):
     with open(self._wdirname / 'Figure_{}.tikz'.format(self.index), 'w') as f:
         self.write(f)
     rv = _run(['latexmk', "-{}".format(latex), "-silent", "-jobname=Figure_{}".format(self.index),
               self.viewdir / 'viewtemplate.tex'], cwd=self._wdir.name)
     if rv.returncode != 0:
         with open(self._wdirname / 'Figure_{}.log'.format(self.index)) as f:
             print(f.read())
     else:
         _copyfile(self._wdirname / 'Figure_{}.pdf'.format(self.index), filename)
def run(cmd, cwd=".") -> str:
    p = _run(shlex.split(cmd),
             stdout=PIPE,
             stderr=STDOUT,
             encoding="utf8",
             cwd=cwd)
    if p.returncode != 0:
        print(p.stdout)
        print(p.stderr)
        raise Exception
    return p.stdout
Exemple #13
0
def run(cmd, **kwargs):
    """ Compatibility function to support python 3.4
    """
    try:
        from subprocess import run as _run
        return _run(cmd, **kwargs)
    except ImportError:
        if 'check' in kwargs:
            del kwargs['check']
            return check_call(cmd, **kwargs)
        else:
            return check_output(cmd, **kwargs)
Exemple #14
0
 def _clear_fpga(self):
     """
     Clear FPGA
     """
     clear_fpga = _run([self._xmutil, 'unloadapp'],
                       stderr=_STDOUT,
                       stdout=_PIPE,
                       universal_newlines=True,
                       check=False)
     if clear_fpga.returncode:
         raise RuntimeError(clear_fpga.stdout)
     print('FPGA cleared')
Exemple #15
0
def run(cmd, **kwargs):
    """ Compatibility function to support python 3.4
    """
    try:
        from subprocess import run as _run
        return _run(cmd, **kwargs)
    except ImportError:
        if 'check' in kwargs:
            del kwargs['check']
            return check_call(cmd, **kwargs)
        else:
            return check_output(cmd, **kwargs)
Exemple #16
0
def start_redis_and_connect(redis):
    from subprocess import check_call as _run, CalledProcessError
    from time import time
    import redis as _redis

    try:
        _run(['redis-server', '--daemonize', 'yes'])
    except CalledProcessError:
        raise RuntimeError(
            'Redis is not installed. Please install and Redis and make it '
            'runable from the redis-server command or initialize the redis '
            'server before starting codeschool.')

    # We have 1s to connect to redis server
    _tf = time() + 1.0
    while time() < _tf:
        try:
            redis.connect()
        except _redis.ConnectionError:
            pass
    else:
        redis.connect()
Exemple #17
0
 def _clear_fpga(self):
     """
     Clear FPGA
     """
     clear_fpga = _run(
         [self._xbutil, 'validate', '-d', 'all', '-r', 'quick'],
         stderr=_STDOUT,
         stdout=_PIPE,
         universal_newlines=True,
         check=False)
     if clear_fpga.returncode:
         raise RuntimeError(clear_fpga.stdout)
     print('FPGA cleared')
Exemple #18
0
def start_redis_and_connect(redis):
    from subprocess import check_call as _run, CalledProcessError
    from time import time
    import redis as _redis

    try:
        _run(['redis-server', '--daemonize', 'yes'])
    except CalledProcessError:
        raise RuntimeError(
            'Redis is not installed. Please install and Redis and make it '
            'runable from the redis-server command or initialize the redis '
            'server before starting codeschool.')

    # We have 1s to connect to redis server
    _tf = time() + 1.0
    while time() < _tf:
        try:
            redis.connect()
        except _redis.ConnectionError:
            pass
    else:
        redis.connect()
Exemple #19
0
 def _reset_fpga(self):
     """
     Reset FPGA including FPGA image.
     """
     reset_image = _run(
         ['fpga-clear-local-image', '-S',
          str(self._fpga_slot_id), '-H'],
         stderr=_STDOUT,
         stdout=_PIPE,
         universal_newlines=True,
         check=False)
     if reset_image.returncode:
         raise RuntimeError(reset_image.stdout)
Exemple #20
0
 def _clear_fpga(self):
     """
     Clear FPGA
     """
     clear_fpga = _run(
         ['fpga-clear-local-image', '-S',
          str(self._fpga_slot_id)],
         stderr=_STDOUT,
         stdout=_PIPE,
         universal_newlines=True,
         check=False)
     if clear_fpga.returncode:
         raise RuntimeError(clear_fpga.stdout)
Exemple #21
0
 def _reset_fpga(self):
     """
     Reset FPGA including FPGA image.
     """
     reset_image = _run(
         [self._xbutil, 'reset', '-d',
          str(self._fpga_slot_id)],
         stderr=_STDOUT,
         stdout=_PIPE,
         universal_newlines=True,
         check=False)
     if reset_image.returncode:
         raise RuntimeError(reset_image.stdout)
Exemple #22
0
 def mock_run(*_, **kwargs):
     """ Create a mock external process result for testing. """
     _run((executable, "--version"), **kwargs)  # validate kwargs
     try:
         data = kwargs.get("input").decode()
     except AttributeError:
         data = None
     content = dumps({
         "data": data,
         "query": kwargs["env"].get("QUERY_STRING"),
     }).encode()
     headers = b"".join((
         b"HTTP/1.1 200 OK\n",
         b"Content-Type: application/json\n",
         b"Last-Modified: Thu, 01 Jan 1970 00:00:00 GMT\n",
         f"Content-Length: {len(content):d}\n".encode(),
         b"Set-Cookie: name=cookie1\n",
         b"Set-Cookie: name=cookie2\n",
         b"\n",
     ))
     response = headers + content
     return _MockCompletedProcess(response)
Exemple #23
0
def install_accelize_drm_library(packages_dir, quiet=False):
    """
    Install Accelize DRM library packages.

    Args:
        packages_dir (str): Directory containing packages.
        quiet (bool): Hide packages manager output.
    """
    # Get packages
    packages_dir = _realpath(packages_dir)
    packages = [
        _join(packages_dir, package_file)
        for package_file in _listdir(packages_dir)
        if (_splitext(package_file)[-1].lower() in ('.deb', '.rpm') and
            '-dev' not in package_file)]

    # Run command
    if quiet:
        run_kwargs = dict(stdout=_PIPE, stderr=_PIPE, universal_newlines=True)
    else:
        run_kwargs = dict()

    _run(detect_package_manager() + ' '.join(packages), shell=True,
         **run_kwargs).check_returncode()
Exemple #24
0
    def _program_fpga(self, fpga_image):
        """
        Program the FPGA with the specified image.

        Args:
            fpga_image (str): FPGA image.
        """
        # Vitis does not reprogram a FPGA that has already the bitstream.
        # So to force it we write another bitstream first.
        clear_image = _join(SCRIPT_DIR, 'clear.awsxclbin')
        load_image = _run([
            self._xbutil, 'program', '-d',
            str(self._fpga_slot_id), '-p', clear_image
        ],
                          stderr=_STDOUT,
                          stdout=_PIPE,
                          universal_newlines=True,
                          check=False)
        if load_image.returncode:
            raise RuntimeError(load_image.stdout)
        print('Cleared AWS XRT slot #%d' % self._fpga_slot_id)

        # Now load the real image
        fpga_image = _realpath(_fsdecode(fpga_image))
        load_image = _run([
            self._xbutil, 'program', '-d',
            str(self._fpga_slot_id), '-p', fpga_image
        ],
                          stderr=_STDOUT,
                          stdout=_PIPE,
                          universal_newlines=True,
                          check=False)
        if load_image.returncode:
            raise RuntimeError(load_image.stdout)
        print('Programmed AWS XRT slot #%d with FPGA image %s' %
              (self._fpga_slot_id, fpga_image))
Exemple #25
0
    def _program_fpga(self, fpga_image):
        """
        Program the FPGA with the specified image.

        Args:
            fpga_image (str): FPGA image.
        """
        load_image = _run([
            'fpga-load-local-image', '-S',
            str(self._fpga_slot_id), '-I', fpga_image
        ],
                          stderr=_STDOUT,
                          stdout=_PIPE,
                          universal_newlines=True)
        if load_image.returncode:
            raise RuntimeError(load_image.stdout)
Exemple #26
0
    def _program_fpga(self, fpga_image):
        """
        Program the FPGA with the specified image.

        Args:
            fpga_image (str): FPGA image.
        """
        load_image = _run(
            [self._xbutil, 'program', '-d', 'all', '-u', fpga_image],
            stderr=_STDOUT,
            stdout=_PIPE,
            universal_newlines=True,
            check=False)
        if load_image.returncode or not search(r'program succeeded',
                                               load_image.stdout):
            raise RuntimeError(load_image.stdout)
        print('Programmed Xilinx XRT with FPGA image %s' % fpga_image)
Exemple #27
0
def detect_package_manager():
    """
    Detect current OS package manager.

    Returns:
        str: Install command.
    """
    for utility, command in (
            ('apt-get', 'apt-get -qq update && '
                        'apt-get install -y --no-install-recommends '),
            ('dnf', 'dnf install -y '),
            ('yum', 'yum install -y ')):
        try:
            if not _run([utility, '--help'],
                        stdout=_DEVNULL, stderr=_DEVNULL).returncode:
                return command
        except FileNotFoundError:
            continue
Exemple #28
0
    def _program_fpga(self, fpga_image):
        """
        Program the FPGA with the specified image.

        Args:
            fpga_image (str): FPGA image.
        """
        # Must clear the FPGA first
        #self._clear_fpga()

        # Now load the bitstream
        image_name = splitext(fpga_image)[0]
        load_image = _run([self._xmutil, 'loadapp', image_name],
                          stderr=_STDOUT,
                          stdout=_PIPE,
                          universal_newlines=True,
                          check=False)
        if load_image.returncode or search(r'error', load_image.stdout):
            raise RuntimeError(load_image.stdout)
        print('Programmed SoM XRT with FPGA image %s' % fpga_image)
Exemple #29
0
 def _reset_fpga(self):
     """
     Reset FPGA including FPGA image.
     """
     list_apps = _run([self._xmutil, 'listapps'],
                      stderr=_STDOUT,
                      stdout=_PIPE,
                      universal_newlines=True,
                      check=False)
     if list_apps.returncode or search(r'error', list_apps.stdout):
         raise RuntimeError(list_apps.stdout)
     image_name = ''
     for line in list_apps.stdout.splitlines():
         m = _match(r'\s*(.+?)\s+', line)
         if m:
             image_name = m.group(1)
     if not image_name:
         print('No loaded bitstream to reset')
         return
     self._program_fpga(image_name + ".som")
Exemple #30
0
def call(command, check=True, pipe_stdout=False, retries=0, **run_kwargs):
    """
    Call command in subprocess.

    Args:
        command (iterable of str): Command
        run_kwargs: subprocess.run keyword arguments.
        check (bool): If True, Check return code for error.
        pipe_stdout (bool): If True, redirect stdout into a pipe, this allow to
            hide outputs from sys.stdout and permit to retrieve stdout as
            "result.stdout".
        retries (int): If True, retry this number of time on error.

    Returns:
        subprocess.CompletedProcess: Utility call result.
    """
    kwargs = dict(universal_newlines=True, stderr=_PIPE)
    kwargs.update(run_kwargs)

    if pipe_stdout:
        kwargs.setdefault('stdout', _PIPE)

    retried = 0
    while True:
        result = _run(command, **kwargs)

        if result.returncode and retried < retries:
            retried += 1
            continue
        break

    if check and result.returncode:
        raise _RuntimeException('\n'.join((
            'Error while running:', ' '.join(command), '',
            (result.stderr or result.stdout or
             warn('See stdout for more information.')).strip())))

    return result
Exemple #31
0
def detect_package_manager():
    """
    Detect current OS package manager.

    Returns:
        str: Install command.
    """
    for utility, command in (
        (
            "apt-get",
            "apt-get -qq update && "
            "apt-get install -y --no-install-recommends ",
        ),
        ("dnf", "dnf install -y "),
        ("yum", "yum install -y "),
        ("rpm", "rpm -i "),
    ):
        try:
            if not _run([utility, "--help"], stdout=_DEVNULL,
                        stderr=_DEVNULL).returncode:
                return command
        except FileNotFoundError:
            continue
Exemple #32
0
    def _program_fpga(self, fpga_image):
        """
        Program the FPGA with the specified image.

        Args:
            fpga_image (str): FPGA image.
        """
        retries = 3
        while True:
            load_image = _run([
                'fpga-load-local-image', '-S',
                str(self._fpga_slot_id), '-I', fpga_image
            ],
                              stderr=_STDOUT,
                              stdout=_PIPE,
                              universal_newlines=True,
                              check=False)
            if load_image.returncode == 0:
                break
            elif ("-110" in load_image.stdout) and (retries != 0):
                retries -= 1
                print('Retry programming')
            else:
                raise RuntimeError(load_image.stdout)
 def run(args):
     cp = _run(args, shell=True, stderr=PIPE, stdout=PIPE)
     if cp.returncode:
         print(cp.stdout)
         raise RuntimeError(cp.stderr)
Exemple #34
0
def run(*args, **kwargs):
    print(args)
    _run(*args, **kwargs)