python对ini文件配置脚本

 1 #!/usr/bin/env python
 2 #
 3 
 4 import argparse
 5 import ConfigParser
 6 import os
 7 import sys
 8 
 9 
10 #print(sys.argv[1:])
11 
12 if __name__ == '__main__':
13     parser = argparse.ArgumentParser(description='Run ./build when present.')
14     parser.add_argument('--project', type=str,  metavar='PROJECT_NAME', required=True)
15     parser.add_argument('--refname', type=str, required=True)
16     parser.add_argument('--inipath', type=str, required=True)
17     parser.add_argument('--cmd', type=str, required=True)
18     args = parser.parse_args()
19 
20     if os.path.exists(args.inipath) == False:
21             sys.exit(1)
22 
23     conf = ConfigParser.ConfigParser()
24     conf.read(args.inipath)
25 
26 
27     lastch = args.refname.rindex('/')
28     branch = args.refname[lastch + 1:]
29 
30     if conf.has_section(args.project) == False:
31         conf.add_section(args.project)
32 
33     if args.cmd == 'lock' :
34         conf.set(args.project, branch, "lock")
35     else:
36         conf.set(args.project, branch, "unlock")
37     conf.write(open(args.inipath,"w"))
38     sys.exit(0)
39     #conf.get(args.project, branch)
40     #has_option(section, option)
41     #remove_option(section, option)
42     #remove_section(section)

 

python对INI文件操作的,会把大写改成小写,重写类

class myconf(ConfigParser.ConfigParser):
  def __init__(self,defaults=None):
    ConfigParser.ConfigParser.__init__(self,defaults=None)
  def optionxform(self, optionstr):
    return optionstr

 

posted @ 2021-10-12 16:51  咸菜白饭  阅读(8)  评论(0)    收藏  举报