Package aloha :: Module files
[hide private]
[frames] | no frames]

Source Code for Module aloha.files

  1  ################################################################################ 
  2  # 
  3  # Copyright (c) 2009 The MadGraph Development team and Contributors 
  4  # 
  5  # This file is a part of the MadGraph 5 project, an application which  
  6  # automatically generates Feynman diagrams and matrix elements for arbitrary 
  7  # high-energy processes in the Standard Model and beyond. 
  8  # 
  9  # It is subject to the MadGraph license which should accompany this  
 10  # distribution. 
 11  # 
 12  # For more information, please visit: http://madgraph.phys.ucl.ac.be 
 13  # 
 14  ################################################################################ 
 15   
 16  """Methods and classes dealing with file access.""" 
 17   
 18  import logging 
 19  import os 
 20  import shutil 
 21   
 22   
 23  logger = logging.getLogger('madgraph.files') 
 24   
 25  #=============================================================================== 
 26  # read_from_file 
 27  #=============================================================================== 
28 -def read_from_file(filename, myfunct, *args, **opt):
29 """Open a file, apply the function myfunct (with sock as an arg) 30 on its content and return the result. Deals properly with errors and 31 returns None if something goes wrong. 32 """ 33 34 try: 35 sock = open(filename, 'r') 36 try: 37 ret_value = myfunct(sock, *args) 38 finally: 39 sock.close() 40 except IOError, (errno, strerror): 41 if opt.has_key('print_error'): 42 if not opt['print_error']: 43 return None 44 logger.error("I/O error on file %s (%s): %s" % (filename,errno, strerror)) 45 return None 46 47 return ret_value
48 49 #=============================================================================== 50 # write_to_file 51 #===============================================================================
52 -def write_to_file(filename, myfunct, *args):
53 """Open a file for writing, apply the function myfunct (with sock as an arg) 54 on its content and return the result. Deals properly with errors and 55 returns None if something goes wrong. 56 """ 57 58 try: 59 sock = open(filename, 'w') 60 try: 61 ret_value = myfunct(sock, *args) 62 finally: 63 sock.close() 64 except IOError, (errno, strerror): 65 logger.error("I/O error (%s): %s" % (errno, strerror)) 66 return None 67 68 return ret_value
69 70 #=============================================================================== 71 # append_to_file 72 #===============================================================================
73 -def append_to_file(filename, myfunct, *args):
74 """Open a file for appending, apply the function myfunct (with 75 sock as an arg) on its content and return the result. Deals 76 properly with errors and returns None if something goes wrong. 77 """ 78 79 try: 80 sock = open(filename, 'a') 81 try: 82 ret_value = myfunct(sock, *args) 83 finally: 84 sock.close() 85 except IOError, (errno, strerror): 86 logger.error("I/O error (%s): %s" % (errno, strerror)) 87 return None 88 89 return ret_value
90 91 #=============================================================================== 92 # check piclke validity 93 #===============================================================================
94 -def is_uptodate(picklefile, path_list=None, min_time=1300120445):
95 """Check if the pickle files is uptodate compare to a list of files. 96 If no files are given, the pickle files is checked against it\' current 97 directory""" 98 99 if not os.path.exists(picklefile): 100 return False 101 102 if not path_list: 103 dirpath = os.path.dirname(picklefile) 104 path_list = [ os.path.join(dirpath, file) for file in \ 105 os.listdir(dirpath)] 106 107 assert type(path_list) == list, 'is_update expect a list of files' 108 109 pickle_date = os.path.getctime(picklefile) 110 if pickle_date < min_time: 111 return False 112 113 for path in path_list: 114 if os.path.getmtime(path) > pickle_date: 115 return False 116 #all pass 117 return True
118 119 120 ################################################################################ 121 ## helper function for universal file treatment 122 ################################################################################
123 -def format_path(path):
124 """Format the path in local format taking in entry a unix format""" 125 if path[0] != '/': 126 return os.path.join(*path.split('/')) 127 else: 128 return os.path.sep + os.path.join(*path.split('/'))
129
130 -def cp(path1, path2, log=True):
131 """ simple cp taking linux or mix entry""" 132 path1 = format_path(path1) 133 path2 = format_path(path2) 134 try: 135 shutil.copy(path1, path2) 136 except IOError, why: 137 if log: 138 logger.warning(why)
139 140
141 -def mv(path1, path2):
142 """simple mv taking linux or mix format entry""" 143 path1 = format_path(path1) 144 path2 = format_path(path2) 145 try: 146 shutil.move(path1, path2) 147 except: 148 # An error can occur if the files exist at final destination 149 if os.path.isfile(path2): 150 os.remove(path2) 151 shutil.move(path1, path2) 152 return 153 elif os.path.isdir(path2) and os.path.exists( 154 os.path.join(path2, os.path.basename(path1))): 155 path2 = os.path.join(path2, os.path.basename(path1)) 156 os.remove(path2) 157 shutil.move(path1, path2) 158 else: 159 raise
160
161 -def ln(file_pos, starting_dir='.', name='', log=True):
162 """a simple way to have a symbolic link whithout to have to change directory 163 starting_point is the directory where to write the link 164 file_pos is the file to link 165 WARNING: not the linux convention 166 """ 167 file_pos = format_path(file_pos) 168 starting_dir = format_path(starting_dir) 169 if not name: 170 name = os.path.split(file_pos)[1] 171 172 # Remove existing link if necessary 173 if os.path.islink(os.path.join(starting_dir, name)): 174 os.remove(os.path.join(starting_dir, name)) 175 176 try: 177 os.symlink(os.path.relpath(file_pos, starting_dir), \ 178 os.path.join(starting_dir, name)) 179 except: 180 if log: 181 logger.warning('Could not link %s at position: %s' % (file_pos, \ 182 os.path.realpath(starting_dir)))
183