1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 """A set of functions performing routine administrative I/O tasks."""
16
17 import contextlib
18 import itertools
19 import logging
20 import os
21 import re
22 import signal
23 import subprocess
24 import sys
25 import StringIO
26 import sys
27 import optparse
28 import time
29 import shutil
30 import traceback
31 import gzip as ziplib
32 from distutils.version import LooseVersion, StrictVersion
33
34 try:
35
36 import madgraph
37 except Exception, error:
38
39 import internal
40 from internal import MadGraph5Error, InvalidCmd
41 import internal.files as files
42 MADEVENT = True
43 else:
44 from madgraph import MadGraph5Error, InvalidCmd
45 import madgraph.iolibs.files as files
46 MADEVENT = False
47
48
49 logger = logging.getLogger('cmdprint.ext_program')
50 logger_stderr = logging.getLogger('madevent.misc')
51 pjoin = os.path.join
57 """Parse a newline separated list of "param=value" as a dictionnary
58 """
59
60 info_dict = {}
61 pattern = re.compile("(?P<name>\w*)\s*=\s*(?P<value>.*)",
62 re.IGNORECASE | re.VERBOSE)
63 for entry in fsock:
64 entry = entry.strip()
65 if len(entry) == 0: continue
66 m = pattern.match(entry)
67 if m is not None:
68 info_dict[m.group('name')] = m.group('value')
69 else:
70 raise IOError, "String %s is not a valid info string" % entry
71
72 return info_dict
73
74
75 -def glob(name, path=''):
76 """call to glob.glob with automatic security on path"""
77 import glob as glob_module
78 path = re.sub('(?P<name>\?|\*|\[|\])', '[\g<name>]', path)
79 return glob_module.glob(pjoin(path, name))
80
81
82
83
84 -def mute_logger(names=['madgraph','ALOHA','cmdprint','madevent'], levels=[50,50,50,50]):
85 """change the logger level and restore those at their initial value at the
86 end of the function decorated."""
87 def control_logger(f):
88 def restore_old_levels(names, levels):
89 for name, level in zip(names, levels):
90 log_module = logging.getLogger(name)
91 log_module.setLevel(level)
92
93 def f_with_no_logger(self, *args, **opt):
94 old_levels = []
95 for name, level in zip(names, levels):
96 log_module = logging.getLogger(name)
97 old_levels.append(log_module.level)
98 log_module.setLevel(level)
99 try:
100 out = f(self, *args, **opt)
101 restore_old_levels(names, old_levels)
102 return out
103 except:
104 restore_old_levels(names, old_levels)
105 raise
106
107 return f_with_no_logger
108 return control_logger
109
110 PACKAGE_INFO = {}
115 """Returns the current version information of the MadGraph5_aMC@NLO package,
116 as written in the VERSION text file. If the file cannot be found,
117 a dictionary with empty values is returned. As an option, an info
118 string can be passed to be read instead of the file content.
119 """
120 global PACKAGE_INFO
121
122 if info_str:
123 info_dict = parse_info_str(StringIO.StringIO(info_str))
124 return info_dict
125
126 if PACKAGE_INFO:
127 return PACKAGE_INFO
128
129 elif MADEVENT:
130 info_dict ={}
131 info_dict['version'] = open(pjoin(internal.__path__[0],'..','..','MGMEVersion.txt')).read().strip()
132 info_dict['date'] = '20xx-xx-xx'
133 PACKAGE_INFO = info_dict
134 else:
135 if PACKAGE_INFO:
136 return PACKAGE_INFO
137 info_dict = files.read_from_file(os.path.join(madgraph.__path__[0],
138 "VERSION"),
139 parse_info_str,
140 print_error=False)
141 PACKAGE_INFO = info_dict
142
143 return info_dict
144
149 """Returns the present time info for use in MG5 command history header.
150 """
151
152 creation_time = time.asctime()
153 time_info = {'time': creation_time,
154 'fill': ' ' * (26 - len(creation_time))}
155
156 return time_info
157
163 """ Returns None if compatible or, it not compatible, a string explaining
164 why it is so."""
165
166 ma5_version = None
167 try:
168 for line in open(pjoin(ma5path,'version.txt'),'r').read().split('\n'):
169 if line.startswith('MA5 version :'):
170 ma5_version=LooseVersion(line[13:].strip())
171 break
172 except:
173 ma5_version = None
174
175 if ma5_version is None:
176 reason = "No MadAnalysis5 version number could be read from the path supplied '%s'."%ma5path
177 reason += "\nThe specified version of MadAnalysis5 will not be active in your session."
178 return reason
179
180 mg5_version = None
181 try:
182 info = get_pkg_info()
183 mg5_version = LooseVersion(info['version'])
184 except:
185 mg5_version = None
186
187
188 if not mg5_version:
189 return None
190
191 if mg5_version < LooseVersion("2.6.1") and ma5_version >= LooseVersion("1.6.32"):
192 reason = "This active MG5aMC version is too old (v%s) for your selected version of MadAnalysis5 (v%s)"%(mg5_version,ma5_version)
193 reason += "\nUpgrade MG5aMC or re-install MA5 from within MG5aMC to fix this compatibility issue."
194 reason += "\nThe specified version of MadAnalysis5 will not be active in your session."
195 return reason
196
197 if mg5_version >= LooseVersion("2.6.1") and ma5_version < LooseVersion("1.6.32"):
198 reason = "Your selected version of MadAnalysis5 (v%s) is too old for this active version of MG5aMC (v%s)."%(ma5_version,mg5_version)
199 reason += "\nRe-install MA5 from within MG5aMC to fix this compatibility issue."
200 reason += "\nThe specified version of MadAnalysis5 will not be active in your session."
201 return reason
202
203 return None
204
209 """Browse the subdirectories of the path 'start_path' and returns the first
210 one found which contains at least one file ending with the string extension
211 given in argument."""
212
213 if not os.path.isdir(start_path):
214 return None
215 subdirs=[pjoin(start_path,dir) for dir in os.listdir(start_path)]
216 for subdir in subdirs:
217 if os.path.isfile(subdir):
218 if os.path.basename(subdir).endswith(extension):
219 return start_path
220 elif os.path.isdir(subdir):
221 path = find_includes_path(subdir, extension)
222 if path:
223 return path
224 return None
225
231 """ Get whether ninja supports quad prec in different ways"""
232
233
234 ninja_config = os.path.abspath(pjoin(
235 ninja_lib_path,os.pardir,'bin','ninja-config'))
236 if os.path.exists(ninja_config):
237 try:
238 p = Popen([ninja_config, '-quadsupport'], stdout=subprocess.PIPE,
239 stderr=subprocess.PIPE)
240 output, error = p.communicate()
241 return 'TRUE' in output.upper()
242 except Exception:
243 pass
244
245
246
247 return False
248
249
250
251
252 -def which(program):
253 def is_exe(fpath):
254 return os.path.exists(fpath) and os.access(\
255 os.path.realpath(fpath), os.X_OK)
256
257 if not program:
258 return None
259
260 fpath, fname = os.path.split(program)
261 if fpath:
262 if is_exe(program):
263 return program
264 else:
265 for path in os.environ["PATH"].split(os.pathsep):
266 exe_file = os.path.join(path, program)
267 if is_exe(exe_file):
268 return exe_file
269 return None
270
286
292 """ Make sure to turn off some dependency of MG5aMC. """
293
294 def tell(msg):
295 if log == 'stdout':
296 print msg
297 elif callable(log):
298 log(msg)
299
300
301 if dependency in ['pjfry','golem','samurai','ninja','collier']:
302 if cmd.options[dependency] not in ['None',None,'']:
303 tell("Deactivating MG5_aMC dependency '%s'"%dependency)
304 cmd.options[dependency] = None
305
307 """ Checks whether the specfieid MG dependency can be activated if it was
308 not turned off in MG5 options."""
309
310 def tell(msg):
311 if log == 'stdout':
312 print msg
313 elif callable(log):
314 log(msg)
315
316 if cmd is None:
317 cmd = MGCmd.MasterCmd()
318
319 if dependency=='pjfry':
320 if cmd.options['pjfry'] in ['None',None,''] or \
321 (cmd.options['pjfry'] == 'auto' and which_lib('libpjfry.a') is None) or\
322 which_lib(pjoin(cmd.options['pjfry'],'libpjfry.a')) is None:
323 tell("Installing PJFry...")
324 cmd.do_install('PJFry')
325
326 if dependency=='golem':
327 if cmd.options['golem'] in ['None',None,''] or\
328 (cmd.options['golem'] == 'auto' and which_lib('libgolem.a') is None) or\
329 which_lib(pjoin(cmd.options['golem'],'libgolem.a')) is None:
330 tell("Installing Golem95...")
331 cmd.do_install('Golem95')
332
333 if dependency=='samurai':
334 raise MadGraph5Error, 'Samurai cannot yet be automatically installed.'
335
336 if dependency=='ninja':
337 if cmd.options['ninja'] in ['None',None,''] or\
338 (cmd.options['ninja'] == './HEPTools/lib' and not MG5dir is None and\
339 which_lib(pjoin(MG5dir,cmd.options['ninja'],'libninja.a')) is None):
340 tell("Installing ninja...")
341 cmd.do_install('ninja')
342
343 if dependency=='collier':
344 if cmd.options['collier'] in ['None',None,''] or\
345 (cmd.options['collier'] == 'auto' and which_lib('libcollier.a') is None) or\
346 which_lib(pjoin(cmd.options['collier'],'libcollier.a')) is None:
347 tell("Installing COLLIER...")
348 cmd.do_install('collier')
349
354 def is_lib(fpath):
355 return os.path.exists(fpath) and os.access(fpath, os.R_OK)
356
357 if not lib:
358 return None
359
360 fpath, fname = os.path.split(lib)
361 if fpath:
362 if is_lib(lib):
363 return lib
364 else:
365 locations = sum([os.environ[env_path].split(os.pathsep) for env_path in
366 ["DYLD_LIBRARY_PATH","LD_LIBRARY_PATH","LIBRARY_PATH","PATH"]
367 if env_path in os.environ],[])
368 for path in locations:
369 lib_file = os.path.join(path, lib)
370 if is_lib(lib_file):
371 return lib_file
372 return None
373
378 """ Return nice information on the current variable """
379
380
381 info = [('type',type(var)),('str', var)]
382 if hasattr(var, 'func_doc'):
383 info.append( ('DOC', var.func_doc) )
384 if hasattr(var, '__doc__'):
385 info.append( ('DOC', var.__doc__) )
386 if hasattr(var, '__dict__'):
387 info.append( ('ATTRIBUTE', var.__dict__.keys() ))
388
389 spaces = ' ' * nb_space
390
391 outstr=''
392 for name, value in info:
393 outstr += '%s%3s : %s\n' % (spaces,name, value)
394
395 return outstr
396
397
398
399
400 wait_once = False
402
403 def deco_retry(f):
404 def deco_f_retry(*args, **opt):
405 for i in range(nb_try):
406 try:
407 return f(*args, **opt)
408 except KeyboardInterrupt:
409 raise
410 except Exception, error:
411 global wait_once
412 if not wait_once:
413 text = """Start waiting for update. (more info in debug mode)"""
414 logger.info(text)
415 logger_stderr.debug('fail to do %s function with %s args. %s try on a max of %s (%s waiting time)' %
416 (str(f), ', '.join([str(a) for a in args]), i+1, nb_try, sleep * (i+1)))
417 logger_stderr.debug('error is %s' % str(error))
418 if __debug__: logger_stderr.debug('and occurred at :'+traceback.format_exc())
419 wait_once = True
420 time.sleep(sleep * (i+1))
421
422 if __debug__:
423 raise
424 raise error.__class__, '[Fail %i times] \n %s ' % (i+1, error)
425 return deco_f_retry
426 return deco_retry
427
432 """return a name of the type xxxx[A-B]yyy
433 where xxx and yyy are the common part between the two names.
434 """
435
436
437 base = [first[i] for i in range(len(first)) if first[:i+1] == last[:i+1]]
438
439 while base and base[0].isdigit():
440 base = base[1:]
441
442 end = [first[-(i+1)] for i in range(len(first)) if first[-(i+1):] == last[-(i+1):]]
443
444 while end and end[-1].isdigit():
445 end = end[:-1]
446 end.reverse()
447
448 base, end = ''.join(base), ''.join(end)
449 if end:
450 name = "%s[%s-%s]%s" % (base, first[len(base):-len(end)], last[len(base):-len(end)],end)
451 else:
452 name = "%s[%s-%s]%s" % (base, first[len(base):], last[len(base):],end)
453 return name
454
455
456
457
458 -def compile(arg=[], cwd=None, mode='fortran', job_specs = True, nb_core=1 ,**opt):
459 """compile a given directory"""
460
461 if 'nocompile' in opt:
462 if opt['nocompile'] == True:
463 if not arg:
464 return
465 if cwd:
466 executable = pjoin(cwd, arg[0])
467 else:
468 executable = arg[0]
469 if os.path.exists(executable):
470 return
471 del opt['nocompile']
472
473 cmd = ['make']
474 try:
475 if nb_core > 1:
476 cmd.append('-j%s' % nb_core)
477 cmd += arg
478 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
479 stderr=subprocess.STDOUT, cwd=cwd, **opt)
480 (out, err) = p.communicate()
481 except OSError, error:
482 if cwd and not os.path.exists(cwd):
483 raise OSError, 'Directory %s doesn\'t exists. Impossible to run make' % cwd
484 else:
485 error_text = "Impossible to compile %s directory\n" % cwd
486 error_text += "Trying to launch make command returns:\n"
487 error_text += " " + str(error) + "\n"
488 error_text += "In general this means that your computer is not able to compile."
489 if sys.platform == "darwin":
490 error_text += "Note that MacOSX doesn\'t have gmake/gfortan install by default.\n"
491 error_text += "Xcode3 contains those required programs"
492 raise MadGraph5Error, error_text
493
494 if p.returncode:
495
496 if not cwd:
497 cwd = os.getcwd()
498 all_file = [f.lower() for f in os.listdir(cwd)]
499 if 'makefile' not in all_file and '-f' not in arg:
500 raise OSError, 'no makefile present in %s' % os.path.realpath(cwd)
501
502 if mode == 'fortran' and not (which('g77') or which('gfortran')):
503 error_msg = 'A fortran compiler (g77 or gfortran) is required to create this output.\n'
504 error_msg += 'Please install g77 or gfortran on your computer and retry.'
505 raise MadGraph5Error, error_msg
506 elif mode == 'cpp' and not which('g++'):
507 error_msg ='A C++ compiler (g++) is required to create this output.\n'
508 error_msg += 'Please install g++ (which is part of the gcc package) on your computer and retry.'
509 raise MadGraph5Error, error_msg
510
511
512 if any(tag.upper() in out.upper() for tag in ['real(kind=16)','real*16',
513 'complex*32']) and mode == 'fortran' and not \
514 ''.join(get_gfortran_version().split('.')) >= '46':
515 if not which('gfortran'):
516 raise MadGraph5Error, 'The fortran compiler gfortran v4.6 or later '+\
517 'is required to compile %s.\nPlease install it and retry.'%cwd
518 else:
519 logger_stderr.error('ERROR, you could not compile %s because'%cwd+\
520 ' your version of gfortran is older than 4.6. MadGraph5_aMC@NLO will carry on,'+\
521 ' but will not be able to compile an executable.')
522 return p.returncode
523
524 error_text = 'A compilation Error occurs '
525 if cwd:
526 error_text += 'when trying to compile %s.\n' % cwd
527 error_text += 'The compilation fails with the following output message:\n'
528 error_text += ' '+out.replace('\n','\n ')+'\n'
529 error_text += 'Please try to fix this compilations issue and retry.\n'
530 error_text += 'Help might be found at https://answers.launchpad.net/mg5amcnlo.\n'
531 error_text += 'If you think that this is a bug, you can report this at https://bugs.launchpad.net/mg5amcnlo'
532 raise MadGraph5Error, error_text
533 return p.returncode
534
536 """ Returns the gfortran version as a string.
537 Returns '0' if it failed."""
538 try:
539 p = Popen([compiler, '-dumpversion'], stdout=subprocess.PIPE,
540 stderr=subprocess.PIPE)
541 output, error = p.communicate()
542 version_finder=re.compile(r"(?P<version>(\d.)*\d)")
543 version = version_finder.search(output).group('version')
544 return version
545 except Exception:
546 return '0'
547
548 -def mod_compilator(directory, new='gfortran', current=None, compiler_type='gfortran'):
549
550 if type(directory)!=list:
551 directory=[directory]
552
553
554 file_to_change=find_makefile_in_dir(directory)
555 if compiler_type == 'gfortran':
556 comp_re = re.compile('^(\s*)FC\s*=\s*(.+)\s*$')
557 var = 'FC'
558 elif compiler_type == 'cpp':
559 comp_re = re.compile('^(\s*)CXX\s*=\s*(.+)\s*$')
560 var = 'CXX'
561 else:
562 MadGraph5Error, 'Unknown compiler type: %s' % compiler_type
563
564 mod = False
565 for name in file_to_change:
566 lines = open(name,'r').read().split('\n')
567 for iline, line in enumerate(lines):
568 result = comp_re.match(line)
569 if result:
570 if new != result.group(2) and '$' not in result.group(2):
571 mod = True
572 lines[iline] = result.group(1) + var + "=" + new
573 elif compiler_type == 'gfortran' and line.startswith('DEFAULT_F_COMPILER'):
574 lines[iline] = "DEFAULT_F_COMPILER = %s" % new
575 elif compiler_type == 'cpp' and line.startswith('DEFAULT_CPP_COMPILER'):
576 lines[iline] = "DEFAULT_CPP_COMPILER = %s" % new
577
578 if mod:
579 open(name,'w').write('\n'.join(lines))
580
581 mod = False
582
584 """Check whether pid exists in the current process table.
585 UNIX only.
586 https://stackoverflow.com/questions/568271/how-to-check-if-there-exists-a-process-with-a-given-pid-in-python
587 """
588 import errno
589
590 if pid < 0:
591 return False
592 if pid == 0:
593
594
595
596
597 raise ValueError('invalid PID 0')
598 try:
599 os.kill(pid, 0)
600 except OSError as err:
601 if err.errno == errno.ESRCH:
602
603 return False
604 elif err.errno == errno.EPERM:
605
606 return True
607 else:
608
609
610 raise
611 else:
612 return True
613
618 """mute_logger (designed to work as with statement),
619 files allow to redirect the output of the log to a given file.
620 """
621
622 - def __init__(self, names, levels, files=None, **opt):
623 assert isinstance(names, list)
624 assert isinstance(names, list)
625
626 self.names = names
627 self.levels = levels
628 if isinstance(files, list):
629 self.files = files
630 else:
631 self.files = [files] * len(names)
632 self.logger_saved_info = {}
633 self.opts = opt
634
645
646 - def __exit__(self, ctype, value, traceback ):
657
659 """ Setup the logger by redirecting them all to logfiles in tmp """
660
661 logs = full_logname.split('.')
662 lognames = [ '.'.join(logs[:(len(logs)-i)]) for i in\
663 range(len(full_logname.split('.')))]
664 for logname in lognames:
665 try:
666 os.remove(path)
667 except Exception, error:
668 pass
669 my_logger = logging.getLogger(logname)
670 hdlr = logging.FileHandler(path)
671
672
673 self.logger_saved_info[logname] = [hdlr, my_logger.handlers]
674
675
676 for old_hdlr in list(my_logger.handlers):
677 my_logger.removeHandler(old_hdlr)
678 my_logger.addHandler(hdlr)
679
680 my_logger.debug('Log of %s' % logname)
681
683 """ Setup the logger by redirecting them all to logfiles in tmp """
684
685 logs = full_logname.split('.')
686 lognames = [ '.'.join(logs[:(len(logs)-i)]) for i in\
687 range(len(full_logname.split('.')))]
688 for logname in lognames:
689 if path:
690 try:
691 os.remove(path)
692 except Exception, error:
693 pass
694 my_logger = logging.getLogger(logname)
695 if logname in self.logger_saved_info:
696 my_logger.removeHandler(self.logger_saved_info[logname][0])
697 for old_hdlr in self.logger_saved_info[logname][1]:
698 my_logger.addHandler(old_hdlr)
699 else:
700 my_logger.setLevel(level)
701
702
703
704
705 nb_open =0
708 """
709 A context manager to temporarily redirect stdout or stderr
710
711 e.g.:
712
713
714 with stdchannel_redirected(sys.stderr, os.devnull):
715 if compiler.has_function('clock_gettime', libraries=['rt']):
716 libraries.append('rt')
717 """
718
719 try:
720 oldstdchannel = os.dup(stdchannel.fileno())
721 dest_file = open(dest_filename, 'w')
722 os.dup2(dest_file.fileno(), stdchannel.fileno())
723 yield
724 finally:
725 if oldstdchannel is not None:
726 os.dup2(oldstdchannel, stdchannel.fileno())
727 os.close(oldstdchannel)
728 if dest_file is not None:
729 dest_file.close()
730
732 '''
733 return the number of open file descriptors for current process
734
735 .. warning: will only work on UNIX-like os-es.
736 '''
737 import subprocess
738 import os
739
740 pid = os.getpid()
741 procs = subprocess.check_output(
742 [ "lsof", '-w', '-Ff', "-p", str( pid ) ] )
743 nprocs = filter(
744 lambda s: s and s[ 0 ] == 'f' and s[1: ].isdigit(),
745 procs.split( '\n' ) )
746
747 return nprocs
748
750 """ Detects whether the specified C++ compiler is clang."""
751
752 try:
753 p = Popen([cpp_compiler, '--version'], stdout=subprocess.PIPE,
754 stderr=subprocess.PIPE)
755 output, error = p.communicate()
756 except Exception, error:
757
758 return False
759 return 'LLVM' in output
760
762 """ Detects if the specified c++ compiler will normally link against the C++
763 standard library -lc++ or -libstdc++."""
764
765 is_clang = detect_if_cpp_compiler_is_clang(cpp_compiler)
766 if is_clang:
767 try:
768 import platform
769 v, _,_ = platform.mac_ver()
770 if not v:
771
772
773 return '-lc++'
774 else:
775 v = float(v.rsplit('.')[1])
776 if v >= 9:
777 return '-lc++'
778 else:
779 return '-lstdc++'
780 except:
781 return '-lstdc++'
782 return '-lstdc++'
783
785 """find the current compiler for the current directory"""
786
787
788
789 if compiler_type == 'fortran':
790 comp = re.compile("^\s*FC\s*=\s*([\w\/\\.\-]+)\s*")
791 elif compiler_type == 'cpp':
792 comp = re.compile("^\s*CXX\s*=\s*([\w\/\\.\-]+)\s*")
793 else:
794 MadGraph5Error, 'Unknown compiler type: %s' % compiler_type
795
796 for line in open(path):
797 if comp.search(line):
798 compiler = comp.search(line).groups()[0]
799 return compiler
800 elif compiler_type == 'fortran' and line.startswith('DEFAULT_F_COMPILER'):
801 return line.split('=')[1].strip()
802 elif compiler_type == 'cpp' and line.startswith('DEFAULT_CPP_COMPILER'):
803 return line.split('=')[1].strip()
804
806 """ return a list of all file starting with makefile in the given directory"""
807
808 out=[]
809
810 if type(directory)==list:
811 for name in directory:
812 out+=find_makefile_in_dir(name)
813 return out
814
815
816 for name in os.listdir(directory):
817 if os.path.isdir(directory+'/'+name):
818 out+=find_makefile_in_dir(directory+'/'+name)
819 elif os.path.isfile(directory+'/'+name) and name.lower().startswith('makefile'):
820 out.append(directory+'/'+name)
821 elif os.path.isfile(directory+'/'+name) and name.lower().startswith('make_opt'):
822 out.append(directory+'/'+name)
823 return out
824
826
827
828 os.path.walk('.', rm_file_extension, '.o')
829
830
831 libraries = ['libblocks.a', 'libgeneric_mw.a', 'libMWPS.a', 'libtools.a', 'libdhelas3.a',
832 'libdsample.a', 'libgeneric.a', 'libmodel.a', 'libpdf.a', 'libdhelas3.so', 'libTF.a',
833 'libdsample.so', 'libgeneric.so', 'libmodel.so', 'libpdf.so']
834 lib_pos='./lib'
835 [os.remove(os.path.join(lib_pos, lib)) for lib in libraries \
836 if os.path.exists(os.path.join(lib_pos, lib))]
837
851
855
859 replace_dict = dict(key_values)
860 replacement_function = lambda match: replace_dict[match.group(0)]
861 pattern = re.compile("|".join([re.escape(k) for k, v in key_values]), re.M)
862 return lambda string: pattern.sub(replacement_function, string)
863
866
869 def deco_check(f):
870 def deco_f(arg, *args, **opt):
871 try:
872 return f(arg, *args, **opt)
873 except OSError, error:
874 logger.debug('try to recover from %s' % error)
875 if isinstance(arg, (list,tuple)):
876 prog = arg[0]
877 else:
878 prog = arg
879
880
881 if error.errno == 13:
882 if os.path.exists(prog):
883 os.system('chmod +x %s' % prog)
884 elif 'cwd' in opt and opt['cwd'] and \
885 os.path.isfile(pjoin(opt['cwd'],arg[0])):
886 os.system('chmod +x %s' % pjoin(opt['cwd'],arg[0]))
887 return f(arg, *args, **opt)
888
889 elif error.errno == 2:
890
891 raise Exception, '%s fails with no such file or directory' \
892 % arg
893 else:
894 raise
895 return deco_f
896 return deco_check
897
898
899 @check_system_error()
900 -def call(arg, *args, **opt):
901 """nice way to call an external program with nice error treatment"""
902 try:
903 return subprocess.call(arg, *args, **opt)
904 except OSError:
905 arg[0] = './%s' % arg[0]
906 return subprocess.call(arg, *args, **opt)
907
908 @check_system_error()
909 -def Popen(arg, *args, **opt):
910 """nice way to call an external program with nice error treatment"""
911 return subprocess.Popen(arg, *args, **opt)
912
913 @check_system_error()
914 -def call_stdout(arg, *args, **opt):
915 """nice way to call an external program with nice error treatment"""
916 try:
917 out = subprocess.Popen(arg, *args, stdout=subprocess.PIPE, **opt)
918 except OSError:
919 arg[0] = './%s' % arg[0]
920 out = subprocess.call(arg, *args, stdout=subprocess.PIPE, **opt)
921
922 str_out = out.stdout.read().strip()
923 return str_out
924
925
926 @multiple_try()
927 -def mult_try_open(filepath, *args, **opt):
928 """try to open a file with multiple try to ensure that filesystem is sync"""
929 return open(filepath, *args, ** opt)
930
931
932
933
934 -def tail(f, n, offset=None):
935 """Reads a n lines from f with an offset of offset lines. The return
936 value is a tuple in the form ``lines``.
937 """
938 avg_line_length = 74
939 to_read = n + (offset or 0)
940
941 while 1:
942 try:
943 f.seek(-(avg_line_length * to_read), 2)
944 except IOError:
945
946
947 f.seek(0)
948 pos = f.tell()
949 lines = f.read().splitlines()
950 if len(lines) >= to_read or pos == 0:
951 return lines[-to_read:offset and -offset or None]
952 avg_line_length *= 1.3
953 avg_line_length = int(avg_line_length)
954
956 """ makes a piping fifo (First-in First-out) file and nicely intercepts
957 error in case the file format of the target drive doesn't suppor tit."""
958
959 try:
960 os.mkfifo(fifo_path)
961 except:
962 raise OSError('MadGraph5_aMCatNLO could not create a fifo file at:\n'+
963 ' %s\n'%fifo_path+'Make sure that this file does not exist already'+
964 ' and that the file format of the target drive supports fifo file (i.e not NFS).')
965
970 """return the last line of a file"""
971
972 return tail(fsock, 1)[0]
973
975 """read a file returning the lines in reverse order for each call of readline()
976 This actually just reads blocks (4096 bytes by default) of data from the end of
977 the file and returns last line in an internal buffer."""
978
979
981 """ readline in a backward way """
982
983 while len(self.data) == 1 and ((self.blkcount * self.blksize) < self.size):
984 self.blkcount = self.blkcount + 1
985 line = self.data[0]
986 try:
987 self.seek(-self.blksize * self.blkcount, 2)
988 self.data = (self.read(self.blksize) + line).split('\n')
989 except IOError:
990 self.seek(0)
991 data = self.read(self.size - (self.blksize * (self.blkcount-1))) + line
992 self.data = data.split('\n')
993
994 if len(self.data) == 0:
995 return ""
996
997 line = self.data.pop()
998 return line + '\n'
999
1000 - def __init__(self, filepos, blksize=4096):
1001 """initialize the internal structures"""
1002
1003
1004 self.size = os.stat(filepos)[6]
1005
1006 self.blksize = blksize
1007
1008 self.blkcount = 1
1009 file.__init__(self, filepos, 'rb')
1010
1011
1012 if self.size > self.blksize:
1013 self.seek(-self.blksize * self.blkcount, 2)
1014 self.data = self.read(self.blksize).split('\n')
1015
1016
1017 if not self.data[-1]:
1018 self.data.pop()
1019
1021 line = self.readline()
1022 if line:
1023 return line
1024 else:
1025 raise StopIteration
1026
1042
1056
1062 """create a temporary directory and ensure this one to be cleaned.
1063 """
1064
1065 - def __init__(self, suffix='', prefix='tmp', dir=None):
1066 self.nb_try_remove = 0
1067 import tempfile
1068 self.path = tempfile.mkdtemp(suffix, prefix, dir)
1069
1070
1071 - def __exit__(self, ctype, value, traceback ):
1072
1073 if False and isinstance(value, Exception):
1074 sprint("Directory %s not cleaned. This directory can be removed manually" % self.path)
1075 return False
1076 try:
1077 shutil.rmtree(self.path)
1078 except OSError:
1079 self.nb_try_remove += 1
1080 if self.nb_try_remove < 3:
1081 time.sleep(10)
1082 self.__exit__(ctype, value, traceback)
1083 else:
1084 logger.warning("Directory %s not completely cleaned. This directory can be removed manually" % self.path)
1085
1088
1090 """replace an attribute of a class with another value for the time of the
1091 context manager
1092 """
1093
1094 - def __init__(self, cls, attribute, value):
1095
1096 self.cls = cls
1097 self.attribute = attribute
1098 if isinstance(attribute, list):
1099 self.old_value = []
1100 for key, onevalue in zip(attribute, value):
1101 self.old_value.append(getattr(cls, key))
1102 setattr(self.cls, key, onevalue)
1103 else:
1104 self.old_value = getattr(cls, attribute)
1105 setattr(self.cls, self.attribute, value)
1106
1107 - def __exit__(self, ctype, value, traceback ):
1108
1109 if isinstance(self.attribute, list):
1110 for key, old_value in zip(self.attribute, self.old_value):
1111 setattr(self.cls, key, old_value)
1112 else:
1113 setattr(self.cls, self.attribute, self.old_value)
1114
1116 return self.old_value
1117
1118
1119
1120
1121 -def gunzip(path, keep=False, stdout=None):
1122 """ a standard replacement for os.system('gunzip -f %s.gz ' % event_path)"""
1123
1124 if not path.endswith(".gz"):
1125 if os.path.exists("%s.gz" % path):
1126 path = "%s.gz" % path
1127 else:
1128 raise Exception, "%(path)s does not finish by .gz and the file %(path)s.gz does not exists" %\
1129 {"path": path}
1130
1131
1132
1133 if os.path.getsize(path) > 1e8:
1134 if stdout:
1135 os.system('gunzip -c %s > %s' % (path, stdout))
1136 else:
1137 os.system('gunzip %s' % path)
1138 return 0
1139
1140 if not stdout:
1141 stdout = path[:-3]
1142 try:
1143 gfile = ziplib.open(path, "r")
1144 except IOError:
1145 raise
1146 else:
1147 try:
1148 open(stdout,'w').write(gfile.read())
1149 except IOError:
1150
1151 if stdout == path:
1152 return
1153 else:
1154 files.cp(path, stdout)
1155
1156 if not keep:
1157 os.remove(path)
1158 return 0
1159
1160 -def gzip(path, stdout=None, error=True, forceexternal=False):
1161 """ a standard replacement for os.system('gzip %s ' % path)"""
1162
1163
1164 if os.path.getsize(path) > 1e9 or forceexternal:
1165 call(['gzip', '-f', path])
1166 if stdout:
1167 if not stdout.endswith(".gz"):
1168 stdout = "%s.gz" % stdout
1169 shutil.move('%s.gz' % path, stdout)
1170 return
1171
1172 if not stdout:
1173 stdout = "%s.gz" % path
1174 elif not stdout.endswith(".gz"):
1175 stdout = "%s.gz" % stdout
1176
1177 try:
1178 ziplib.open(stdout,"w").write(open(path).read())
1179 except OverflowError:
1180 gzip(path, stdout, error=error, forceexternal=True)
1181 except Exception:
1182 if error:
1183 raise
1184 else:
1185 os.remove(path)
1186
1191 """ a convinient class to open a file """
1192
1193 web_browser = None
1194 eps_viewer = None
1195 text_editor = None
1196 configured = False
1197
1199 """open a file"""
1200
1201
1202 if not self.configured:
1203 self.configure()
1204
1205 try:
1206 extension = filename.rsplit('.',1)[1]
1207 except IndexError:
1208 extension = ''
1209
1210
1211
1212 if extension in ['html','htm','php']:
1213 self.open_program(self.web_browser, filename, background=True)
1214 elif extension in ['ps','eps']:
1215 self.open_program(self.eps_viewer, filename, background=True)
1216 else:
1217 self.open_program(self.text_editor,filename, mac_check=False)
1218
1219
1220 @classmethod
1243
1244 @classmethod
1282
1283
1284 @staticmethod
1286 """find a valid shell program in the list"""
1287
1288 for p in possibility:
1289 if which(p):
1290 logger.info('Using default %s \"%s\". ' % (program, p) + \
1291 'Set another one in ./input/mg5_configuration.txt')
1292 return p
1293
1294 logger.info('No valid %s found. ' % program + \
1295 'Please set in ./input/mg5_configuration.txt')
1296 return None
1297
1298
1299 - def open_program(self, program, file_path, mac_check=True, background=False):
1300 """ open a file with a given program """
1301
1302 if mac_check==True and sys.platform == 'darwin':
1303 return self.open_mac_program(program, file_path)
1304
1305
1306 if program:
1307 arguments = program.split()
1308 arguments.append(file_path)
1309
1310 if not background:
1311 subprocess.call(arguments)
1312 else:
1313 import thread
1314 thread.start_new_thread(subprocess.call,(arguments,))
1315 else:
1316 logger.warning('Not able to open file %s since no program configured.' % file_path + \
1317 'Please set one in ./input/mg5_configuration.txt')
1318
1320 """ open a text with the text editor """
1321
1322 if not program:
1323
1324 os.system('open %s' % file_path)
1325 elif which(program):
1326
1327 arguments = program.split()
1328 arguments.append(file_path)
1329 subprocess.call(arguments)
1330 else:
1331
1332 os.system('open -a %s %s' % (program, file_path))
1333
1353
1355 """ Try and guess what shell type does the user use."""
1356 try:
1357 if os.environ['SHELL'].endswith('bash'):
1358 return 'bash'
1359 elif os.environ['SHELL'].endswith('tcsh'):
1360 return 'tcsh'
1361 else:
1362
1363 return None
1364 except KeyError:
1365 return None
1366
1368 """ check if a path is executable"""
1369 try:
1370 return os.access(path, os.X_OK)
1371 except Exception:
1372 return False
1373
1375 """Option Peaser which raise an error instead as calling exit"""
1376
1377 - def exit(self, status=0, msg=None):
1382
1384 """Returns the current line number in our program."""
1385
1386 if not __debug__:
1387 return
1388
1389
1390 import inspect
1391 if opt.has_key('cond') and not opt['cond']:
1392 return
1393
1394 use_print = False
1395 if opt.has_key('use_print') and opt['use_print']:
1396 use_print = True
1397
1398 if opt.has_key('log'):
1399 log = opt['log']
1400 else:
1401 log = logging.getLogger('madgraph')
1402 if opt.has_key('level'):
1403 level = opt['level']
1404 else:
1405 level = logging.getLogger('madgraph').level
1406 if level == 0:
1407 use_print = True
1408
1409
1410
1411
1412 if opt.has_key('wait'):
1413 wait = bool(opt['wait'])
1414 else:
1415 wait = False
1416
1417 lineno = inspect.currentframe().f_back.f_lineno
1418 fargs = inspect.getframeinfo(inspect.currentframe().f_back)
1419 filename, lineno = fargs[:2]
1420
1421
1422 try:
1423 source = inspect.getsourcelines(inspect.currentframe().f_back)
1424 line = source[0][lineno-source[1]]
1425 line = re.findall(r"misc\.sprint\(\s*(.*)\)\s*($|#)", line)[0][0]
1426 if line.startswith("'") and line.endswith("'") and line.count(",") ==0:
1427 line= ''
1428 elif line.startswith("\"") and line.endswith("\"") and line.count(",") ==0:
1429 line= ''
1430 elif line.startswith(("\"","'")) and len(args)==1 and "%" in line:
1431 line= ''
1432 except Exception:
1433 line=''
1434
1435 if line:
1436 intro = ' %s = \033[0m' % line
1437 else:
1438 intro = ''
1439
1440
1441 if not use_print:
1442 log.log(level, ' '.join([intro]+[str(a) for a in args]) + \
1443 ' \033[1;30m[%s at line %s]\033[0m' % (os.path.basename(filename), lineno))
1444 else:
1445 print ' '.join([intro]+[str(a) for a in args]) + \
1446 ' \033[1;30m[%s at line %s]\033[0m' % (os.path.basename(filename), lineno)
1447
1448 if wait:
1449 raw_input('press_enter to continue')
1450 elif opt.has_key('sleep'):
1451 time.sleep(int(opt['sleep']))
1452
1453 return
1454
1455
1456
1457
1458 -def equal(a,b,sig_fig=6, zero_limit=True):
1459 """function to check if two float are approximatively equal"""
1460 import math
1461
1462 if isinstance(sig_fig, int):
1463 if not a or not b:
1464 if zero_limit:
1465 if zero_limit is not True:
1466 power = zero_limit
1467 else:
1468 power = sig_fig + 1
1469 else:
1470 return a == b
1471 else:
1472 power = sig_fig - int(math.log10(abs(a)))
1473
1474 return ( a==b or abs(int(a*10**power) - int(b*10**power)) < 10)
1475 else:
1476 return abs(a-b) < sig_fig
1477
1478
1479
1480
1481
1482
1483
1484 -class chdir:
1486 self.newPath = newPath
1487
1489 self.savedPath = os.getcwd()
1490 os.chdir(self.newPath)
1491
1492 - def __exit__(self, etype, value, traceback):
1493 os.chdir(self.savedPath)
1494
1495
1496
1497
1498
1499 -def timeout(func, args=(), kwargs={}, timeout_duration=1, default=None):
1500 '''This function will spwan a thread and run the given function using the args, kwargs and
1501 return the given default value if the timeout_duration is exceeded
1502 '''
1503 import threading
1504 class InterruptableThread(threading.Thread):
1505 def __init__(self):
1506 threading.Thread.__init__(self)
1507 self.result = default
1508 def run(self):
1509 try:
1510 self.result = func(*args, **kwargs)
1511 except Exception,error:
1512 print error
1513 self.result = default
1514 it = InterruptableThread()
1515 it.start()
1516 it.join(timeout_duration)
1517 return it.result
1518
1519
1520
1521
1522
1523 -class digest:
1524
1526 try:
1527 return self.test_hashlib()
1528 except Exception:
1529 pass
1530 try:
1531 return self.test_md5()
1532 except Exception:
1533 pass
1534 try:
1535 return self.test_zlib()
1536 except Exception:
1537 pass
1538
1540 import hashlib
1541 def digest(text):
1542 """using mg5 for the hash"""
1543 t = hashlib.md5()
1544 t.update(text)
1545 return t.hexdigest()
1546 return digest
1547
1549 import md5
1550 def digest(text):
1551 """using mg5 for the hash"""
1552 t = md5.md5()
1553 t.update(text)
1554 return t.hexdigest()
1555 return digest
1556
1558 import zlib
1559 def digest(text):
1560 return zlib.adler32(text)
1561
1562 digest = digest().test_all()
1569 self.cmd_args = args
1570 self.cmd_opts = opts
1571 self.execution_state = False
1572
1574 self.max_vms_memory = 0
1575 self.max_rss_memory = 0
1576
1577 self.t1 = None
1578 self.t0 = time.time()
1579 self.p = subprocess.Popen(*self.cmd_args,**self.cmd_opts)
1580 self.execution_state = True
1581
1583 if not self.check_execution_state():
1584 return False
1585
1586 self.t1 = time.time()
1587
1588
1589
1590 flash = subprocess.Popen("ps -p %i -o rss"%self.p.pid,
1591 shell=True,stdout=subprocess.PIPE,stderr=open(os.devnull,"w"))
1592 stdout_list = flash.communicate()[0].split('\n')
1593 rss_memory = int(stdout_list[1])
1594
1595 vms_memory = 0
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624 self.max_vms_memory = max(self.max_vms_memory,vms_memory)
1625 self.max_rss_memory = max(self.max_rss_memory,rss_memory)
1626
1627 return self.check_execution_state()
1628
1630
1631
1632 return self.p.poll() == None
1633
1635 if not self.execution_state:
1636 return False
1637 if self.is_running():
1638 return True
1639 self.executation_state = False
1640 self.t1 = time.time()
1641 return False
1642
1643 - def close(self,kill=False):
1644
1645 if self.p.poll() == None:
1646 if kill:
1647 self.p.kill()
1648 else:
1649 self.p.terminate()
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662 -class Applenotification(object):
1663
1665 self.init = False
1666 self.working = True
1667
1669 try:
1670 import Foundation
1671 import objc
1672 self.NSUserNotification = objc.lookUpClass('NSUserNotification')
1673 self.NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
1674 except:
1675 self.working=False
1676 self.working=True
1677
1678 - def __call__(self,subtitle, info_text, userInfo={}):
1679
1680 if not self.init:
1681 self.load_notification()
1682 if not self.working:
1683 return
1684 try:
1685 notification = self.NSUserNotification.alloc().init()
1686 notification.setTitle_('MadGraph5_aMC@NLO')
1687 notification.setSubtitle_(subtitle)
1688 notification.setInformativeText_(info_text)
1689 try:
1690 notification.setUserInfo_(userInfo)
1691 except:
1692 pass
1693 self.NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification)
1694 except:
1695 pass
1696
1697
1698
1699 apple_notify = Applenotification()
1702
1703 done_notification = False
1704 message_aprilfirst =\
1705 {'error': ['Be careful, a cat is eating a lot of fish today. This makes the code unstable.',
1706 'Really, this sounds fishy.',
1707 'A Higgs boson walks into a church. The priest says "We don\'t allow Higgs bosons in here." The Higgs boson replies, "But without me, how can you have mass?"',
1708 "Why does Heisenberg detest driving cars? Because, every time he looks at the speedometer he gets lost!",
1709 "May the mass times acceleration be with you.",
1710 "NOTE: This product may actually be nine-dimensional. If this is the case, functionality is not affected by the extra five dimensions.",
1711 "IMPORTANT: This product is composed of 100%% matter: It is the responsibility of the User to make sure that it does not come in contact with antimatter.",
1712 "",
1713 'The fish are out of jokes. See you next year for more!'],
1714
1715
1716 }
1717
1718 default_banner_1 = "************************************************************\n" + \
1719 "* *\n" + \
1720 "* W E L C O M E to *\n" + \
1721 "* M A D G R A P H 5 _ a M C @ N L O *\n" + \
1722 "* *\n" + \
1723 "* *\n"
1724
1725
1726 default_banner_2 = "* *\n" + \
1727 "%s" + \
1728 "* *\n" + \
1729 "* The MadGraph5_aMC@NLO Development Team - Find us at *\n" + \
1730 "* https://server06.fynu.ucl.ac.be/projects/madgraph *\n" + \
1731 "* and *\n" + \
1732 "* http://amcatnlo.web.cern.ch/amcatnlo/ *\n" + \
1733 "* *\n" + \
1734 "* Type 'help' for in-line help. *\n" + \
1735 "* Type 'tutorial' to learn how MG5 works *\n" + \
1736 "* Type 'tutorial aMCatNLO' to learn how aMC@NLO works *\n" + \
1737 "* Type 'tutorial MadLoop' to learn how MadLoop works *\n" + \
1738 "* *\n" + \
1739 "************************************************************"
1740
1741 May4_banner = "* _____ *\n" + \
1742 "* ,-~\" \"~-. *\n" + \
1743 "* * ,^ ___ ^. * *\n" + \
1744 "* * / .^ ^. \ * *\n" + \
1745 "* * Y l o ! Y * *\n" + \
1746 "* * l_ `.___.' _,[ * *\n" + \
1747 "* * |^~\"--------------~\"\"^| * *\n" + \
1748 "* * ! May the 4th ! * *\n" + \
1749 "* * \ / * *\n" + \
1750 "* * ^. .^ * *\n" + \
1751 "* * \"-.._____.,-\" * *\n"
1752
1753 special_banner = {(4,5): May4_banner}
1754
1755
1757
1758 try:
1759 now = time.localtime()
1760 date = now.tm_mday, now.tm_mon
1761 if date in [(1,4)]:
1762 if msgtype in EasterEgg.message_aprilfirst:
1763 choices = EasterEgg.message_aprilfirst[msgtype]
1764 if len(choices) == 0:
1765 return
1766 elif len(choices) == 1:
1767 msg = choices[0]
1768 else:
1769 import random
1770 msg = choices[random.randint(0,len(choices)-2)]
1771 EasterEgg.message_aprilfirst[msgtype].remove(msg)
1772
1773 elif msgtype=='loading' and date in self.special_banner:
1774 self.change_banner(date)
1775 return
1776 else:
1777 return
1778 if MADEVENT:
1779 return
1780
1781 import os
1782 import pwd
1783 username =pwd.getpwuid( os.getuid() )[ 0 ]
1784 msg = msg % {'user': username}
1785 if sys.platform == "darwin":
1786 self.call_apple(msg)
1787 else:
1788 self.call_linux(msg)
1789 except Exception, error:
1790 sprint(error)
1791 pass
1792
1794 try:
1795 self.call_apple(msg)
1796 except:
1797 pass
1798
1804
1805
1807
1808
1809 p = subprocess.Popen("osascript -e 'get volume settings'", stdout=subprocess.PIPE, shell=True)
1810 output, _ = p.communicate()
1811
1812 info = dict([[a.strip() for a in l.split(':',1)] for l in output.strip().split(',')])
1813 muted = False
1814 if 'output muted' in info and info['output muted'] == 'true':
1815 muted = True
1816 elif 'output volume' in info and info['output volume'] == '0':
1817 muted = True
1818
1819 if muted:
1820 if not EasterEgg.done_notification:
1821 apple_notify('On April first','turn up your volume!')
1822 EasterEgg.done_notification = True
1823 else:
1824 os.system('say %s' % msg)
1825
1826
1828
1829 fishPath = madgraph.MG5DIR+"/input/.cowgraph.cow"
1830 if os.path.exists(fishPath):
1831 fishPath = " -f " + fishPath
1832
1833
1834
1835 fishPole = which('cowthink')
1836 if not os.path.exists(fishPole):
1837 if os.path.exists(which('cowsay')):
1838 fishPole = which('cowsay')
1839 else:
1840 return
1841
1842
1843 fishCmd = fishPole + fishPath + " " + msg
1844 os.system(fishCmd)
1845
1848 """ return v2 if v1>v2
1849 return v1 if v1<v2
1850 return v1 if v1=v2
1851 return v1 if v2 is not in 1.2.3.4.5 format
1852 return v2 if v1 is not in 1.2.3.4.5 format
1853 """
1854 from itertools import izip_longest
1855 for a1, a2 in izip_longest(v1, v2, fillvalue=0):
1856 try:
1857 a1= int(a1)
1858 except:
1859 return v2
1860 try:
1861 a2= int(a2)
1862 except:
1863 return v1
1864 if a1 > a2:
1865 return v2
1866 elif a1 < a2:
1867 return v1
1868 return v1
1869
1870
1871
1872 plugin_support = {}
1910
1911
1912
1913 -def set_global(loop=False, unitary=True, mp=False, cms=False):
1943 return deco_f_set
1944 return deco_set
1945
1950 """convenient way to import a plugin file/function"""
1951
1952 try:
1953 _temp = __import__('PLUGIN.%s' % module, globals(), locals(), fcts, -1)
1954 except ImportError:
1955 try:
1956 _temp = __import__('MG5aMC_PLUGIN.%s' % module, globals(), locals(), fcts, -1)
1957 except ImportError:
1958 raise MadGraph5Error, error_msg
1959
1960 if not fcts:
1961 return _temp
1962 elif len(fcts) == 1:
1963 return getattr(_temp,fcts[0])
1964 else:
1965 return [getattr(_temp,name) for name in fcts]
1966
1967 -def from_plugin_import(plugin_path, target_type, keyname=None, warning=False,
1968 info=None):
1969 """return the class associated with keyname for a given plugin class
1970 if keyname is None, return all the name associated"""
1971
1972 validname = []
1973 for plugpath in plugin_path:
1974 plugindirname = os.path.basename(plugpath)
1975 for plug in os.listdir(plugpath):
1976 if os.path.exists(pjoin(plugpath, plug, '__init__.py')):
1977 try:
1978 with stdchannel_redirected(sys.stdout, os.devnull):
1979 __import__('%s.%s' % (plugindirname,plug))
1980 except Exception, error:
1981 if warning:
1982 logger.warning("error detected in plugin: %s.", plug)
1983 logger.warning("%s", error)
1984 continue
1985 plugin = sys.modules['%s.%s' % (plugindirname,plug)]
1986 if hasattr(plugin, target_type):
1987 if not is_plugin_supported(plugin):
1988 continue
1989 if keyname is None:
1990 validname += getattr(plugin, target_type).keys()
1991 else:
1992 if keyname in getattr(plugin, target_type):
1993 if not info:
1994 logger.info('Using from plugin %s mode %s' % (plug, keyname), '$MG:BOLD')
1995 else:
1996 logger.info(info % {'plug': plug, 'key':keyname}, '$MG:BOLD')
1997 return getattr(plugin, target_type)[keyname]
1998
1999 if not keyname:
2000 return validname
2001
2002
2003
2004
2005 python_lhapdf=None
2007 """load the python module of lhapdf return None if it can not be loaded"""
2008
2009
2010 global python_lhapdf
2011 if python_lhapdf:
2012 if python_lhapdf == -1:
2013 return None
2014 else:
2015 return python_lhapdf
2016
2017 use_lhapdf=False
2018 try:
2019 lhapdf_libdir=subprocess.Popen([lhapdfconfig,'--libdir'],\
2020 stdout=subprocess.PIPE).stdout.read().strip()
2021 except:
2022 use_lhapdf=False
2023 return False
2024 else:
2025 try:
2026 candidates=[dirname for dirname in os.listdir(lhapdf_libdir) \
2027 if os.path.isdir(os.path.join(lhapdf_libdir,dirname))]
2028 except OSError:
2029 candidates=[]
2030 for candidate in candidates:
2031 if os.path.isfile(os.path.join(lhapdf_libdir,candidate,'site-packages','lhapdf.so')):
2032 sys.path.insert(0,os.path.join(lhapdf_libdir,candidate,'site-packages'))
2033 try:
2034 import lhapdf
2035 use_lhapdf=True
2036 break
2037 except ImportError:
2038 sys.path.pop(0)
2039 continue
2040 if not use_lhapdf:
2041 try:
2042 candidates=[dirname for dirname in os.listdir(lhapdf_libdir+'64') \
2043 if os.path.isdir(os.path.join(lhapdf_libdir+'64',dirname))]
2044 except OSError:
2045 candidates=[]
2046 for candidate in candidates:
2047 if os.path.isfile(os.path.join(lhapdf_libdir+'64',candidate,'site-packages','lhapdf.so')):
2048 sys.path.insert(0,os.path.join(lhapdf_libdir+'64',candidate,'site-packages'))
2049 try:
2050 import lhapdf
2051 use_lhapdf=True
2052 break
2053 except ImportError:
2054 sys.path.pop(0)
2055 continue
2056 if not use_lhapdf:
2057 try:
2058 import lhapdf
2059 use_lhapdf=True
2060 except ImportError:
2061 print 'fail'
2062 logger.warning("Failed to access python version of LHAPDF: "\
2063 "If the python interface to LHAPDF is available on your system, try "\
2064 "adding its location to the PYTHONPATH environment variable and the"\
2065 "LHAPDF library location to LD_LIBRARY_PATH (linux) or DYLD_LIBRARY_PATH (mac os x).")
2066
2067 if use_lhapdf:
2068 python_lhapdf = lhapdf
2069 python_lhapdf.setVerbosity(0)
2070 else:
2071 python_lhapdf = None
2072 return python_lhapdf
2073
2075 """implement newton method for solving f(x)=0, df is the derivate"""
2076 x = x0
2077 iter=0
2078 while abs(f(x)) > error:
2079 iter+=1
2080 x = x - f(x)/df(x)
2081 if iter ==maxiter:
2082 sprint('fail to solve equation')
2083 raise Exception
2084 return x
2085
2086 -def wget(http, path, *args, **opt):
2087 """a wget function for both unix and mac"""
2088
2089 if sys.platform == "darwin":
2090 return call(['curl', '-L', http, '-o%s' % path], *args, **opt)
2091 else:
2092 return call(['wget', http, '--output-document=%s'% path], *args, **opt)
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117