[Erp5-report] r13070 - /umigumi/trunk/custom/erp5_cd/root/lsb-init-patch.py

nobody at svn.erp5.org nobody at svn.erp5.org
Tue Feb 27 11:06:20 CET 2007


Author: kevin
Date: Tue Feb 27 11:06:18 2007
New Revision: 13070

URL: http://svn.erp5.org?rev=13070&view=rev
Log:
This little python script let us patch bad init scriptt to make them LSB compliant

Added:
    umigumi/trunk/custom/erp5_cd/root/lsb-init-patch.py

Added: umigumi/trunk/custom/erp5_cd/root/lsb-init-patch.py
URL: http://svn.erp5.org/umigumi/trunk/custom/erp5_cd/root/lsb-init-patch.py?rev=13070&view=auto
==============================================================================
--- umigumi/trunk/custom/erp5_cd/root/lsb-init-patch.py (added)
+++ umigumi/trunk/custom/erp5_cd/root/lsb-init-patch.py Tue Feb 27 11:06:18 2007
@@ -1,0 +1,181 @@
+#! /usr/bin/python
+
+##############################################################################
+#
+# Copyright (C) 2007 Nexedi SA
+#                    Kevin DELDYCKE  <kevin at nexedi.com>
+#
+# This program is Free Software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA ?02111-1307, USA.
+#
+##############################################################################
+
+"""
+Code to dynamiccaly patch init files to make them LSB compliant.
+"""
+
+import sys, os
+
+
+INIT_FOLDER = "/etc/rc.d/init.d/"
+TMP_FOLDER  = "/tmp/"
+
+
+def usage():
+  print """Usage : %s <init_filename>
+""" % sys.argv[0]
+
+
+if __name__ == "__main__":
+
+  # Get all parameters
+  try:
+    init_file = sys.argv[1]
+  except getopt.GetoptError:
+    # print help information and exit:
+    usage()
+    sys.exit(2)
+
+  #init_patch = [
+  #{ 'file'       : 'ultrabayd'
+  #, 'name'       : 'ultrabayd'
+  #, 'description': ''
+  #},
+  #{ 'file'       : 'bacula-fd'
+  #, 'name'       : 'bacula'
+  #, 'description': ''
+  #},
+  #{ 'file'       : 'zope'
+  #, 'name'       : 'zope'
+  #, 'description': ''
+  #},
+  #{ 'file'       : 'oki4daemon'
+  #, 'name'       : 'oki4daemon'
+  #, 'description': ''
+  #},
+  #{ 'file'       : 'mysqld-ndb_cpcd'
+  #, 'name'       : 'mysqld-ndb_cpcd'
+  #, 'description': ''
+  #},
+  #{ 'file'       : 'mysqld-ndbd'
+  #, 'name'       : 'mysqld-ndbd'
+  #, 'description': ''
+  #},
+  #{ 'file'       : 'mysqld-ndb_mgmd'
+  #, 'name'       : 'mysqld-ndb_mgmd'
+  #, 'description': ''
+  #},
+  #{ 'file'       : 'htcacheclean'
+  #, 'name'       : 'htcacheclean'
+  #, 'description': ''
+  #},
+
+
+  #]
+
+  #ultrabayd
+  #bacula-fd
+  #zope
+  #oki4daemon
+  #mysqld-ndb_cpcd
+  #mysqld-ndbd
+  #mysqld-ndb_mgmd
+  #htcacheclean
+
+  # Calculte some path
+  init_file_path = os.path.abspath("%s/%s" % (INIT_FOLDER, init_file))
+  patch_path     = os.path.abspath("%s/%s.patch" % (TMP_FOLDER, init_file))
+
+  # Clean-up previous custom patche
+  os.system('rm -f %s' % patch_path)
+
+  # Get current init script content
+  file_object = open(init_file_path, 'r')
+  if file_object == None:
+    raise "FATAL !", "'%s' file not found !" % init_file_path
+  line_count = 0
+
+  # Get old chkconfig levels
+  chkconfig_levels = None
+  while True:
+    line = file_object.readline()
+    if not line:
+      break
+    line_count += 1
+    line = line.strip()
+    if line.startswith("# chkconfig:"):
+      chkconfig_levels = line.split(':')[1].strip().split(' ')[0]
+      if chkconfig_levels == '-':
+        chkconfig_levels = None
+      break
+  if chkconfig_levels == None:
+    chkconfig_levels = "2345"  # default start levels
+
+  # Transform old level list to new LSB compliant start and stop level list
+  start_level_list = []
+  stop_level_list  = []
+  for level in range(7):
+    level = str(level)
+    if level in chkconfig_levels:
+      start_level_list.append(level)
+    else:
+      stop_level_list.append(level)
+  lsb_start_levels = ' '.join(start_level_list)
+  lsb_stop_levels  = ' '.join(stop_level_list)
+
+  # Auto detect the place where the LSB compliant patch should be append in the current init file
+  # At this moment, the file object line is set to "# chkconfig:" position:
+  #   now we just have to iterate until the end of comments.
+  while True:
+    line = file_object.readline()
+    if not line:
+      break
+    line_count += 1
+    line = line.strip()
+    if not (len(line) == 0 or line.startswith('#')):
+      break
+
+  file_object.close()
+
+  print line_count
+
+  # Generate the new LSB patch
+  lsb_patch = """--- %s.orig\t2007-02-26 16:06:05.000000000 +0100
++++ %s\t2007-02-26 16:06:36.000000000 +0100
+@@ -%s,0 +%s,9 @@
++### BEGIN INIT INFO
++# Provides: %s
++# Required-Start: $local_fs $network $remote_fs
++# Required-Stop: $local_fs $network $remote_fs
++# Default-Start: %s
++# Default-Stop: %s
++# Short-Description: %s
++# Description: %s
++### END INIT INFO""" % ( init_file
+                        , init_file
+                        , line_count - 1
+                        , line_count
+                        , init_file
+                        , lsb_start_levels
+                        , lsb_stop_levels
+                        , 'Dummy short description'
+                        , 'Dummy very very very very very very very long description'
+                        )
+
+  # Generate commands to create the patch file and apply it
+  os.system("echo '%s' > %s" % (lsb_patch, patch_path))
+  os.system('patch --directory=%s < %s' % (INIT_FOLDER, patch_path))
+
+  # Clean-up previous custom patche
+  os.system('rm -f %s' % patch_path)




More information about the Erp5-report mailing list