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