aboutsummaryrefslogtreecommitdiff
blob: 034c47a7aee09ecf4571ccacf34d9848b7d4cd83 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/env python2

import optparse

import os
import sys
import time

import logfs.fstracer
from helpers import colorize_output, events_analysis
from package_utils import portage_utils, portage_misc_functions, portage_log_parser

def parse_args():
  """
	This function parses an input args
	It dies if bad args passed
  """
  
  args_parser=optparse.OptionParser("%prog [options] <command>")
  args_parser.add_option("-b","--block",action="store_true",  
	dest="strict_block", default="", 
	help="strict mode: Deny all access to files from non-dependency packages.")
  args_parser.add_option("--blockpkgs",action="store", type="string", 
	dest="packages", default="", 
	help="Block access to files from these packages.")
  args_parser.add_option("-f","--files", action="store_true", dest="show_files", 
	default=False, help="Show all files, accessed and missing ( not founded).")
  args_parser.add_option("-v","--verbose", action="store_true", dest="verbose", 
	default=False, help="Show non-important packages, "
	  "unknown package and unknown stage")
  args_parser.add_option("-n","--numfiles",action="store", type="string", 
    dest="numfiles", 
    default=10, 
	help="Maximum number of files from each package to show (default is 10)")
  args_parser.add_option("-C","--nocolor",action="store_true", dest="nocolor", 
	default=False, help="Don't colorize output")

  args_parser.add_option("--hooklib",action="store_const", dest="approach", 
	const="hooklib", help="Use ld_preload logging approach (default)")
  args_parser.add_option("--fusefs",action="store_const", dest="approach", 
	const="fusefs", help="Use FUSE logging approach (slow, but reliable)")
  args_parser.set_defaults(approach="hooklib")

  args_parser.epilog="Example: %s -blockpkgs lsof,cowsay emerge bash" % (
	os.path.basename(sys.argv[0]))
  args_parser.disable_interspersed_args()
  (options, args) = args_parser.parse_args()
  if len(args)==0:
	args_parser.print_help()
	exit(1) 

  return options,args

def init_environment():
  portage_api=portage_misc_functions.portage_api()
  system_packages = portage_api.get_system_packages_list()

  return portage_api, system_packages

def init_runtime_vars(portage_api, options,args):
  runtime_vars={} # This is here mainly for grouping. We are trying to 
				  # get as much data about an environment as possible
				  
  runtime_vars["starttime"]=int(time.time())

  # trivial check for emerge proccess
  if os.path.basename(args[0])=="emerge":
	runtime_vars["is_emerge"]=True
	emergeaction ,emergeopts, emergefiles=portage_api.parse_emerge_args(args[1:])
	runtime_vars["raw_emerge_parameters"]=args[1:]	
	runtime_vars["emerge_parameters"]=(emergeaction ,emergeopts, emergefiles)
	runtime_vars["mergelist"]=portage_api.get_merge_list(
	  runtime_vars["raw_emerge_parameters"])

	if len(emergefiles)>1:
	  print "Please, install packages one by one to get more accurate reports"

	if len(runtime_vars["mergelist"])==0:
	  print "No packages will be installed or there is an error with counting"

  else:
	runtime_vars["is_emerge"]=False
	runtime_vars["deps_all"]=[]
	
	# find a full path to the program
	program_path=None
	program_name=args[0]
	def is_exe(fpath):
	  return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

	fpath, fname = os.path.split(program_name)
	if fpath:
	  if is_exe(program_name):
		program_path=program_name
	else:
	  for path in os.environ["PATH"].split(os.pathsep):
		exe_file = os.path.join(path, program_name)
		if is_exe(exe_file):
		  program_path=exe_file
	
	if program_path==None:
	  print("Failed to find the program %s. Check its existance." 
		% program_name)
	  exit(1)

	runtime_vars["full_path"]=program_path


  return runtime_vars

def get_filter_function(options,args,system_packages,portage_api):
  # handling --block
  # exits if package name is bad
  if not options.packages and not options.strict_block:
	return lambda eventname,filename,stage: True
  elif not options.strict_block and options.packages:
	packages=options.packages.split(",")
	files_to_block=[]
	for package in packages:
	  files_in_package=portage_utils.getfilesbypackage(package)
	  if len(files_in_package)==0:
		print "Bad package name: %s. Exiting" % package
		exit(1)
	  files_to_block+=files_in_package
	files_to_block={}.fromkeys(files_to_block)
	# new filter function
	def filter(eventname,filename,stage):
	  return not filename in files_to_block
	return filter
  #elif options.strict_block and options.packages:
	#print "You can't use --block and --blockpkgs options togeter\n"
	#exit(1)
  elif options.strict_block: # this option is very strict
							 # because blocking logic is complex
	
	print "Building a list of files to block. This may take some time"
	# we get list of all files and substract list of allowed files from it
	allfiles=portage_utils.get_all_packages_files()
	allowedpkgs=[]
	allowedpkgs+=system_packages
	if runtime_vars["is_emerge"]: # blocking logic for emerge
	  print "Notice: you can use emerge_strict command instead of autodep emerge."
	  print "Using this command allows to build any number of a packages.\n"

	  # including all dependencies of portage
	  allowedpkgs+=list(portage_api.get_dep("portage",["RDEPEND"]))
	  (emergeaction ,emergeopts, emergefiles)=runtime_vars["emerge_parameters"]
	  if len(emergefiles)>1:
		print "You can't install several packages with option -b"
		exit(1)
	  if len(runtime_vars["mergelist"])!=1:
		print "You can't install several packages with option -b."
		print "Emerge tried to install several packages: %s. " % runtime_vars["mergelist"]
		print "You can force emerge to merge a package without any other "\
			   "packages with emerge --nodeps option or you can install these "\
			   "packages first"

		exit(1)
	  pkg=runtime_vars["mergelist"][0]
	  depslist=list(portage_api.get_deps_for_package_building(pkg))
	  allowedpkgs+=depslist
	  # manually add all python interpreters to this list
	  allowedpkgs+=["python"]
	else:
	  program_path=runtime_vars["full_path"]
	  file_to_package=portage_utils.getpackagesbyfiles([program_path])
	  if program_path not in file_to_package:
		print "Failed to find a package for %s" % program_path
	  else:
		allowedpkgs+=[file_to_package[program_path]]
		depslist=list(
		  portage_api.get_deps(file_to_package[program_path],["RDEPEND"]))
		allowedpkgs+=depslist
		
	print allowedpkgs
	
	# remember the allowedpkgs in deps_all. It is bad to do it here.
	runtime_vars["deps_all"]=allowedpkgs
	
	allowedfiles=[]
	for pkg in allowedpkgs:
	  allowedfiles+=portage_utils.getfilesbypackage(pkg)
 
	allowedfiles=set(allowedfiles)
	
	deniedfiles=allfiles-allowedfiles
	
	print "The list size is about %dM" % (int(sys.getsizeof(deniedfiles))/1024/1024)
	def filter(eventname,filename,stage):
	  if filename in deniedfiles:
		return False
	  return True
	return filter
	
portage_api, system_packages=init_environment()
options,args=parse_args()
runtime_vars=init_runtime_vars(portage_api,options,args)

color_printer=colorize_output.color_printer(not options.nocolor)

filter_function=get_filter_function(options,args,system_packages,portage_api)

# launching program
events=logfs.fstracer.getfsevents(args[0], args,approach=options.approach,filterproc=filter_function)
runtime_vars["endtime"]=int(time.time())
print "Program finished, analyzing dependencies"

if runtime_vars["is_emerge"]:
  # try to get information about packages merged sucessfully
  #try:
  runtime_vars["deps_all"]=set([])
  runtime_vars["deps_buildtime"]=set([])
  for pkg in runtime_vars["mergelist"]:
	runtime_vars["deps_all"]=runtime_vars["deps_all"].union(
	  portage_api.get_deps_for_package_building(pkg))
	runtime_vars["deps_buildtime"]=runtime_vars["deps_buildtime"].union(
	  portage_api.get_dep(pkg,["DEPEND"]))
	  
  runtime_vars["deps_portage"]=portage_api.get_dep('portage',["RDEPEND"])
  
	  
	#pkgs=portage_log_parser.get_list_of_merged_packages(
	#	  runtime_vars["starttime"],runtime_vars["endtime"]
	#	 )
	#if len(pkgs) > 1:
	#  print "Several packages were installed. The report will be inaccurate"
	#elif len(pkgs)==0:
	#  print "None packages have been installed sucessfully. The report will be inaccurate"
	#runtime_vars["pkgs_installed"]=pkgs
	#runtime_vars["deps_buildtime"]=[]
	#runtime_vars["deps_all"]=[]
	#for pkg in pkgs:
	#  runtime_vars["deps_buildtime"]+=portage_api.get_deps(pkg,["DEPEND"])
	#  runtime_vars["deps_all"]+=portage_api.get_deps(pkg,["DEPEND","RDEPEND"])
  #except:
	#print "Non-critical error while parsing logfile of emerge"
	#runtime_vars["is_emerge"]=False # shutting down all emerge handling logic
  #pass

# get unique filenames
filenames=set()
for stage in events:
  succ_events=set(events[stage][0])
  fail_events=set(events[stage][1])
  filenames=filenames.union(succ_events)
  filenames=filenames.union(fail_events)
filenames=list(filenames)

file_to_package=portage_utils.getpackagesbyfiles(filenames)

# This part is completly unreadable. 
# It converting one complex struct(returned by getfsevents) to another complex
# struct which good for generating output.
#
# Old struct is also used during output

packagesinfo={}

for stage in sorted(events):
  succ_events=events[stage][0]
  fail_events=events[stage][1]
  
  for filename in succ_events:
	if filename in file_to_package:
	  package=file_to_package[filename]
	else:
	  package="unknown"
	  
	if not package in packagesinfo:
	  packagesinfo[package]={}
	stageinfo=packagesinfo[package]
	if not stage in stageinfo:
	  stageinfo[stage]={}
	  
	filesinfo=stageinfo[stage]
	if not filename in filesinfo:
	  filesinfo[filename]={"found":[],"notfound":[]}
	filesinfo[filename]["found"]=succ_events[filename]
	
  for filename in fail_events:
	if filename in file_to_package:
	  package=file_to_package[filename]
	else:
	  package="unknown"
	if not package in packagesinfo:
	  packagesinfo[package]={}
	stageinfo=packagesinfo[package]
	if not stage in stageinfo:
	  stageinfo[stage]={}
	  
	filesinfo=stageinfo[stage]
	if not filename in filesinfo:
	  filesinfo[filename]={"found":[],"notfound":[]}
	filesinfo[filename]["notfound"]=fail_events[filename]

# generating output
stagesorder={"clean":1,"setup":2,"unpack":3,"prepare":4,"configure":5,"compile":6,"test":7,
			 "install":8,"preinst":9,"postinst":10,"prerm":11,"postrm":12,"unknown":13}

# print information grouped by package	  
for package in sorted(packagesinfo):
  # not showing special directory package
  if package=="directory":
	continue
  
  if package=="unknown" and not options.verbose:
	continue


  is_pkg_in_dep=package in runtime_vars["deps_all"]
  is_pkg_in_system=package in system_packages
  is_pkg_in_portage_dep=runtime_vars["is_emerge"] and package in runtime_vars["deps_portage"]
  is_pkg_python="dev-lang/python" in package

  stages=[]
  for stage in sorted(packagesinfo[package].keys(), key=stagesorder.get):
	if stage!="unknown" or options.verbose or not runtime_vars["is_emerge"]:
	  stages.append(stage)

  if len(stages)!=0:
	filenames={}
	for stage in stages:
	  for filename in packagesinfo[package][stage]:
		if len(packagesinfo[package][stage][filename]["found"])!=0:
		  was_readed,was_writed=packagesinfo[package][stage][filename]["found"]
		  if not filename in filenames:
			filenames[filename]=['ok',was_readed,was_writed]
		  else:
			status, old_was_readed, old_was_writed=filenames[filename]
			filenames[filename]=[
			  'ok',old_was_readed | was_readed, old_was_writed | was_writed 
			]
		if len(packagesinfo[package][stage][filename]["notfound"])!=0:
		  was_notfound,was_blocked=packagesinfo[package][stage][filename]["notfound"]
		  if not filename in filenames:
			filenames[filename]=['err',was_notfound,was_blocked]
		  else:
			status, old_was_notfound, old_was_blocked=filenames[filename]
			filenames[filename]=[
			  'err',old_was_notfound | was_notfound, old_was_blocked | was_blocked 
			]
		  

	if is_pkg_in_dep:
	  color_printer.printmsg("text","[OK]")
	elif is_pkg_in_system:
	  color_printer.printmsg("text","[SYSTEM]")	  
	elif is_pkg_in_portage_dep:
	  color_printer.printmsg("text","[PORTAGE DEP]")	  
	elif is_pkg_python:
	  color_printer.printmsg("text","[INTERPRETER]")	  
	elif not events_analysis.is_package_useful(package,stages,filenames.keys()):
	  color_printer.printmsg("text","[LIKELY OK]")
	else:
	  color_printer.printmsg("warning","[NOT IN DEPS]")
	# show information about accessed files

	print "%-40s: %s"%(package,stages)
#	if options.show_files:

	# this is here for readability
	action={
	  ('ok',False,False):"accessed",
	  ('ok',True,False):"readed",
	  ('ok',False,True):"writed",
	  ('ok',True,True):"readed and writed",
	  ('err',False,False):"other error",
	  ('err',True,False):"not found",
	  ('err',False,True):"blocked",
	  ('err',True,True):"not found and blocked"
	}
	
	filescounter=0
	
	for filename in filenames:
	  event_info=tuple(filenames[filename])
	  print "  %-56s %-21s" % (filename,action[event_info])
	  filescounter+=1
	  if options.show_files:
		continue
	  elif filescounter>options.numfiles:
		print "  ... and %d more ...\n" % (len(filenames)-10)
		break

# print not founded files with stages
if options.show_files:
  filenames={}
  print "\nNot founded files:"
  for stage in sorted(events, key=stagesorder.get):
	print "%s:" % stage
	
	action={
	  (True,False):"file not found",
	  (True,True):"blocked and not found",
	  (False,True):"blocked",
	  (False,False):"other error"
	}

	fail_events=events[stage][1]
	
	for filename in sorted(fail_events, key=file_to_package.get):
	  reason=tuple(fail_events[filename])
	  print "  %-56s %-21s" % (filename,action[reason])