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 This module contains utility functions for file operations.
33 """
34
35 import sys
36 import os
37 import os.path
38 import tempfile
39 from fnmatch import fnmatch
40
41 from eoxserver.core.exceptions import InternalError
42
44 """
45 This function mimicks the behaviour of the ``find`` shell command.
46 It expects a directory path ``dir`` and a file name pattern
47 ``pattern`` which may contain wildcards as accepted by the
48 :func:`fnmatch.fnmatch` function. It returns a list of paths to
49 matching files in ``dir`` and its subdirectories.
50
51 If ``dir`` does not exist or does not point to a directory or if no
52 matching files are found an empty list is returned.
53
54 Directories and files whose name starts with "." are omitted.
55 """
56 filenames = []
57
58 if os.path.exists(dir) and os.path.isdir(dir):
59 for path in os.listdir(dir):
60 if not path.startswith("."):
61 if os.path.isdir(os.path.join(dir, path)):
62 filenames.extend(findFiles(os.path.join(dir, path), pattern))
63 elif fnmatch(path, pattern):
64 filenames.append(os.path.join(dir, path))
65
66 return filenames
67
69 """
70 This function takes a module path ``path`` as argument and returns
71 the corresponding dotted name of the module.
72 """
73
74 module_name = [os.path.splitext(os.path.basename(path))[0]]
75 tmp_path = os.path.abspath(os.path.dirname(path))
76
77 search_paths = [dir for dir in sys.path]
78 if '' in search_paths:
79 search_paths.remove('')
80 search_paths.append(os.getcwd())
81
82 while tmp_path != "/" and tmp_path not in search_paths:
83 tmp_path, name = os.path.split(tmp_path)
84 module_name.append(name)
85
86 if tmp_path == "/":
87 raise InternalError("'%s' not on Python path." % path)
88 else:
89 return ".".join(reversed(module_name))
90
91
93 """ temporary file object - ``with - as`` statement friendly """
94
95 - def __init__( self , suffix , prefix = "" ) :
96 _ , self.__fname = tempfile.mkstemp(suffix,prefix)
97
99 """Converts class to name of the temporary file."""
100 return self.__fname
101
103 """Begin of ``with - as`` block - returns name of the temporary file."""
104 return self.__fname
105
106 - def __exit__( self , type, value, traceback) :
107 """End of ``with - as`` block - discards the temporary file."""
108 os.remove(self.__fname)
109