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