aboutsummaryrefslogtreecommitdiff
blob: 5d34eaef0c8031f77d41c0e65e76b3fdb3502d09 (plain)
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
'''
moudules/scan/module.py
Module loading and run list generator
'''

import logging
import os
import yaml

from portage.module import InvalidModuleName, Modules
from portage.util import stack_lists

MODULES_PATH = os.path.dirname(__file__)
# initial development debug info
logging.debug("module path: %s", MODULES_PATH)


class ModuleConfig(object):
	'''Holds the scan modules configuration information and
	creates the ordered list of modulles to run'''

	def __init__(self, configpaths):
		'''Module init

		@param configpaths: ordered list of filepaths to load
		'''
		self.configpaths = [os.path.join(path, 'repository.yaml') for path in configpaths]
		logging.debug("ModuleConfig; configpaths: %s", self.configpaths)

		self.controller = Modules(path=MODULES_PATH, namepath="repoman.modules.scan")
		logging.debug("ModuleConfig; module_names: %s", self.controller.module_names)

		self._configs = None
		self.enabled = []
		self.pkgs_loop = []
		self.ebuilds_loop = []
		self.final_loop = []
		self.modules_forced = ['ebuild', 'mtime']
		self.load_configs()
		for loop in ['pkgs', 'ebuilds', 'final']:
			logging.debug("ModuleConfig; Processing loop %s", loop)
			setattr(self, '%s_loop' % loop, self._determine_list(loop))
		self.linechecks = stack_lists(c['linechecks_modules'].split() for c in self._configs)

	def load_configs(self, configpaths=None):
		'''load the config files in order

		@param configpaths: ordered list of filepaths to load
		'''
		if configpaths:
			self.configpaths = configpaths
		elif not self.configpaths:
			logging.error("ModuleConfig; Error: No repository.yaml files defined")
		configs = []
		for path in self.configpaths:
			logging.debug("ModuleConfig; Processing: %s", path)
			if os.path.exists(path):
				try:
					with open(path, 'r') as inputfile:
						configs.append(yaml.safe_load(inputfile.read()))
				except IOError as error:
					logging,error("Failed to load file: %s", inputfile)
					logging.exception(error)
			logging.debug("ModuleConfig; completed : %s", path)
		logging.debug("ModuleConfig; new _configs: %s", configs)
		self._configs = configs

	def _determine_list(self, loop):
		'''Determine the ordered list from the config data and
		the moule_runsIn value in the module_spec

		@returns: list of modules
		'''
		lists = [c['scan_modules'].split() for c in self._configs]
		stacked = self.modules_forced + stack_lists(lists)
		mlist = []
		try:
			for mod in stacked:
				logging.debug("ModuleConfig; checking loop %s, module: %s, in: %s",
					loop, mod, self.controller.get_spec(mod, 'module_runsIn'))
				if loop in self.controller.get_spec(mod, 'module_runsIn'):
					mlist.append(mod)
		except InvalidModuleName:
			logging.error("ModuleConfig; unknown module: %s, skipping", mod)

		logging.debug("ModuleConfig; mlist: %s", mlist)
		return mlist