在python中搜索命令输出以获取字符串

时间:2018-07-27 08:16:40

标签: python grep subprocess

我想从python中检查f2py使用了什么编译器。

执行中:

[Windows::UI::Xaml::Data::Bindable]
public ref class DataType sealed : public INotifyPropertyChanged {
...
}

产生以下输出:

f2py -c --help-fcompiler

是否有一种简单的方法来运行此命令,并在找到的Fortran编译器下面存储/输出以下行:

Gnu95FCompiler instance properties:
  archiver        = ['/usr/bin/gfortran', '-cr']
  compile_switch  = '-c'
  compiler_f77    = ['/usr/bin/gfortran', '-Wall', '-g', '-ffixed-form', '-
                    fno-second-underscore', '-fPIC', '-O3', '-funroll-loops']
  compiler_f90    = ['gfortran', '-Wall', '-g', '-fno-second-underscore', '-
                    fPIC', '-O3', '-funroll-loops']
  compiler_fix    = ['gfortran', '-Wall', '-g', '-ffixed-form', '-fno-second
                    -underscore', '-Wall', '-g', '-fno-second-underscore', '-
                    fPIC', '-O3', '-funroll-loops']
  libraries       = ['gfortran']
  library_dirs    = ['/usr/lib/gcc/x86_64-linux-gnu/7', '/usr/lib/gcc/x86_64
                    -linux-gnu/7']
  linker_exe      = ['/usr/bin/gfortran', '-Wall', '-Wall']
  linker_so       = ['/usr/bin/gfortran', '-Wall', '-g', '-Wall', '-g', '-
                    shared']
  object_switch   = '-o '
  ranlib          = ['/usr/bin/gfortran']
  version         = LooseVersion ('7')
  version_cmd     = ['/usr/bin/gfortran', '-dumpversion']
Fortran compilers found:
  --fcompiler=gnu95  GNU Fortran 95 compiler (7)
Compilers available for this platform, but not found:
  --fcompiler=absoft   Absoft Corp Fortran Compiler
  --fcompiler=compaq   Compaq Fortran Compiler
  --fcompiler=g95      G95 Fortran Compiler
  --fcompiler=gnu      GNU Fortran 77 compiler
  --fcompiler=intel    Intel Fortran Compiler for 32-bit apps
  --fcompiler=intele   Intel Fortran Compiler for Itanium apps
  --fcompiler=intelem  Intel Fortran Compiler for 64-bit apps
  --fcompiler=lahey    Lahey/Fujitsu Fortran 95 Compiler
  --fcompiler=nag      NAGWare Fortran 95 Compiler
  --fcompiler=nagfor   NAG Fortran Compiler
  --fcompiler=pathf95  PathScale Fortran Compiler
  --fcompiler=pg       Portland Group Fortran Compiler
  --fcompiler=vast     Pacific-Sierra Research Fortran 90 Compiler
Compilers not available on this platform:
  --fcompiler=flang     Portland Group Fortran LLVM Compiler
  --fcompiler=hpux      HP Fortran 90 Compiler
  --fcompiler=ibm       IBM XL Fortran Compiler
  --fcompiler=intelev   Intel Visual Fortran Compiler for Itanium apps
  --fcompiler=intelv    Intel Visual Fortran Compiler for 32-bit apps
  --fcompiler=intelvem  Intel Visual Fortran Compiler for 64-bit apps
  --fcompiler=mips      MIPSpro Fortran Compiler
  --fcompiler=none      Fake Fortran compiler
  --fcompiler=sun       Sun or Forte Fortran 95 Compiler
For compiler details, run 'config_fc --verbose' setup comm

我希望能够检查使用的编译器是GNU编译器。

到目前为止,我所拥有的是:

Fortran compilers found:
  --fcompiler=gnu95  GNU Fortran 95 compiler (7)

这在我的系统上似乎可以正常工作,但是我担心是否有人在其他系统上的setup.py(正在使用该文件的地方)上运行此文件,

原因是,如果我只是在check_compiler()中打印结果:

打印(结果)

我得到以下信息:

def check_compiler():

   result = subprocess.check_output('f2py -c --help-fcompiler | grep -A 1 \'Fortran compilers found\' ',shell=True)
   print('GNU' in str(result))

我不确定为什么在这里\ n不会拆分此结果,所以如果有人知道我将不胜感激。

0 个答案:

没有答案