Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1732662
  • 博文数量: 42
  • 博客积分: 10036
  • 博客等级: 上将
  • 技术积分: 2285
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 17:08
文章存档

2011年(3)

2010年(3)

2009年(5)

2008年(31)

分类: Python/Ruby

2008-07-11 21:49:25

这是来自anaconda-11.1.2.36中一个简单的程序,属于设置系统配置的大钢类下。
#
# desktop.py - install data for default desktop and run level
#
# Matt Wilson
#
# Copyright 2001-2002 Red Hat, Inc.
#
# 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 string  #导入模块string

from rhpl.simpleconfig import SimpleConfigFile #这是个第三类的模块

import logging #导入模块logging
log = logging.getLogger("anaconda")#调用logging的实例getLogger并起名为anaconda

class Desktop (SimpleConfigFile):#定义类Desktop,
#
# This class represents the default desktop to run and the default runlevel
# to start in
#解释了此类的主要实现的目的或功能
    def setDefaultRunLevel(self, runlevel):  #定义setDefaultRunLevel方法
        if str(runlevel) != "3" and str(runlevel) != "5": #判断必须为5
            raise RuntimeError, "Desktop::setDefaultRunLevel() - Must specify runlevel as 3 or 5!"          #raise关键字类似于try...exception
        self.runlevel = runlevel  #

    def getDefaultRunLevel(self): #定义getDefaultRunLevel方法
        return self.runlevel

    def setDefaultDesktop(self, desktop): #定义setDefaultDesktop方法
        self.info["DESKTOP"] = desktop

    def getDefaultDesktop(self): #定义getDefaultDesktop方法
        return self.get("DESKTOP")

    def __init__ (self):        #__init__构造器实现
        SimpleConfigFile.__init__ (self)
        self.runlevel = 3       #定义默认runlevel为3

    def write (self, instPath):  #定义 write方法
        try:
            inittab = open (instPath + '/etc/inittab', 'r') #以读的方式打开/etc/inttab文件(注:instPath是一个全局变量,应是挂在当前系统下磁盘上的/mnt/sysimage目录
        except IOError: #异常抛出
            log.warning ("there is no inittab, bad things will happen!")
            return
        lines = inittab.readlines ()#读取inittab文件
        inittab.close ()
        inittab = open (instPath + '/etc/inittab', 'w')        #以写的方式打开
        for line in lines: #以每行为单位将inittab读入内存
            if len (line) > 3 and line[:3] == "id:": #找到以id:为起始的那一行,且这行要大于3
                fields = string.split (line, ':') #以:切片这一行
                fields[1] = str (self.runlevel)   #在id:后添加runlevel变量,即3(默认)
                line = string.join (fields, ':')  #将id:3合成一行,添加\n
            inittab.write (line)#写入inittab
        inittab.close ()#关闭文件描述符

    f = open(instPath + "/etc/sysconfig/desktop", "w")
    f.write(str (self))
    f.close()#同理,将上面定义的DESKTOP内容,写入desktop文件。



desktop.py主要定义了一个类--Desktop,类是没有生命的,必须被实例化!那么此文的目的就是希望以后遇到程序调用Desktop.*的句点标识法时,能够知道这个程序想干什么,其实功能本身并不复杂,就是决定是那个运行级别3还是5,写入/etc/sysconfig/desktop文件一句话=DESKTOP="xxxx"xxx为KDE或GNOME。

看这段代码的时候,我最大的感慨就是它写入inittab这个文件是干净和利索。因为这是萦绕我一周多的问题,主要是对python的切片和文件输入/输出的理解不够。感谢python,感谢此程序的作者Matt Wilson。
阅读(1440) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~