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