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 from __future__ import absolute_import
19 import logging
20 import os
21 import shutil
22
23
24 logger = logging.getLogger('madgraph.files')
25
26
27
28
30 """Open a file, apply the function myfunct (with sock as an arg)
31 on its content and return the result. Deals properly with errors and
32 returns None if something goes wrong.
33 """
34 try:
35 if 'binary' in opt and opt['binary']:
36 sock = open(filename, 'rb')
37 else:
38 sock = open(filename, 'r')
39 try:
40 ret_value = myfunct(sock, *args)
41 finally:
42 sock.close()
43 except IOError as xxx_todo_changeme:
44 (errno, strerror) = xxx_todo_changeme.args
45 if 'print_error' in opt:
46 if not opt['print_error']:
47 return None
48 logger.error("I/O error on file %s (%s): %s" % (filename,errno, strerror))
49 return None
50
51 return ret_value
52
53
54
55
57 """Open a file for writing, apply the function myfunct (with sock as an arg)
58 on its content and return the result. Deals properly with errors and
59 returns None if something goes wrong.
60 """
61
62 try:
63 if 'binary' not in opts or not opts['binary']:
64 sock = open(filename, 'w')
65 else:
66 sock = open(filename, 'wb')
67
68 try:
69 ret_value = myfunct(sock, *args, **opts)
70 finally:
71 sock.close()
72 except IOError as xxx_todo_changeme1:
73 (errno, strerror) = xxx_todo_changeme1.args
74 if 'log' not in opts or opts['log']:
75 logger.error("I/O error (%s): %s" % (errno, strerror))
76 return None
77
78 return ret_value
79
80
81
82
84 """Open a file for appending, apply the function myfunct (with
85 sock as an arg) on its content and return the result. Deals
86 properly with errors and returns None if something goes wrong.
87 """
88
89 try:
90 sock = open(filename, 'a')
91 try:
92 ret_value = myfunct(sock, *args)
93 finally:
94 sock.close()
95 except IOError as xxx_todo_changeme2:
96 (errno, strerror) = xxx_todo_changeme2.args
97 logger.error("I/O error (%s): %s" % (errno, strerror))
98 return None
99
100 return ret_value
101
102
103
104
105 -def is_uptodate(picklefile, path_list=None, min_time=1343682423):
106 """Check if the pickle files is uptodate compare to a list of files.
107 If no files are given, the pickle files is checked against it\' current
108 directory"""
109
110 if not os.path.exists(picklefile):
111 return False
112
113 if path_list is None:
114 dirpath = os.path.dirname(picklefile)
115 path_list = [ os.path.join(dirpath, file) for file in \
116 os.listdir(dirpath)]
117
118 assert type(path_list) == list, 'is_update expect a list of files'
119
120 pickle_date = os.path.getctime(picklefile)
121 if pickle_date < min_time:
122 return False
123
124 for path in path_list:
125 try:
126 if os.path.getmtime(path) > pickle_date:
127 return False
128 except Exception:
129 continue
130
131 return True
132
133
134
135
136
143
144 -def cp(path1, path2, log=True, error=False):
145 """ simple cp taking linux or mix entry"""
146 path1 = format_path(path1)
147 path2 = format_path(path2)
148 try:
149 shutil.copy(path1, path2)
150 except IOError as why:
151 import madgraph.various.misc as misc
152 try:
153 if os.path.exists(path2):
154 path2 = os.path.join(path2, os.path.split(path1)[1])
155 shutil.copytree(path1, path2)
156 except IOError as why:
157 if error:
158 raise
159 if log:
160 logger.warning(why)
161 else:
162 misc.sprint("fail to cp", why)
163 except shutil.Error:
164
165 pass
166
167 -def rm(path, log=True):
168 """removes path, that can be a single element or a list"""
169 if type(path) == list:
170 for p in path:
171 rm(p, log)
172 else:
173 path = format_path(path)
174 try:
175 os.remove(path)
176 except OSError:
177 shutil.rmtree(path, ignore_errors = True)
178
179
180
181 -def mv(path1, path2):
182 """simple mv taking linux or mix format entry"""
183 path1 = format_path(path1)
184 path2 = format_path(path2)
185 try:
186 shutil.move(path1, path2)
187 except Exception:
188
189 if os.path.isfile(path2):
190 os.remove(path2)
191 shutil.move(path1, path2)
192 return
193 elif os.path.isdir(path2) and os.path.exists(
194 os.path.join(path2, os.path.basename(path1))):
195 path2 = os.path.join(path2, os.path.basename(path1))
196 os.remove(path2)
197 shutil.move(path1, path2)
198 else:
199 raise
200
202
203 with open(src,'ab') as wfd:
204 for f in add:
205 with open(f,'rb') as fd:
206 shutil.copyfileobj(fd, wfd, 1024*1024*100)
207
208
209
210 -def ln(file_pos, starting_dir='.', name='', log=True, cwd=None, abspath=False):
211 """a simple way to have a symbolic link without to have to change directory
212 starting_point is the directory where to write the link
213 file_pos is the file to link
214 WARNING: not the linux convention
215 """
216 file_pos = format_path(file_pos)
217 starting_dir = format_path(starting_dir)
218 if not name:
219 name = os.path.split(file_pos)[1]
220
221 if cwd:
222 if not os.path.isabs(file_pos):
223 file_pos = os.path.join(cwd, file_pos)
224 if not os.path.isabs(starting_dir):
225 starting_dir = os.path.join(cwd, starting_dir)
226
227
228 path = os.path.join(starting_dir, name)
229 if os.path.exists(path):
230 if os.path.realpath(path) != os.path.realpath(file_pos):
231 os.remove(os.path.join(starting_dir, name))
232 else:
233 return
234
235 if not abspath:
236 target = os.path.relpath(file_pos, starting_dir)
237 else:
238 target = file_pos
239
240 try:
241 os.symlink(target, os.path.join(starting_dir, name))
242 except Exception as error:
243 if log:
244 logger.debug(error)
245 logger.warning('Could not link %s at position: %s' % (file_pos, \
246 os.path.realpath(starting_dir)))
247
249 if not os.path.exists(dst):
250 os.makedirs(dst)
251 for item in os.listdir(src):
252 s = os.path.join(src, item)
253 d = os.path.join(dst, item)
254 if os.path.isdir(s):
255 copytree(s, d, symlinks, ignore)
256 else:
257 shutil.copy2(s, d)
258