Fix displaying of exceptions to console
[portpeek.git] / portpeek
1 #!/usr/bin/python
2 #
3 # Copyright 2006 Mike Pagano
4 # Distributed under the terms of the GNU General Public License v2
5 #
6 # $Header$
7 # Author: Mike Pagano <mpagano@gentoo.org>
8 #
9 # Portions written ripped from 
10 # - equery, by Karl Trygve Kalleberg <karltk@gentoo.org>
11 # - gentoolkit.py, by Karl Trygve Kalleberg <karltk@gentoo.org>
12 # - portage.py
13 # - emerge
14 #
15 #
16
17 __author__ = "Michael Pagano"
18 __email__ = "mpagano@gentoo.org"
19 __version__ = "2.0.19"
20 __productname__ = "portpeek"
21 __description__ = "Displays user unmasked ebuilds and installable options from the portage tree"
22
23 import sys, os, portage.output, fileinput, re, gentoolkit
24 from gentoolkit.versionmatch import VersionMatch,errors
25 from portage.const import USER_CONFIG_PATH
26 from portage.versions import catpkgsplit,pkgcmp,pkgsplit
27 from portage.exception import InvalidAtom
28 from gentoolkit.cpv import CPV
29 from gentoolkit.query import Query
30 from gentoolkit.flag import get_flags
31
32 # support python 2
33 try:
34     input = raw_input
35 except NameError:
36     pass
37
38 porttree = portage.db[portage.root]["porttree"]
39 settings = portage.config(clone=portage.settings)
40
41 show_changes_only_flag = False
42 checking_package_unmask = False
43 checking_package_mask = False
44 print_overlay_flag = False
45 info = 0
46 debug = 1
47 logLevel = info
48 show_removable_only_flag = False
49 stable_list = []
50 stable_listNg = [] # handle package.accept_keywords
51 unmask_list = []
52 tilde = 0
53 processing_package_use = False
54 using_gentoo_as_overlay = False
55 overlay_list = []
56 fix_confirm = True
57 fix_asked = False
58 use_flag_dict = {}
59 useremove_display = ""
60 invalid_flag_found = False
61
62 try:
63     PORTAGE_CONFIGROOT
64 except NameError:
65     PORTAGE_CONFIGROOT="/"
66
67 USER_CONFIG_PATH=PORTAGE_CONFIGROOT + USER_CONFIG_PATH
68
69 #parameters
70 options = [
71 "--keyword",
72 "--unmask",
73 "--mask",
74 "--all",
75 "--changes-only",
76 "--version",
77 "--version",
78 "--help",
79 "--removable-only",
80 "--debug",
81 "--fix", 
82 "--tilde-check",
83 "--no-color",
84 "--package-use",
85 "--fix-confirm"
86 ]
87
88 mappings = {
89 "k":"--keyword",
90 "u":"--unmask",
91 "m":"--mask",
92 "a":"--all",
93 "c":"--changes-only",
94 "V":"--version",
95 "v":"--version",
96 "h":"--help",
97 "r":"--removable-only",
98 "d":"--debug",
99 "f":"--fix", 
100 "t":"--tilde-check",
101 "n":"--no-color",
102 "s":"--package-use",
103 "q":"--fix-confirm"
104 }
105
106 cmdline = []
107 overlays = [settings["PORTDIR_OVERLAY"]]
108
109 def print_usage():
110     # Print full usage information for this tool to the console.
111     print ("\nUsage: " + portage.output.turquoise(__productname__) +  portage.output.yellow(" command "))
112     print ("       " + portage.output.turquoise(__productname__) + portage.output.green(" [ options ]") +  portage.output.yellow(" command "))
113     print ("       " + portage.output.turquoise(__productname__) + portage.output.green(" [-c]") + portage.output.yellow(" [akmu]"))
114     print ("       " + portage.output.turquoise(__productname__) + portage.output.green(" [-r]") + portage.output.yellow(" [akmu]"))
115     print ("       " + portage.output.turquoise(__productname__) + portage.output.green(" [-f]") + portage.output.yellow(" [akmu]"))
116     print ("       " + portage.output.turquoise(__productname__) + portage.output.green(" [-F]") + portage.output.yellow(" [akmu]"))
117     print (portage.output.yellow(" command ") + " can be ")
118     print (portage.output.yellow(" -a, --all") + "       - show all matches")
119     print (portage.output.yellow(" -k, --keyword") + "       - show matches from package.keywords and package.accept_keywords only")
120     print (portage.output.yellow(" -m, --mask") + "      - show matches from package.mask only")
121     print (portage.output.yellow(" -u, --unmask") + "        - show matched from package.unmask only")
122     print (portage.output.yellow(" -s, --package.use") + "   - show matches from package.use only")
123     
124     print (portage.output.yellow(" -f, --fix") + "       - will remove the stabled and invalid packages without asking for confirmation")
125     print (portage.output.yellow(" -q, --confirm-fix") +"    - will remove the stabled and invalid packages asking for confirmation before doing so")
126     print (portage.output.yellow(" -h, --help") + "      - display this message")
127     print (portage.output.yellow(" -d, --debug") + "         - display more verbose output for debugging")
128     print (portage.output.yellow(" -V, --version") + "       - display version info")
129     print (portage.output.green("options") + " are ")
130     print (portage.output.green(" -c, --changes-only") + \
131         "   - show all matches that have upgrade option, use with " + \
132         "<" + portage.output.yellow(" k ") + "|" + portage.output.yellow(" u ") + \
133         "|" + portage.output.yellow(" m ") + "|" + \
134         portage.output.yellow(" a ") + ">")
135     print (portage.output.green(" -n, --no-color") + \
136         "   - suppress color output")
137     print (portage.output.green(" -r, --removable-only") + \
138         "   - show all matches that can be removed from package files, use with " + \
139         "<" + portage.output.yellow(" k ") + "|" + portage.output.yellow(" u ") + \
140         "|" + portage.output.yellow(" m ") + "|" + \
141         portage.output.yellow(" a ") + ">\n")
142
143 def get_keywords(package, var):
144     mytree = porttree
145     filtered_keywords = ""
146     try:
147         keywords = package.environment("KEYWORDS").split()
148     except KeyError as error:
149         print ("!!! Portpeek caught Exception:" + format(error))
150         print ("!!! This package/version seems to be no longer available, " + \
151         "please check and update/unmerge it") 
152         return "Not Available/Deprecated"
153     
154     #filtered_keywords = filter_keywords(keywords[0])
155     filtered_keywords = filter_keywords(keywords)
156     return filtered_keywords
157
158
159
160 # this is the main function for portpeek
161 # TODO comment this code!
162 def parse_line(line, filename):     
163     global info,debug,using_gentoo_as_overlay
164
165     using_gentoo_as_overlay = False
166     pkgs = None
167     ebuild_output = ""
168     check_pkg = ""
169     not_installed_pkgs = 0
170     pkg_length = 0
171     atom_check="<>="
172     original_line = line
173
174     # if the line has special characters, we need to make sure the original line is used for matching
175     special_line = False
176
177     if ( (re.match('[><:\*]',line) != None) ):
178         special_line = True
179
180     print_output(debug,portage.output.blue("Analyzing line: " + line))
181         # determine if we are also check ~ prefixed code
182     if  (tilde == 1): 
183         atom_check="<>=~"
184
185     diffs_found = False
186     display_done = False
187
188     fields = line.replace("\t", " ").split(" ")
189
190     if len(fields) > 0:
191         #if the line does not start with an atom such as (= or >), do not validate 
192         #status as this tool is for check specific versions and not entire packages 
193         # a ~cpv should be handled like an =cpv if requested bythe parameter -t
194         check_pkg = fields[0] # this should be one of <>=~
195         overlay_index = check_pkg.find("::")
196         if ( overlay_index >= 0):
197             overlay_list = check_pkg.rsplit("::")
198             if (len(overlay_list) > 0):
199                 overlay_name = overlay_list[1]
200                 if (overlay_name == "gentoo"):
201                     using_gentoo_as_overlay = True
202             check_pkg = check_pkg[0:check_pkg.find("::")]
203
204         if check_pkg[0] not in atom_check:
205             return
206         if (tilde == 1):
207             if check_pkg[0] in "~":
208                 check_tilde_masked_pkg(check_pkg, filename)
209                 return
210
211         # determine if the package exists
212         try:
213             package_exists = portage.portdb.xmatch("match-all", fields[0])
214         except InvalidAtom:
215             package_exists = False
216
217         if package_exists:
218             # return a Package List based on the cpv
219             query = Query(check_pkg)
220             #query = Query(fields[0])
221             pkgs = []
222
223             try:
224                 pkgs = query.smart_find(True,True,True,True,False,True)
225             except errors.GentoolkitException as err:
226                 pass
227
228             if (pkgs != None):
229                 pkg_length = len(pkgs)
230                 not_installed_pkgs = 0
231                 display_done = False
232
233                                 # go through each package version for a specific version found above
234                 for current_package in pkgs:
235                     if not current_package.is_installed():
236
237                                                 # we have found a package that is in our file, but not installed
238                         not_installed_pkgs = not_installed_pkgs + 1
239
240                         # check to see if specific version of pkg is not installed 
241                         # and display if true
242                         check_pkg = fields[0]
243                         if check_pkg[0] in atom_check:
244                             check_pkg = check_pkg[1:]
245
246                         if (check_pkg == str(current_package.cpv)):
247                             if (not checking_package_mask):
248                                                                 # package is not instaleld
249                                 print_output(info,portage.output.green("\n" + str(current_package.cpv) + ": ") + portage.output.yellow("Not Installed") , current_package, filename)
250                                 if "package.keywords" in filename:
251                                     stable_list.append(str(current_package.cpv))
252                                 if "package.accept_keywords" in filename:
253                                     stable_listNg.append(str(current_package.cpv))
254                                 unmask_list.append(str(current_package.cpv))
255                             else:
256                                                                 # package is masked, and not installed, this is normal use of package.mask
257                                 print_output(info,portage.output.green("" + str(current_package.cpv) + ": ") + portage.output.yellow("Package Masked"),current_package, filename)
258                             display_done = True
259                         continue
260
261                                         # package is installed
262                                         # retrieve the keywords for a file
263                     keywords = "%s" % (get_keywords(current_package,"KEYWORDS").split())
264                     if (keywords.find("Available/Deprecated") >= 0):
265                         continue
266
267                                         #retrieve the mask status of a specific package
268                     pkgmask = _get_mask_status(current_package, False)
269
270                     #determine if installed package is unmasked, if so, display keywords as green
271                     stable = check_for_stable_release(current_package)
272
273                     # do not display if keywords don't exist
274                     if keywords == "[]":
275                         continue
276
277                     if stable:
278                         ebuild_output = portage.output.green("Installed: ") + \
279                             portage.output.turquoise(str(current_package.cpv)) + \
280                             portage.output.green("  Keywords " + keywords)
281                         if "package.unmask" in filename:
282                             unmask_list.append(str(current_package.cpv))
283                         if "package.keywords" in filename:
284                             stable_list.append(str(current_package.cpv))
285                         if "package.accept_keywords" in filename:
286                             stable_listNg.append(str(current_package.cpv))
287                     else:
288                         if (not show_removable_only_flag):
289                             ebuild_output = portage.output.green("Installed: ") + \
290                                 portage.output.turquoise(str(current_package.cpv)) + \
291                                 portage.output.yellow("  Keywords " + keywords) 
292                         else:
293                             ebuild_output = portage.output.yellow(str(current_package.cpv))
294
295                                         # check package.unmask
296                     if (checking_package_unmask):
297                         if (not is_pkg_package_masked(str(current_package.cpv))):
298
299                                                         # package is in package.unmask unnecessarily
300                             ebuild_output = ebuild_output + ": " + portage.output.yellow("Not package masked")
301                             if "package.unmask" in filename:
302                                 unmask_list.append(str(current_package.cpv))
303                                 print_output (info, "" +  ebuild_output,None, filename)
304                                 continue
305
306                                         # print once
307                     ebuild_search_key_printed = False
308                     if stable:
309                         diffs_found = False
310                         ebuild_search_key_printed = True
311                         print_output(info,"\n" + ebuild_output,current_package, filename)
312                     elif not show_changes_only_flag and not show_removable_only_flag:
313                         diffs_found = False
314                         ebuild_search_key_printed = True
315                         print_output(info,"\n" + ebuild_output,current_package)
316
317                                         # go through all versions of a package
318                     query = Query(current_package.category + "/" + current_package.name)
319
320                     all_pkgs = []
321
322                     try:
323                         all_pkgs = query.smart_find(True,True,True,True,False,True)
324                     except errors.GentoolkitException as err:
325                         print_output(debug,portage.output.blue("Package " + current_package.category + "/" + current_package.name + " not found."))
326
327                     for a_package in all_pkgs:
328                         if not a_package.is_installed():
329                                                         # a_package is not installed
330                             pkgmask = _get_mask_status(a_package, False)
331                                                         # print status line of package we are now checking
332                             print_output(debug,portage.output.blue("Checking package: " + str(a_package.cpv) +".pkgmask is " + str(pkgmask)))
333                                                         # if package versions are different
334                             if (VersionMatch(CPV(current_package.cpv)).match(CPV(a_package.cpv))):
335                                 diffs_found = True
336                                 keylist = a_package.environment("KEYWORDS")
337                                 keywords = "%s" % (filter_keywords(keylist)).split()
338                                 #if a_package is masked
339                                 if pkgmask > 0 and not show_removable_only_flag:
340                                     if show_changes_only_flag and not ebuild_search_key_printed:
341                                         print_output (info, "\n" +  ebuild_output, current_package)
342                                         ebuild_search_key_printed = True
343                                         check_for_stable_release(current_package)
344                                     if (pkgmask >= 3):
345                                         print_output (info,portage.output.red("Available: " + str(a_package.cpv) + " [M] Keywords: " + keywords),a_package)
346                                     else:
347                                         print_output (info,portage.output.brown("Available: " + str(a_package.cpv) + " Keywords: " + keywords),a_package)
348                                 else:
349                                     if show_changes_only_flag and not ebuild_search_key_printed:
350                                         print_output (info,"\n" + ebuild_output,current_package)
351                                         ebuild_search_key_printed = True
352                                         print_output(info,portage.output.green("Available: " + str(a_package.cpv) + " Keywords: " + keywords),a_package)
353                 #else:
354                     #print (portage.output.red ("\nCannot find package: " + check_pkg))
355
356                 # if package does not exist, and current_package is None
357                 # then make package using fields[0]
358
359                 # display if pkg/cat is not installed (missing version)
360                 if not_installed_pkgs ==  pkg_length:
361                     if not display_done:
362                         if (not checking_package_mask):
363                             print_output(info,portage.output.green("\n" + fields[0] + ": ") + portage.output.yellow("Not Installed"),current_package)
364                             if "package.keywords" in filename:
365                                 if (special_line == False):
366                                     stable_list.append(str(current_package.cpv))
367                                 else:
368                                     stable_list.append(original_line)
369                             if "package.accept_keywords" in filename:
370                                 if (special_line == False):
371                                     stable_listNg.append(str(current_package.cpv))
372                                 else:
373                                     stable_listNg.append(original_line)
374                             if (special_line == False):
375                                 unmask_list.append(str(current_package.cpv))
376                             else:
377                                 unmask_list.append(original_line)
378                         else:
379                             print_output (info,portage.output.green("\n" + str(current_package.cpv) + ": ") + portage.output.yellow("Package Masked"),current_package)
380         else:
381             diffs_found = True
382             print (portage.output.red ("\nPackage: " + fields[0] + " not found. Please check " + filename + " to validate entry"))
383             if "package.keywords" in filename:
384                 stable_list.append(fields[0])
385             if "package.accept_keywords" in filename:
386                 stable_listNg.append(fields[0])
387             unmask_list.append(fields[0])
388             show_all_versions(fields[0], filename)
389     current_package = ""
390
391     return diffs_found
392
393 # adding support for etc/portage/package.keywords/<whatever>/package.keywords
394 def get_recursive_info(filename):
395
396     # determine if filename is a directory
397     if os.path.isdir(filename):
398         # get listing of directory
399         filenames = os.listdir(filename)
400         for file_name in filenames:
401             get_recursive_info(filename+os.path.sep+file_name)
402     else:   
403         get_info(filename)
404
405 def get_info(filename):
406
407     diffs_found = False
408     no_file = False
409     filedescriptor = None
410
411     try:
412         filedescriptor = open(filename)
413         for line in filedescriptor.readlines():
414             line = line.strip()
415             if len(line) <= 0:
416                 continue
417             elif line.find("#") >= 0:
418                 # found '#' remove comment
419                 if (skipFile(line,filename)):
420                     return
421                 line = line[0:line.find("#")]
422                 line = line.strip()
423                 if len(line) <= 0:
424                     continue
425             if (processing_package_use == False):
426                 diffs_found = parse_line(line, filename)
427             else:
428                 # process package.use
429                 diffs_found = parse_package_use(line,filename)
430     except IOError:
431         print (portage.output.red("Could not find file " + filename))
432         no_file = True
433
434     if not diffs_found and no_file:
435         print (portage.output.brown("No ebuild options found."))
436
437     # close file
438     if (filedescriptor != None):
439         filedescriptor.close()
440
441 # parse the package.use file and look for packages
442 # not installed
443 def parse_package_use(line, filename):
444
445     global info,debug
446     pkgs = None
447     check_pkg = ""
448     pkg_length = 0
449     atom_check="<>="
450     any_version = False
451     has_atom = True
452
453     diffs_found = False
454     package_installed = False
455     fields = line.replace("\t", " ").split(" ")
456
457     if len(fields) > 0:
458         check_pkg = fields[0] # this could be one of <>=
459         if check_pkg[0] not in atom_check:
460             has_atom = False
461         else:
462             check_pkg = check_pkg[1:]
463
464
465         # if there is a wildcard, check to make sure at least one
466         # of the packages is installed
467         if check_pkg.find("/*") >= 0:
468             query = Query(check_pkg)
469             pkgs = []
470             try:
471                 pkgs = query.smart_find(True,True,True,True,False,True)
472             except errors.GentoolkitException as err:
473                 print_output(debug,portage.output.blue("parse_package_use: Package " + check_pkg + " not found."))
474                 return False
475
476             if (pkgs != None):
477                 pkg_length = len(pkgs)
478             for current_package in pkgs:
479                 # on wildcard scenario, return False if one package is installed
480                 if current_package.is_installed():
481                     check_useflags(current_package,line)
482                     return False
483         else:
484             # look for any version of check_pkg installed as there is 
485             # no version specified in package.use
486             package_exists = portage.portdb.xmatch("match-all", check_pkg)
487             if package_exists:
488                 # get all package versions
489                 query = Query(check_pkg)
490                 pkgs = []
491                 try:
492                     pkgs = query.smart_find(True,True,True,True,False,True)
493                 except errors.GentoolkitException as err:
494                     print_output(debug,portage.output.blue("Package " + check_pkg + " not found."))
495     
496                 if (pkgs != None):
497                     pkg_length = len(pkgs)
498     
499                     # go through each package version for a specific version found above
500                     if (has_atom == False):
501                         for current_package in pkgs:
502                             if current_package.is_installed():
503                                 check_useflags(current_package,line)
504                                 package_installed = True
505                     else: 
506                         # go through each package version for a specific version found above
507                         for current_package in pkgs:
508                             if (str(current_package.cpv) == check_pkg):
509                                 if not current_package.is_installed():
510                                     print_output(info,portage.output.green("\n" + check_pkg + ": ") + portage.output.yellow("Not Installed"),current_package)
511                                     if "package.keywords" in filename:
512                                         stable_list.append(check_pkg)
513                                     if "package.accept_keywords" in filename:
514                                         stable_listNg.append(check_pkg)
515                                     unmask_list.append(check_pkg)
516                                     return True
517                                 else:
518                                     check_useflags(current_package,line)
519                                     return False
520             else:
521                 print (portage.output.red ("\nPackage: " + fields[0] + " not found. Please check " + filename + " to validate entry"))
522                 if "package.keywords" in filename:
523                     stable_list.append(check_pkg)
524                 if "package.accept_keywords" in filename:
525                     stable_listNg.append(check_pkg)
526                 unmask_list.append(check_pkg)
527                 return True
528     if (package_installed == False):
529         # package does not exists
530         print_output(info,portage.output.green("\n" + check_pkg + ": ") + portage.output.yellow("Not Installed"),current_package)
531         if "package.keywords" in filename:
532             stable_list.append(check_pkg)
533         if "package.accept_keywords" in filename:
534             stable_listNg.append(check_pkg)
535         unmask_list.append(check_pkg)
536         return True
537
538     return False
539
540
541
542
543 # skip the file if portpeek-skip found in a comment
544 # you can put this in the middle of a file and it will
545 #skip all entrie below it, this is useful to speed things
546 #up by not checking ~kde versions, for example
547 def skipFile (line, filename):
548     if ( (line == None) or (filename == None)):
549         return False
550
551     if line.find("portpeek-skip") >= 0:
552         return True
553
554     return False
555
556 # parts blatantly stolen from equery
557 # if pure is true, then get "true" mask status that is
558 # not affected by entries in /etc/portage/package.*
559 def _get_mask_status(pkg, pure):
560     pkgmask = 0
561
562     if (pkg == None):
563         return 0
564
565     if pkg.is_masked():
566         pkgmask = pkgmask + 3
567
568     if pure:
569         try:
570             keywords = portage.portdb.aux_get(str(pkg.cpv), ["KEYWORDS"])
571             keywords = keywords[0].split()
572         except KeyError:
573             # cpv does not exist
574             return 0
575     else:
576         keywords = pkg.environment("KEYWORDS").split()
577
578     # first check for stable arch, stop there if it is found
579     if settings["ARCH"] in keywords:
580         return 0
581
582     if "~" + settings["ARCH"] in keywords:
583         pkgmask = pkgmask + 1
584     elif "-*" in keywords or "-" + settings["ARCH"] in keywords:
585         pkgmask = pkgmask + 2
586     
587     return pkgmask
588
589 def is_pkg_package_masked(cpv):
590
591     mysplit = catpkgsplit(cpv)
592     if not mysplit:
593         raise ValueError("invalid CPV: %s" % cpv)
594     if not portage.portdb.cpv_exists(cpv):
595         return False
596
597     mycp = mysplit[0] + "/" + mysplit[1]
598
599     pmaskdict = settings._mask_manager._pmaskdict
600     if mycp in pmaskdict:
601         for package in pmaskdict[mycp]:
602             if cpv in portage.portdb.xmatch("match-all", package):
603                 return True
604                 
605     return False    
606
607 # filter out keywords for archs other than the current one
608 def filter_keywords(keywords):
609     filtered_keywords = ""
610
611     #for key in key_list:
612     for key in keywords:
613         key = key.replace("[", '')
614         key = key.replace("]", "")
615         key = key.replace(",", "")
616         arch=settings["ARCH"]
617
618         # remove '~' from key for comparison
619         key_comparison = key.lstrip('~')
620         if key_comparison == arch:
621             if len(filtered_keywords) != 0:
622                 filtered_keywords = filtered_keywords + " "
623             filtered_keywords = filtered_keywords + key
624         elif "-*" in key:
625             if len(filtered_keywords) != 0:
626                 filtered_keywords = filtered_keywords + " "
627             filtered_keywords = filtered_keywords + key
628
629
630     return filtered_keywords
631
632 # check to see if we have a stable release
633 # in our package.* files that we can remove
634 def check_for_stable_release(pkg):
635     if not is_pkg_package_masked(str(pkg.cpv)):
636         status = _get_mask_status(pkg, True)
637         if status == 0:
638             return True
639     return False
640
641 #print version info
642 def print_version():
643     # Print the version of this tool to the console.
644     print (__productname__ + "(" + __version__ + ") - " + \
645         __description__)
646     print ("Author(s): " + __author__)
647
648 # function to go through a ~cp without a version
649 # and set for removal from file if no masked package version exists
650 def check_tilde_masked_pkg(package_name, filename):
651     ebuild_output=""
652     variable_version = ""
653
654     orig_package_name = package_name
655     package_name = package_name.replace('~','')
656
657     print_output(debug,portage.output.blue("check_tilde_maskd_pkg: orig_package-name is " + orig_package_name))
658     print_output(debug,portage.output.blue("check_tilde_maskd_pkg: package_name is " + package_name))
659
660     query = Query(package_name, True)
661
662     packages = []
663     try:
664         packages = query.smart_find(True,True,True,True,False,True)
665     except errors.GentoolkitException as err:
666         print_output(debug,portage.output.blue("Package " + package_name + " not found."))
667
668     no_versions_installed = True
669     for package in packages:
670         if package.is_installed():
671             no_versions_installed = False
672
673     if (no_versions_installed == True):
674         ebuild_output = portage.output.green("\n" + package_name + ": ") + portage.output.yellow("Not Installed")
675         if "package.unmask" in filename:
676             unmask_list.append(orig_package_name)
677         if "package.keywords" in filename:
678             stable_list.append(orig_package_name)
679         if "package.accept_keywords" in filename:
680             stable_listNg.append(orig_package_name)
681         print (ebuild_output + portage.output.brown(" : " + filename))
682
683         return
684
685     # get all packages matching cat/package
686     if (packages != None):
687         for current_package in packages:
688             print_output(debug,portage.output.blue("check_tilde_maskd_pkg: current_package is " + str(current_package.cpv)))
689             print_output(debug,portage.output.blue("comparing " + package_name + " to " + str(current_package.cpv)))
690             if (pkgcmp(pkgsplit(package_name),pkgsplit(str(current_package.cpv))) <=0 ):
691             #if (pkgcmp(package_name, str(current_package.cpv)) <= 0):
692                 packageObj = gentoolkit.package.Package(str(current_package.cpv))
693                 if (packageObj == None):
694                     # we could not create a package object
695                     return
696                 pkgmask = _get_mask_status(packageObj, True)
697
698                 print_output(debug,portage.output.blue("check_tilde_maskd_pkg: current_package is " + str(current_package.cpv) + " and pkgmask is " + str(pkgmask)))
699
700                 if "package.unmask" in filename:
701                     if (is_pkg_package_masked(str(current_package.cpv))):
702                         # package was found as masked
703                         return
704                     else:
705                         # package is not masked
706                         unmask_list.append(str(current_package.cpv))
707                         ebuild_output = portage.output.yellow(str(current_package.cpv)) + ": " + portage.output.yellow("Not package masked")
708                         print_output(info,ebuild_output, package, filename)
709                         return
710                 else:
711                     if (pkgmask >= 1):
712                         # package was found as masked
713                         return 
714                     else:
715                         # at this point we have no packages >= ~cpv that are masked, present for removal
716                         # package does not have any masked versions
717                         ebuild_output = portage.output.green(package_name + " has no masked versions")
718                         
719                         if "package.unmask" in filename:
720                             unmask_list.append(orig_package_name)
721                         if "package.keywords" in filename:
722                             stable_list.append(orig_package_name)
723                         if "package.accept_keywords" in filename:
724                             stable_listNg.append(orig_package_name)
725                         print_output(info,ebuild_output, package, filename)
726
727 #helper function to print avail pks when version does not exist
728 def show_all_versions(pkg, filename):
729
730     # is package masked
731     is_package_masked = False
732     pkgArr = portage.pkgsplit(pkg)
733
734     if pkgArr is None or len(pkgArr) == 0:
735         return
736
737     # determine if category/package is masked 
738     package = pkgArr[0]
739
740     operator = portage.dep.Atom(pkg).operator
741     if operator is not None:
742         package = package[len(operator):]
743
744     # package is category/package and pkg is category/package-version
745     # is category/package-version we are checking package masked?
746     #if portage.settings.pmaskdict.has_key(package):
747     pmaskdict = settings._mask_manager._pmaskdict
748     if package in pmaskdict:
749         pkg_list = pmaskdict.get(package)
750         # iterate through list array looking for pkg
751         for pkg_check in pkg_list:
752             operator = portage.get_operator(pkg_check)
753             if operator is None:
754                 if pkg_check == package:
755                     is_package_masked = True
756
757     query = Query(package)
758
759     all_pkgs = []
760
761     try:
762         all_pkgs = query.smart_find(True,True,True,True,False,True)
763     except errors.GentoolkitException as err:
764         print_output(debug,portage.output.blue("Package " + package + " not found."))
765
766     for current_package in all_pkgs:
767         keywords = "%s" % (current_package.environment("KEYWORDS").split())
768         keywords = filter_keywords(keywords)
769         keywords = "[%s]" % (keywords)
770         ebuild = current_package.ebuild_path()
771         if ebuild:
772             pkgmask = _get_mask_status(current_package, True)
773             if is_package_masked:
774                 print (portage.output.red("Available: " + str(current_package.cpv) + " [M] Keywords: " + keywords))
775             elif pkgmask > 4:
776                 print (portage.output.red("Available: " + str(current_package.cpv) + " [M] Keywords: " + keywords))
777             elif pkgmask == 4 or pkgmask == 1:
778                 print (portage.output.brown("Available: " + str(current_package.cpv) + " Keywords: " + keywords))
779             else:
780                 print (portage.output.green("Available: " + str(current_package.cpv) + " Keywords: " + keywords))
781                 if "package.keywords" in filename:
782                     stable_list.append(str(current_package.cpv))
783                 if "package.accept_keywords" in filename:
784                     stable_listNg.append(str(current_package.cpv))
785
786
787 def get_useflags(package):
788     iuse, final_use = get_flags(package.cpv, final_setting=True)
789     return iuse
790
791 def check_useflags(package,line):
792
793     global useremove_display, invalid_flag_found
794     invalid_flag_found = False
795
796     if ((package is None) or (package == "")):
797         return
798
799     useflag_removal_display=""
800
801     if (len(line) <= 0):
802         return
803
804     iuse = get_useflags(package)
805
806     useflags_fromfile = line.replace("\t", " ").split(" ")
807     package_string = useflags_fromfile.pop(0)
808
809     clean_useflags_list = []
810     # remove + or -
811     atom = "-+"
812
813     #clean list from portage of + or -
814     clean_iuse = []
815     for item in iuse:
816         if item[0] in atom:
817             clean_iuse.append(item[1:])
818         else:
819             clean_iuse.append(item)
820
821     valid_flag_list = []
822     for original_flag in useflags_fromfile:
823         flag = original_flag
824         if original_flag[0] in atom:
825             flag = original_flag[1:]
826         if flag not in clean_iuse:
827             print_output (info,portage.output.red("use flag: " + flag + " is invalid for : " + str(package.cpv)))
828             useflag_removal_display += "Removing use flag: " + flag + " for package " + str(package.cpv)
829             invalid_flag_found = True
830         else:
831             valid_flag_list.append(original_flag)
832
833     # if valid_flag_list is empty, there are no valid flags
834     if ( (len(valid_flag_list) > 0) and (len(useflag_removal_display) >0)):
835         useremove_display += useflag_removal_display + "\n"
836     elif ( len(valid_flag_list) == 0):
837         useremove_display += "No valid use flags found for package " + str(package.cpv) + ". Removing line: " + line + "\n"
838         if ( invalid_flag_found != True):
839             print_output (info,portage.output.red("No valid use flags found for package " + str(package.cpv)))
840         invalid_flag_found = True
841
842     valid_flag_list.insert(0,package_string);
843     if (invalid_flag_found == True):
844         use_flag_dict[line] = valid_flag_list
845
846     return valid_flag_list
847
848
849 def clean_useflagsFile(filename):
850
851     if "--fix-confirm" in cmdline:
852         if (confirmFix() == False):
853             return
854     
855     display_line = ""
856
857     try:
858         # determine if filename is a directory
859         if os.path.isdir(filename):
860             # get listing of directory
861             filenames = os.listdir(filename)
862             for file_name in filenames:
863                 cleanuseflagsFile(filename+os.path.sep+file_name)
864             return
865         else:
866             #go through stable array and remove line if found
867             for line in fileinput.input(filename,inplace =1):
868                 itemFound = False
869                 line = line.strip()
870
871                 # make sure line is not empty and do not remove commented out lines
872                 if len(line) <= 0:
873                     continue
874                 elif line.find("#") == 0:
875                     print (line)
876                     continue
877
878                 check_for_change = ""
879                 use_flag_dict.get(line,"")
880                 try:
881                     check_for_change = use_flag_dict[line]
882                     if ( len(check_for_change) > 1):
883                         print (" ".join(check_for_change))
884                 except KeyError as error:
885                     print (line)
886
887             fileinput.close()
888     except OSError as error:
889         print (portage.output.red("Modify/Read access to file: " + filename + " failed: " + format(error)))
890
891     if (len(useremove_display) > 0):
892         print (portage.output.red("\n" + useremove_display))
893
894     return
895
896 def handle_if_overlay(package):
897     overlay_text = ""
898     global print_overlay_flag,using_gentoo_as_overlay
899
900     if (using_gentoo_as_overlay):
901         return overlay_text
902
903     print_overlay_flag = True
904
905     ebuild_path,overlay_path = porttree.dbapi.findname2(str(package.cpv))
906     index = -1
907     try:
908         index = overlay_list.index(overlay_path)
909     except ValueError as error:
910         overlay_list.append(overlay_path)
911         index = overlay_list.index(overlay_path)
912
913     overlay_text = " [%s]" % (str(index+1))
914
915     return overlay_text
916
917 # if the overlay_text was displayed to the user
918 # we need to display the string at the end 
919 # this array will store the overlays to be displayed
920 def print_overlay_text():
921    
922     global print_overlay_flag
923     
924     if (not print_overlay_flag):
925         return
926     
927     if (len(overlay_list) <= 0):
928         return
929
930     index = 1
931     for x in overlay_list:
932         print (portage.output.turquoise("[" + str(index) + "] ") + x)
933         index = index + 1
934     
935     print ("\n")
936
937 #helper function to print output
938 def print_output(log_level,output_string, package=None, filename=None):
939     
940     global logLevel
941
942     if package != None:
943         if (package.is_overlay()):
944             output_string = "%s%s" % (output_string,portage.output.turquoise(handle_if_overlay(package)))
945             #output_string = output_string + portage.output.turquoise(handle_if_overlay(package))
946         else:
947             if filename != None:
948                 output_string = "%s%s" % (output_string,portage.output.brown(" : " + filename))
949                 #output_string = output_string + portage.output.brown(" : " + filename)
950
951     if (log_level <= logLevel):
952         print (output_string)
953
954 # remove stabled files that are no longer needed from package.keywords
955 # or package.mask
956 # includes support for etc/portage/package.keywords/<whatever>/package.keywords
957 def cleanFile (filename):
958
959     removeDups = []
960     removed_list = []
961
962     if "--fix-confirm" in cmdline:
963         if (confirmFix() == False):
964             return
965
966     # if the file or directory does not exist
967     # exit out
968     if (os.path.exists(filename) == False):
969         return
970     
971     if "package.keywords" in filename:
972         if ( len(stable_list) == 0):
973             return
974         removeDups = stable_list
975     elif "package.accept_keywords" in filename:
976         if ( len(stable_listNg) == 0):
977             return
978         removeDups = stable_listNg
979     else:
980         if ( len(unmask_list) == 0):
981             return
982         removeDups = unmask_list
983
984     removedDict = {}
985
986     try:
987         # determine if filename is a directory
988         if os.path.isdir(filename):
989             # get listing of directory
990             filenames = os.listdir(filename)
991             for file_name in filenames:
992                 cleanFile(filename+os.path.sep+file_name)
993             return
994         else:   
995             #go through stable array and remove line if found
996             for line in fileinput.input(filename,inplace =1):
997                 itemFound = False
998                 line = line.strip()
999
1000                 # make sure line is not empty and do not remove commented out lines
1001                 if len(line) <= 0:
1002                     continue
1003                 elif line.find("#") == 0:
1004                     print (line)
1005                     continue
1006
1007                 for item in removeDups:
1008                     if item in line:
1009                         removed_list.append(item)
1010                         removedDict[filename] = item
1011                         itemFound = True
1012                         removeDups.pop(removeDups.index(item))
1013                         break
1014                 if (itemFound == False):
1015                     print (line)
1016             fileinput.close()
1017     except OSError as error:
1018         print (portage.output.red("Modify/Read access to file: " + filename + " failed: " + format(error)))
1019     
1020     if (len(removed_list) > 0):
1021         print ("\n")
1022         for package in removed_list:
1023             print (portage.output.red("Removing from: ") + portage.output.yellow(filename) + ": " + portage.output.green(package) + "\n")
1024
1025 # ask the user if they want to fix their files
1026 # and remove unneeded entries
1027 # Return true if they say yes and False if they say no
1028 def confirmFix():
1029     
1030     global fix_asked,fix_confirm
1031
1032     if (fix_asked == True):
1033         return fix_confirm
1034
1035     # only ask if we actually have anything to check
1036     if ( (len(stable_list) == 0) and 
1037           (len(stable_listNg) == 0) and
1038           (len(unmask_list) == 0) and
1039           (invalid_flag_found == False)):
1040         fix_confirm = True
1041         return fix_confirm
1042
1043     fix_asked = True 
1044
1045     valid = {"yes":"yes",   "y":"yes", 
1046              "no":"no",     "n":"no"}
1047    
1048     prompt = portage.output.bold("Remove entries from files [") + portage.output.green("y") + "/" + portage.output.red("n") + portage.output.bold("]") + " "
1049
1050     while 1:
1051         sys.stdout.write('\n' + prompt)
1052         choice = input().lower()
1053         if choice == '':
1054             fix_confirm = False
1055             break
1056         elif choice in valid.keys():
1057             if (choice == 'y' or choice == 'yes'):
1058                 fix_confirm = True
1059                 break
1060             else:
1061                 fix_confirm = False
1062                 break 
1063         else:
1064             sys.stdout.write("Please respond with 'yes' or 'no' "\
1065                              "(or 'y' or 'n').\n")
1066
1067     return fix_confirm
1068
1069
1070 # main
1071 if __name__ == "__main__":
1072
1073     if len(sys.argv) == 1:
1074         print_usage()
1075         sys.exit(1)
1076
1077     # soooooo stolen from emerge
1078     tmpcmdline = sys.argv[1:]
1079
1080     for cmd in tmpcmdline:
1081         if cmd[0:1] == "-" and cmd[1:2] != "-":
1082             for cmd_item in cmd[1:]:
1083                 if cmd_item in mappings:
1084                     if mappings[cmd_item] in cmdline:
1085                         print ()
1086                         print ("*** Warning: Redundant use of ", mappings[cmd_item])
1087                     else:
1088                         cmdline.append(mappings[cmd_item])
1089                 else:
1090                     print ("!!! Error: -"+cmd_item+" is an invalid option.")
1091                     sys.exit(-1)
1092         else:
1093             cmdline.append(cmd)
1094
1095     #parse long options
1096     for cmd in cmdline:
1097         if len(cmd)>=2 and cmd[0:2]=="--":
1098             try:
1099                 i = options.index(cmd)
1100                 continue
1101             except ValueError:
1102                 print ("!!! Error: -"+cmd+" is an invalid option.")
1103                 sys.exit(-1)
1104
1105     if "--changes-only" in cmdline:
1106         cmdline.remove("--changes-only")
1107         show_changes_only_flag = True
1108
1109     if "--removable-only" in cmdline:
1110         cmdline.remove("--removable-only")
1111         show_removable_only_flag = True
1112
1113     if "--debug" in cmdline:
1114         logLevel = debug
1115
1116     if "--no-color" in cmdline:
1117         portage.output.nocolor()
1118
1119     if "--tilde-check" in cmdline:
1120         tilde=1
1121
1122     if "--version" in cmdline:
1123         print_version()
1124         sys.exit(0)
1125
1126     if "--fix-confirm" in cmdline:
1127         print ("here")
1128         if '--fix' not in cmdline:
1129             cmdline.append("--fix")
1130     
1131     if "--all" in cmdline:
1132         tmpcmdline = ["--all"]
1133         if "--fix" in cmdline:
1134             tmpcmdline.append("--fix")
1135             if "--fix-confirm" in cmdline:
1136                 tmpcmdline.append("--fix-confirm")
1137                 fix_confirm = False
1138         cmdline=tmpcmdline      
1139
1140     if "--help" in cmdline:
1141         print_usage()
1142         sys.exit(0)
1143     
1144     if (show_changes_only_flag and show_removable_only_flag):
1145         print ("Please select only one of --show-removable (-r) or --changes-only")
1146         print ("Use --help for more info.")
1147         sys.exit(0)
1148         
1149     for cmd in cmdline:
1150         if cmd == "--keyword":
1151             print (portage.output.bold("\npackage.keywords:"))
1152             get_recursive_info(USER_CONFIG_PATH + "/package.keywords")
1153             if "--fix" in cmdline:
1154                 cleanFile(USER_CONFIG_PATH + "/package.keywords")
1155             print (portage.output.bold("Done\n"))
1156             print (portage.output.bold("\npackage.accept_keywords:"))
1157             get_recursive_info(USER_CONFIG_PATH + "/package.accept_keywords")
1158             if "--fix" in cmdline:
1159                 cleanFile(USER_CONFIG_PATH + "/package.accept_keywords")
1160             print (portage.output.bold("Done\n"))
1161         elif cmd == "--unmask": 
1162             print (portage.output.bold("\npackage.unmask:"))
1163             checking_package_unmask = True
1164             get_recursive_info(USER_CONFIG_PATH + "/package.unmask")
1165             checking_package_unmask = False
1166             if "--fix" in cmdline:
1167                 cleanFile(USER_CONFIG_PATH + "/package.unmask")
1168         elif cmd == "--mask":
1169             checking_package_mask = True
1170             print (portage.output.bold("\npackage.mask:"))
1171             get_recursive_info(USER_CONFIG_PATH + "/package.mask")
1172             print (portage.output.bold("Done\n"))
1173             checking_package_mask = False
1174             if "--fix" in cmdline:
1175                 cleanFile(USER_CONFIG_PATH + "/package.mask")
1176         elif cmd == "--package-use":
1177             print (portage.output.bold("\npackage.use:"))
1178             processing_package_use = True
1179             get_recursive_info(USER_CONFIG_PATH + "/package.use")
1180             if "--fix" in cmdline:
1181                 cleanFile(USER_CONFIG_PATH + "/package.use")
1182                 clean_useflagsFile(USER_CONFIG_PATH + "/package.use")
1183             print (portage.output.bold("Done\n"))
1184         elif cmd == "--all":
1185             print (portage.output.bold("\npackage.keywords:"))
1186             get_recursive_info(USER_CONFIG_PATH + "/package.keywords")
1187             print (portage.output.bold("\npackage.accept_keywords:"))
1188             get_recursive_info(USER_CONFIG_PATH + "/package.accept_keywords")
1189             print (portage.output.bold("\npackage.unmask:"))
1190             checking_package_unmask = True
1191             get_recursive_info(USER_CONFIG_PATH + "/package.unmask")
1192             checking_package_unmask = False
1193             checking_package_mask = True
1194             print (portage.output.bold("\npackage.mask:"))
1195             get_recursive_info(USER_CONFIG_PATH + "/package.mask")
1196             print (portage.output.bold("\npackage.use:"))
1197             processing_package_use = True
1198             get_recursive_info(USER_CONFIG_PATH + "/package.use")
1199             if "--fix" in cmdline:
1200                 cleanFile(USER_CONFIG_PATH + "/package.keywords")
1201                 cleanFile(USER_CONFIG_PATH + "/package.accept_keywords")
1202                 cleanFile(USER_CONFIG_PATH + "/package.unmask")
1203                 cleanFile(USER_CONFIG_PATH + "/package.use")
1204                 cleanFile(USER_CONFIG_PATH + "/package.mask")
1205                 clean_useflagsFile(USER_CONFIG_PATH + "/package.use")
1206             print (portage.output.bold("\nDone\n"))
1207
1208     print_overlay_text()
1209
1210     if len(cmdline) == 0:
1211         if show_changes_only_flag or show_removable_only_flag:
1212             if (show_changes_only_flag):
1213                 print (portage.output.green("-c") + " or " + portage.output.green("--changes-only") + " must be accompanied by one of the following:")
1214             else:
1215                 print (portage.output.green("-r") + " or " + portage.output.green("--removable-only") + " must be accompanied by one of the following:")
1216
1217             print ("     " + portage.output.yellow("-k") + " or " + portage.output.yellow("--keyword"))
1218             print ("     " + portage.output.yellow("-u") + " or " + portage.output.yellow("--unmask"))
1219             print ("     " + portage.output.yellow("-m") + " or " + portage.output.yellow("--mask"))
1220             print ("     " + portage.output.yellow("-a") + " or " + portage.output.yellow("--all"))
1221
1222             print_usage()
1223