1
2
3
4
5
6
7
8
9
10
11
12
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
27
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 try:
34 sock = open(filename, 'r')
35 try:
36 ret_value = myfunct(sock, *args)
37 finally:
38 sock.close()
39 except IOError, (errno, strerror):
40 if opt.has_key('print_error'):
41 if not opt['print_error']:
42 return None
43 logger.error("I/O error on file %s (%s): %s" % (filename,errno, strerror))
44 return None
45
46 return ret_value
47
48
49
50
52 """Open a file for writing, apply the function myfunct (with sock as an arg)
53 on its content and return the result. Deals properly with errors and
54 returns None if something goes wrong.
55 """
56
57 try:
58 sock = open(filename, 'w')
59 try:
60 ret_value = myfunct(sock, *args)
61 finally:
62 sock.close()
63 except IOError, (errno, strerror):
64 if 'log' not in opts or opts['log']:
65 logger.error("I/O error (%s): %s" % (errno, strerror))
66 return None
67
68 return ret_value
69
70
71
72
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
93
94 -def is_uptodate(picklefile, path_list=None, min_time=1343682423):
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 path_list is None:
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 try:
115 if os.path.getmtime(path) > pickle_date:
116 return False
117 except Exception:
118 continue
119
120 return True
121
122
123
124
125
132
133 -def cp(path1, path2, log=True, error=False):
134 """ simple cp taking linux or mix entry"""
135 path1 = format_path(path1)
136 path2 = format_path(path2)
137 try:
138 shutil.copy(path1, path2)
139 except IOError, why:
140 try:
141 if os.path.exists(path2):
142 path2 = os.path.join(path2, os.path.split(path1)[1])
143 shutil.copytree(path1, path2)
144 except IOError, why:
145 if error:
146 raise
147 if log:
148 logger.warning(why)
149 except shutil.Error:
150
151 pass
152
153 -def rm(path, log=True):
154 """removes path, that can be a single element or a list"""
155 if type(path) == list:
156 for p in path:
157 rm(p, log)
158 else:
159 path = format_path(path)
160 try:
161 os.remove(path)
162 except OSError:
163 shutil.rmtree(path, ignore_errors = True)
164
165
166
167 -def mv(path1, path2):
168 """simple mv taking linux or mix format entry"""
169 path1 = format_path(path1)
170 path2 = format_path(path2)
171 try:
172 shutil.move(path1, path2)
173 except Exception:
174
175 if os.path.isfile(path2):
176 os.remove(path2)
177 shutil.move(path1, path2)
178 return
179 elif os.path.isdir(path2) and os.path.exists(
180 os.path.join(path2, os.path.basename(path1))):
181 path2 = os.path.join(path2, os.path.basename(path1))
182 os.remove(path2)
183 shutil.move(path1, path2)
184 else:
185 raise
186
187 -def ln(file_pos, starting_dir='.', name='', log=True, cwd=None, abspath=False):
188 """a simple way to have a symbolic link without to have to change directory
189 starting_point is the directory where to write the link
190 file_pos is the file to link
191 WARNING: not the linux convention
192 """
193 file_pos = format_path(file_pos)
194 starting_dir = format_path(starting_dir)
195 if not name:
196 name = os.path.split(file_pos)[1]
197
198 if cwd:
199 if not os.path.isabs(file_pos):
200 file_pos = os.path.join(cwd, file_pos)
201 if not os.path.isabs(starting_dir):
202 starting_dir = os.path.join(cwd, starting_dir)
203
204
205 if os.path.exists(os.path.join(starting_dir, name)):
206 os.remove(os.path.join(starting_dir, name))
207
208 if not abspath:
209 target = os.path.relpath(file_pos, starting_dir)
210 else:
211 target = file_pos
212
213 try:
214 os.symlink(target, os.path.join(starting_dir, name))
215 except Exception:
216 if log:
217 logger.warning('Could not link %s at position: %s' % (file_pos, \
218 os.path.realpath(starting_dir)))
219