#! /usr/bin/env python import os import os.path import sys import time import re import logging from popen2 import popen2 def test_x(filename): return os.path.isfile(filename) def test_f(filename): return os.path.isfile(filename) """recreation of grub-install in python scripting used to create helper code for pygtk grub conf editor """ class GrubShell: # Initialize some variables. prefix= "/usr" exec_prefix= prefix sbindir= exec_prefix+"/sbin" grub_shell= sbindir+"/grub" grub_prefix= "/boot/grub" logging.basicConfig() log = logging.getLogger("grub") log.setLevel( logging.INFO) def run_grub(self, args, *input): # stdout.readlines of command return self._run_grub_shell(args, input) def run_grub_shell(self, args, input): # stdout.readlines of command command= self.grub_shell+" --batch "+args self.log.info( command) child_stdout, child_stdin= popen2( command) for line in input: self.log.info( "grub> %s", line) child_stdin.write(line+"\n") child_stdin.write("quit\n") child_stdin.close() stdout_lines= child_stdout.readlines() child_stdout.close() return stdout_lines def has_grub_error(self, stdout_lines): # boolean for line in stdout_lines: if re.match( "Error [0-9]*: ", line): return True return False class GrubError( Exception): pass def run2(self, args, *input): # stdout.readlines of command ; GrubError result= self.run_grub_shell(args, input) if self.has_grub_error( result): raise GrubShell.GrubError( result) def run1(self, argv): # stdout.readlines of command ; GrubError args= "" cmds= [] rest= False for item in argv: if rest: cmds.append(item) elif re.match("^--$", item): rest= True elif re.match("^-.*", item): args += "'"+item+"'" else: cmds.append(item) rest= True result= self.run_grub_shell(args, cmds) if self.has_grub_error( result): raise GrubShell.GrubError( result) return result def run(self, argv): return self.run1(argv) if __name__ == "__main__": for line in GrubShell().run(sys.argv[1:]): print line, ## --root-directory=DIR ## (meaning:) a separate partion to host /boot/grub/ files