def check_basic(self, *kws): s, o = exec_command.exec_command( '"%s" -c "raise \'Ignore me.\'"' % self.pyexe, **kws ) assert_(s != 0) assert_(o != "") s, o = exec_command.exec_command( '"%s" -c "import sys;sys.stderr.write(\'0\');' "sys.stderr.write('1');sys.stderr.write('2')\"" % self.pyexe, **kws ) assert_(s == 0) assert_(o == "012") s, o = exec_command.exec_command( '"%s" -c "import sys;sys.exit(15)"' % self.pyexe, **kws ) assert_(s == 15) assert_(o == "") s, o = exec_command.exec_command( '"%s" -c "print(\'Heipa\'")' % self.pyexe, **kws ) assert_(s == 0) assert_(o == "Heipa")
def check_nt(self, **kws): s, o = exec_command.exec_command('echo path=%path%') self.assertEqual(s, 0) self.assertNotEqual(o, '') s, o = exec_command.exec_command( '"%s" -c "import sys;sys.stderr.write(sys.platform)"' % self.pyexe) self.assertEqual(s, 0) self.assertEqual(o, 'win32')
def check_nt(self, **kws): s, o = exec_command.exec_command('cmd /C echo path=%path%') self.assertEqual(s, 0) self.assertNotEqual(o, '') s, o = exec_command.exec_command( '"%s" -c "import sys;sys.stderr.write(sys.platform)"' % self.pyexe) self.assertEqual(s, 0) self.assertEqual(o, 'win32')
def check_nt(self, **kws): s, o = exec_command.exec_command('cmd /C echo path=%path%') assert_(s == 0) assert_(o != '') s, o = exec_command.exec_command( '"%s" -c "import sys;sys.stderr.write(sys.platform)"' % self.pyexe) assert_(s == 0) assert_(o == 'win32')
def get_version(self): if platform.system() == 'Windows': version_cmd = "icl -dummy" status, output = exec_command(version_cmd, use_tee=0) version = re.search(r'Version\s*([\d.]+)', output).group(1) else: version_cmd = "icc -dumpversion" status, version = exec_command(version_cmd, use_tee=0) return version
def get_version(self): if platform.system() == 'Windows': # Intel compiler on Windows does not have way of getting version. Need to parse string using regex to # extract version string. Regex from here: https://stackoverflow.com/a/26480961/5140953 version_cmd = "icl -dummy" status, output = exec_command(version_cmd, use_tee=0) version = re.search(r'Version\s*([\d.]+)', output).group(1) else: version_cmd = "icc -dumpversion" status, version = exec_command(version_cmd, use_tee=0) return version
def test_exec_command_stderr(): # Test posix version: with redirect_stdout(TemporaryFile(mode='w+')): with redirect_stderr(StringIO()): exec_command.exec_command("cd '.'") if os.name == 'posix': # Test general (non-posix) version: with emulate_nonposix(): with redirect_stdout(TemporaryFile()): with redirect_stderr(StringIO()): exec_command.exec_command("cd '.'")
def test_exec_command_stderr(): # Test posix version: with redirect_stdout(TemporaryFile()): with redirect_stderr(StringIO.StringIO()): exec_command.exec_command("cd '.'") if os.name == 'posix': # Test general (non-posix) version: with emulate_nonposix(): with redirect_stdout(TemporaryFile()): with redirect_stderr(StringIO.StringIO()): exec_command.exec_command("cd '.'")
def test_exec_command_stderr(): # Test posix version: with redirect_stdout(TemporaryFile(mode='w+')): with redirect_stderr(StringIO()): with assert_warns(DeprecationWarning): exec_command.exec_command("cd '.'") if os.name == 'posix': # Test general (non-posix) version: with emulate_nonposix(): with redirect_stdout(TemporaryFile()): with redirect_stderr(StringIO()): with assert_warns(DeprecationWarning): exec_command.exec_command("cd '.'")
def test_exec_command_stderr(): # Test posix version: with redirect_stdout(TemporaryFile(mode="w+")): with redirect_stderr(StringIO()): with assert_warns(DeprecationWarning): exec_command.exec_command("cd '.'") if os.name == "posix": # Test general (non-posix) version: with emulate_nonposix(): with redirect_stdout(TemporaryFile()): with redirect_stderr(StringIO()): with assert_warns(DeprecationWarning): exec_command.exec_command("cd '.'")
def get_libgcc_dir(self): status, output = exec_command(self.compiler_f77 + ['-print-libgcc-file-name'], use_tee=0) if not status: return os.path.dirname(output) return None
def get_target(self): status, output = exec_command(self.compiler_f77 + ["-v"], use_tee=0) if not status: m = TARGET_R.search(output) if m: return m.group(1) return ""
def run(self): mandir = os.path.join(self.build_base, 'man') self.mkpath(mandir) from pkg_resources import iter_entry_points for entrypoint in iter_entry_points(group='console_scripts'): if not entrypoint.module_name.startswith('obspy'): continue output = os.path.join(mandir, entrypoint.name + '.1') print('Generating %s ...' % (output)) exec_command([ self.help2man, '--no-info', '--no-discard-stderr', '--output', output, '"%s -m %s"' % (sys.executable, entrypoint.module_name) ])
def compile(source, modulename = 'untitled', extra_args = '', verbose = 1, source_fn = None ): ''' Build extension module from processing source with f2py. Read the source of this function for more information. ''' from numpy.distutils.exec_command import exec_command import tempfile if source_fn is None: fname = os.path.join(tempfile.mktemp()+'.f') else: fname = source_fn f = open(fname, 'w') f.write(source) f.close() args = ' -c -m %s %s %s'%(modulename, fname, extra_args) c = '%s -c "import numpy.f2py as f2py2e;f2py2e.main()" %s' %(sys.executable, args) s, o = exec_command(c) if source_fn is None: try: os.remove(fname) except OSError: pass return s
def get_version(self,*args,**kwds): version = FCompiler.get_version(self,*args,**kwds) if version is None and sys.platform.startswith('aix'): # use lslpp to find out xlf version lslpp = find_executable('lslpp') xlf = find_executable('xlf') if os.path.exists(xlf) and os.path.exists(lslpp): s,o = exec_command(lslpp + ' -Lc xlfcmp') m = re.search('xlfcmp:(?P<version>\d+([.]\d+)+)', o) if m: version = m.group('version') xlf_dir = '/etc/opt/ibmcmp/xlf' if version is None and os.path.isdir(xlf_dir): # linux: # If the output of xlf does not contain version info # (that's the case with xlf 8.1, for instance) then # let's try another method: l = os.listdir(xlf_dir) l.sort() l.reverse() l = [d for d in l if os.path.isfile(os.path.join(xlf_dir,d,'xlf.cfg'))] if l: from distutils.version import LooseVersion self.version = version = LooseVersion(l[0]) return version
def run(self): mandir = os.path.join(self.build_base, 'man') self.mkpath(mandir) from pkg_resources import iter_entry_points for entrypoint in iter_entry_points(group='console_scripts'): if not entrypoint.module_name.startswith('obspy'): continue output = os.path.join(mandir, entrypoint.name + '.1') print('Generating %s ...' % (output)) exec_command([self.help2man, '--no-info', '--no-discard-stderr', '--output', output, '"%s -m %s"' % (sys.executable, entrypoint.module_name)])
def get_target(self): status, output = exec_command(self.compiler_f77 + ['-v'], use_tee=0) if not status: m = TARGET_R.search(output) if m: return m.group(1) return ""
def get_output(self, body, headers=None, include_dirs=None, libraries=None, library_dirs=None, lang="c"): """Try to compile, link to an executable, and run a program built from 'body' and 'headers'. Returns the exit status code of the program and its output. """ from distutils.ccompiler import CompileError, LinkError self._check_compiler() exitcode, output = 255, '' try: src, obj, exe = self._link(body, headers, include_dirs, libraries, library_dirs, lang) exe = os.path.join('.', exe) exitstatus, output = exec_command(exe, execute_in='.') if hasattr(os, 'WEXITSTATUS'): exitcode = os.WEXITSTATUS(exitstatus) if os.WIFSIGNALED(exitstatus): sig = os.WTERMSIG(exitstatus) log.error('subprocess exited with signal %d' % (sig, )) if sig == signal.SIGINT: # control-C raise KeyboardInterrupt else: exitcode = exitstatus log.info("success!") except (CompileError, LinkError): log.info("failure.") self._clean() return exitcode, output
def CCompiler_get_version(self, force=0, ok_status=[0]): """ Compiler version. Returns None if compiler is not available. """ if not force and hasattr(self, 'version'): return self.version try: version_cmd = self.version_cmd except AttributeError: return None cmd = ' '.join(version_cmd) try: matcher = self.version_match except AttributeError: try: pat = self.version_pattern except AttributeError: return None def matcher(version_string): m = re.match(pat, version_string) if not m: return None version = m.group('version') return version status, output = exec_command(cmd, use_tee=0) version = None if status in ok_status: version = matcher(output) if not version: log.warn("Couldn't match compiler version for %r" % (output, )) else: version = LooseVersion(version) self.version = version return version
def get_output(self, body, headers=None, include_dirs=None, libraries=None, library_dirs=None, lang="c"): """Try to compile, link to an executable, and run a program built from 'body' and 'headers'. Returns the exit status code of the program and its output. """ from distutils.ccompiler import CompileError, LinkError self._check_compiler() exitcode, output = 255, '' try: src, obj, exe = self._link(body, headers, include_dirs, libraries, library_dirs, lang) exe = os.path.join('.', exe) exitstatus, output = exec_command(exe, execute_in='.') if hasattr(os, 'WEXITSTATUS'): exitcode = os.WEXITSTATUS(exitstatus) if os.WIFSIGNALED(exitstatus): sig = os.WTERMSIG(exitstatus) log.error('subprocess exited with signal %d' % (sig,)) if sig == signal.SIGINT: # control-C raise KeyboardInterrupt else: exitcode = exitstatus log.info("success!") except (CompileError, LinkError): log.info("failure.") self._clean() return exitcode, output
def CCompiler_get_version(self, force=0, ok_status=[0]): """ Compiler version. Returns None if compiler is not available. """ if not force and hasattr(self,'version'): return self.version try: version_cmd = self.version_cmd except AttributeError: return None cmd = ' '.join(version_cmd) try: matcher = self.version_match except AttributeError: try: pat = self.version_pattern except AttributeError: return None def matcher(version_string): m = re.match(pat, version_string) if not m: return None version = m.group('version') return version status, output = exec_command(cmd,use_tee=0) version = None if status in ok_status: version = matcher(output) if not version: log.warn("Couldn't match compiler version for %r" % (output,)) else: version = LooseVersion(version) self.version = version return version
def CCompiler_get_version(self, force=False, ok_status=[0]): """Compiler version. Returns None if compiler is not available.""" if not force and hasattr(self, 'version'): return self.version self.find_executables() try: version_cmd = self.version_cmd except AttributeError: return None if not version_cmd or not version_cmd[0]: return None try: matcher = self.version_match except AttributeError: try: pat = self.version_pattern except AttributeError: return None def matcher(version_string): m = re.match(pat, version_string) if not m: return None version = m.group('version') return version status, output = exec_command(version_cmd, use_tee=0) version = None if status in ok_status: version = matcher(output) if version: version = LooseVersion(version) self.version = version return version
def compile(source, modulename='untitled', extra_args='', verbose=1, source_fn=None): ''' Build extension module from processing source with f2py. Read the source of this function for more information. ''' from numpy.distutils.exec_command import exec_command import tempfile if source_fn is None: fname = os.path.join(tempfile.mktemp() + '.f') else: fname = source_fn f = open(fname, 'w') f.write(source) f.close() args = ' -c -m %s %s %s' % (modulename, fname, extra_args) c = '%s -c "import numpy.f2py as f2py2e;f2py2e.main()" %s' % ( sys.executable, args) s, o = exec_command(c) if source_fn is None: try: os.remove(fname) except OSError: pass return s
def execute(self, *args): """ Run generated setup.py file with given arguments. """ if not args: raise ValueError('need setup.py arguments') self.debug(self.generate(dry_run=False)) cmd = [sys.executable, 'setup.py'] + list(args) self.debug('entering %r directory' % (self.path)) self.debug('executing command %r' % (' '.join(cmd))) try: r = exec_command(cmd, execute_in=self.path, use_tee=False) except: self.info('leaving %r directory' % (self.path)) raise else: try: self._show_warnings_errors(r[1]) except Exception, msg: #print msg self.warning(r[1]) if not r[0]: self.debug('SUCCESS!') else: self.warning('FAILURE') self.debug('leaving %r directory' % (self.path))
def CCompiler_get_version(self, force=False, ok_status=[0]): """Compiler version. Returns None if compiler is not available.""" if not force and hasattr(self,'version'): return self.version self.find_executables() try: version_cmd = self.version_cmd except AttributeError: return None if not version_cmd or not version_cmd[0]: return None try: matcher = self.version_match except AttributeError: try: pat = self.version_pattern except AttributeError: return None def matcher(version_string): m = re.match(pat, version_string) if not m: return None version = m.group('version') return version status, output = exec_command(version_cmd,use_tee=0) version = None if status in ok_status: version = matcher(output) if version: version = LooseVersion(version) self.version = version return version
def execute(self, *args): """ Run generated setup.py file with given arguments. """ if not args: raise ValueError('need setup.py arguments') self.debug(self.generate(dry_run=False)) cmd = [sys.executable,'setup.py'] + list(args) self.debug('entering %r directory' % (self.path)) self.debug('executing command %r' % (' '.join(cmd))) try: r = exec_command(cmd, execute_in=self.path, use_tee=False) except: self.info('leaving %r directory' % (self.path)) raise else: try: self._show_warnings_errors(r[1]) except Exception, msg: #print msg self.warning(r[1]) if not r[0]: self.debug('SUCCESS!') else: self.warning('FAILURE') self.debug('leaving %r directory' % (self.path))
def compile(source, modulename = 'untitled', extra_args = '', verbose = 1, source_fn = None ): ''' Build extension module from processing source with f2py. Read the source of this function for more information. ''' from numpy.distutils.exec_command import exec_command import tempfile if source_fn is None: f = tempfile.NamedTemporaryFile(suffix='.f') else: f = open(source_fn, 'w') try: f.write(source) f.flush() args = ' -c -m %s %s %s'%(modulename, f.name, extra_args) c = '%s -c "import numpy.f2py as f2py2e;f2py2e.main()" %s' % \ (sys.executable, args) s, o = exec_command(c) finally: f.close() return s
def get_version(self, *args, **kwds): version = FCompiler.get_version(self, *args, **kwds) if version is None and sys.platform.startswith('aix'): # use lslpp to find out xlf version lslpp = find_executable('lslpp') xlf = find_executable('xlf') if os.path.exists(xlf) and os.path.exists(lslpp): s, o = exec_command(lslpp + ' -Lc xlfcmp') m = re.search('xlfcmp:(?P<version>\d+([.]\d+)+)', o) if m: version = m.group('version') xlf_dir = '/etc/opt/ibmcmp/xlf' if version is None and os.path.isdir(xlf_dir): # linux: # If the output of xlf does not contain version info # (that's the case with xlf 8.1, for instance) then # let's try another method: l = os.listdir(xlf_dir) l.sort() l.reverse() l = [ d for d in l if os.path.isfile(os.path.join(xlf_dir, d, 'xlf.cfg')) ] if l: from distutils.version import LooseVersion self.version = version = LooseVersion(l[0]) return version
def check_execute_in(self, **kws): with tempdir() as tmpdir: fn = "file" tmpfile = os.path.join(tmpdir, fn) with open(tmpfile, 'w') as f: f.write('Hello') s, o = exec_command.exec_command( '"%s" -c "f = open(\'%s\', \'r\'); f.close()"' % (self.pyexe, fn), **kws) assert_(s != 0) assert_(o != '') s, o = exec_command.exec_command( '"%s" -c "f = open(\'%s\', \'r\'); print(f.read()); ' 'f.close()"' % (self.pyexe, fn), execute_in=tmpdir, **kws) assert_(s == 0) assert_(o == 'Hello')
def spawn(self, cmd, display=None): if type(cmd) is type([]) and os.name == 'nt': cmd = _nt_quote_args(cmd) s,o = exec_command(cmd, use_tee=0) if s: from distutils.ccompiler import DistutilsExecError raise DistutilsExecError,\ 'Command "%s" failed with exit status %d' % (cmd, s)
def run(self): mandir = os.path.join(self.build_base, 'man') self.mkpath(mandir) from pkg_resources import EntryPoint for entrypoint in ENTRY_POINTS['console_scripts']: ep = EntryPoint.parse(entrypoint) if not ep.module_name.startswith('obspy'): continue output = os.path.join(mandir, ep.name + '.1') print('Generating %s ...' % (output)) exec_command([self.help2man, '--no-info', '--no-discard-stderr', '--output', output, '"%s -m %s"' % (sys.executable, ep.module_name)])
def run(self): mandir = os.path.join(self.build_base, 'man') self.mkpath(mandir) from pkg_resources import EntryPoint for entrypoint in ENTRY_POINTS['console_scripts']: ep = EntryPoint.parse(entrypoint) if not ep.module_name.startswith('obspy'): continue output = os.path.join(mandir, ep.name + '.1') print('Generating %s ...' % (output)) exec_command([ self.help2man, '--no-info', '--no-discard-stderr', '--output', output, '"%s -m %s"' % (sys.executable, ep.module_name) ])
def _libs_with_msvc_and_fortran(self, fcompiler, c_libraries, c_library_dirs): if fcompiler is None: return for libname in c_libraries: if libname.startswith('msvc'): continue fileexists = False for libdir in c_library_dirs or []: libfile = os.path.join(libdir, '%s.lib' % (libname)) if os.path.isfile(libfile): fileexists = True break if fileexists: continue # make g77-compiled static libs available to MSVC fileexists = False for libdir in c_library_dirs: libfile = os.path.join(libdir, 'lib%s.a' % (libname)) if os.path.isfile(libfile): # copy libname.a file to name.lib so that MSVC linker # can find it libfile2 = os.path.join(self.build_temp, libname + '.lib') copy_file(libfile, libfile2) if self.build_temp not in c_library_dirs: c_library_dirs.append(self.build_temp) fileexists = True break if fileexists: continue log.warn('could not find library %r in directories %s' % (libname, c_library_dirs)) # Always use system linker when using MSVC compiler. f_lib_dirs = [] for dir in fcompiler.library_dirs: # correct path when compiling in Cygwin but with normal Win # Python if dir.startswith('/usr/lib'): s, o = exec_command(['cygpath', '-w', dir], use_tee=False) if not s: dir = o f_lib_dirs.append(dir) c_library_dirs.extend(f_lib_dirs) # make g77-compiled static libs available to MSVC for lib in fcompiler.libraries: if not lib.startswith('msvc'): c_libraries.append(lib) p = combine_paths(f_lib_dirs, 'lib' + lib + '.a') if p: dst_name = os.path.join(self.build_temp, lib + '.lib') if not os.path.isfile(dst_name): copy_file(p[0], dst_name) if self.build_temp not in c_library_dirs: c_library_dirs.append(self.build_temp)
def check_execute_in(self, **kws): with tempdir() as tmpdir: fn = "file" tmpfile = os.path.join(tmpdir, fn) f = open(tmpfile, 'w') f.write('Hello') f.close() s, o = exec_command.exec_command( '"%s" -c "f = open(\'%s\', \'r\'); f.close()"' % (self.pyexe, fn), **kws) self.assertNotEqual(s, 0) self.assertNotEqual(o, '') s, o = exec_command.exec_command( '"%s" -c "f = open(\'%s\', \'r\'); print(f.read()); ' 'f.close()"' % (self.pyexe, fn), execute_in=tmpdir, **kws) self.assertEqual(s, 0) self.assertEqual(o, 'Hello')
def check_execute_in(self, **kws): with tempdir() as tmpdir: fn = "file" tmpfile = os.path.join(tmpdir, fn) f = open(tmpfile, 'w') f.write('Hello') f.close() s, o = exec_command.exec_command( '"%s" -c "f = open(\'%s\', \'r\'); f.close()"' % (self.pyexe, fn), **kws) assert_(s != 0) assert_(o != '') s, o = exec_command.exec_command( '"%s" -c "f = open(\'%s\', \'r\'); print(f.read()); ' 'f.close()"' % (self.pyexe, fn), execute_in=tmpdir, **kws) assert_(s == 0) assert_(o == 'Hello')
def compile_fortran(source, module_name, extra_args='', folder='./'): with tempfile.NamedTemporaryFile('w', suffix='.f90') as f: f.write(source) f.flush() args = ' -c -m {} {} {}'.format(module_name, f.name, extra_args) command = 'cd "{}" && "{}" -c "import numpy.f2py as f2py;f2py.main()" {}'.format( folder, sys.executable, args) status, output = exec_command(command) return status, output, command
def compile_fortran(fname, module_name, extra_flags=''): folder = os.path.dirname(os.path.realpath(__file__)) args = (f" -c -m {module_name} {fname} " f"--f90flags={extra_flags}") command = (f'cd "{folder}" && "{sys.executable}" -c ' f'"import numpy.f2py as f2py;f2py.main()" {args}') print(command) status, output = exec_command(command) return status, output, command
def check_execute_in(self, **kws): with tempdir() as tmpdir: fn = "file" tmpfile = os.path.join(tmpdir, fn) with open(tmpfile, "w") as f: f.write("Hello") s, o = exec_command.exec_command( "\"%s\" -c \"f = open('%s', 'r'); f.close()\"" % (self.pyexe, fn), **kws ) assert_(s != 0) assert_(o != "") s, o = exec_command.exec_command( "\"%s\" -c \"f = open('%s', 'r'); print(f.read()); " 'f.close()"' % (self.pyexe, fn), execute_in=tmpdir, **kws ) assert_(s == 0) assert_(o == "Hello")
def target_architecture(self, extra_opts=()): """Return the architecture that the compiler will build for. This is most useful for detecting universal compilers in OS X.""" extra_opts = list(extra_opts) status, output = exec_command(self.compiler_f90 + ['-v'] + extra_opts, use_tee=False) if status == 0: m = re.match(r'(?m)^Target: (.*)$', output) if m: return m.group(1) return None
def _can_target(self, cmd, arch): """Return true is the compiler support the -arch flag for the given architecture.""" newcmd = cmd[:] newcmd.extend(["-arch", arch, "-v"]) st, out = exec_command(" ".join(newcmd)) if st == 0: for line in out.splitlines(): m = re.search(_R_ARCHS[arch], line) if m: return True return False
def CCompiler_get_version(self, force=False, ok_status=[0]): """ Return compiler version, or None if compiler is not available. Parameters ---------- force : bool, optional If True, force a new determination of the version, even if the compiler already has a version attribute. Default is False. ok_status : list of int, optional The list of status values returned by the version look-up process for which a version string is returned. If the status value is not in `ok_status`, None is returned. Default is ``[0]``. Returns ------- version : str or None Version string, in the format of `distutils.version.LooseVersion`. """ if not force and hasattr(self, 'version'): return self.version self.find_executables() try: version_cmd = self.version_cmd except AttributeError: return None if not version_cmd or not version_cmd[0]: return None try: matcher = self.version_match except AttributeError: try: pat = self.version_pattern except AttributeError: return None def matcher(version_string): m = re.match(pat, version_string) if not m: return None version = m.group('version') return version status, output = exec_command(version_cmd, use_tee=0) version = None if status in ok_status: version = matcher(output) if version: version = LooseVersion(version) self.version = version return version
def test_exec_command_stdout(): # Regression test for gh-2999 and gh-2915. # There are several packages (nose, scipy.weave.inline, Sage inline # Fortran) that replace stdout, in which case it doesn't have a fileno # method. This is tested here, with a do-nothing command that fails if the # presence of fileno() is assumed in exec_command. # The code has a special case for posix systems, so if we are on posix test # both that the special case works and that the generic code works. # Test posix version: with redirect_stdout(StringIO.StringIO()): with redirect_stderr(TemporaryFile()): exec_command.exec_command("cd '.'") if os.name == 'posix': # Test general (non-posix) version: with emulate_nonposix(): with redirect_stdout(StringIO.StringIO()): with redirect_stderr(TemporaryFile()): exec_command.exec_command("cd '.'")
def test_exec_command_stdout(): # Regression test for gh-2999 and gh-2915. # There are several packages (nose, scipy.weave.inline, Sage inline # Fortran) that replace stdout, in which case it doesn't have a fileno # method. This is tested here, with a do-nothing command that fails if the # presence of fileno() is assumed in exec_command. # The code has a special case for posix systems, so if we are on posix test # both that the special case works and that the generic code works. # Test posix version: with redirect_stdout(StringIO()): with redirect_stderr(TemporaryFile()): exec_command.exec_command("cd '.'") if os.name == 'posix': # Test general (non-posix) version: with emulate_nonposix(): with redirect_stdout(StringIO()): with redirect_stderr(TemporaryFile()): exec_command.exec_command("cd '.'")
def CCompiler_get_version(self, force=False, ok_status=[0]): """ Return compiler version, or None if compiler is not available. Parameters ---------- force : bool, optional If True, force a new determination of the version, even if the compiler already has a version attribute. Default is False. ok_status : list of int, optional The list of status values returned by the version look-up process for which a version string is returned. If the status value is not in `ok_status`, None is returned. Default is ``[0]``. Returns ------- version : str or None Version string, in the format of `distutils.version.LooseVersion`. """ if not force and hasattr(self,'version'): return self.version self.find_executables() try: version_cmd = self.version_cmd except AttributeError: return None if not version_cmd or not version_cmd[0]: return None try: matcher = self.version_match except AttributeError: try: pat = self.version_pattern except AttributeError: return None def matcher(version_string): m = re.match(pat, version_string) if not m: return None version = m.group('version') return version status, output = exec_command(version_cmd,use_tee=0) version = None if status in ok_status: version = matcher(output) if version: version = LooseVersion(version) self.version = version return version
def compile(source, modulename='untitled', extra_args='', verbose=True, source_fn=None, extension='.f' ): """ Build extension module from processing source with f2py. Parameters ---------- source : str Fortran source of module / subroutine to compile modulename : str, optional The name of the compiled python module extra_args : str, optional Additional parameters passed to f2py verbose : bool, optional Print f2py output to screen source_fn : str, optional Name of the file where the fortran source is written. The default is to use a temporary file with the extension provided by the `extension` parameter extension : {'.f', '.f90'}, optional Filename extension if `source_fn` is not provided. The extension tells which fortran standard is used. The default is `.f`, which implies F77 standard. .. versionadded:: 1.11.0 """ from numpy.distutils.exec_command import exec_command import tempfile if source_fn is None: f = tempfile.NamedTemporaryFile(suffix=extension) else: f = open(source_fn, 'w') try: f.write(source) f.flush() args = ' -c -m {} {} {}'.format(modulename, f.name, extra_args) c = '{} -c "import numpy.f2py as f2py2e;f2py2e.main()" {}' c = c.format(sys.executable, args) status, output = exec_command(c) if verbose: print(output) finally: f.close() return status
def run(self): mandir = os.path.join(self.build_base, "man") self.mkpath(mandir) from pkg_resources import iter_entry_points for entrypoint in iter_entry_points(group="console_scripts"): if not entrypoint.module_name.startswith("obspy"): continue output = os.path.join(mandir, entrypoint.name + ".1") print("Generating %s ..." % (output)) exec_command( [ self.help2man, "--no-info", "--no-discard-stderr", "--output", output, '"%s -m %s"' % (sys.executable, entrypoint.module_name), ] )
def check_basic(self, *kws): s, o = exec_command.exec_command( '"%s" -c "raise \'Ignore me.\'"' % self.pyexe, **kws) self.assertNotEqual(s, 0) self.assertNotEqual(o, '') s, o = exec_command.exec_command( '"%s" -c "import sys;sys.stderr.write(\'0\');' 'sys.stderr.write(\'1\');sys.stderr.write(\'2\')"' % self.pyexe, **kws) self.assertEqual(s, 0) self.assertEqual(o, '012') s, o = exec_command.exec_command( '"%s" -c "import sys;sys.exit(15)"' % self.pyexe, **kws) self.assertEqual(s, 15) self.assertEqual(o, '') s, o = exec_command.exec_command( '"%s" -c "print(\'Heipa\'")' % self.pyexe, **kws) self.assertEqual(s, 0) self.assertEqual(o, 'Heipa')
def check_basic(self, *kws): s, o = exec_command.exec_command( '"%s" -c "raise \'Ignore me.\'"' % self.pyexe, **kws) assert_(s != 0) assert_(o != '') s, o = exec_command.exec_command( '"%s" -c "import sys;sys.stderr.write(\'0\');' 'sys.stderr.write(\'1\');sys.stderr.write(\'2\')"' % self.pyexe, **kws) assert_(s == 0) assert_(o == '012') s, o = exec_command.exec_command( '"%s" -c "import sys;sys.exit(15)"' % self.pyexe, **kws) assert_(s == 15) assert_(o == '') s, o = exec_command.exec_command( '"%s" -c "print(\'Heipa\'")' % self.pyexe, **kws) assert_(s == 0) assert_(o == 'Heipa')
def _link (self, body, headers, include_dirs, libraries, library_dirs, lang): if self.compiler.compiler_type=='msvc': libraries = (libraries or [])[:] library_dirs = (library_dirs or [])[:] if lang in ['f77', 'f90']: lang = 'c' # always use system linker when using MSVC compiler if self.fcompiler: for d in self.fcompiler.library_dirs or []: # correct path when compiling in Cygwin but with # normal Win Python if d.startswith('/usr/lib'): s, o = exec_command(['cygpath', '-w', d], use_tee=False) if not s: d = o library_dirs.append(d) for libname in self.fcompiler.libraries or []: if libname not in libraries: libraries.append(libname) for libname in libraries: if libname.startswith('msvc'): continue fileexists = False for libdir in library_dirs or []: libfile = os.path.join(libdir, '%s.lib' % (libname)) if os.path.isfile(libfile): fileexists = True break if fileexists: continue # make g77-compiled static libs available to MSVC fileexists = False for libdir in library_dirs: libfile = os.path.join(libdir, 'lib%s.a' % (libname)) if os.path.isfile(libfile): # copy libname.a file to name.lib so that MSVC linker # can find it libfile2 = os.path.join(libdir, '%s.lib' % (libname)) copy_file(libfile, libfile2) self.temp_files.append(libfile2) fileexists = True break if fileexists: continue log.warn('could not find library %r in directories %s' \ % (libname, library_dirs)) elif self.compiler.compiler_type == 'mingw32': generate_manifest(self) return self._wrap_method(old_config._link, lang, (body, headers, include_dirs, libraries, library_dirs, lang))
def CCompiler_spawn(self, cmd, display=None): if display is None: display = cmd if is_sequence(display): display = ' '.join(list(display)) log.info(display) if is_sequence(cmd) and os.name == 'nt': cmd = _nt_quote_args(list(cmd)) s,o = exec_command(cmd) if s: if is_sequence(cmd): cmd = ' '.join(list(cmd)) print o raise DistutilsExecError,\ 'Command "%s" failed with exit status %d' % (cmd, s)
def get_libgfortran_dir(self): if sys.platform[:5] == 'linux': libgfortran_name = 'libgfortran.so' elif sys.platform == 'darwin': libgfortran_name = 'libgfortran.dylib' else: libgfortran_name = None libgfortran_dir = None if libgfortran_name: find_lib_arg = ['-print-file-name={0}'.format(libgfortran_name)] status, output = exec_command( self.compiler_f77 + find_lib_arg, use_tee=0) if not status: libgfortran_dir = os.path.dirname(output) return libgfortran_dir
def CCompiler_spawn(self, cmd, display=None): if display is None: display = cmd if is_sequence(display): display = ' '.join(list(display)) log.info(display) s,o = exec_command(cmd) if s: if is_sequence(cmd): cmd = ' '.join(list(cmd)) print o if re.search('Too many open files', o): msg = '\nTry rerunning setup command until build succeeds.' else: msg = '' raise DistutilsExecError,\ 'Command "%s" failed with exit status %d%s' % (cmd, s, msg)