-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibchecker.py
134 lines (101 loc) · 4.01 KB
/
libchecker.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
#!/usr/bin/python3.6
import logging
import time
from pydoc import locate
from threading import Thread, Event
from packaging import version
from config import Config
from librariesio import LibraryInfoGetter
from mongodb import LatestLibraryInfo
class LibraryChecker():
"""
Class responsible for checking if a new
version of some library was released in
order to take one or more actions.
"""
def __init__(self, config):
"""
The class requires parameters defined in the configuration file.
"""
self.__config = config
self.__platform = Config.get_value(self.__config, "libraries_platform")
self.__name = Config.get_value(self.__config, "library_name")
logging.info("Library to be checked: %s. Platform: %s.",
self.__name, self.__platform)
api_key = Config.get_value(self.__config, "librariesio_api_key")
self.__getter = LibraryInfoGetter(api_key, self.__platform, self.__name)
mongodb_config = self.__config.get("mongodb", None)
self.__latest = LatestLibraryInfo(**mongodb_config)
def check(self):
"""
Checks if a new version was released. If so,
execute some previously defined actions.
Always store the latest version data into MongoDB.
"""
_id = self.get_id()
current_info = self.__getter.get()
latest_info = self.__latest.get(_id)
if self.__new_version_released(current_info, latest_info):
logging.info("New version released: %s. Previous version: %s.",
current_info["latest_release_number"],
latest_info["info"]["latest_release_number"])
actions_config = self.__config.get("actions", None)
for action_config in actions_config:
self.__execute_action(action_config, current_info, latest_info)
else:
logging.info("No new version released yet.")
if current_info:
library_info = {"_id": _id, "info": current_info}
self.__latest.set(library_info)
def get_id(self):
"""
Returns the libchecker id.
"""
return "%s_%s" % (self.__platform, self.__name)
def __new_version_released(self, current_info, latest_info):
"""
Returns if the current version is greater than the latest.
"""
if not current_info or not latest_info:
return False
current_version = current_info["latest_release_number"]
latest_version = latest_info["info"]["latest_release_number"]
return version.parse(current_version) > version.parse(latest_version)
def __execute_action(self, action_config, current_info, latest_info):
"""
Executes the pre-configured action.
The action class must be in the actions module
and must implement the method execute.
"""
action_classpath = "actions.%s" % Config.get_value(action_config, "classname")
action_class = locate(action_classpath)
parameters = action_config["parameters"]
action_instance = action_class(**parameters)
logging.info("%s about to be executed...", action_classpath)
action_instance.execute(self.__platform, self.__name, current_info, latest_info["info"])
class LibraryCheckerThread(Thread):
"""
Thread responsible for checking the release of a library every minute.
"""
def __init__(self, config):
"""
The thread's name is the libchecker's id.
"""
super().__init__()
self.__stopped = Event()
self.__checker = LibraryChecker(config)
self.name = self.__checker.get_id()
def stop(self):
"""
Stops the thread.
"""
self.__stopped.set()
self.join()
def run(self):
"""
Checks every minute if a release was made.
"""
while not self.__stopped.is_set():
self.__checker.check()
logging.info("Sleeping 1m")
time.sleep(60)