2006年03月31日 星期五 14:41
说得有理。其实用dictionary更容易,而且还可以交互式,现推出第二个改进后的脚本: #!/usr/bin/python import os ''' firstly,you should define a dict for assign some permission''' permission_group={ 'manager': 'BROWSER_VIEW,CHANGESET_VIEW,CONFIG_VIEW,FILE_VIEW,LOG_VIEW,MILESTONE_ADMIN,MILESTONE_CREATE,MILESTONE_DELETE,MILESTONE_MODIFY,MILESTONE_VIEW,REPORT_ADMIN,REPORT_CREATE,REPORT_DELETE,REPORT_MODIFY,REPORT_SQL_VIEW,REPORT_VIEW,ROADMAP_ADMIN,ROADMAP_VIEW,SEARCH_VIEW,TICKET_ADMIN,TICKET_APPEND,TICKET_CHGPROP,TICKET_CREATE,TICKET_MODIFY,TICKET_VIEW,TIMELINE_VIEW,TRAC_ADMIN,WIKI_ADMIN,WIKI_CREATE,WIKI_DELETE,WIKI_MODIFY,WIKI_VIEW', 'developer': '', 'anonymous': 'BROWSER_VIEW,LOG_VIEW,FILE_VIEW,CHANGESET_VIEW,TICKET_VIEW,MILESTONE_VIEW,ROADMAP_VIEW,REPORT_VIEW,WIKI_VIEW,TIMELINE_VIEW,SEARCH_VIEW', } ''' then you should define some person ''' person={ 'team1': 'alexxu,arielgu,ericgao,geminisshao,thunderzhang,junzhao,rogersong,johnshen,jackytian,sanzhong,yitangcheng', 'team2': '', } ''' main ''' for name,item in person.items(): if item == '': continue print 'The <<< %s >>> contains %s \n' %(name,item) team=raw_input('choose team which your want to do? ') for name,item in permission_group.items(): if item == '':continue print 'The <<< %s >>> permission is :\n %s\n' %(name,item) group=raw_input('What permission do you want to do? ') for permit in permission_group[group].split(','): print '%s -- %s ' %(group,permit) command = 'trac-admin /var/trac permission add %s %s' %(group,permit) if os.system(command) == 0: print command,' Success' else: print command,'Failed' for pers in person[team].split(','): print '%s -- %s ' %(pers,group) command='trac-admin /var/trac permission add %s %s' %(pers,group) if os.system(command) == 0: print command,' Success' else: print command,'Failed' -----原始邮件----- 发件人: python-chinese-bounces at lists.python.cn [mailto:python-chinese-bounces at lists.python.cn]代表 limodou 发送时间: 2006年3月31日 13:47 收件人: python-chinese at lists.python.cn 主题: Re: [python-chinese] 关于使用python编写一个trac权限的小脚本,希望 能改进 On 3/31/06, GeminisShao at viatech.com.cn <GeminisShao at viatech.com.cn> wrote: > > 因为在trac中只能使用trac-admin命令一个个提交权限,很麻烦,所以就想写一个脚本来搞定它,如果是在以前,我就会用shell来做,现在我想用python来锻练一下: > > trac-access-file content as follow: > permission_all = > BROWSER_VIEW,CHANGESET_VIEW,CONFIG_VIEW,FILE_VIEW,LOG_VIEW,MILESTONE_ADMIN,MILESTONE_CREATE,MILESTONE_DELETE,MILESTONE_MODIFY,MILESTONE_VIEW,REPORT_ADMIN,REPORT_CREATE,REPORT_DELETE,REPORT_MODIFY,REPORT_SQL_VIEW,REPORT_VIEW,ROADMAP_ADMIN,ROADMAP_VIEW,SEARCH_VIEW,TICKET_ADMIN,TICKET_APPEND,TICKET_CHGPROP,TICKET_CREATE,TICKET_MODIFY,TICKET_VIEW,TIMELINE_VIEW,TRAC_ADMIN,WIKI_ADMIN,WIKI_CREATE,WIKI_DELETE,WIKI_MODIFY,WIKI_VIEW > > team_all = > alexxu,arielgu,ericgao,geminisshao,thunderzhang,junzhao,rogersong,johnshen,jackytian,sanzhong,yitangcheng > > permission_allow = > BROWSER_VIEW,LOG_VIEW,FILE_VIEW,CHANGESET_VIEW,TICKET_VIEW,MILESTONE_VIEW,ROADMAP_VIEW,REPORT_VIEW,WIKI_VIEW,TIMELINE_VIEW,SEARCH_VIEW > > > 我已经编写了一个脚本permission.py: > > #!/usr/bin/python > import os > f = open("/etc/apache2/trac-access-file","r"); > str = f.read() > for line in str.split('\n'): > if line == '': > continue > > elif line[0:8] == 'team_all': > for unit in line[11:].split(','): > command= "trac-admin /var/trac permission add %s > team_all anonymous " %(unit) > if os.system(command) == 0: > print command,' Success' > else: > print command,'Failed' > continue > > elif line[0:16] == 'permission_allow': > for unit in line[19:].split(','): > command= "trac-admin /var/trac permission add > team_all %s" %(unit) > command2= "trac-admin /var/trac permission add > anonymous %s" %(unit) > if os.system(command) == 0 or > os.system(command2)==0: > print command,' Success' > else: > print command,'Failed' > continue > > elif line[0:14] == 'permission_all': > for unit in line[17:].split(','): > command= "trac-admin /var/trac permission remove > anonymous %s" %(unit) > if os.system(command) == 0: > print command,' Success' > else: > print command,'Failed' > continue > > 我这个脚本只能算个简单的工作脚本而已,肯定会有高人能提出改进的意见,请不吝赐教,给思路也行。谢谢 > 不错。就是取名字的时候你使用定长。比较好的方式是使用splite('=', 1)来处理,这样按=进行拆分。然后再去掉首尾空格就好了。使用定长的话一旦前面的串名发生变化,改动就比较多,而且不通用。 另外权限可以与用户分开,这样互不影响。另外还可以有两种工作模式,一种是预设好的,即有哪些组,有哪些权限。一种是运行后再修改,这样可以提供一个交互环境。 -- I like python! My Blog: http://www.donews.net/limodou My Django Site: http://www.djangocn.org NewEdit Maillist: http://groups.google.com/group/NewEdit
Zeuux © 2025
京ICP备05028076号