Move global portage import to the top and add writemsg and
authorZac Medico <zmedico@gentoo.org>
Sat, 8 Aug 2009 21:45:57 +0000 (21:45 -0000)
committerZac Medico <zmedico@gentoo.org>
Sat, 8 Aug 2009 21:45:57 +0000 (21:45 -0000)
writemsg_stdout imports for safe unicode output.

svn path=/main/trunk/; revision=13952

bin/portageq

index a166eb2b5a40fd2dc1b5a687b90fe51496ca795c..941c5b80779a344df9aa2a66bcb46ba9a05301dc 100755 (executable)
@@ -23,6 +23,26 @@ import os
 
 import types
 
+# Avoid sandbox violations after python upgrade.
+pym_path = os.path.join(os.path.dirname(
+       os.path.dirname(os.path.realpath(__file__))), "pym")
+if os.environ.get("SANDBOX_ON") == "1":
+       sandbox_write = os.environ.get("SANDBOX_WRITE", "").split(":")
+       if pym_path not in sandbox_write:
+               sandbox_write.append(pym_path)
+               os.environ["SANDBOX_WRITE"] = \
+                       ":".join(filter(None, sandbox_write))
+       del sandbox_write
+
+try:
+       import portage
+except ImportError:
+       sys.path.insert(0, pym_path)
+       import portage
+del pym_path
+
+from portage.util import writemsg, writemsg_stdout
+
 #-----------------------------------------------------------------------------
 #
 # To add functionality to this tool, add a function below.
@@ -534,9 +554,10 @@ def usage(argv):
        # Show our commands -- we do this by scanning the functions in this
        # file, and formatting each functions documentation.
        #
+       non_commands = frozenset(['exithandler', 'main',
+               'usage', 'writemsg', 'writemsg_stdout'])
        commands = sorted(k for k, v in globals().iteritems() \
-               if type(v) is types.FunctionType and \
-               k not in ('usage', 'main', 'exithandler'))
+               if type(v) is types.FunctionType and k not in non_commands)
 
        for name in commands:
                # Drop non-functions
@@ -584,24 +605,6 @@ def main():
                        sys.exit(os.EX_USAGE)
                os.environ["ROOT"] = sys.argv[2]
 
-       # Avoid sandbox violations after python upgrade.
-       from os import path as osp
-       pym_path = osp.join(osp.dirname(
-               osp.dirname(osp.realpath(__file__))), "pym")
-       if os.environ.get("SANDBOX_ON") == "1":
-               sandbox_write = os.environ.get("SANDBOX_WRITE", "").split(":")
-               if pym_path not in sandbox_write:
-                       sandbox_write.append(pym_path)
-                       os.environ["SANDBOX_WRITE"] = \
-                               ":".join(filter(None, sandbox_write))
-
-       global portage
-       try:
-               import portage
-       except ImportError:
-               sys.path.insert(0, pym_path)
-               import portage
-
        args = sys.argv[2:]
        if args and not isinstance(args[0], unicode):
                for i in xrange(len(args)):