aboutsummaryrefslogtreecommitdiff
blob: 1d7834fced1a9dc36812a786ca6675627c297d27 (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
#!/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

# dies if bad args
def parse_args():
  args_parser=optparse.OptionParser("%prog [options] <command>")
  args_parser.add_option("-b", "--block",action="store", type="string", 
	dest="packages", default="", 
	help="block an access to files from this packages")
  args_parser.add_option("-f","--files", action="store_true", dest="show_files", 
	default=False, help="show accessed files and not founded files")
  args_parser.add_option("-v","--verbose", action="store_true", dest="verbose", 
	default=False, help="show non-important packages, "
	  "show unknown package and unknown stage")
  args_parser.add_option("-C","--nocolor",action="store_true", dest="nocolor", 
	default=False, help="don't output color")

  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 -b 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

portage_api=portage_misc_functions.portage_api()
system_packages = portage_api.get_system_packages_list()

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())
#print package_utils.portage_log_parser.get_list_of_merged_packages(1244256830)

#quit(1)

options,args=parse_args()


color_printer=colorize_output.color_printer(not options.nocolor)


if args[0]=="emerge":
  runtime_vars["is_emerge"]=True
  emergeaction ,emergeopts, emergefiles=portage_api.parse_emerge_args(args[1:])
  runtime_vars["emerge_parameters"]=(emergeaction ,emergeopts, emergefiles)
  if len(emergefiles)>1:
	print "Please, install packages one by one to get more accurate reports"
else:
  runtime_vars["is_emerge"]=False
  

filter_function=lambda eventname,filename,stage: True

# handling --block
if 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
  filter_function=filter

# 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:
	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"
	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"])
	
	#print runtime_vars["deps_buildtime"]
	#print runtime_vars["deps_all"]
  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]

#print events_converted_for_output

# 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

  if package in system_packages and not options.verbose:
	continue

  is_attention_pkg=runtime_vars["is_emerge"] and package not in runtime_vars["deps_all"]
  

  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]=[was_readed,was_writed]
		  else:
			old_was_readed, old_was_writed=filenames[filename]
			filenames[filename]=[old_was_readed | was_readed, old_was_writed | was_writed ]

	if not is_attention_pkg:
	  color_printer.printmsg("text","[OK]")
	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={
		(False,False):"accessed",
		(True,False):"readed",
		(False,True):"writed",
		(True,True):"readed and writed"
	  }

	  for filename in filenames:
		event_info=tuple(filenames[filename])
		print "  %-56s %-21s" % (filename,action[event_info])

# 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])