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 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 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_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 _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) 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 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 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 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 numpy1.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 _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 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 get_output(self, body, headers=None, include_dirs=None, libraries=None, library_dirs=None, lang="c", use_tee=None): """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. """ # 2008-11-16, RemoveMe warnings.warn("\n+++++++++++++++++++++++++++++++++++++++++++++++++\n" \ "Usage of get_output is deprecated: please do not \n" \ "use it anymore, and avoid configuration checks \n" \ "involving running executable on the target machine.\n" \ "+++++++++++++++++++++++++++++++++++++++++++++++++\n", DeprecationWarning, stacklevel=2) from distutils.ccompiler import CompileError, LinkError self._check_compiler() exitcode, output = 255, '' try: grabber = GrabStdout() try: src, obj, exe = self._link(body, headers, include_dirs, libraries, library_dirs, lang) grabber.restore() except Exception: output = grabber.data grabber.restore() raise exe = os.path.join('.', exe) exitstatus, output = exec_command(exe, execute_in='.', use_tee=use_tee) 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_spawn(self, cmd, display=None): """ Execute a command in a sub-process. Parameters ---------- cmd : str The command to execute. display : str or sequence of str, optional The text to add to the log file kept by `numpy.distutils`. If not given, `display` is equal to `cmd`. Returns ------- None Raises ------ DistutilsExecError If the command failed, i.e. the exit status was not 0. """ 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)) try: print(o) except UnicodeError: # When installing through pip, `o` can contain non-ascii chars pass 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))
def check_posix(self, **kws): s, o = exec_command.exec_command("echo Hello", **kws) assert_(s == 0) assert_(o == 'Hello') s, o = exec_command.exec_command('echo $AAA', **kws) assert_(s == 0) assert_(o == '') s, o = exec_command.exec_command('echo "$AAA"', AAA='Tere', **kws) assert_(s == 0) assert_(o == 'Tere') s, o = exec_command.exec_command('echo "$AAA"', **kws) assert_(s == 0) assert_(o == '') if 'BBB' not in os.environ: os.environ['BBB'] = 'Hi' s, o = exec_command.exec_command('echo "$BBB"', **kws) assert_(s == 0) assert_(o == 'Hi') s, o = exec_command.exec_command('echo "$BBB"', BBB='Hey', **kws) assert_(s == 0) assert_(o == 'Hey') s, o = exec_command.exec_command('echo "$BBB"', **kws) assert_(s == 0) assert_(o == 'Hi') del os.environ['BBB'] s, o = exec_command.exec_command('echo "$BBB"', **kws) assert_(s == 0) assert_(o == '') s, o = exec_command.exec_command('this_is_not_a_command', **kws) assert_(s != 0) assert_(o != '') s, o = exec_command.exec_command('echo path=$PATH', **kws) assert_(s == 0) assert_(o != '') s, o = exec_command.exec_command( '"%s" -c "import sys,os;sys.stderr.write(os.name)"' % self.pyexe, **kws) assert_(s == 0) assert_(o == 'posix')