PageRenderTime 38ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lcgcmt/pyLCG/python/lcg/aa/spi/processSh.py

https://github.com/mana-fwk/lcgwaf
Python | 135 lines | 127 code | 8 blank | 0 comment | 7 complexity | 3521577faf0c3fea1351ab793c8dd312 MD5 | raw file
  1. import optparse,sys,re,os,time,platform,socket
  2. import lcg.aa.config
  3. class processSh :
  4. def __init__(self, options):
  5. self.argv0 = sys.argv[0]
  6. self.varpat = re.compile('\${[A-Za-z_]+}')
  7. self.osenv = os.environ
  8. self.rem = '# ' + '*'*78 + '\n'
  9. self.incVars = options.varincludes
  10. self.excVars = options.varexcludes
  11. self.incInfo = options.infincludes
  12. self.excInfo = options.infexcludes
  13. self.pinfo = options.pinfo
  14. self.pvars = options.pvars
  15. self.pscrpt = options.pscrpt
  16. self.pexpnd = options.pexpnd
  17. self.pall = True
  18. if self.pinfo or self.pvars or self.pscrpt or self.pexpnd : self.pall = False
  19. self.infolst = {'Hostname': socket.gethostname(),
  20. 'IP' : socket.gethostbyname(socket.gethostname()),
  21. 'Time' : time.ctime(),
  22. 'Uname' : ' '.join(platform.uname()),
  23. 'PATH' : os.environ['PATH']
  24. }
  25. for i in self.incInfo :
  26. try:
  27. self.infolst[i] = os.environ[i]
  28. except KeyError,e :
  29. print '%s: WARNING: variable %s not found' % (self.argv0, i)
  30. self.varmax = 0
  31. self.filehdl = None
  32. self.filenam = ''
  33. self.filelns = []
  34. self.filelno = 0
  35. self.filevrs = {}
  36. def reset(self):
  37. self.varmax = 0
  38. self.filehdl = None
  39. self.filenam = ''
  40. self.filelns = []
  41. self.filelno = 0
  42. self.filevrs = {}
  43. map(self.fillVars, self.incVars)
  44. def fillVars(self, v):
  45. while v[0] in ('$',' ','{') : v = v[1:]
  46. while v[-1] in (' ', '}') : v = v[:-1]
  47. if not self.filevrs.has_key(v):
  48. if self.osenv.has_key(v): self.filevrs[v] = self.osenv[v]
  49. else: print '%s: WARNING: Environment variable %s found in script %s, line %s, not part of environment' % (self.argv0, v, self.filenam, self.filelno)
  50. def extractVars(self, line):
  51. self.filelno += 1
  52. exvars = self.varpat.findall(line)
  53. map(self.fillVars, exvars)
  54. def vmax(self, x):
  55. self.varmax = max(self.varmax,x)
  56. def printInfo(self):
  57. inf = self.rem
  58. kmax = 0
  59. infKeys = self.infolst.keys()
  60. map(infKeys.remove, self.excInfo)
  61. for k in infKeys : kmax = max(kmax, len(k))
  62. for k in infKeys : inf += '%s : %s\n' % (k.ljust(kmax), self.infolst[k])
  63. return inf[:-1]
  64. def printVars(self):
  65. vs = self.rem
  66. map(self.vmax, map(len, self.filevrs.keys()))
  67. for v in self.filevrs:
  68. vs += '%s: %s\n' % (v.ljust(self.varmax+1), self.filevrs[v])
  69. return vs[:-1]
  70. def printFile(self, expand=False):
  71. pf = self.rem
  72. for l in self.filelns:
  73. if expand:
  74. lvars = self.varpat.findall(l)
  75. for v in lvars :
  76. pv = v[2:-1]
  77. if pv not in self.excVars : l = l.replace(v,self.filevrs[pv])
  78. pf += l
  79. return pf[:-1]
  80. def processFile(self, filename):
  81. self.reset()
  82. self.filenam = filename
  83. try:
  84. self.filehdl = open(self.filenam,'r')
  85. except Exception, e:
  86. print '%s: ERROR: opening file %s: %s' % (self.argv0, self.filenam, e)
  87. map(self.filelns.append, self.filehdl.readlines())
  88. self.filehdl.close()
  89. map(self.extractVars, self.filelns)
  90. if self.pall or self.pinfo : print self.printInfo()
  91. if self.pall or self.pvars : print self.printVars()
  92. if self.pall or self.pscrpt : print self.printFile()
  93. if self.pall or self.pexpnd : print self.printFile(expand=True)
  94. if __name__ == '__main__':
  95. op = optparse.OptionParser()
  96. op.add_option('-V', '--include_variable', action='append', type='string', dest='varincludes', default=[])
  97. op.add_option('-v', '--exclude_variable', action='append', type='string', dest='varexcludes', default=[])
  98. op.add_option('-I', '--include_info', action='append', type='string', dest='infincludes', default=[])
  99. op.add_option('-i', '--exclude_info', action='append', type='string', dest='infexcludes', default=[])
  100. op.add_option('-a', '--print-info' , action='store_true', dest='pinfo', default='False')
  101. op.add_option('-b', '--print-variables' , action='store_true', dest='pvars', default='False')
  102. op.add_option('-c', '--print-script' , action='store_true', dest='pscrpt', default='False')
  103. op.add_option('-d', '--print-script-expanded', action='store_true', dest='pexpnd', default='False')
  104. (options,args) = op.parse_args()
  105. p = processSh(options)
  106. map( p.processFile, args )