Bump version
[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.6"
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
175     # if the line has special characters, we need to make sure the original line is used for matching
176     special_line = False
177
178     if ( (re.match('[><:\*]',line) != None) ):
179         special_line = True
180
181     print_output(debug,portage.output.blue("Analyzing line: " + line))
182
183     diffs_found = False
184     display_done = False
185
186     fields = line.replace("\t", " ").split(" ")
187
188     if len(fields) > 0:
189         #if the line does not start with an atom such as (= or >), do not validate 
190         #status as this tool is for check specific versions and not entire packages 
191         # a ~cpv should be handled like an =cpv if requested bythe parameter -t
192         check_pkg = fields[0] # this should be one of <>=~
193         overlay_index = check_pkg.find("::")
194         if ( overlay_index >= 0):
195             overlay_list = check_pkg.rsplit("::")
196             if (len(overlay_list) > 0):
197                 overlay_name = overlay_list[1]
198                 if (overlay_name == "gentoo"):
199                     using_gentoo_as_overlay = True
200             check_pkg = check_pkg[0:check_pkg.find("::")]
201
202         if check_pkg[0] not in atom_check:
203
204             # if this is package_unmask, then check the non-atom containing package to see if any version is masked
205
206             if (checking_package_unmask == True):
207                 
208                 print_output(debug,portage.output.blue("check_pkg is " + check_pkg + " found"))
209                 if (is_any_cpv_masked(check_pkg) == False):
210                     print_output(info,portage.output.red("No masked versions of " + check_pkg + " found. Tagged for removal."))
211                     unmask_list.append(str(check_pkg))
212             return
213         if (tilde == 1):
214             if check_pkg[0] in "~":
215                 check_tilde_masked_pkg(check_pkg, filename)
216                 return
217
218         # determine if the package exists
219         try:
220             package_exists = portage.portdb.xmatch("match-all", fields[0])
221         except InvalidAtom:
222             package_exists = False
223
224         if package_exists:
225             # return a Package List based on the cpv
226             query = Query(check_pkg)
227             #query = Query(fields[0])
228             pkgs = []
229
230             try:
231                 pkgs = query.smart_find(True,True,True,True,False,True)
232             except errors.GentoolkitException as err:
233                 pass
234
235             if (pkgs != None):
236                 pkg_length = len(pkgs)
237                 not_installed_pkgs = 0
238                 display_done = False
239
240                                 # go through each package version for a specific version found above
241                 for current_package in pkgs:
242                     if not current_package.is_installed():
243
244                                                 # we have found a package that is in our file, but not installed
245                         not_installed_pkgs = not_installed_pkgs + 1
246
247                         # check to see if specific version of pkg is not installed 
248                         # and display if true
249                         check_pkg = fields[0]
250                         if check_pkg[0] in atom_check:
251                             check_pkg = check_pkg[1:]
252
253                         if (check_pkg == str(current_package.cpv)):
254                             if (not checking_package_mask):
255                                                                 # package is not instaleld
256                                 print_output(info,portage.output.yellow("\n" + str(current_package.cpv) + ": ") + portage.output.red("Not Installed") , current_package, filename)
257                                 if "package.keywords" in filename:
258                                     stable_list.append(str(current_package.cpv))
259                                 if "package.accept_keywords" in filename:
260                                     stable_listNg.append(str(current_package.cpv))
261                                 unmask_list.append(str(current_package.cpv))
262                             else:
263                                                                 # package is masked, and not installed, this is normal use of package.mask
264                                 print_output(info,portage.output.green("" + str(current_package.cpv) + ": ") + portage.output.yellow("Package Masked"),current_package, filename)
265                             display_done = True
266                         continue
267
268                                         # package is installed
269                                         # retrieve the keywords for a file
270                     keywords = "%s" % (get_keywords(current_package,"KEYWORDS").split())
271                     if (keywords.find("Available/Deprecated") >= 0):
272                         continue
273
274                                         #retrieve the mask status of a specific package
275                     pkgmask = _get_mask_status(current_package, False)
276
277                     #determine if installed package is unmasked, if so, display keywords as green
278                     stable = check_for_stable_release(current_package)
279
280                     # do not display if keywords don't exist
281                     if keywords == "[]":
282                         continue
283
284                     if stable:
285                         ebuild_output = portage.output.green("Installed: ") + \
286                             portage.output.turquoise(str(current_package.cpv)) + \
287                             portage.output.green("  Keywords " + keywords)
288                         if "package.unmask" in filename:
289                             unmask_list.append(str(current_package.cpv))
290                         if "package.keywords" in filename:
291                             stable_list.append(str(current_package.cpv))
292                         if "package.accept_keywords" in filename:
293                             stable_listNg.append(str(current_package.cpv))
294                     else:
295                         if (not show_removable_only_flag):
296                             if (not checking_package_unmask):
297                                 ebuild_output = portage.output.green("Installed: ") + \
298                                 portage.output.turquoise(str(current_package.cpv)) + \
299                                 portage.output.yellow("  Keywords " + keywords) 
300                         else:
301                             ebuild_output = portage.output.yellow(str(current_package.cpv))
302
303                                         # check package.unmask
304                     if (checking_package_unmask):
305                         if (not is_pkg_package_masked(str(current_package.cpv))):
306
307                                                         # package is in package.unmask unnecessarily
308                             ebuild_output = ebuild_output + ": " + portage.output.yellow("Not package masked")
309                             if "package.unmask" in filename:
310                                 unmask_list.append(str(current_package.cpv))
311                                 print_output (info, "" +  ebuild_output,None, filename)
312                                 continue
313
314                                         # print once
315                     ebuild_search_key_printed = False
316                     if stable:
317                         diffs_found = False
318                         ebuild_search_key_printed = True
319                         print_output(info,"\n" + ebuild_output,current_package, filename)
320                     elif not show_changes_only_flag and not show_removable_only_flag:
321                         diffs_found = False
322                         ebuild_search_key_printed = True
323                         print_output(info,"\n" + ebuild_output,current_package)
324
325                                         # go through all versions of a package
326                     query = Query(current_package.category + "/" + current_package.name)
327
328                     all_pkgs = []
329
330                     try:
331                         all_pkgs = query.smart_find(True,True,True,True,False,True)
332                     except errors.GentoolkitException as err:
333                         print_output(debug,portage.output.blue("Package " + current_package.category + "/" + current_package.name + " not found."))
334
335                     for a_package in all_pkgs:
336                         if not a_package.is_installed():
337                                                         # a_package is not installed
338                             pkgmask = _get_mask_status(a_package, False)
339                                                         # print status line of package we are now checking
340                             print_output(debug,portage.output.blue("Checking package: " + str(a_package.cpv) +".pkgmask is " + str(pkgmask)))
341                                                         # if package versions are different
342                             if (VersionMatch(CPV(current_package.cpv)).match(CPV(a_package.cpv))):
343                                 diffs_found = True
344                                 keylist = a_package.environment("KEYWORDS")
345                                 keywords = "%s" % (filter_keywords(keylist)).split()
346                                 #if a_package is masked
347                                 if pkgmask > 0 and not show_removable_only_flag:
348                                     if show_changes_only_flag and not ebuild_search_key_printed:
349                                         print_output (info, "\n" +  ebuild_output, current_package)
350                                         ebuild_search_key_printed = True
351                                         check_for_stable_release(current_package)
352                                     if (pkgmask >= 3):
353                                         print_output (info,portage.output.red("Available: " + str(a_package.cpv) + " [M] Keywords: " + keywords),a_package)
354                                     else:
355                                         print_output (info,portage.output.brown("Available: " + str(a_package.cpv) + " Keywords: " + keywords),a_package)
356                                 else:
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                                         print_output(info,portage.output.green("Available: " + str(a_package.cpv) + " Keywords: " + keywords),a_package)
361                 #else:
362                     #print (portage.output.red ("\nCannot find package: " + check_pkg))
363
364                 # if package does not exist, and current_package is None
365                 # then make package using fields[0]
366
367                 # display if pkg/cat is not installed (missing version)
368                 if not_installed_pkgs ==  pkg_length:
369                     if not display_done:
370                         if (not checking_package_mask):
371                             print_output(info,portage.output.yellow("\n" + fields[0] + ": ") + portage.output.red("Not Installed"),current_package)
372                             if "package.keywords" in filename:
373                                 if (special_line == False):
374                                     stable_list.append(str(current_package.cpv))
375                                 else:
376                                     stable_list.append(original_line)
377                             if "package.accept_keywords" in filename:
378                                 if (special_line == False):
379                                     stable_listNg.append(str(current_package.cpv))
380                                 else:
381                                     stable_listNg.append(original_line)
382                             if (special_line == False):
383                                 unmask_list.append(str(current_package.cpv))
384                             else:
385                                 unmask_list.append(original_line)
386                         else:
387                             print_output (info,portage.output.green("\n" + str(current_package.cpv) + ": ") + portage.output.yellow("Package Masked"),current_package)
388         else:
389             diffs_found = True
390             print (portage.output.red ("\nPackage: " + fields[0] + " not found. Please check " + filename + " to validate entry"))
391             if "package.keywords" in filename:
392                 stable_list.append(fields[0])
393             if "package.accept_keywords" in filename:
394                 stable_listNg.append(fields[0])
395             unmask_list.append(fields[0])
396             show_all_versions(fields[0], filename)
397     current_package = ""
398
399     return diffs_found
400
401 # adding support for etc/portage/package.keywords/<whatever>/package.keywords
402 def get_recursive_info(filename):
403
404     # determine if filename is a directory
405     if os.path.isdir(filename):
406         # get listing of directory
407         filenames = os.listdir(filename)
408         for file_name in filenames:
409             get_recursive_info(filename+os.path.sep+file_name)
410     else:   
411         get_info(filename)
412
413 def get_info(filename):
414
415     diffs_found = False
416     no_file = False
417     filedescriptor = None
418
419     try:
420         filedescriptor = open(filename)
421         for line in filedescriptor.readlines():
422             line = line.strip()
423             if len(line) <= 0:
424                 continue
425             elif line.find("#") >= 0:
426                 # found '#' remove comment
427                 if (skipFile(line,filename)):
428                     return
429                 line = line[0:line.find("#")]
430                 line = line.strip()
431                 if len(line) <= 0:
432                     continue
433             if (processing_package_use == False):
434                 diffs_found = parse_line(line, filename)
435             else:
436                 # process package.use
437                 diffs_found = parse_package_use(line,filename)
438     except IOError:
439         print (portage.output.red("Could not find file " + filename))
440         no_file = True
441
442     if not diffs_found and no_file:
443         print (portage.output.brown("No ebuild options found."))
444
445     # close file
446     if (filedescriptor != None):
447         filedescriptor.close()
448
449 # parse the package.use file and look for packages
450 # not installed
451 def parse_package_use(line, filename):
452
453     global info,debug
454     print_output(debug,portage.output.blue("parse_package_use: Line: " + line))
455     pkgs = None
456     check_pkg = ""
457     pkg_length = 0
458     atom_check="<>="
459     any_version = False
460     has_atom = True
461
462     diffs_found = False
463     package_installed = False
464     fields = line.replace("\t", " ").split(" ")
465
466     if len(fields) > 0:
467         check_pkg = fields[0] # this could be one of <>=
468         if check_pkg[0] not in atom_check:
469             has_atom = False
470         else:
471             check_pkg = check_pkg[1:]
472             if check_pkg[0] in atom_check:
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
753                 if "package.unmask" in filename:
754                     if (is_pkg_package_masked(str(current_package.cpv))):
755                         # package was found as masked
756                         return
757                     else:
758                         # package is not masked
759                         unmask_list.append(str(current_package.cpv))
760                         ebuild_output = portage.output.yellow(str(current_package.cpv)) + ": " + portage.output.yellow("Not package masked")
761                         print_output(info,ebuild_output, package, filename)
762                         return
763                 else:
764                     if (is_pkg_package_masked(str(current_package.cpv))):
765                         # package was found as masked
766                         return 
767         else:
768             # at this point we have no packages >= ~cpv that are masked, present for removal
769             # package does not have any masked versions
770             ebuild_output = portage.output.green(package_name + " has no masked versions")
771             
772             if "package.unmask" in filename:
773                 unmask_list.append(orig_package_name)
774             if "package.keywords" in filename:
775                 stable_list.append(orig_package_name)
776             if "package.accept_keywords" in filename:
777                 stable_listNg.append(orig_package_name)
778             print_output(info,ebuild_output, package, filename)
779
780 #helper function to print avail pks when version does not exist
781 def show_all_versions(pkg, filename):
782
783     # is package masked
784     is_package_masked = False
785     pkgArr = portage.pkgsplit(pkg)
786
787     if pkgArr is None or len(pkgArr) == 0:
788         return
789
790     # determine if category/package is masked 
791     package = pkgArr[0]
792
793     operator = portage.dep.Atom(pkg).operator
794     if operator is not None:
795         package = package[len(operator):]
796
797     # package is category/package and pkg is category/package-version
798     # is category/package-version we are checking package masked?
799     #if portage.settings.pmaskdict.has_key(package):
800     pmaskdict = settings._mask_manager._pmaskdict
801     if package in pmaskdict:
802         pkg_list = pmaskdict.get(package)
803         # iterate through list array looking for pkg
804         for pkg_check in pkg_list:
805             operator = portage.get_operator(pkg_check)
806             if operator is None:
807                 if pkg_check == package:
808                     is_package_masked = True
809
810     query = Query(package)
811
812     all_pkgs = []
813
814     try:
815         all_pkgs = query.smart_find(True,True,True,True,False,True)
816     except errors.GentoolkitException as err:
817         print_output(debug,portage.output.blue("Package " + package + " not found."))
818
819     for current_package in all_pkgs:
820         keywords = "%s" % (current_package.environment("KEYWORDS").split())
821         keywords = filter_keywords(keywords)
822         keywords = "[%s]" % (keywords)
823         ebuild = current_package.ebuild_path()
824         if ebuild:
825             pkgmask = _get_mask_status(current_package, True)
826             if is_package_masked:
827                 print (portage.output.red("Available: " + str(current_package.cpv) + " [M] Keywords: " + keywords))
828             elif pkgmask > 4:
829                 print (portage.output.red("Available: " + str(current_package.cpv) + " [M] Keywords: " + keywords))
830             elif pkgmask == 4 or pkgmask == 1:
831                 print (portage.output.brown("Available: " + str(current_package.cpv) + " Keywords: " + keywords))
832             else:
833                 print (portage.output.green("Available: " + str(current_package.cpv) + " Keywords: " + keywords))
834                 if "package.keywords" in filename:
835                     stable_list.append(str(current_package.cpv))
836                 if "package.accept_keywords" in filename:
837                     stable_listNg.append(str(current_package.cpv))
838
839
840 def get_useflags(package):
841     iuse = get_iuse(package.cpv)
842     return iuse
843
844 def check_useflags_all_versions(pkgs, line, check_pkgs):
845
846     global useremove_display, invalid_flag_found 
847     invalid_flag_found = False
848    
849     print_output(debug,portage.output.blue("ENTERED check_useflags_all_versions: " + check_pkgs))
850
851     potential_invalid_flag = []
852     valid_flag_list = []
853     for package in pkgs:
854
855         # if package not installed, move on
856         if (not package.is_installed()):
857             continue;
858
859         print_output(debug,portage.output.blue("check_useflags_all_versions: package: " + package.cpv))
860         if ((package is None) or (package == "")):
861             return
862
863         useflag_removal_display=""
864
865         if (len(line) <= 0):
866             return
867
868         iuse = get_useflags(package)
869
870         #for iuse_item in iuse:
871         #    print_output(debug,portage.output.blue(("iuse_item is " + iuse_item)))
872     
873         useflags_fromfile = line.replace("\t", " ").split(" ")
874
875         #for useflags_fromfile_item in useflags_fromfile:
876         #    print_output(debug,portage.output.blue(("useflags_fromfile_item is " + useflags_fromfile_item)))
877
878         package_string = useflags_fromfile.pop(0)
879         print_output(debug,portage.output.blue(("package_string is " + package_string)))
880     
881         clean_useflags_list = []
882         # remove + or -
883         atom = "-+"
884     
885         #clean list from portage of + or -
886         clean_iuse = []
887         for item in iuse:
888             if item[0] in atom:
889                 clean_iuse.append(item[1:])
890             else:
891                 clean_iuse.append(item)
892     
893         for original_flag in useflags_fromfile:
894             if (original_flag is None or original_flag == ""):
895                 continue
896             flag = original_flag
897             if original_flag[0] in atom:
898                 flag = original_flag[1:]
899             if flag not in clean_iuse:
900                 print_output(debug,portage.output.blue(("found invalid flag: " + flag)))
901
902                 # only add to invalid list if it's not in valid list
903                 try:
904                     index = valid_flag_list.index(original_flag)
905                 except ValueError as error:
906
907                     try:
908                        index = potential_invalid_flag.index(original_flag)
909                     except ValueError:
910                        print_output(debug,portage.output.blue(original_flag + " not found for " + package.cpv))
911                        potential_invalid_flag.append(original_flag)
912
913             else:
914                 print_output(debug,portage.output.blue(("found valid flag: " + flag)))
915                 try:
916                     index = valid_flag_list.index(original_flag)
917                 except ValueError as error:
918                     valid_flag_list.append(original_flag)
919
920                 try:
921                     index = potential_invalid_flag.index(original_flag)
922                     potential_invalid_flag.remove(original_flag)
923                 except ValueError as error:
924                     continue
925
926
927     # if potential_invalid_flag list is empty, we are done
928     if (len(potential_invalid_flag) <= 0):
929         return
930
931     invalid_flag_found = True
932
933     # build sentence
934     invalid_flags = ""
935     verb = "is"
936     for inv_flag in potential_invalid_flag:
937         if (len(invalid_flags) > 0):
938             invalid_flags += ","
939             verb = "are"
940         invalid_flags += inv_flag
941
942     # if there are no valid flags at all, we remove the line
943     if ( len(valid_flag_list) == 0):
944         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"))
945         useremove_display += "Removing line: " + line + "\n"
946         use_flag_dict[line] = ""
947         return
948
949     # if there are values in potential_invalid_flag, we need to remove them
950
951     if (len(potential_invalid_flag) > 0):
952         removal_text = "use flags: "
953     else:
954         removal_text = "use flag: "
955
956     if ( (len(valid_flag_list) > 0)):
957         useremove_display += "Removing " + removal_text + invalid_flags + " for package " + check_pkgs + "\n"
958         print (portage.output.yellow(removal_text) +  portage.output.red(invalid_flags) + portage.output.yellow(" " + verb + " invalid for " + str(package.cpv)))
959
960     valid_flag_list.insert(0,package_string);
961
962     if (len(potential_invalid_flag) > 0):
963         use_flag_dict[line] = valid_flag_list
964         
965     return valid_flag_list
966
967 def check_useflags(package,line):
968
969     global useremove_display, invalid_flag_found 
970     invalid_flag_found = False
971
972     print_output(debug,portage.output.blue("check_useflags: package: " + package.cpv))
973     if ((package is None) or (package == "")):
974         return
975
976     useflag_removal_display=""
977
978     if (len(line) <= 0):
979         return
980
981     iuse = get_useflags(package)
982
983     #for iuse_item in iuse:
984     #    print_output(debug,portage.output.blue(("iuse_item is " + iuse_item)))
985
986     useflags_fromfile = line.replace("\t", " ").split(" ")
987     #for useflags_fromfile_item in useflags_fromfile:
988     #    print_output(debug,portage.output.blue(("useflags_fromfile_item is " + useflags_fromfile_item)))
989
990     package_string = useflags_fromfile.pop(0)
991     print_output(debug,portage.output.blue(("package_string is " + package_string)))
992
993     clean_useflags_list = []
994     # remove + or -
995     atom = "-+"
996
997     #clean list from portage of + or -
998     clean_iuse = []
999     for item in iuse:
1000         if item[0] in atom:
1001             clean_iuse.append(item[1:])
1002         else:
1003             clean_iuse.append(item)
1004
1005     valid_flag_list = []
1006     for original_flag in useflags_fromfile:
1007         if (original_flag is None or original_flag == ""):
1008             continue
1009         flag = original_flag
1010         if original_flag[0] in atom:
1011             flag = original_flag[1:]
1012         if flag not in clean_iuse:
1013             print_output (info,portage.output.red("use flag: " + flag + " is invalid for : " + str(package.cpv)))
1014             useflag_removal_display += "Removing use flag: " + flag + " for package " + str(package.cpv)
1015             invalid_flag_found = True
1016         else:
1017             valid_flag_list.append(original_flag)
1018
1019     # if valid_flag_list is empty, there are no valid flags
1020     if ( (len(valid_flag_list) > 0) and (len(useflag_removal_display) >0)):
1021         useremove_display += useflag_removal_display + "\n"
1022     elif ( len(valid_flag_list) == 0):
1023         useremove_display += "No valid use flags found for package " + str(package.cpv) + ". Removing line: " + line + "\n"
1024         if ( invalid_flag_found != True):
1025             print_output (info,portage.output.red("No valid use flags found for package " + str(package.cpv)))
1026         invalid_flag_found = True
1027
1028     valid_flag_list.insert(0,package_string);
1029     if (invalid_flag_found == True):
1030         use_flag_dict[line] = valid_flag_list
1031
1032     return valid_flag_list
1033
1034
1035 def clean_useflagsFile(filename):
1036
1037     if "--fix-confirm" in cmdline:
1038         if (confirmFix() == False):
1039             return
1040     
1041     display_line = ""
1042     removed_list = []
1043
1044     try:
1045         # determine if filename is a directory
1046         if os.path.isdir(filename):
1047             # get listing of directory
1048             filenames = os.listdir(filename)
1049             for file_name in filenames:
1050                 clean_useflagsFile(filename+os.path.sep+file_name)
1051             return
1052         else:
1053             #go through stable array and remove line if found
1054             for line in fileinput.input(filename,inplace =1):
1055                 itemFound = False
1056                 line = line.strip()
1057
1058                 # make sure line is not empty and do not remove commented out lines
1059                 if len(line) <= 0:
1060                     continue
1061                 elif line.find("#") == 0:
1062                     print (line)
1063                     continue
1064
1065                 check_for_change = ""
1066                 use_flag_dict.get(line,"")
1067                 try:
1068                     check_for_change = use_flag_dict[line]
1069                     removed_list.append(line)
1070                     if ( len(check_for_change) > 1):
1071                         print (" ".join(check_for_change))
1072                 except KeyError as error:
1073                     print (line)
1074
1075             fileinput.close()
1076     except OSError as error:
1077         print (portage.output.red("Modify/Read access to file: " + filename + " failed: " + format(error)))
1078
1079     if (len(removed_list) > 0):
1080         print ("\n")
1081         for package in removed_list:
1082             print (portage.output.red("Removing from: ") + portage.output.yellow(filename + ": Invalid use flag(s) from ")  + portage.output.green(package) + "\n")
1083
1084     return
1085
1086 def handle_if_overlay(package):
1087     overlay_text = ""
1088     global print_overlay_flag,using_gentoo_as_overlay
1089
1090     if (using_gentoo_as_overlay):
1091         return overlay_text
1092
1093     print_overlay_flag = True
1094
1095     ebuild_path,overlay_path = porttree.dbapi.findname2(str(package.cpv))
1096     index = -1
1097     try:
1098         index = overlay_list.index(overlay_path)
1099     except ValueError as error:
1100         overlay_list.append(overlay_path)
1101         index = overlay_list.index(overlay_path)
1102
1103     overlay_text = " [%s]" % (str(index+1))
1104
1105     return overlay_text
1106
1107 # if the overlay_text was displayed to the user
1108 # we need to display the string at the end 
1109 # this array will store the overlays to be displayed
1110 def print_overlay_text():
1111    
1112     global print_overlay_flag
1113     
1114     if (not print_overlay_flag):
1115         return
1116     
1117     if (len(overlay_list) <= 0):
1118         return
1119
1120     index = 1
1121     for x in overlay_list:
1122         print (portage.output.turquoise("[" + str(index) + "] ") + x)
1123         index = index + 1
1124     
1125     print ("\n")
1126
1127 #helper function to print output
1128 def print_output(log_level,output_string, package=None, filename=None):
1129     
1130     global logLevel
1131
1132     if package != None:
1133         if (package.is_overlay()):
1134             output_string = "%s%s" % (output_string,portage.output.turquoise(handle_if_overlay(package)))
1135             #output_string = output_string + portage.output.turquoise(handle_if_overlay(package))
1136         else:
1137             if filename != None:
1138                 output_string = "%s%s" % (output_string,portage.output.brown(" : " + filename))
1139                 #output_string = output_string + portage.output.brown(" : " + filename)
1140
1141     if (log_level <= logLevel):
1142         print (output_string)
1143
1144 # remove stabled files that are no longer needed from package.keywords
1145 # or package.mask
1146 # includes support for etc/portage/package.keywords/<whatever>/package.keywords
1147 def cleanFile (filename):
1148
1149     removeDups = []
1150     removed_list = []
1151
1152     if "--fix-confirm" in cmdline:
1153         if (confirmFix() == False):
1154             return
1155
1156     # if the file or directory does not exist
1157     # exit out
1158     if (os.path.exists(filename) == False):
1159         return
1160     
1161     if "package.keywords" in filename:
1162         if ( len(stable_list) == 0):
1163             return
1164         removeDups = stable_list
1165     elif "package.accept_keywords" in filename:
1166         if ( len(stable_listNg) == 0):
1167             return
1168         removeDups = stable_listNg
1169     else:
1170         if ( len(unmask_list) == 0):
1171             return
1172         removeDups = unmask_list
1173
1174     removedDict = {}
1175
1176     try:
1177         # determine if filename is a directory
1178         if os.path.isdir(filename):
1179             # get listing of directory
1180             filenames = os.listdir(filename)
1181             for file_name in filenames:
1182                 cleanFile(filename+os.path.sep+file_name)
1183             return
1184         else:   
1185             #go through stable array and remove line if found
1186             for line in fileinput.input(filename,inplace =1):
1187                 itemFound = False
1188                 line = line.strip()
1189
1190                 # make sure line is not empty and do not remove commented out lines
1191                 if len(line) <= 0:
1192                     continue
1193                 elif line.find("#") == 0:
1194                     print (line)
1195                     continue
1196
1197                 for item in removeDups:
1198                     if item in line:
1199                         removed_list.append(item)
1200                         removedDict[filename] = item
1201                         itemFound = True
1202                         removeDups.pop(removeDups.index(item))
1203                         break
1204                 if (itemFound == False):
1205                     print (line)
1206             fileinput.close()
1207     except OSError as error:
1208         print (portage.output.red("Modify/Read access to file: " + filename + " failed: " + format(error)))
1209     
1210     if (len(removed_list) > 0):
1211         print ("\n")
1212         for package in removed_list:
1213             print (portage.output.red("Removing from: ") + portage.output.yellow(filename) + ": " + portage.output.green(package) + "\n")
1214
1215 # ask the user if they want to fix their files
1216 # and remove unneeded entries
1217 # Return true if they say yes and False if they say no
1218 def confirmFix():
1219     
1220     global fix_asked,fix_confirm
1221
1222     if (fix_asked == True):
1223         return fix_confirm
1224
1225     # only ask if we actually have anything to check
1226     if ( (len(stable_list) == 0) and 
1227           (len(stable_listNg) == 0) and
1228           (len(unmask_list) == 0) and
1229           (invalid_flag_found == False) and 
1230           (len(use_flag_dict) == 0)):
1231         fix_confirm = True
1232         return fix_confirm
1233
1234     fix_asked = True 
1235
1236     valid = {"yes":"yes",   "y":"yes", 
1237              "no":"no",     "n":"no"}
1238    
1239     prompt = portage.output.bold("Remove entries from files [") + portage.output.green("y") + "/" + portage.output.red("n") + portage.output.bold("]") + " "
1240
1241     while 1:
1242         sys.stdout.write('\n' + prompt)
1243         choice = input().lower()
1244         if choice == '':
1245             fix_confirm = False
1246             break
1247         elif choice in valid.keys():
1248             if (choice == 'y' or choice == 'yes'):
1249                 fix_confirm = True
1250                 break
1251             else:
1252                 fix_confirm = False
1253                 break 
1254         else:
1255             sys.stdout.write("Please respond with 'yes' or 'no' "\
1256                              "(or 'y' or 'n').\n")
1257
1258     return fix_confirm
1259
1260
1261 def check_for_any_installed_version(pkgs):
1262     
1263     for package in pkgs:
1264         if (package.is_installed()):
1265             return True
1266
1267     return False
1268
1269 # main
1270 if __name__ == "__main__":
1271
1272     if len(sys.argv) == 1:
1273         print_usage()
1274         sys.exit(1)
1275
1276     # soooooo stolen from emerge
1277     tmpcmdline = sys.argv[1:]
1278
1279     for cmd in tmpcmdline:
1280         if cmd[0:1] == "-" and cmd[1:2] != "-":
1281             for cmd_item in cmd[1:]:
1282                 if cmd_item in mappings:
1283                     if mappings[cmd_item] in cmdline:
1284                         print ()
1285                         print ("*** Warning: Redundant use of ", mappings[cmd_item])
1286                     else:
1287                         cmdline.append(mappings[cmd_item])
1288                 else:
1289                     print ("!!! Error: -"+cmd_item+" is an invalid option.")
1290                     sys.exit(-1)
1291         else:
1292             cmdline.append(cmd)
1293
1294     #parse long options
1295     for cmd in cmdline:
1296         if len(cmd)>=2 and cmd[0:2]=="--":
1297             try:
1298                 i = options.index(cmd)
1299                 continue
1300             except ValueError:
1301                 print ("!!! Error: -"+cmd+" is an invalid option.")
1302                 sys.exit(-1)
1303
1304     if "--changes-only" in cmdline:
1305         cmdline.remove("--changes-only")
1306         show_changes_only_flag = True
1307
1308     if "--removable-only" in cmdline:
1309         cmdline.remove("--removable-only")
1310         show_removable_only_flag = True
1311
1312     if "--debug" in cmdline:
1313         logLevel = debug
1314
1315     if "--no-color" in cmdline:
1316         portage.output.nocolor()
1317
1318     if "--tilde-check" in cmdline:
1319         tilde=1
1320
1321     if "--version" in cmdline:
1322         print_version()
1323         sys.exit(0)
1324
1325     if "--fix-confirm" in cmdline:
1326         if '--fix' not in cmdline:
1327             cmdline.append("--fix")
1328     
1329     if "--all" in cmdline:
1330         tmpcmdline = ["--all"]
1331         if "--fix" in cmdline:
1332             tmpcmdline.append("--fix")
1333             if "--fix-confirm" in cmdline:
1334                 tmpcmdline.append("--fix-confirm")
1335                 fix_confirm = False
1336         cmdline=tmpcmdline      
1337
1338     if "--help" in cmdline:
1339         print_usage()
1340         sys.exit(0)
1341     
1342     if (show_changes_only_flag and show_removable_only_flag):
1343         print ("Please select only one of --show-removable (-r) or --changes-only")
1344         print ("Use --help for more info.")
1345         sys.exit(0)
1346         
1347     for cmd in cmdline:
1348         if cmd == "--keyword":
1349             print (portage.output.bold("\npackage.keywords:"))
1350             get_recursive_info(USER_CONFIG_PATH + "/package.keywords")
1351             if "--fix" in cmdline:
1352                 cleanFile(USER_CONFIG_PATH + "/package.keywords")
1353             print (portage.output.bold("Done\n"))
1354             print (portage.output.bold("\npackage.accept_keywords:"))
1355             get_recursive_info(USER_CONFIG_PATH + "/package.accept_keywords")
1356             if "--fix" in cmdline:
1357                 cleanFile(USER_CONFIG_PATH + "/package.accept_keywords")
1358             print (portage.output.bold("Done\n"))
1359         elif cmd == "--unmask": 
1360             print (portage.output.bold("\npackage.unmask:"))
1361             checking_package_unmask = True
1362             get_recursive_info(USER_CONFIG_PATH + "/package.unmask")
1363             checking_package_unmask = False
1364             if "--fix" in cmdline:
1365                 cleanFile(USER_CONFIG_PATH + "/package.unmask")
1366         elif cmd == "--mask":
1367             checking_package_mask = True
1368             print (portage.output.bold("\npackage.mask:"))
1369             get_recursive_info(USER_CONFIG_PATH + "/package.mask")
1370             print (portage.output.bold("Done\n"))
1371             checking_package_mask = False
1372             if "--fix" in cmdline:
1373                 cleanFile(USER_CONFIG_PATH + "/package.mask")
1374         elif cmd == "--package-use":
1375             print (portage.output.bold("\npackage.use:"))
1376             processing_package_use = True
1377             get_recursive_info(USER_CONFIG_PATH + "/package.use")
1378             if "--fix" in cmdline:
1379                 cleanFile(USER_CONFIG_PATH + "/package.use")
1380                 clean_useflagsFile(USER_CONFIG_PATH + "/package.use")
1381             print (portage.output.bold("Done\n"))
1382         elif cmd == "--all":
1383             print (portage.output.bold("\npackage.keywords:"))
1384             get_recursive_info(USER_CONFIG_PATH + "/package.keywords")
1385             print (portage.output.bold("\npackage.accept_keywords:"))
1386             get_recursive_info(USER_CONFIG_PATH + "/package.accept_keywords")
1387             print (portage.output.bold("\npackage.unmask:"))
1388             checking_package_unmask = True
1389             get_recursive_info(USER_CONFIG_PATH + "/package.unmask")
1390             checking_package_unmask = False
1391             checking_package_mask = True
1392             print (portage.output.bold("\npackage.mask:"))
1393             get_recursive_info(USER_CONFIG_PATH + "/package.mask")
1394             print (portage.output.bold("\npackage.use:"))
1395             processing_package_use = True
1396             get_recursive_info(USER_CONFIG_PATH + "/package.use")
1397             if "--fix" in cmdline:
1398                 cleanFile(USER_CONFIG_PATH + "/package.keywords")
1399                 cleanFile(USER_CONFIG_PATH + "/package.accept_keywords")
1400                 cleanFile(USER_CONFIG_PATH + "/package.unmask")
1401                 cleanFile(USER_CONFIG_PATH + "/package.use")
1402                 cleanFile(USER_CONFIG_PATH + "/package.mask")
1403                 clean_useflagsFile(USER_CONFIG_PATH + "/package.use")
1404             print (portage.output.bold("\nDone\n"))
1405
1406     print_overlay_text()
1407
1408     if len(cmdline) == 0:
1409         if show_changes_only_flag or show_removable_only_flag:
1410             if (show_changes_only_flag):
1411                 print (portage.output.green("-c") + " or " + portage.output.green("--changes-only") + " must be accompanied by one of the following:")
1412             else:
1413                 print (portage.output.green("-r") + " or " + portage.output.green("--removable-only") + " must be accompanied by one of the following:")
1414
1415             print ("     " + portage.output.yellow("-k") + " or " + portage.output.yellow("--keyword"))
1416             print ("     " + portage.output.yellow("-u") + " or " + portage.output.yellow("--unmask"))
1417             print ("     " + portage.output.yellow("-m") + " or " + portage.output.yellow("--mask"))
1418             print ("     " + portage.output.yellow("-a") + " or " + portage.output.yellow("--all"))
1419
1420             print_usage()
1421