-
Notifications
You must be signed in to change notification settings - Fork 29
/
storage.py
543 lines (415 loc) · 19 KB
/
storage.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# Copyright Swiss Data Science Center (SDSC). A partnership between
# École Polytechnique Fédérale de Lausanne (EPFL) and
# Eidgenössische Technische Hochschule Zürich (ETHZ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Logic for handling a data storage."""
import functools
import itertools
import os
import re
import shlex
import tempfile
from collections import defaultdict
from pathlib import Path
from shutil import move, which
from subprocess import PIPE, STDOUT, check_output, run
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union
import pathspec
from renku.core import errors
from renku.core.config import get_value
from renku.core.constant import RENKU_LFS_IGNORE_PATH, RENKU_PROTECTED_PATHS
from renku.core.util import communication
from renku.core.util.git import get_in_submodules, run_command
from renku.core.util.os import expand_directories, parse_file_size
from renku.domain_model.project_context import project_context
if TYPE_CHECKING:
from renku.infrastructure.repository import Repository
_CMD_STORAGE_INSTALL = ["git", "lfs", "install", "--local"]
_CMD_STORAGE_TRACK = ["git", "lfs", "track", "--"]
_CMD_STORAGE_UNTRACK = ["git", "lfs", "untrack", "--"]
_CMD_STORAGE_CLEAN = ["git", "lfs", "clean"]
_CMD_STORAGE_CHECKOUT = ["git", "lfs", "checkout"]
_CMD_STORAGE_PULL = ["git", "lfs", "pull", "-I"]
_CMD_STORAGE_MIGRATE_IMPORT = ["git", "lfs", "migrate", "import"]
_CMD_STORAGE_MIGRATE_INFO = ["git", "lfs", "migrate", "info", "--top", "42000"]
_CMD_STORAGE_LIST = ["git", "lfs", "ls-files", "-n"]
_CMD_STORAGE_STATUS = ["git", "lfs", "status"]
_LFS_HEADER = "version https://git-lfs.github.com/spec/"
class RenkuGitWildMatchPattern(pathspec.patterns.GitWildMatchPattern):
"""Custom GitWildMatchPattern matcher."""
__slots__ = ("pattern",)
def __init__(self, pattern, include=None):
"""Initialize RenkuRegexPattern."""
super().__init__(pattern, include)
self.pattern = pattern
pathspec.util.register_pattern("renku_gitwildmatch", RenkuGitWildMatchPattern)
def check_external_storage_wrapper(fn):
"""Check availability of external storage on methods that need it.
Raises:
``errors.ExternalStorageNotInstalled``: If external storage isn't installed.
``errors.ExternalStorageDisabled``: If external storage isn't enabled.
"""
@functools.wraps(fn)
def wrapper(*args, **kwargs):
if not check_external_storage():
pass
else:
return fn(*args, **kwargs)
return wrapper
@functools.lru_cache
def storage_installed() -> bool:
"""Verify that git-lfs is installed and on system PATH."""
return bool(which("git-lfs"))
def storage_installed_locally() -> bool:
"""Verify that git-lfs is installed for the project."""
repo_config = project_context.repository.get_configuration(scope="local")
return repo_config.has_section('filter "lfs"')
def check_external_storage():
"""Check if repository has external storage enabled.
Raises:
``errors.ExternalStorageNotInstalled``: If external storage isn't installed.
``errors.ExternalStorageDisabled``: If external storage isn't enabled.
"""
installed_locally = storage_installed_locally()
is_storage_installed = installed_locally and storage_installed()
if project_context.external_storage_requested and not is_storage_installed:
raise errors.ExternalStorageDisabled()
if installed_locally and not storage_installed():
raise errors.ExternalStorageNotInstalled()
return is_storage_installed
def renku_lfs_ignore() -> pathspec.PathSpec:
"""Gets pathspec for files to not add to LFS."""
ignore_path = project_context.path / RENKU_LFS_IGNORE_PATH
if not os.path.exists(ignore_path):
return pathspec.PathSpec.from_lines("renku_gitwildmatch", RENKU_PROTECTED_PATHS)
with ignore_path.open("r") as f:
# NOTE: Append `renku_protected_paths` at the end to give it the highest priority
lines = itertools.chain(f, RENKU_PROTECTED_PATHS)
return pathspec.PathSpec.from_lines("renku_gitwildmatch", lines)
def get_minimum_lfs_file_size() -> int:
"""The minimum size of a file in bytes to be added to lfs."""
size = get_value("renku", "lfs_threshold")
return parse_file_size(size)
def init_external_storage(force: bool = False) -> None:
"""Initialize the external storage for data."""
try:
result = run(
_CMD_STORAGE_INSTALL + (["--force"] if force else []),
stdout=PIPE,
stderr=STDOUT,
cwd=project_context.path,
text=True,
)
if result.returncode != 0:
raise errors.GitLFSError(f"Error executing 'git lfs install: \n {result.stdout}")
except (KeyboardInterrupt, OSError) as e:
raise errors.ParameterError(f"Couldn't run 'git lfs':\n{e}")
@check_external_storage_wrapper
def track_paths_in_storage(*paths: Union[Path, str]) -> Optional[List[str]]:
"""Track paths in the external storage."""
if not project_context.external_storage_requested or not check_external_storage():
return None
# Calculate which paths can be tracked in lfs
track_paths: List[str] = []
attrs = project_context.repository.get_attributes(*paths)
for path in paths:
path = Path(path)
# Do not track symlinks in LFS
if path.is_symlink():
continue
# Do not add files with filter=lfs in .gitattributes
if attrs.get(str(path), {}).get("filter") == "lfs" or not (project_context.path / path).exists():
continue
relative_path = Path(path).relative_to(project_context.path) if path.is_absolute() else path
if (
path.is_dir()
and not renku_lfs_ignore().match_file(relative_path)
and not any(renku_lfs_ignore().match_tree(str(relative_path)))
):
track_paths.append(str(path / "**"))
elif not renku_lfs_ignore().match_file(str(relative_path)):
file_size = os.path.getsize(str(os.path.relpath(project_context.path / path, os.getcwd())))
if file_size >= get_minimum_lfs_file_size():
track_paths.append(str(relative_path))
if track_paths:
try:
result = run_command(
_CMD_STORAGE_TRACK,
*track_paths,
stdout=PIPE,
stderr=STDOUT,
cwd=project_context.path,
universal_newlines=True,
)
if result and result.returncode != 0:
raise errors.GitLFSError(f"Error executing 'git lfs track: \n {result.stdout}")
except (KeyboardInterrupt, OSError) as e:
raise errors.ParameterError(f"Couldn't run 'git lfs':\n{e}")
show_message = get_value("renku", "show_lfs_message")
if track_paths and (show_message is None or show_message.lower() == "true"):
files_list = "\n\t".join(track_paths)
communication.info(
f"Adding these files to Git LFS:\n\t{files_list}"
"\nTo disable this message in the future, run:\n\trenku config set show_lfs_message false"
)
return track_paths
@check_external_storage_wrapper
def untrack_paths_from_storage(*paths: Union[Path, str]) -> None:
"""Untrack paths from the external storage."""
try:
result = run_command(
_CMD_STORAGE_UNTRACK,
*paths,
stdout=PIPE,
stderr=STDOUT,
cwd=project_context.path,
universal_newlines=True,
)
if result and result.returncode != 0:
raise errors.GitLFSError(f"Error executing 'git lfs untrack: \n {result.stdout}")
except (KeyboardInterrupt, OSError) as e:
raise errors.ParameterError(f"Couldn't run 'git lfs':\n{e}")
@check_external_storage_wrapper
def list_tracked_paths() -> List[Path]:
"""List paths tracked in lfs."""
try:
files = check_output(_CMD_STORAGE_LIST, cwd=project_context.path, encoding="UTF-8")
except (KeyboardInterrupt, OSError) as e:
raise errors.ParameterError(f"Couldn't run 'git lfs ls-files':\n{e}")
files_split: List[Path] = [project_context.path / f for f in files.splitlines()]
return files_split
@check_external_storage_wrapper
def list_unpushed_lfs_paths(repository: "Repository") -> List[Path]:
"""List paths tracked in lfs for a repository."""
if len(repository.remotes) < 1 or (repository.active_branch and not repository.active_branch.remote_branch):
raise errors.GitConfigurationError(
f"No git remote is configured for {project_context.path} branch "
+ f"{repository.active_branch.name}." # type: ignore
+ "Cleaning the storage cache would lead to a loss of data as "
+ "it is not on a server. Please see "
+ "https://www.atlassian.com/git/tutorials/syncing for "
+ "information on how to sync with a remote."
)
try:
status = check_output(_CMD_STORAGE_STATUS, cwd=project_context.path, encoding="UTF-8")
except (KeyboardInterrupt, OSError) as e:
raise errors.ParameterError(f"Couldn't run 'git lfs status':\n{e}")
files = status.split("Objects to be committed:")[0].splitlines()[2:]
return [project_context.path / f.rsplit("(", 1)[0].strip() for f in files if f.strip()]
@check_external_storage_wrapper
def pull_paths_from_storage(repository: "Repository", *paths: Union[Path, str]):
"""Pull paths from LFS."""
project_dict = defaultdict(list)
for path in expand_directories(paths):
sub_repository, _, path = get_in_submodules(repository, repository.head.commit, path)
try:
absolute_path = Path(path).resolve()
relative_path = absolute_path.relative_to(project_context.path)
except ValueError: # An external file
continue
project_dict[sub_repository.path].append(shlex.quote(str(relative_path)))
for project_path, file_paths in project_dict.items():
result = run_command(
_CMD_STORAGE_PULL,
*file_paths,
separator=",",
cwd=project_path,
stdout=PIPE,
stderr=STDOUT,
universal_newlines=True,
)
if result and result.returncode != 0:
raise errors.GitLFSError(f"Cannot pull LFS objects from server:\n {result.stdout}")
@check_external_storage_wrapper
def clean_storage_cache(*check_paths: Union[Path, str]) -> Tuple[List[str], List[str]]:
"""Remove paths from lfs cache."""
project_dict = defaultdict(list)
repositories: Dict[Path, "Repository"] = {}
tracked_paths: Dict[Path, List[Path]] = {}
unpushed_paths: Dict[Path, List[Path]] = {}
untracked_paths: List[str] = []
local_only_paths: List[str] = []
repository = project_context.repository
for path in expand_directories(check_paths):
current_repository, _, path = get_in_submodules(repository=repository, commit=repository.head.commit, path=path)
try:
absolute_path = Path(path).resolve()
relative_path = absolute_path.relative_to(project_context.path)
except ValueError: # An external file
continue
if project_context.path not in tracked_paths:
tracked_paths[project_context.path] = list_tracked_paths()
if project_context.path not in unpushed_paths:
u_paths = list_unpushed_lfs_paths(current_repository)
unpushed_paths[project_context.path] = u_paths
if absolute_path in unpushed_paths[project_context.path]:
local_only_paths.append(str(relative_path))
elif absolute_path not in tracked_paths[project_context.path]:
untracked_paths.append(str(relative_path))
else:
project_dict[project_context.path].append(str(relative_path))
repositories[project_context.path] = current_repository
for project_path, paths in project_dict.items():
current_repository = repositories[project_path]
for path in paths:
with open(path) as tracked_file:
try:
header = tracked_file.read(len(_LFS_HEADER))
if header == _LFS_HEADER:
# file is not pulled
continue
except UnicodeDecodeError:
# likely a binary file, not lfs pointer file
pass
with tempfile.NamedTemporaryFile(mode="w+t", encoding="utf-8", delete=False) as tmp, open(
path, "r+t"
) as input_file:
result = run(_CMD_STORAGE_CLEAN, cwd=project_path, stdin=input_file, stdout=tmp, text=True)
if result.returncode != 0:
raise errors.GitLFSError(f"Error executing 'git lfs clean: \n {result.stdout}")
tmp_path = tmp.name
move(tmp_path, path)
# get lfs sha hash
old_pointer = current_repository.get_raw_content(path=path, revision="HEAD")
old_pointer = old_pointer.splitlines()[1]
old_pointer = old_pointer.split(" ")[1].split(":")[1]
prefix1 = old_pointer[:2]
prefix2 = old_pointer[2:4]
# remove from lfs cache
object_path = project_context.path / ".git" / "lfs" / "objects" / prefix1 / prefix2 / old_pointer
object_path.unlink()
# add paths so they don't show as modified
current_repository.add(*paths)
return untracked_paths, local_only_paths
@check_external_storage_wrapper
def checkout_paths_from_storage(*paths: Union[Path, str]):
"""Checkout a paths from LFS."""
result = run_command(
_CMD_STORAGE_CHECKOUT,
*paths,
cwd=project_context.path,
stdout=PIPE,
stderr=STDOUT,
universal_newlines=True,
)
if result and result.returncode != 0:
raise errors.GitLFSError(f"Error executing 'git lfs checkout: \n {result.stdout}")
def check_requires_tracking(*paths: Union[Path, str]) -> Optional[List[str]]:
"""Check paths and return a list of those that must be tracked."""
if not project_context.external_storage_requested:
return None
attrs = project_context.repository.get_attributes(*paths)
track_paths: List[str] = []
for path in paths:
absolute_path = Path(os.path.abspath(project_context.path / path))
path = str(path)
# Do not track symlinks in LFS
if absolute_path.is_symlink():
continue
# Do not add files with filter=lfs in .gitattributes
if attrs.get(path, {}).get("filter") == "lfs":
continue
if not absolute_path.is_dir():
if renku_lfs_ignore().match_file(path):
continue
if os.path.getsize(absolute_path) < get_minimum_lfs_file_size():
continue
track_paths.append(path)
return track_paths
def get_lfs_migrate_filters() -> Tuple[List[str], List[str]]:
"""Gets include, exclude and above filters for lfs migrate."""
def add_migrate_pattern(pattern, collection):
if pattern in RENKU_PROTECTED_PATHS:
return
pattern = pattern.strip()
if pattern.endswith("*"):
return
pattern = pattern.rstrip("/")
collection.append(f"{pattern}/**")
includes = []
excludes = []
for p in renku_lfs_ignore().patterns:
if p.regex is None:
continue
pattern = p.pattern.replace(os.linesep, "").replace("\n", "")
if pattern.startswith("!"):
pattern = pattern.replace("!", "", 1)
if p.include: # File ignored by LFS
excludes.append(pattern)
add_migrate_pattern(pattern, excludes)
else:
includes.append(pattern)
add_migrate_pattern(pattern, includes)
if excludes:
excludes = ["--exclude", ",".join(excludes)]
if includes:
includes = ["--include", ",".join(includes)]
return includes, excludes
def check_lfs_migrate_info(everything: bool = False, use_size_filter: bool = True) -> List[str]:
"""Return list of file groups in history should be in LFS."""
ref = (
["--everything"]
if everything or not project_context.repository.active_branch
else ["--include-ref", project_context.repository.active_branch.name]
)
includes, excludes = get_lfs_migrate_filters()
ignore_pointers = ["--pointers", "ignore"]
command = _CMD_STORAGE_MIGRATE_INFO + ref + includes + excludes
# NOTE: ``lfs migrate info`` supports ``--above`` while ``lfs migrate import`` doesn't.
if use_size_filter:
above = ["--above", str(get_minimum_lfs_file_size())]
command += above
try:
lfs_output = run(
command + ignore_pointers,
stdout=PIPE,
stderr=STDOUT,
cwd=project_context.path,
text=True,
)
except (KeyboardInterrupt, OSError) as e:
raise errors.GitError(f"Couldn't run 'git lfs migrate info':\n{e}")
if lfs_output.returncode != 0:
# NOTE: try running without --pointers (old versions of git lfs)
try:
lfs_output = run(command, stdout=PIPE, stderr=STDOUT, cwd=project_context.path, text=True)
except (KeyboardInterrupt, OSError) as e:
raise errors.GitError(f"Couldn't run 'git lfs migrate info':\n{e}")
if lfs_output.returncode != 0:
raise errors.GitLFSError(f"Error executing 'git lfs migrate info: \n {lfs_output.stdout}")
groups: List[str] = []
files_re = re.compile(r"(.*\s+[\d.]+\s+\S+).*")
for line in lfs_output.stdout.split("\n"):
match = files_re.match(line)
if match:
groups.append(match.groups()[0])
if groups and use_size_filter:
# NOTE: Since there are some large files, remove the size filter so that users get list of all files that
# will be moved to LFS.
return check_lfs_migrate_info(everything=everything, use_size_filter=False)
return groups
def migrate_files_to_lfs(paths: List[str]):
"""Migrate files to Git LFS."""
if paths:
includes: List[str] = ["--include", ",".join(paths)]
excludes: List[str] = []
else:
includes, excludes = get_lfs_migrate_filters()
command = _CMD_STORAGE_MIGRATE_IMPORT + includes + excludes
try:
lfs_output = run(command, stdout=PIPE, stderr=STDOUT, cwd=project_context.path, text=True)
except (KeyboardInterrupt, OSError) as e:
raise errors.GitError(f"Couldn't run 'git lfs migrate import':\n{e}")
if lfs_output.returncode != 0:
raise errors.GitLFSError(f"Error executing 'git lfs migrate import: \n {lfs_output.stdout}")