-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
289 lines (218 loc) · 8.16 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# -*- coding: utf-8 -*-
# This file is part of the pymfony package.
#
# (c) Alexandre Quercia <[email protected]>
#
# For the full copyright and license information, please view the LICENSE
# file that was distributed with this source code.
from __future__ import absolute_import;
import os.path;
import sys;
if sys.version_info[0] >= 3:
from urllib.parse import urlparse;
else:
from urlparse import urlparse;
from pymfony.component.system import Object;
from pymfony.component.system.oop import interface;
from pymfony.component.system.types import String;
from pymfony.component.system.types import Array;
from pymfony.component.system.exception import InvalidArgumentException;
from pymfony.component.system.exception import RuntimeException;
from pymfony.component.system.serializer import unserialize;
from pymfony.component.system.serializer import serialize;
"""
"""
@interface
class FileLocatorInterface(Object):
"""
@author Fabien Potencier <[email protected]>
"""
def locate(self, name, currentPath = None, first = True):
"""Returns a full path for a given file name.
@param name: mixed The file name to locate
@param currentPath: string The current path
@param first: boolean Whether to return the first occurrence
or an array of filenames
@return: string|list The full path to the file|A list of file paths
@raise InvalidArgumentException: When file is not found
"""
pass;
class FileLocator(FileLocatorInterface):
"""FileLocator uses an array of pre-defined paths to find files.
@author Fabien Potencier <[email protected]>
"""
def __init__(self, paths = None):
"""Constructor.
@param paths: string|list A path or an array of paths where to look
for resources
"""
if paths is None:
self._paths = list();
elif isinstance(paths, String):
self._paths = [paths];
else:
self._paths = list(paths);
def locate(self, name, currentPath = None, first = True):
"""Returns a full path for a given file name.
@param name: mixed The file name to locate
@param currentPath: string The current path
@param first: boolean Whether to return the first occurrence
or an array of filenames
@return: string|list The full path to the file|A list of file paths
@raise InvalidArgumentException: When file is not found
"""
if self.__isAbsolutePath(name):
if not os.path.exists(name):
raise InvalidArgumentException(
'The file "{0}" does not exist.'.format(name)
);
return name;
filepaths = list();
paths = [];
if currentPath:
paths.append(currentPath);
paths.extend(self._paths);
for path in paths:
filename = os.path.join(path, name);
if os.path.exists(filename):
if first:
return filename;
filepaths.append(filename);
if not filepaths:
raise InvalidArgumentException(
'The file "{0}" does not exist (in: {1}).'
''.format(name, ", ".join(paths))
);
return Array.uniq(filepaths);
def __isAbsolutePath(self, path):
"""Returns whether the file path is an absolute path.
@param path: string A file path
@return Boolean
"""
if (path.startswith('/') or path.startswith('\\')
or ( len(path) > 3 and path[0].isalpha()
and path[1] == ':'
and (path[2] == '\\' or path[2] == '/')
)
or urlparse(path)[0]
):
return True;
return False;
class ConfigCache(Object):
"""ConfigCache manages PHP cache files.
When debug is enabled, it knows when to flush the cache
thanks to an array of ResourceInterface instances.
@author Fabien Potencier <[email protected]>
"""
def __init__(self, path, debug):
"""Constructor.
@param string path The absolute cache path
@param Boolean debug Whether debugging is enabled or not
"""
self.__file = path;
self.__debug = bool(debug);
def __str__(self):
"""Gets the cache file path.
@return string The cache file path
"""
return self.__file;
def isFresh(self):
"""Checks if the cache is still fresh.:
This method always returns True when debug is off and the
cache file exists.
@return Boolean True if the cache is fresh, False otherwise:
"""
if not os.path.isfile(self.__file) :
return False;
if not self.__debug :
return True;
metadata = self.__file+'.meta';
if not os.path.isfile(metadata) :
return False;
time = os.path.getmtime(self.__file);
f = open(metadata);
content = f.read();
f.close();
meta = unserialize(content);
for resource in meta :
if not resource.isFresh(time) :
return False;
return True;
def write(self, content, metadata = None):
"""Writes cache.
@param string content The content to write in the cache
@param ResourceInterface[] metadata An array of ResourceInterface instances
@raise RuntimeException When cache file can't be wrote
"""
assert isinstance(metadata, list) or metadata is None;
dirname = os.path.dirname(self.__file);
if not os.path.isdir(dirname) :
try:
os.makedirs(dirname, 0o777);
except os.error:
raise RuntimeException('Unable to create the {0} directory'.format(dirname));
elif not os.access(dirname, os.W_OK) :
raise RuntimeException('Unable to write in the {0} directory'.format(dirname));
try:
suffix = 0;
while os.path.exists(self.__file+str(suffix)):
suffix += 1;
tmpFile = self.__file+str(suffix);
f = open(tmpFile, 'w');
f.write(content);
f.close();
if os.path.exists(self.__file):
os.remove(self.__file);
os.rename(tmpFile, self.__file);
if hasattr(os, 'chmod'):
umask = os.umask(0o220);
os.umask(umask);
os.chmod(self.__file, 0o666 & ~umask);
except Exception:
raise RuntimeException('Failed to write cache file "{0}".'.format(self.__file));
else:
try:
if hasattr(os, 'chmod'):
umask = os.umask(0o220);
os.umask(umask);
os.chmod(self.__file, 0o666 & ~umask);
except Exception:
pass;
finally:
try:
f.close();
except Exception:
pass;
if os.path.exists(tmpFile):
os.remove(tmpFile);
if None is not metadata and True is self.__debug :
filename = self.__file+'.meta';
content = serialize(metadata);
try:
suffix = 0;
while os.path.exists(filename+str(suffix)):
suffix += 1;
tmpFile = filename+str(suffix);
f = open(tmpFile, 'w');
f.write(content);
f.close();
if os.path.exists(filename):
os.remove(filename);
os.rename(tmpFile, filename);
except Exception:
pass;
else:
try:
if hasattr(os, 'chmod'):
umask = os.umask(0o220);
os.umask(umask);
os.chmod(filename, 0o666 & ~umask);
except Exception:
pass;
finally:
try:
f.close();
except Exception:
pass;
if os.path.exists(tmpFile):
os.remove(tmpFile);