This repository has been archived by the owner on Apr 20, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache_manager.py
128 lines (111 loc) · 3.57 KB
/
cache_manager.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
def load_packages_from_file(binary_file):
"""Load packages list from cache.
Arguments:
binary_file -- cache file of packages
"""
import pickle
with open(binary_file, "rb") as binary_packages:
packages_loaded = pickle.load(binary_packages)
print("[LOG] {} Package load from {}".format(
len(packages_loaded), binary_file))
return packages_loaded
def get_package_info(info):
"""Get package description and url from info.
Arguments:
info -- The line to parse
"""
description = ""
url = ""
info = info.strip("\n").strip("# ")
precedent_character = ""
get_url = False
for character in info:
if not get_url and precedent_character == "-":
get_url = character == " "
if get_url:
continue
description += character
elif get_url:
url += character
else:
description += character
precedent_character = character
if not url:
url = "no url"
del precedent_character, get_url
return description[:-2], url
def load_packages_from_directory(directory, *ignore_file):
"""Load packages from scripts.
Arguments:
directory -- load cache of this directory
*ignore_file -- string name of files to ignore
"""
import glob
import os
packages_loaded = dict()
os.chdir(directory)
counter_packages = 1
for file in glob.glob("*.sh"):
package_name = file.split(".")[0]
package_description = ""
with open(file, "r") as open_file:
package_info = str(open_file.readline())
if package_info[0] == "#":
package_description, package_url = get_package_info(
package_info)
else:
package_description = package_url = package_name
if file in ignore_file:
print("File {} ignored".format(file))
else:
packages_loaded[counter_packages] = [
package_name,
package_description,
package_url,
str(directory + file)
]
counter_packages += 1
os.chdir("..")
return packages_loaded
def make_bin_from_packages(packages_list, file_name="packages.bin"):
"""Create binary file from list.
Arguments:
packages_list -- dictionary of packages to cache
*ignore_file -- string name of files to ignore
"""
import pickle
with open(file_name, "wb") as file:
pickle.dump(packages_list, file, protocol=0)
print("[LOG] {} Packages store into {}".format(
len(packages_list), file_name))
def get_packages(directory, *test):
"""Get packages.
Arguments:
directory -- directory where packages are
*test -- specification of file using in test
"""
import os
from_file = directory.strip("/") + ".bin"
if os.path.exists(from_file):
return load_packages_from_file(from_file)
else:
if os.path.exists(directory):
packages = load_packages_from_directory(directory, *test)
make_bin_from_packages(packages, from_file)
return packages
else:
print("[LOG] {} directory not found".format(directory))
return {}
def delete_cache(directory):
"""Delete binary packages cache.
Arguments:
directory -- delete the cache of this directory
"""
import os
try:
cache_file = directory.strip("/") + ".bin"
os.chdir(directory)
os.remove(cache_file)
return True
except Exception:
return False