forked from mlflow/mlflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_utils.py
391 lines (294 loc) · 12 KB
/
file_utils.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import codecs
import errno
import gzip
import os
import posixpath
import shutil
import sys
import tarfile
import tempfile
from six.moves.urllib.request import pathname2url
from six.moves.urllib.parse import unquote
from six.moves import urllib
import yaml
from mlflow.entities import FileInfo
from mlflow.exceptions import MissingConfigException
ENCODING = "utf-8"
def is_directory(name):
return os.path.isdir(name)
def is_file(name):
return os.path.isfile(name)
def exists(name):
return os.path.exists(name)
def list_all(root, filter_func=lambda x: True, full_path=False):
"""
List all entities directly under 'dir_name' that satisfy 'filter_func'
:param root: Name of directory to start search
:param filter_func: function or lambda that takes path
:param full_path: If True will return results as full path including `root`
:return: list of all files or directories that satisfy the criteria.
"""
if not is_directory(root):
raise Exception("Invalid parent directory '%s'" % root)
matches = [x for x in os.listdir(root) if filter_func(os.path.join(root, x))]
return [os.path.join(root, m) for m in matches] if full_path else matches
def list_subdirs(dir_name, full_path=False):
"""
Equivalent to UNIX command:
``find $dir_name -depth 1 -type d``
:param dir_name: Name of directory to start search
:param full_path: If True will return results as full path including `root`
:return: list of all directories directly under 'dir_name'
"""
return list_all(dir_name, os.path.isdir, full_path)
def list_files(dir_name, full_path=False):
"""
Equivalent to UNIX command:
``find $dir_name -depth 1 -type f``
:param dir_name: Name of directory to start search
:param full_path: If True will return results as full path including `root`
:return: list of all files directly under 'dir_name'
"""
return list_all(dir_name, os.path.isfile, full_path)
def find(root, name, full_path=False):
"""
Search for a file in a root directory. Equivalent to:
``find $root -name "$name" -depth 1``
:param root: Name of root directory for find
:param name: Name of file or directory to find directly under root directory
:param full_path: If True will return results as full path including `root`
:return: list of matching files or directories
"""
path_name = os.path.join(root, name)
return list_all(root, lambda x: x == path_name, full_path)
def mkdir(root, name=None): # noqa
"""
Make directory with name "root/name", or just "root" if name is None.
:param root: Name of parent directory
:param name: Optional name of leaf directory
:return: Path to created directory
"""
target = os.path.join(root, name) if name is not None else root
try:
os.makedirs(target)
except OSError as e:
if e.errno != errno.EEXIST or not os.path.isdir(target):
raise e
return target
def make_containing_dirs(path):
"""
Create the base directory for a given file path if it does not exist; also creates parent
directories.
"""
dir_name = os.path.dirname(path)
if not os.path.exists(dir_name):
os.makedirs(dir_name)
def write_yaml(root, file_name, data, overwrite=False):
"""
Write dictionary data in yaml format.
:param root: Directory name.
:param file_name: Desired file name. Will automatically add .yaml extension if not given
:param data: data to be dumped as yaml format
:param overwrite: If True, will overwrite existing files
"""
if not exists(root):
raise MissingConfigException("Parent directory '%s' does not exist." % root)
file_path = os.path.join(root, file_name)
yaml_file_name = file_path if file_path.endswith(".yaml") else file_path + ".yaml"
if exists(yaml_file_name) and not overwrite:
raise Exception("Yaml file '%s' exists as '%s" % (file_path, yaml_file_name))
try:
with codecs.open(yaml_file_name, mode='w', encoding=ENCODING) as yaml_file:
yaml.safe_dump(data, yaml_file, default_flow_style=False, allow_unicode=True)
except Exception as e:
raise e
def read_yaml(root, file_name):
"""
Read data from yaml file and return as dictionary
:param root: Directory name
:param file_name: File name. Expects to have '.yaml' extension
:return: Data in yaml file as dictionary
"""
if not exists(root):
raise MissingConfigException(
"Cannot read '%s'. Parent dir '%s' does not exist." % (file_name, root))
file_path = os.path.join(root, file_name)
if not exists(file_path):
raise MissingConfigException("Yaml file '%s' does not exist." % file_path)
try:
with codecs.open(file_path, mode='r', encoding=ENCODING) as yaml_file:
return yaml.safe_load(yaml_file)
except Exception as e:
raise e
class TempDir(object):
def __init__(self, chdr=False, remove_on_exit=True):
self._dir = None
self._path = None
self._chdr = chdr
self._remove = remove_on_exit
def __enter__(self):
self._path = os.path.abspath(tempfile.mkdtemp())
assert os.path.exists(self._path)
if self._chdr:
self._dir = os.path.abspath(os.getcwd())
os.chdir(self._path)
return self
def __exit__(self, tp, val, traceback):
if self._chdr and self._dir:
os.chdir(self._dir)
self._dir = None
if self._remove and os.path.exists(self._path):
shutil.rmtree(self._path)
assert not self._remove or not os.path.exists(self._path)
assert os.path.exists(os.getcwd())
def path(self, *path):
return os.path.join("./", *path) if self._chdr else os.path.join(self._path, *path)
def read_file_lines(parent_path, file_name):
"""
Return the contents of the file as an array where each element is a separate line.
:param parent_path: Full path to the directory that contains the file.
:param file_name: Leaf file name.
:return: All lines in the file as an array.
"""
file_path = os.path.join(parent_path, file_name)
with codecs.open(file_path, mode='r', encoding=ENCODING) as f:
return f.readlines()
def read_file(parent_path, file_name):
"""
Return the contents of the file.
:param parent_path: Full path to the directory that contains the file.
:param file_name: Leaf file name.
:return: The contents of the file.
"""
file_path = os.path.join(parent_path, file_name)
with codecs.open(file_path, mode='r', encoding=ENCODING) as f:
return f.read()
def get_file_info(path, rel_path):
"""
Returns file meta data : location, size, ... etc
:param path: Path to artifact
:return: `FileInfo` object
"""
if is_directory(path):
return FileInfo(rel_path, True, None)
else:
return FileInfo(rel_path, False, os.path.getsize(path))
def get_relative_path(root_path, target_path):
"""
Remove root path common prefix and return part of `path` relative to `root_path`.
:param root_path: Root path
:param target_path: Desired path for common prefix removal
:return: Path relative to root_path
"""
if len(root_path) > len(target_path):
raise Exception("Root path '%s' longer than target path '%s'" % (root_path, target_path))
common_prefix = os.path.commonprefix([root_path, target_path])
return os.path.relpath(target_path, common_prefix)
def mv(target, new_parent):
shutil.move(target, new_parent)
def write_to(filename, data):
with codecs.open(filename, mode="w", encoding=ENCODING) as handle:
handle.write(data)
def append_to(filename, data):
with open(filename, "a") as handle:
handle.write(data)
def make_tarfile(output_filename, source_dir, archive_name, custom_filter=None):
# Helper for filtering out modification timestamps
def _filter_timestamps(tar_info):
tar_info.mtime = 0
return tar_info if custom_filter is None else custom_filter(tar_info)
unzipped_filename = tempfile.mktemp()
try:
with tarfile.open(unzipped_filename, "w") as tar:
tar.add(source_dir, arcname=archive_name, filter=_filter_timestamps)
# When gzipping the tar, don't include the tar's filename or modification time in the
# zipped archive (see https://docs.python.org/3/library/gzip.html#gzip.GzipFile)
with gzip.GzipFile(filename="", fileobj=open(output_filename, 'wb'), mode='wb', mtime=0) \
as gzipped_tar, open(unzipped_filename, 'rb') as tar:
gzipped_tar.write(tar.read())
finally:
os.remove(unzipped_filename)
def _copy_project(src_path, dst_path=""):
"""
Internal function used to copy MLflow project during development.
Copies the content of the whole directory tree except patterns defined in .dockerignore.
The MLflow is assumed to be accessible as a local directory in this case.
:param dst_path: MLflow will be copied here
:return: name of the MLflow project directory
"""
def _docker_ignore(mlflow_root):
docker_ignore = os.path.join(mlflow_root, '.dockerignore')
patterns = []
if os.path.exists(docker_ignore):
with open(docker_ignore, "r") as f:
patterns = [x.strip() for x in f.readlines()]
def ignore(_, names):
import fnmatch
res = set()
for p in patterns:
res.update(set(fnmatch.filter(names, p)))
return list(res)
return ignore if patterns else None
mlflow_dir = "mlflow-project"
# check if we have project root
assert os.path.isfile(os.path.join(src_path, "setup.py")), "file not found " + str(
os.path.abspath(os.path.join(src_path, "setup.py")))
shutil.copytree(src_path, os.path.join(dst_path, mlflow_dir),
ignore=_docker_ignore(src_path))
return mlflow_dir
def _copy_file_or_tree(src, dst, dst_dir=None):
"""
:return: The path to the copied artifacts, relative to `dst`
"""
dst_subpath = os.path.basename(os.path.abspath(src))
if dst_dir is not None:
dst_subpath = os.path.join(dst_dir, dst_subpath)
dst_path = os.path.join(dst, dst_subpath)
if os.path.isfile(src):
dst_dirpath = os.path.dirname(dst_path)
if not os.path.exists(dst_dirpath):
os.makedirs(dst_dirpath)
shutil.copy(src=src, dst=dst_path)
else:
shutil.copytree(src=src, dst=dst_path)
return dst_subpath
def get_parent_dir(path):
return os.path.abspath(os.path.join(path, os.pardir))
def relative_path_to_artifact_path(path):
if os.path == posixpath:
return path
if os.path.abspath(path) == path:
raise Exception("This method only works with relative paths.")
return unquote(pathname2url(path))
def path_to_local_file_uri(path):
"""
Convert local filesystem path to local file uri.
"""
path = pathname2url(path)
if path == posixpath.abspath(path):
return "file://{path}".format(path=path)
else:
return "file:{path}".format(path=path)
def path_to_local_sqlite_uri(path):
"""
Convert local filesystem path to sqlite uri.
"""
path = posixpath.abspath(pathname2url(os.path.abspath(path)))
prefix = "sqlite://" if sys.platform == "win32" else "sqlite:///"
return prefix + path
def local_file_uri_to_path(uri):
"""
Convert URI to local filesystem path.
No-op if the uri does not have the expected scheme.
"""
path = urllib.parse.urlparse(uri).path if uri.startswith("file:") else uri
return urllib.request.url2pathname(path)
def get_local_path_or_none(path_or_uri):
"""Check if the argument is a local path (no scheme or file:///) and return local path if true,
None otherwise.
"""
parsed_uri = urllib.parse.urlparse(path_or_uri)
if len(parsed_uri.scheme) == 0 or parsed_uri.scheme == "file" and len(parsed_uri.netloc) == 0:
return local_file_uri_to_path(path_or_uri)
else:
return None