forked from westerndigitalcorporation/stat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stat_configuration.py
64 lines (49 loc) · 2.27 KB
/
stat_configuration.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
#!/usr/bin/env python
# SPDX-FileCopyrightText: (c) 2020 Western Digital Corporation or its affiliates,
# Arseniy Aharonov <[email protected]>
#
# SPDX-License-Identifier: MIT
import os
import re
import stat_attributes as attributes
from services import toPosixPath, readTextFileAtOnce, listMakefiles, Configuration, SingletonMeta, meta_class
from stat_makefile import StatMakefile
def calculateConfigurationVersion():
value = str(os.stat(attributes.CONFIG_FILENAME).st_mtime) if os.path.isfile(attributes.CONFIG_FILENAME) else None
return value
class StatConfiguration(meta_class(SingletonMeta, Configuration)):
def __init__(self):
super(StatConfiguration, self).__init__()
self.update(TOOL_VERSION=attributes.VERSION,
OUTPUT_DIR=attributes.OUTPUT_DIRECTORY,
STAT_ROOT=toPosixPath(os.path.relpath(attributes.TOOL_PATH)),
DUMMIES_DIR=attributes.DUMMIES_DIRECTORY)
self.__products = [item[:-4] for item in listMakefiles(attributes.PRODUCT_DIRECTORY)]
self.__autoGenerated = {}
self.__readUserConfiguration()
self.__readAutoGenerated()
def __readUserConfiguration(self):
if os.path.isfile(attributes.CONFIG_FILENAME):
self.update(StatMakefile(attributes.CONFIG_FILENAME))
def __readAutoGenerated(self):
if os.path.isfile(attributes.AUTO_GENERATED_MAKEFILE):
text = readTextFileAtOnce(attributes.AUTO_GENERATED_MAKEFILE)
self.__autoGenerated.update(re.findall(r'\s*(\w+)\s*=\s*(.+)\s*', text))
@property
def products(self):
return self.__products
@property
def defaultProduct(self):
product = self.__autoGenerated.get('PRODUCT_FLAVOR', None)
return product if product in self.products else None
def isStale(self):
return self.defaultProduct is None \
or self.__autoGenerated.get('TOOL_VERSION', None) != attributes.VERSION \
or self.__autoGenerated.get('CONFIG_VERSION', None) != calculateConfigurationVersion()
class StatConfigurationException(Exception):
"""
Custom exception for STAT services
"""
RESOURCE_NOT_FOUND = "No resource by filename '{0}' was found."
if __name__ == '__main__':
pass