-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
74 lines (62 loc) · 2.49 KB
/
utils.py
File metadata and controls
74 lines (62 loc) · 2.49 KB
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
import os
import subprocess
import pygit2
def is_overlayfs_mounted(directory):
with open('/proc/mounts', 'r') as mount_file:
for line in mount_file:
if 'overlay' in line and directory in line:
return True
return False
def is_ogit_mounted(config):
return os.path.exists(config["commit-graph-file-name"])
def is_ofs_nested(config):
merge_directory = config["ogit-working-directory-name"] + "/" + config["intermediate-merge-directory-prefix"] + "0"
with open('/proc/mounts', 'r') as mount_file:
for line in mount_file:
if 'overlay' in line and merge_directory in line:
return True
return False
def generate_layer_path(config, commit_hash):
layer_path = commit_hash[:2] + "/" + commit_hash[2:config["hash-length"]]
return layer_path
def get_current_commit_hash():
try:
with open("HEAD", 'r', encoding='utf-8') as head_file:
return head_file.read()
except Exception as e:
print(f'Error: not found HEAD file')
return None
def get_full_hash(config, short_hash):
repository = pygit2.Repository(config["repository-path"])
try:
commit = repository[short_hash]
return commit.id.__str__()
except KeyError:
print(f"commit not found: {short_hash}")
return None
# full hashじゃなくてもいいが,fullじゃないと大規模リポジトリではhash衝突する可能性もある
def restore_hash_from_layer_name(config, layer_name):
short_hash = layer_name[:2] + layer_name[3:config["hash-length"]+1]
# return get_full_hash(config, short_hash)
return short_hash
def umount(dir_name):
try:
cmd = ["sudo", "umount", dir_name]
subprocess.run(cmd, check=True, text=True)
except subprocess.CalledProcessError as e:
print(f'Error: {e}')
def umount_all(config):
if is_overlayfs_mounted(config["ogit-mount-directory-name"]):
umount(config["ogit-mount-directory-name"])
if not is_ofs_nested(config):
return
imd_dirs = os.listdir(config["ogit-working-directory-name"])
imd_dir_prefix = config["intermediate-merge-directory-prefix"]
imd_number = 0
while ((imd_dir_prefix + str(imd_number)) in imd_dirs) and imd_number < 500:
merge_directory = config["ogit-working-directory-name"] + "/" + imd_dir_prefix + str(imd_number)
if is_overlayfs_mounted(merge_directory):
umount(merge_directory)
else:
break
imd_number += 1