overwrite old xorg.conf if it's from us
authorChristian Hofstaedtler <ch@grml.org>
Mon, 15 Nov 2010 19:48:10 +0000 (20:48 +0100)
committerChristian Hofstaedtler <ch@grml.org>
Mon, 15 Nov 2010 19:54:28 +0000 (20:54 +0100)
also don't rely on os.unlink, and just let the sudo'd mv handle the
existing file.

grml-x

diff --git a/grml-x b/grml-x
index 6874c92..995b4c4 100755 (executable)
--- a/grml-x
+++ b/grml-x
@@ -106,6 +106,20 @@ def which(program):
 
     return None
 
+XORG_CONF_HEADER = "# Automatically generated by grml-x."
+def check_old_xorg_conf(filename, overwrite):
+    # True: no problem, we can create/overwrite the config file
+    # False: pre-existing config file, and we are not to overwrite it
+    if overwrite: return True
+    if not os.path.exists(filename): return True
+    try:
+        f = file(filename, 'r')
+        lines = f.readlines()
+        f.close()
+        return (not XORG_CONF_HEADER in lines)
+    except IOError:
+        return False
+
 parser = OptionParser(usage="usage: %prog [options] [window-manager]")
 parser.add_option("--nostart", action="store_false", dest="start_server", default=True,
                 help="Don't start X server")
@@ -132,13 +146,10 @@ def main():
         print "W: running as root is unsupported and may not work."
         time.sleep(1)
 
-    if os.path.exists(options.xorg_conf):
-        if options.overwrite:
-            os.unlink(options.xorg_conf)
-        else:
-            print "E: Not overwriting existing %r without --force." % options.xorg_conf
-            print "I: If you previously ran grml-x, use startx /usr/bin/x-window-manager"
-            return 1
+    if not check_old_xorg_conf(options.xorg_conf, options.overwrite):
+        print "E: Not overwriting existing %r without --force." % options.xorg_conf
+        print "I: If you previously ran grml-x, use startx /usr/bin/x-window-manager"
+        return 1
 
     if 'xmode' in bootparams and not options.mode: options.mode = bootparams['xmode']
     if 'xmodule' in bootparams and not options.module: options.module = bootparams['xmodule']
@@ -192,12 +203,14 @@ def main():
     if monitor or device or len(screen.data) > 0 or screen.subsect != '':
         try:
             f = tempfile.NamedTemporaryFile(delete=False)
-            f.write('# Automatically generated by grml-x.\n')
-            f.write('# See man xorg.conf or /etc/X11/xorg.conf.example for more\n')
+            f.write(XORG_CONF_HEADER + "\n")
+            f.write("# DO NOT MODIFY, YOUR CHANGES WILL BE LOST - OR REMOVE ALL HEADER LINES\n")
+            f.write("# See man xorg.conf or /etc/X11/xorg.conf.example for more\n")
             if monitor: f.write(str(monitor))
             if device: f.write(str(device))
             f.write(str(screen))
             f.flush()
+            os.fchmod(f.fileno(), 0644)
             run_program(['sudo', 'mv', '-f', f.name, options.xorg_conf])
         finally:
             f.close()