-
Notifications
You must be signed in to change notification settings - Fork 54
/
fs_util.py
183 lines (148 loc) · 5.85 KB
/
fs_util.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
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# (C) British Crown Copyright 2012-8 Met Office.
#
# This file is part of Rose, a framework for meteorological suites.
#
# Rose is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Rose is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Rose. If not, see <http://www.gnu.org/licenses/>.
# -----------------------------------------------------------------------------
"""File system utilities with event reporting."""
import errno
import os
from rose.reporter import Event
import shutil
class FileSystemEvent(Event):
"""An event raised on a file system operation."""
CHDIR = "chdir"
COPY = "copy"
CREATE = "create"
DELETE = "delete"
INSTALL = "install"
RENAME = "rename"
SYMLINK = "symlink"
TOUCH = "touch"
def __init__(self, action, target, source=None):
self.action = action
# FIXME: We need to sort out the verbosity for file system events
if self.action == self.COPY:
self.level = Event.V
self.target = target
self.source = source
Event.__init__(self, action, target, source)
def __str__(self):
target = self.target
if self.source:
target = "%s <= %s" % (self.target, self.source)
return "%s: %s" % (self.action, target)
class FileSystemUtil(object):
"""File system utilities with event reporting."""
def __init__(self, event_handler=None):
self.event_handler = event_handler
def handle_event(self, *args, **kwargs):
"""Handle an event using the runner's event handler."""
if callable(self.event_handler):
return self.event_handler(*args, **kwargs)
def chdir(self, path):
"""Wrap os.chdir."""
cwd = os.getcwd()
os.chdir(path)
if cwd != os.getcwd():
event = FileSystemEvent(FileSystemEvent.CHDIR, path + "/")
self.handle_event(event)
def copy2(self, source, target):
"""Wrap shutil.copy2."""
self.makedirs(self.dirname(target))
shutil.copy2(source, target)
event = FileSystemEvent(FileSystemEvent.COPY, source, target)
self.handle_event(event)
def copytree(self, source, target):
"""Wrap shutil.copytree."""
self.makedirs(self.dirname(target))
shutil.copytree(source, target)
event = FileSystemEvent(FileSystemEvent.COPY, source, target)
self.handle_event(event)
def delete(self, path):
"""Delete a file or a directory."""
if os.path.islink(path) or os.path.isfile(path):
os.unlink(path)
self.handle_event(FileSystemEvent(FileSystemEvent.DELETE, path))
elif os.path.isdir(path):
# Ignore errors, so that broken symlinks are ignored
shutil.rmtree(path, ignore_errors=True)
# Try again w/o ignore_errors, if required
if os.path.isdir(path):
shutil.rmtree(path)
event = FileSystemEvent(FileSystemEvent.DELETE, path + "/")
self.handle_event(event)
def dirname(self, path):
"""Wrap os.path.dirname.
Unlike os.path.dirname, return "." instead of an empty string if result
is the current working directory.
"""
dir_ = os.path.dirname(path)
if dir_:
return dir_
else:
return "."
def install(self, path):
"""Create an empty file in path."""
self.delete(path)
open(path, "wb").close()
event = FileSystemEvent(FileSystemEvent.INSTALL, path)
self.handle_event(event)
def makedirs(self, path):
"""Wrap os.makedirs. Does nothing if directory exists."""
# Attempt to handle race conditions
while not os.path.isdir(path):
self.delete(path)
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST:
pass
else:
raise exc
else:
event = FileSystemEvent(FileSystemEvent.CREATE, path)
self.handle_event(event)
def rename(self, source, target):
"""Wrap os.rename. Create directory of target if it does not exist."""
self.makedirs(self.dirname(target))
os.rename(source, target)
event = FileSystemEvent(FileSystemEvent.RENAME, source, target)
self.handle_event(event)
def symlink(self, source, target, no_overwrite_mode=False):
"""Wrap os.symlink.
Create directory of target if it does not exist.
If no_overwrite_mode is not specified or not True, remove target if it
is not a symbolic link pointing to source.
"""
if os.path.islink(target) and os.readlink(target) == source:
return
self.makedirs(self.dirname(target))
if not no_overwrite_mode:
self.delete(target)
try:
os.symlink(source, target)
except OSError as exc:
if exc.filename is None: # Python does not set filename :-(
exc.filename = target
raise exc
event = FileSystemEvent(FileSystemEvent.SYMLINK, source, target)
self.handle_event(event)
def touch(self, path):
"""Touch a file."""
open(path, "a").close()
os.utime(path, None)
self.handle_event(FileSystemEvent(FileSystemEvent.TOUCH, path))