PYTHON   12

argdb.py

Guest on 26th May 2021 07:45:17 PM

  1. #
  2. #  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3. #  SLEPc - Scalable Library for Eigenvalue Problem Computations
  4. #  Copyright (c) 2002-2021, Universitat Politecnica de Valencia, Spain
  5. #
  6. #  This file is part of SLEPc.
  7. #  SLEPc is distributed under a 2-clause BSD license (see LICENSE).
  8. #  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  9. #
  10.  
  11. import os, sys
  12.  
  13. class ArgDB:
  14.  
  15.   def __init__(self,argv):
  16.     # standardize options
  17.     for l in range(1,len(argv)):
  18.       name = argv[l]
  19.       if name.startswith('--enable'):
  20.         argv[l] = name.replace('--enable','--with')
  21.         if name.find('=') == -1: argv[l] += '=1'
  22.       elif name.startswith('--disable'):
  23.         argv[l] = name.replace('--disable','--with')
  24.         if name.find('=') == -1: argv[l] += '=0'
  25.         elif name.endswith('=1'): argv[l].replace('=1','=0')
  26.       elif name.startswith('--without'):
  27.         argv[l] = name.replace('--without','--with')
  28.         if name.find('=') == -1: argv[l] += '=0'
  29.         elif name.endswith('=1'): argv[l].replace('=1','=0')
  30.       elif name.startswith('--with'):
  31.         if name.find('=') == -1: argv[l] += '=1'
  32.     self.argdb = argv[1:]
  33.     self.useda = []
  34.  
  35.   def UsedArgs(self):
  36.     return ' '.join(self.useda)
  37.  
  38.   def PopString(self,keyword):
  39.     string = ''
  40.     numhits = 0
  41.     while True:
  42.       found = 0
  43.       for i, s in enumerate(self.argdb):
  44.         if s.startswith('--'+keyword+'='):
  45.           string = s.split('=',1)[1]
  46.           found = 1
  47.           numhits = numhits + 1
  48.           self.useda.append(self.argdb[i])
  49.           del self.argdb[i]
  50.           break
  51.       if not found:
  52.         break
  53.     return string,numhits
  54.  
  55.   def PopPath(self,keyword,exist=False):
  56.     string = ''
  57.     numhits = 0
  58.     while True:
  59.       found = 0
  60.       for i, s in enumerate(self.argdb):
  61.         if s.startswith('--'+keyword+'='):
  62.           string = os.path.expanduser(s.split('=')[1].rstrip('/'))
  63.           found = 1
  64.           numhits = numhits + 1
  65.           self.useda.append(self.argdb[i])
  66.           del self.argdb[i]
  67.           break
  68.       if not found:
  69.         break
  70.     if exist and numhits and not os.path.exists(string):
  71.       sys.exit('ERROR: The path given to --'+keyword+' does not exist: '+string)
  72.     return string,numhits
  73.  
  74.   def PopUrl(self,keyword):
  75.     value = False
  76.     string = ''
  77.     numhits = 0
  78.     while True:
  79.       found = 0
  80.       for i, s in enumerate(self.argdb):
  81.         if s.startswith('--'+keyword):
  82.           value = not s.endswith('=0')
  83.           try: string = s.split('=')[1]
  84.           except IndexError: pass
  85.           found = 1
  86.           numhits = numhits + 1
  87.           self.useda.append(self.argdb[i])
  88.           del self.argdb[i]
  89.           break
  90.       if not found:
  91.         break
  92.     return string,value,numhits
  93.  
  94.   def PopBool(self,keyword):
  95.     value = False
  96.     numhits = 0
  97.     while True:
  98.       found = 0
  99.       for i, s in enumerate(self.argdb):
  100.         if s.startswith('--'+keyword+'='):
  101.           value = not s.endswith('=0')
  102.           found = 1
  103.           numhits = numhits + 1
  104.           self.useda.append(self.argdb[i])
  105.           del self.argdb[i]
  106.           break
  107.       if not found:
  108.         break
  109.     return value,numhits
  110.  
  111.   def PopHelp(self):
  112.     value = False
  113.     numhits = 0
  114.     while True:
  115.       found = 0
  116.       for i, s in enumerate(self.argdb):
  117.         if s.startswith('--h') or s.startswith('-h') or s.startswith('-?'):
  118.           value = True
  119.           found = 1
  120.           numhits = numhits + 1
  121.           del self.argdb[i]
  122.           break
  123.       if not found:
  124.         break
  125.     return value
  126.  
  127.   def ErrorPetscOptions(self):
  128.     petscopts = []
  129.     strings = ['with-precision','with-scalar-type','with-clanguage','with-cc','with-cxx','with-fc','with-blaslapack-lib','with-scalapack-dir','with-elemental-dir']
  130.     for s in strings:
  131.       value,found = self.PopString(s)
  132.       if found: petscopts.append(s)
  133.     bools = ['with-fortran-bindings','with-64-bit-indices','with-shared-libraries','with-single-library','with-debugging','with-cuda','with-mpi','with-openmp']
  134.     for s in bools:
  135.       value,found = self.PopBool(s)
  136.       if found: petscopts.append(s)
  137.     urls = ['download-f2cblaslapack','download-mumps','download-scalapack','download-elemental']
  138.     for s in urls:
  139.       url,flag,found = self.PopUrl(s)
  140.       if found: petscopts.append(s)
  141.     if petscopts:
  142.       sys.exit('ERROR: The following options belong to PETSc configure: '+', '.join(petscopts)+'\nUse -h for help')
  143.  
  144.   def ErrorIfNotEmpty(self):
  145.     if self.argdb:
  146.       sys.exit('ERROR: Invalid arguments '+' '.join(self.argdb)+'\nUse -h for help')

Raw Paste


Login or Register to edit or fork this paste. It's free.