def checkdep_dvipng(): try: s = subprocess.Popen(['dvipng', '-version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = s.communicate() line = stdout.decode('ascii').split('\n')[1] v = line.split()[-1] return v except (IndexError, ValueError, OSError): return None
def checkdep_pdftops(): try: s = subprocess.Popen(['pdftops','-v'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) for line in s.stderr: if b'version' in line: v = byte2str(line.split()[-1]) return v except (IndexError, ValueError, UnboundLocalError, OSError): return None
def checkdep_dvipng(): try: s = subprocess.Popen(['dvipng', '-version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) line = s.stdout.readlines()[1] v = byte2str(line.split()[-1]) return v except (IndexError, ValueError, OSError): return None
def run(self): print(self.__class__.__name__ + " en cours d'exécution.") p = subprocess.Popen(["touch", self.output_file("output1")]) p.wait() # self.session().delete_content(self.output_table("FooBase")) for i in range(10): f = self.output_table("FooBaseP")(name="Foowrapper5 - " + str(i)) self.session().add(f) self.session().commit() time.sleep(1)
def __call__(self, orig, dest): if (not self._proc # First run. or self._proc.poll() is not None): # Inkscape terminated. env = os.environ.copy() # If one passes e.g. a png file to Inkscape, it will try to # query the user for conversion options via a GUI (even with # `--without-gui`). Unsetting `DISPLAY` prevents this (and causes # GTK to crash and Inkscape to terminate, but that'll just be # reported as a regular exception below). env.pop("DISPLAY", None) # May already be unset. # Do not load any user options. # `os.environ` needs native strings on Py2+Windows. env[str("INKSCAPE_PROFILE_DIR")] = os.devnull # Old versions of Inkscape (0.48.3.1, used on Travis as of now) # seem to sometimes deadlock when stderr is redirected to a pipe, # so we redirect it to a temporary file instead. This is not # necessary anymore as of Inkscape 0.92.1. self._stderr = TemporaryFile() self._proc = subprocess.Popen( [str("inkscape"), "--without-gui", "--shell"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=self._stderr, env=env) if not self._read_to_prompt(): raise OSError("Failed to start Inkscape") try: fsencode = os.fsencode except AttributeError: # Py2. def fsencode(s): return s.encode(sys.getfilesystemencoding()) # Inkscape uses glib's `g_shell_parse_argv`, which has a consistent # behavior across platforms, so we can just use `shlex.quote`. orig_b, dest_b = map(_shlex_quote_bytes, map(fsencode, [orig, dest])) if b"\n" in orig_b or b"\n" in dest_b: # Who knows whether the current folder name has a newline, or if # our encoding is even ASCII compatible... Just fall back on the # slow solution (Inkscape uses `fgets` so it will always stop at a # newline). return make_external_conversion_command( lambda old, new: [str('inkscape'), '-z', old, '--export-png', new])(orig, dest) self._proc.stdin.write(orig_b + b" --export-png=" + dest_b + b"\n") self._proc.stdin.flush() if not self._read_to_prompt(): # Inkscape's output is not localized but gtk's is, so the # output stream probably has a mixed encoding. Using # `getfilesystemencoding` should at least get the filenames # right... self._stderr.seek(0) raise ImageComparisonFailure(self._stderr.read().decode( sys.getfilesystemencoding(), "replace"))
def checkdep_inkscape(): try: s = subprocess.Popen(['inkscape','-V'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) for line in s.stdout: if b'Inkscape' in line: v = byte2str(line.split()[1]) break return v except (IndexError, ValueError, UnboundLocalError, OSError): return None
def checkdep_tex(): try: s = subprocess.Popen(['tex','-version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) line = byte2str(s.stdout.readlines()[0]) pattern = '3\.1\d+' match = re.search(pattern, line) v = match.group(0) return v except (IndexError, ValueError, AttributeError, OSError): return None
def checkdep_xmllint(): try: s = subprocess.Popen(['xmllint','--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) for line in s.stderr: if b'version' in line: v = line.split()[-1].decode('ascii') break return v except (IndexError, ValueError, UnboundLocalError, OSError): return None
def __init__(self): # create a tmp directory for running latex, remember to cleanup self.tmpdir = tempfile.mkdtemp(prefix="mpl_pgf_lm_") LatexManager._unclean_instances.add(self) # test the LaTeX setup to ensure a clean startup of the subprocess self.texcommand = get_texcommand() self.latex_header = LatexManager._build_latex_header() latex_end = "\n\\makeatletter\n\\@@end\n" try: latex = subprocess.Popen([self.texcommand, "-halt-on-error"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, cwd=self.tmpdir) except OSError as e: if e.errno == errno.ENOENT: raise RuntimeError("Latex command not found. " "Install '%s' or change pgf.texsystem to the desired command." % self.texcommand ) else: raise RuntimeError("Error starting process '%s'" % self.texcommand) test_input = self.latex_header + latex_end stdout, stderr = latex.communicate(test_input.encode("utf-8")) if latex.returncode != 0: raise LatexError("LaTeX returned an error, probably missing font or error in preamble:\n%s" % stdout) # open LaTeX process for real work latex = subprocess.Popen([self.texcommand, "-halt-on-error"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, cwd=self.tmpdir) self.latex = latex self.latex_stdin_utf8 = codecs.getwriter("utf8")(self.latex.stdin) # write header with 'pgf_backend_query_start' token self._stdin_writeln(self._build_latex_header()) # read all lines until our 'pgf_backend_query_start' token appears self._expect("*pgf_backend_query_start") self._expect_prompt() # cache for strings already processed self.str_cache = {}
def checkdep_pdftops(): try: s = subprocess.Popen(['pdftops', '-v'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = s.communicate() lines = stderr.decode('ascii').split('\n') for line in lines: if 'version' in line: v = line.split()[-1] return v except (IndexError, ValueError, UnboundLocalError, OSError): return None
def __init__(self, args): """ Start the subprocess so it may start accepting commands. *args* is a list of commandline arguments to pass to `subprocess.Popen`. """ self._name = args[0] self._process = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
def checkdep_tex(): try: s = subprocess.Popen(['tex', '-version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = s.communicate() line = stdout.decode('ascii').split('\n')[0] pattern = '3\.1\d+' match = re.search(pattern, line) v = match.group(0) return v except (IndexError, ValueError, AttributeError, OSError): return None
def checkdep_ghostscript(): try: if sys.platform == 'win32': command_args = ['gswin32c', '--version'] else: command_args = ['gs', '--version'] s = subprocess.Popen(command_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) v = byte2str(s.stdout.read()[:-1]) return v except (IndexError, ValueError, OSError): return None
def run(self): print(self.__class__.__name__ + " en cours d'exécution.") p = subprocess.Popen(["touch", self.output_file("output1")]) p.wait() # self.session().delete_content(self.output_table("FooBase")) for i in range(1000): f = self.output_table("FooBase")(name="Foowrapper5 - " + str(i)) self.session().add(f) self.session().commit() self.log("info", "coucou") self.log("warning", "coucou") self.log("error", "coucou") self.log("debug", "coucou")
def convert(old, new): cmdline = cmd(old, new) pipe = subprocess.Popen(cmdline, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = pipe.communicate() errcode = pipe.wait() if not os.path.exists(new) or errcode: msg = "Conversion command failed:\n%s\n" % ' '.join(cmdline) if stdout: msg += "Standard output:\n%s\n" % stdout if stderr: msg += "Standard error:\n%s\n" % stderr raise IOError(msg)
def checkdep_inkscape(): try: s = subprocess.Popen(['inkscape', '-V'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = s.communicate() lines = stdout.decode('ascii').split('\n') for line in lines: if 'Inkscape' in line: v = line.split()[1] break return v except (IndexError, ValueError, UnboundLocalError, OSError): return None
def isAvailable(cls): ''' Check to see if a MovieWriter subclass is actually available by running the commandline tool. ''' try: subprocess.Popen(cls.bin_path(), shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) return True except OSError: return False
def fc_match(pattern, fontext): fontexts = get_fontext_synonyms(fontext) ext = "." + fontext try: pipe = subprocess.Popen(['fc-match', '-sv', pattern], stdout=subprocess.PIPE) output = pipe.communicate()[0] except OSError: return None if pipe.returncode == 0: for match in _fc_match_regex.finditer(output): file = match.group(1) if os.path.splitext(file)[1][1:] in fontexts: return file return None
def _run(self): # Uses subprocess to call the program for assembling frames into a # movie file. *args* returns the sequence of command line arguments # from a few configuration options. command = self._args() if verbose.ge('debug'): output = sys.stdout else: output = subprocess.PIPE verbose.report('MovieWriter.run: running command: %s' % ' '.join(command)) self._proc = subprocess.Popen(command, shell=False, stdout=output, stderr=output, stdin=subprocess.PIPE)
def find_tex_file(filename, format=None): """ Find a file in the texmf tree. Calls :program:`kpsewhich` which is an interface to the kpathsea library [1]_. Most existing TeX distributions on Unix-like systems use kpathsea. It is also available as part of MikTeX, a popular distribution on Windows. Parameters ---------- filename : string or bytestring format : string or bytestring Used as the value of the `--format` option to :program:`kpsewhich`. Could be e.g. 'tfm' or 'vf' to limit the search to that type of files. References ---------- .. [1] `Kpathsea documentation <http://www.tug.org/kpathsea/>`_ The library that :program:`kpsewhich` is part of. """ if six.PY3: # we expect these to always be ascii encoded, but use utf-8 # out of caution if isinstance(filename, bytes): filename = filename.decode('utf-8', errors='replace') if isinstance(format, bytes): format = format.decode('utf-8', errors='replace') cmd = [str('kpsewhich')] if format is not None: cmd += ['--format=' + format] cmd += [filename] matplotlib.verbose.report('find_tex_file(%s): %s' % (filename, cmd), 'debug') # stderr is unused, but reading it avoids a subprocess optimization # that breaks EINTR handling in some Python versions: # http://bugs.python.org/issue12493 # https://github.com/matplotlib/matplotlib/issues/633 pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) result = pipe.communicate()[0].rstrip() matplotlib.verbose.report('find_tex_file result: %s' % result, 'debug') return result.decode('ascii')
def get_fontconfig_fonts(fontext='ttf'): """ Grab a list of all the fonts that are being tracked by fontconfig by making a system call to ``fc-list``. This is an easy way to grab all of the fonts the user wants to be made available to applications, without needing knowing where all of them reside. """ fontext = get_fontext_synonyms(fontext) fontfiles = {} try: pipe = subprocess.Popen(['fc-list', '', 'file'], stdout=subprocess.PIPE) output = pipe.communicate()[0] except OSError, IOError: # Calling fc-list did not work, so we'll just return nothing return fontfiles
def checkdep_ghostscript(): if sys.platform == 'win32': gs_execs = ['gswin32c', 'gswin64c', 'gs'] else: gs_execs = ['gs'] for gs_exec in gs_execs: try: s = subprocess.Popen([gs_exec, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = s.communicate() if s.returncode == 0: v = stdout[:-1].decode('ascii') return gs_exec, v except (IndexError, ValueError, OSError): pass return None, None
def isAvailable(cls): ''' Check to see if a MovieWriter subclass is actually available by running the commandline tool. ''' if not cls.bin_path(): return False try: p = subprocess.Popen(cls.bin_path(), shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, creationflags=subprocess_creation_flags) p.communicate() return True except OSError: return False
def check_for(texsystem): header = r""" \documentclass{minimal} \usepackage{pgf} \begin{document} \typeout{pgfversion=\pgfversion} \makeatletter \@@end """ try: latex = subprocess.Popen(["xelatex", "-halt-on-error"], stdin=subprocess.PIPE, stdout=subprocess.PIPE) stdout, stderr = latex.communicate(header.encode("utf8")) except OSError: return False return latex.returncode == 0
def verify(filename): """Verify the file through some sort of verification tool.""" if not os.path.exists(filename): raise IOError("'%s' does not exist" % filename) base, extension = filename.rsplit('.', 1) verifier = verifiers.get(extension, None) if verifier is not None: cmd = verifier(filename) pipe = subprocess.Popen(cmd, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = pipe.communicate() errcode = pipe.wait() if errcode != 0: msg = "File verification command failed:\n%s\n" % ' '.join(cmd) if stdout: msg += "Standard output:\n%s\n" % stdout if stderr: msg += "Standard error:\n%s\n" % stderr raise IOError(msg)
def find_tex_file(filename, format=None): """ Find a file in the texmf tree. Calls :program:`kpsewhich` which is an interface to the kpathsea library [1]_. Most existing TeX distributions on Unix-like systems use kpathsea. It is also available as part of MikTeX, a popular distribution on Windows. Parameters ---------- filename : string or bytestring format : string or bytestring Used as the value of the `--format` option to :program:`kpsewhich`. Could be e.g. 'tfm' or 'vf' to limit the search to that type of files. References ---------- .. [1] `Kpathsea documentation <http://www.tug.org/kpathsea/>`_ The library that :program:`kpsewhich` is part of. """ # we expect these to always be ascii encoded, but use utf-8 # out of caution if isinstance(filename, bytes): filename = filename.decode('utf-8', errors='replace') if isinstance(format, bytes): format = format.decode('utf-8', errors='replace') cmd = ['kpsewhich'] if format is not None: cmd += ['--format=' + format] cmd += [filename] _log.debug('find_tex_file(%s): %s', filename, cmd) pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE) result = pipe.communicate()[0].rstrip() _log.debug('find_tex_file result: %s', result) return result.decode('ascii')
def fc_match(pattern, fontext): fontexts = get_fontext_synonyms(fontext) ext = "." + fontext try: pipe = subprocess.Popen( ['fc-match', '-s', '--format=%{file}\\n', pattern], stdout=subprocess.PIPE, stderr=subprocess.PIPE) output = pipe.communicate()[0] except (OSError, IOError): return None # The bulk of the output from fc-list is ascii, so we keep the # result in bytes and parse it as bytes, until we extract the # filename, which is in sys.filesystemencoding(). if pipe.returncode == 0: for fname in output.split(b'\n'): try: fname = six.text_type(fname, sys.getfilesystemencoding()) except UnicodeDecodeError: continue if os.path.splitext(fname)[1][1:] in fontexts: return fname return None
def run(self): print(self.__class__.__name__ + " en cours d'exécution.") p = subprocess.Popen(["touch", self.output_file("output1")]) p.wait() time.sleep(1)
def run(self): p = subprocess.Popen(["touch", self.output_file("output1")]) p.wait() time.sleep(1)