Pin pip to 20.3.3 and disable tmpfs in DIB
[ta/build-tools.git] / tools / package.py
1 # Copyright 2019 Nokia
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import yaml
16
17 from tools.statics import PACKAGES_CONFIG_PATH
18
19
20 class PackageConfigReader(object):
21     def __init__(self, config_file=PACKAGES_CONFIG_PATH):
22         self.config_file = config_file
23         self.config = None
24         self._read_file()
25
26     def _read_file(self):
27         with open(self.config_file, 'r') as fp:
28             self.config = yaml.load(fp)
29
30     def get(self, package_operation_type):
31         return self._get_packages_by_type(package_operation_type)
32
33     def _get_packages_by_type(self, package_operation_type):
34         if self.config is None:
35             return []
36         if package_operation_type == 'install':
37             return self._get_installed_packages()
38         elif package_operation_type == 'uninstall':
39             return self._get_uninstalled_packages()
40         raise Exception('Never here')
41
42     def _get_uninstalled_packages(self):
43         return sorted([p for p in self.config if not self._is_install_package(p)])
44
45     def _get_installed_packages(self):
46         return sorted([p for p in self.config if self._is_install_package(p)])
47
48     def _is_install_package(self, package):
49         if self.config[package] is None:
50             return True
51         if self.config[package].get('uninstall') is True:
52             return False
53         return True