CMS 3D CMS Logo

utils.py
Go to the documentation of this file.
1 #-*- coding: utf-8 -*-
2 #pylint: disable-msg=W0122,R0914
3 
4 """
5 File : utils.py
6 Author : Valentin Kuznetsov <vkuznet@gmail.com>
7 Description: Utilities module
8 """
9 from __future__ import print_function
10 
11 # system modules
12 from builtins import range
13 import os
14 import re
15 import sys
16 import pwd
17 import pprint
18 import subprocess
19 
20 # template tag pattern
21 TAG = re.compile(r'[a-zA-Z0-9]')
22 
23 def parse_word(word):
24  "Parse word which contas double underscore tag"
25  output = set()
26  words = word.split()
27  for idx in range(0, len(words)):
28  pat = words[idx]
29  if pat and len(pat) > 4 and pat[:2] == '__': # we found enclosure
30  tag = pat[2:pat.rfind('__')]
31  if tag.find('__') != -1: # another pattern
32  for item in tag.split('__'):
33  if TAG.match(item):
34  output.add('__%s__' % item)
35  else:
36  output.add('__%s__' % tag)
37  return output
38 
39 def test_env(tdir, tmpl):
40  """
41  Test user environment, look-up if user has run cmsenv, otherwise
42  provide meaningful error message back to the user.
43  """
44  if not tdir or not os.path.isdir(tdir):
45  print("Unable to access template dir: %s" % tdir)
46  sys.exit(1)
47  if not os.listdir(tdir):
48  print("No template files found in template dir %s" % tdir)
49  sys.exit(0)
50  if not tmpl:
51  msg = "No template type is provided, "
52  msg += "see available templates via --templates option"
53  print(msg)
54  sys.exit(1)
55 
56 def functor(code, kwds, debug=0):
57  """
58  Auto-generate and execute function with given code and configuration
59  For details of compile/exec/eval see
60  http://lucumr.pocoo.org/2011/2/1/exec-in-python/
61  """
62  args = []
63  for key, val in kwds.items():
64  if isinstance(val, str):
65  arg = '%s="%s"' % (key, val)
66  elif isinstance(val, list):
67  arg = '%s=%s' % (key, val)
68  else:
69  msg = 'Unsupported data type "%s" <%s>' % (val, type(val))
70  raise Exception(msg)
71  args.append(arg)
72  func = '\nimport sys'
73  func += '\nimport StringIO'
74  func += "\ndef func(%s):\n" % ','.join(args)
75  func += code
76  func += """
77 def capture():
78  "Capture snippet printous"
79  old_stdout = sys.stdout
80  sys.stdout = StringIO.StringIO()
81  func()
82  out = sys.stdout.getvalue()
83  sys.stdout = old_stdout
84  return out\n
85 capture()\n"""
86  if debug:
87  print("\n### generated code\n")
88  print(func)
89  # compile python code as exec statement
90  obj = compile(func, '<string>', 'exec')
91  # define execution namespace
92  namespace = {}
93  # execute compiled python code in given namespace
94  exec(obj, namespace)
95  # located generated function object, run it and return its results
96  return namespace['capture']()
97 
98 def user_info(ainput=None):
99  "Return user name and office location, based on UNIX finger"
100  if ainput:
101  return ainput
102  pwdstr = pwd.getpwnam(os.getlogin())
103  author = pwdstr.pw_gecos
104  if author and isinstance(author, str):
105  author = author.split(',')[0]
106  return author
107 
108 def code_generator(kwds):
109  """
110  Code generator function, parse user arguments, load and
111  return appropriate template generator module.
112  """
113  debug = kwds.get('debug', None)
114  if debug:
115  print("Configuration:")
116  pprint.pprint(kwds)
117  try:
118  klass = kwds.get('tmpl')
119  mname = 'FWCore.Skeletons.%s' % klass.lower()
120  module = __import__(mname, fromlist=[klass])
121  except ImportError as err:
122  klass = 'AbstractPkg'
123  module = __import__('FWCore.Skeletons.pkg', fromlist=[klass])
124  if debug:
125  print("%s, will use %s" % (str(err), klass))
126  obj = getattr(module, klass)(kwds)
127  return obj
128 
129 def tree(idir):
130  "Print directory content, similar to tree UNIX command"
131  if idir[-1] == '/':
132  idir = idir[-1]
133  dsep = ''
134  fsep = ''
135  dtot = -1 # we'll not count initial directory
136  ftot = 0
137  for root, dirs, files in os.walk(idir):
138  dirs = root.split('/')
139  ndirs = len(dirs)
140  if ndirs > 1:
141  dsep = '| '*(ndirs-1)
142  print('%s%s/' % (dsep, dirs[-1]))
143  dtot += 1
144  for fname in files:
145  fsep = dsep + '|--'
146  print('%s %s' % (fsep, fname))
147  ftot += 1
148  if dtot == -1 or not dtot:
149  dmsg = ''
150  else:
151  dmsg = '%s directories,' % dtot
152  if ftot:
153  fmsg = '%s file' % ftot
154  if ftot > 1:
155  fmsg += 's'
156  else:
157  fmsg = ''
158  if dmsg and fmsg:
159  print("Total: %s %s" % (dmsg, fmsg))
160  else:
161  print("No directories/files in %s" % idir)
def user_info(ainput=None)
Definition: utils.py:98
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
def tree(idir)
Definition: utils.py:129
def test_env(tdir, tmpl)
Definition: utils.py:39
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def parse_word(word)
Definition: utils.py:23
def code_generator(kwds)
Definition: utils.py:108
def functor(code, kwds, debug=0)
Definition: utils.py:56
#define str(s)