aboutsummaryrefslogtreecommitdiff
blob: a9ac753bac2a1b45166ae8e2b295eb26b0fd8b8c (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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/python

import os
import re
import platform
import logging
import glob
from portage.output import bold, red, blue, yellow, green, nocolor

from stuff import scan
from collect import prepare_search_dirs, parse_revdep_config, collect_libraries_from_dir, collect_binaries_from_dir
from assign import assign_packages
from cache import save_cache
from settings import SETTINGS



def prepare_checks(files_to_check, libraries, bits):
	''' Calls scanelf for all files_to_check, then returns found libraries and dependencies
	'''

	libs = [] # libs found by scanelf
	dependencies = [] # list of lists of files (from file_to_check) that uses
					  # library (for dependencies[id] and libs[id] => id==id)

	for line in scan(['-M', str(bits), '-nBF', '%F %n'], files_to_check, SETTINGS['CMD_MAX_ARGS']):
	#call_program(['scanelf', '-M', str(bits), '-nBF', '%F %n',]+files_to_check).strip().split('\n'):
		r = line.strip().split(' ')
		if len(r) < 2: # no dependencies?
			continue

		deps = r[1].split(',')
		for d in deps:
			if d in libs:
				i = libs.index(d)
				dependencies[i].append(r[0])
			else:
				libs.append(d)
				dependencies.append([r[0],])
	return (libs, dependencies)


def extract_dependencies_from_la(la, libraries, to_check, logger):
	broken = []
	for f in la:
		if not os.path.exists(f):
			continue

		for line in open(f, 'r').readlines():
			line = line.strip()
			if line.startswith('dependency_libs='):
				m = re.match("dependency_libs='([^']+)'", line)
				if m is not None:
					for el in m.group(1).split(' '):
						el = el.strip()
						if len(el) < 1 or el.startswith('-'):
							continue

						if el in la or el in libraries:
							pass
						else:
							if to_check:
								_break = False
								for tc in to_check:
									if tc in el:
										_break = True
										break
								if not _break:
									continue

							logger.info(yellow(' * ') + f + ' is broken (requires: ' + bold(el))
							broken.append(f)
	return broken


def find_broken(found_libs, system_libraries, to_check):
	''' Search for broken libraries.
		Check if system_libraries contains found_libs, where
		system_libraries is list of obsolute pathes and found_libs
		is list of library names.
	'''

	# join libraries and looking at it as string is way too faster than for-jumping

	broken = []
	sl = '|'.join(system_libraries)

	if not to_check:
		for f in found_libs:
			if f+'|' not in sl:
				broken.append(found_libs.index(f))
	else:
		for tc in to_check:
			for f in found_libs:
				if tc in f:# and f+'|' not in sl:
					broken.append(found_libs.index(f))

	return broken


def main_checks(found_libs, broken, dependencies, logger):
	''' Checks for broken dependencies.
		found_libs have to be the same as returned by prepare_checks
		broken is list of libraries found by scanelf
		dependencies is the value returned by prepare_checks
	'''

	broken_pathes = []

	for b in broken:
		f = found_libs[b]
		logger.info('Broken files that requires: ' + bold(f))
		for d in dependencies[b]:
			logger.info(yellow(' * ') + d)
			broken_pathes.append(d)
	return broken_pathes


def analyse(logger=logging, libraries=None, la_libraries=None, libraries_links=None, binaries=None, _libs_to_check=set()):
	"""Main program body.  It will collect all info and determine the
	pkgs needing rebuilding.

	@param logger: logger used for logging messages, instance of logging.Logger
				   class. Can be logging (RootLogger).
	@param _libs_to_check Libraries that need to be checked only
	@rtype list: list of pkgs that need rebuilding
	"""

	if libraries and la_libraries and libraries_links and binaries:
		logger.info(blue(' * ') + bold('Found a valid cache, skipping collecting phase'))
	else:
		#TODO: add partial cache (for ex. only libraries) when found for some reason

		logger.warn(green(' * ') + bold('Collecting system binaries and libraries'))
		bin_dirs, lib_dirs = prepare_search_dirs(logger)

		masked_dirs, masked_files, ld = parse_revdep_config()
		lib_dirs = lib_dirs.union(ld)
		bin_dirs = bin_dirs.union(ld)
		masked_dirs = masked_dirs.union(set(['/lib/modules', '/lib32/modules', '/lib64/modules',]))

		logger.info(green(' * ') + bold('Collecting dynamic linking informations'))
		libraries, la_libraries, libraries_links, symlink_pairs = collect_libraries_from_dir(lib_dirs, masked_dirs, logger)
		binaries = collect_binaries_from_dir(bin_dirs, masked_dirs, logger)

		if SETTINGS['USE_TMP_FILES']:
			save_cache(to_save={'libraries':libraries, 'la_libraries':la_libraries, 'libraries_links':libraries_links, 'binaries':binaries})


	logger.debug('Found '+ str(len(libraries)) + ' libraries (+' + str(len(libraries_links)) + ' symlinks) and ' + str(len(binaries)) + ' binaries')

	logger.warn(green(' * ') + bold('Checking dynamic linking consistency'))
	logger.debug('Search for ' + str(len(binaries)+len(libraries)) + ' within ' + str(len(libraries)+len(libraries_links)))
	libs_and_bins = libraries+binaries

	#l = []
	#for line in call_program(['scanelf', '-M', '64', '-BF', '%F',] + libraries).strip().split('\n'):
		#l.append(line)
	#libraries = l

	found_libs = []
	dependencies = []


	_bits, linkg = platform.architecture()
	if _bits.startswith('32'):
		bits = 32
	elif _bits.startswith('64'):
		bits = 64

	for av_bits in glob.glob('/lib[0-9]*') or ('/lib32',):
		bits = int(av_bits[4:])
		_libraries = scan(['-M', str(bits), '-BF', '%F'], libraries+libraries_links, SETTINGS['CMD_MAX_ARGS'])
		#call_program(['scanelf', '-M', str(bits), '-BF', '%F',] + libraries+libraries_links).strip().split('\n')

		found_libs, dependencies = prepare_checks(libs_and_bins, _libraries, bits)

		broken = find_broken(found_libs, _libraries, _libs_to_check)
		broken_la = extract_dependencies_from_la(la_libraries, _libraries, _libs_to_check, logger)

		bits /= 2
		bits = int(bits)

	broken_pathes = main_checks(found_libs, broken, dependencies, logger)
	broken_pathes += broken_la

	logger.warn(green(' * ') + bold('Assign files to packages'))

	return assign_packages(broken_pathes, logger)



if __name__ == '__main__':
	print "This script shouldn't be called directly"