Merged from trunk -r11455:11460
authorFabian Groffen <grobian@gentoo.org>
Sun, 24 Aug 2008 13:26:44 +0000 (13:26 -0000)
committerFabian Groffen <grobian@gentoo.org>
Sun, 24 Aug 2008 13:26:44 +0000 (13:26 -0000)
   | 11456   | Add a new filter_protected command which is similar to       |
   | zmedico | is_protected but works by reading filenames from stdin and   |
   |         | writing to stdout only the filenames that are protected.     |
   |         | This allows an unlimited number of files to be checked via a |
   |         | single portageq call.                                        |

   | 11457   | Flush stdout after the loop inside filter_protected()        |
   | zmedico | completes.                                                   |

   | 11458   | Fix the loop in filter_protected() to continue when          |
   | zmedico | necessary due to an error.                                   |

   | 11459   | Fix spelling typos in error messages.                        |
   | zmedico |                                                              |

   | 11460   | Fix grammar. Thanks to ABCD.                                 |
   | zmedico |                                                              |

svn path=/main/branches/prefix/; revision=11463

bin/portageq

index 7fe52b3da7d2795be91bb3dfc45cd87599197e0e..2c87ac7042746519ce6b34bbb94593eb4eed85ac 100755 (executable)
@@ -220,7 +220,7 @@ def is_protected(argv):
        The filename must begin with <root>.
        """
        if len(argv) != 2:
-               sys.stderr.write("ERROR: expeced 2 parameters, got %d!\n" % len(argv))
+               sys.stderr.write("ERROR: expected 2 parameters, got %d!\n" % len(argv))
                sys.stderr.flush()
                return 2
 
@@ -261,6 +261,66 @@ def is_protected(argv):
 
 is_protected.uses_root = True
 
+def filter_protected(argv):
+       """<root>
+       Read filenames from stdin and write them to stdout if they are protected.
+       All filenames are delimited by \\n and must begin with <root>.
+       """
+       if len(argv) != 1:
+               sys.stderr.write("ERROR: expected 1 parameter, got %d!\n" % len(argv))
+               sys.stderr.flush()
+               return 2
+
+       root, = argv
+       out = sys.stdout
+       err = sys.stderr
+       cwd = None
+       try:
+               cwd = os.getcwd()
+       except OSError:
+               pass
+
+       import shlex
+       from portage.util import ConfigProtect
+
+       settings = portage.settings
+       protect = shlex.split(settings.get("CONFIG_PROTECT", ""))
+       protect_mask = shlex.split(settings.get("CONFIG_PROTECT_MASK", ""))
+       protect_obj = ConfigProtect(root, protect, protect_mask)
+
+       protected = 0
+       errors = 0
+
+       for line in sys.stdin:
+               filename = line.rstrip("\n")
+               f = portage.normalize_path(filename)
+               if not f.startswith(os.path.sep):
+                       if cwd is None:
+                               err.write("ERROR: cwd does not exist!\n")
+                               err.flush()
+                               errors += 1
+                               continue
+                       f = os.path.join(cwd, f)
+                       f = portage.normalize_path(f)
+
+               if not f.startswith(root):
+                       err.write("ERROR: file paths must begin with <root>!\n")
+                       err.flush()
+                       errors += 1
+                       continue
+
+               if protect_obj.isprotected(f):
+                       protected += 1
+                       out.write("%s\n" % filename)
+       out.flush()
+
+       if errors:
+               return 2
+
+       return 0
+
+filter_protected.uses_root = True
+
 def best_visible(argv):
        """<root> [<category/package>]+
        Returns category/package-version (without .ebuild).