免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 11263 | 回复: 0
打印 上一主题 下一主题

My first python program --append kernel-parameter [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-10 11:54 |只看该作者 |倒序浏览
全文如下:最初的版本,实现的功能有限,也少必要的参数检测,持续更行中!
用到了构建命令行工具的模块optparse.文字处理string.对文件描述符的操作。
#!/usr/bin/env python
#
#akpw_grub.py:append kernel-parameters with grub.conf
#
# (in alphabetical order...)
#
#lijiansheng(lijiangsheng1@gmail.com)
#
# Maybe  And many others
#
#
# This software may be freely redistributed under the terms of the GNU
# library public license.
#
# You should have received a copy of the GNU Library Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import optparse
import ConfigParser
import re,os,sys
import string
f = open('/etc/grub.conf','r')
clines = f.readlines()
f.close()
dictory =[
("noacpi"),
("acpi=off")
]
def list_current():
    print """************************below is your current running Linux OS kernel-parameterse************************
       """
    if os.access("/proc/cmdline", os.R_OK):
       path = "/proc/cmdline"
    else:
       path = None;
    if path is not None:
         f = open(path, "r")
         lines = f.readlines()
         parameter=lines[0]
    print  parameter
#next regular match so output all kernel-parameters   
    print """********************below is your current grub.conf about kernel-parameterse*****************************
       """
    if os.access("/etc/grub.conf", os.R_OK):
       grub = "/etc/grub.conf"
    else:
       grub = None;
    if grub is not None:
         f = open(grub, "r")
         lines = f.readlines()
    for i in xrange(len(lines)):
        if lines[0] != "#":
           line = string.strip(lines)
           try:
               line = string.split(line,'#')[0]
           except: pass
           tokens = string.split(line)
#           if tokens[0] == "kernel":
           if lines[1] == "k" or lines[0] == "k":
              print lines
    f.close()
#           print tokens
    if os.access("/etc/grub.conf" , os.W_OK):
       print """****************************************Now, go on
             """         
    else:
         print """unknown error
               """
    append_new()
def append_new():
    selection = raw_input("Please Input Your Kernel-Parameter:\n")
    print ("your input kernel parameter is " + selection )
    yes_no = raw_input("are you sure add this parameter?(yes/no) " )
    if yes_no == "yes":   
       fd = open('/etc/grub.conf', 'r')
       clines = fd.readlines()
       for i in xrange(len(clines)):
           if clines[0] != "#":
              line = string.strip(clines)
              try:
                  line = string.split(line,'#')[0]
              except: pass
              tokens = string.split(line)
#       for strline in clines:
           if clines[1] == "k" or clines[0] == "k":
#           if strline[:6] == "kernel":
              stringline = line.replace(line,line + ' %s\n' % selection)
              print stringline
       fd.close()
       fd1 = open('/etc/grub.conf', 'w')
      
       for i in xrange(len(clines)):
           if clines[0] != "#":
              line = string.strip(clines)
              try:
                  line = string.split(line,'#')[0]
              except: pass
              tokens = string.split(line)
           if clines[1] == "k" or clines[0] == "k":
              stringline = line.replace(line,line + ' %s\n' % selection)
              fd1.writelines(stringline)
           else:
              if clines[0] != "#":
                 line = string.strip(clines)
                 try:
                    line = string.split(line,'#')[0]
                 except: pass
                 fd1.writelines(['%s' % line + '\n'])  
#              print 'next is your changed kernel %s' % stringline
           
       fd1.close()
def main():
    """Welcome to use redflag server,enjoy it! """
  
    p = optparse.OptionParser(description='change your boot kernel-parameterse and append your configure file,like grub',
                                    prog='akpw_grub',
                                    version='akpw_grub 0.1;write by lijiangsheng1@gmail.com',
                                    usage= '%prog [options] arg ')
    p.add_option('--add','-a',help='append kernel-parameters and work immediately.')
    p.add_option('--list','-l',default=True,help='list current kernel-parameters')
    options, arguments = p.parse_args()
   
    if options.add:
        append_new()
    elif options.list:
         list_current()
if __name__ == '__main__':
   if os.getuid() != 0:
       print """you must be run this program as root!!"""
       sys.exit(10)   
   main()


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/6303/showart_1077624.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP