[PATCH 07/17] Handle watched processes exiting.

David Shea dshea at redhat.com
Sun Sep 21 19:36:59 UTC 2014


Do this by putting most of the global part of the anaconda script
into a main() method and looking for ExitError thrown by main. Lightly
rearrange some imports and variables to keep the ones that need to be
global at the global level.
---
 anaconda | 154 ++++++++++++++++++++++++++++++++++-----------------------------
 1 file changed, 84 insertions(+), 70 deletions(-)

diff --git a/anaconda b/anaconda
index e91ddca..d17cc1f 100755
--- a/anaconda
+++ b/anaconda
@@ -883,64 +883,7 @@ def cleanPStore():
     from pyanaconda.iutil import dir_tree_map
     dir_tree_map("/sys/fs/pstore", os.unlink, files=True, dirs=False)
 
-if __name__ == "__main__":
-    # check if the CLI help is requested and return it at once,
-    # without importing random stuff and spamming stdout
-    if ("--help" in sys.argv) or ("-h" in sys.argv) or ("--version" in sys.argv):
-        # we skip the full logging initialisation, but we need to do at least
-        # this much (redirect any log messages to stdout) to get rid of the
-        # harmless but annoying "no handlers found" message on stdout
-        import logging
-        log = logging.getLogger("anaconda")
-        log.addHandler(logging.StreamHandler(stream=sys.stdout))
-        parseArguments()
-
-    print("Starting installer, one moment...")
-
-    # Allow a file to be loaded as early as possible
-    try:
-        # pylint: disable=import-error,unused-import
-        import updates_disk_hook
-    except ImportError:
-        pass
-
-    # this handles setting up updates for pypackages to minimize the set needed
-    setupPythonUpdates()
-    setupPythonPath()
-
-    # init threading before Gtk can do anything and before we start using threads
-    # initThreading initializes the threadMgr instance, import it afterwards
-    from pyanaconda.threads import initThreading, AnacondaThread
-    initThreading()
-    from pyanaconda.threads import threadMgr
-
-    from pyanaconda.i18n import _
-
-    from pyanaconda import constants
-    from pyanaconda.addons import collect_addon_paths
-    from pyanaconda import geoloc
-
-    # do this early so we can set flags before initializing logging
-    from pyanaconda.flags import flags, can_touch_runtime_system
-    (opts, depr) = parseArguments(boot_cmdline=flags.cmdline)
-
-    if opts.images:
-        flags.imageInstall = True
-    elif opts.dirinstall:
-        flags.dirInstall = True
-
-    # Set up logging as early as possible.
-    import logging
-    from pyanaconda import anaconda_log
-    anaconda_log.init()
-    anaconda_log.logger.setupVirtio()
-
-    from pyanaconda import network
-    network.setup_ifcfg_log()
-
-    log = logging.getLogger("anaconda")
-    stdoutLog = logging.getLogger("anaconda.stdout")
-
+def main():
     if os.geteuid() != 0:
         stdoutLog.error("anaconda must be run as root.")
         sys.exit(1)
@@ -960,12 +903,6 @@ if __name__ == "__main__":
     #    stdoutLog.warn("Boot argument '%s' is deprecated. "
     #                   "In the future, use 'inst.%s'.", arg, arg)
 
-    # pull this in to get product name and versioning
-    from pyanaconda import product
-
-    from pyanaconda import isys
-
-    from pyanaconda import iutil
 
     iutil.ipmi_report(constants.IPMI_STARTED)
 
@@ -981,7 +918,6 @@ if __name__ == "__main__":
         iutil.setTargetPhysicalRoot(root_path)
         iutil.setSysroot(root_path)
 
-    from pyanaconda import vnc
     from pyanaconda import kickstart
     from pyanaconda import ntp
     from pyanaconda import keyboard
@@ -994,8 +930,6 @@ if __name__ == "__main__":
     else:
         print("anaconda %s (pre-release) started." % verdesc)
 
-    from pyanaconda.anaconda import Anaconda
-    anaconda = Anaconda()
     iutil.setup_translations()
 
     # reset python's default SIGINT handler
@@ -1095,6 +1029,7 @@ if __name__ == "__main__":
     os.system("udevadm control --env=ANACONDA=1")
 
     # Collect all addon paths
+    from pyanaconda.addons import collect_addon_paths
     addon_paths = collect_addon_paths(constants.ADDON_PATHS)
 
     # If we were given a kickstart file on the command line, parse (but do not
@@ -1199,7 +1134,7 @@ if __name__ == "__main__":
             if len(url_parts) == 2:
                 path = url_parts[1]
             elif len(url_parts) == 3:
-                fstype = url_parts[1]   # XXX not used
+                fstype = url_parts[1]   # pylint: disable=unused-variable
                 path = url_parts[2]
 
             ksdata.method.partition = device
@@ -1256,7 +1191,6 @@ if __name__ == "__main__":
         # no kickstart or bootoption - use default
         localization.setup_locale(constants.DEFAULT_LANG, ksdata.lang)
 
-    import blivet
     blivet.enable_installer_mode()
 
     # now start the interface
@@ -1283,7 +1217,7 @@ if __name__ == "__main__":
 
     from pyanaconda.anaconda_argparse import name_path_pairs
 
-    image_count = 0
+    global image_count
     try:
         for (name, path) in name_path_pairs(opts.images):
             log.info("naming disk image '%s' '%s'", path, name)
@@ -1340,6 +1274,7 @@ if __name__ == "__main__":
         # and other values or geoloc not being present as True
         use_geolocation = flags.cmdline.getbool('geoloc', True)
 
+    from pyanaconda import geoloc
     if use_geolocation:
         provider_id = constants.GEOLOC_DEFAULT_PROVIDER
         # check if a provider was specified by an option
@@ -1369,4 +1304,83 @@ if __name__ == "__main__":
     anaconda._intf.setup(ksdata)
     anaconda._intf.run()
 
+if __name__ == "__main__":
+    # check if the CLI help is requested and return it at once,
+    # without importing random stuff and spamming stdout
+    if ("--help" in sys.argv) or ("-h" in sys.argv) or ("--version" in sys.argv):
+        # we skip the full logging initialisation, but we need to do at least
+        # this much (redirect any log messages to stdout) to get rid of the
+        # harmless but annoying "no handlers found" message on stdout
+        import logging
+        log = logging.getLogger("anaconda")
+        log.addHandler(logging.StreamHandler(stream=sys.stdout))
+        parseArguments()
+
+    print("Starting installer, one moment...")
+
+    # Allow a file to be loaded as early as possible
+    try:
+        # pylint: disable=import-error,unused-import
+        import updates_disk_hook
+    except ImportError:
+        pass
+
+    # this handles setting up updates for pypackages to minimize the set needed
+    setupPythonUpdates()
+    setupPythonPath()
+
+    # Set up global imports
+
+    # init threading before Gtk can do anything and before we start using threads
+    # initThreading initializes the threadMgr instance, import it afterwards
+    from pyanaconda.threads import initThreading, AnacondaThread
+    initThreading()
+    from pyanaconda.threads import threadMgr
+
+    # do this early so we can set flags before initializing logging
+    from pyanaconda.flags import flags, can_touch_runtime_system
+    (opts, depr) = parseArguments(boot_cmdline=flags.cmdline)
+    if opts.images:
+        flags.imageInstall = True
+    elif opts.dirinstall:
+        flags.dirInstall = True
+
+    # Set up logging as early as possible.
+    import logging
+    from pyanaconda import anaconda_log
+    anaconda_log.init()
+    anaconda_log.logger.setupVirtio()
+
+    from pyanaconda import network
+    network.setup_ifcfg_log()
+
+    log = logging.getLogger("anaconda")
+    stdoutLog = logging.getLogger("anaconda.stdout")
+
+    from pyanaconda.i18n import _
+    from pyanaconda import constants
+    from pyanaconda import vnc
+    from pyanaconda import iutil
+    from pyanaconda import isys
+
+    # pull this in to get product name and versioning
+    from pyanaconda import product
+
+    import blivet
+
+    # Set up global variables
+    from pyanaconda.anaconda import Anaconda
+    anaconda = Anaconda()
+
+    image_count = 0
+
+    try:
+        main()
+    except ExitError as e:
+        # X crashed or something
+        print("Anaconda is unable to continue: %s" % e.message)
+        iutil.ipmi_report(constants.IPMI_ABORTED)
+        # Re-raise the exception in case meh can still run
+        raise
+
 # vim:tw=78:ts=4:et:sw=4
-- 
1.9.3



More information about the anaconda-patches mailing list