123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # @File : confapi.py
- # @Author: becivells
- #@Contact : becivells@gmail.com
- # @Date : 2017/12/6
- #@Software : PyCharm
- # @Desc :
- import os
- import sys
- import json
- import shutil
- import argparse
- BASE_DIR = os.path.dirname(os.path.realpath(__file__))
- conf_path = BASE_DIR + os.sep + 'git.conf'
- git_serverip = '123.206.177.125'
- def addconf(uid,name,path,ip=None,user='apache',interval=3,cmd='git pull origin master',update=False):
- if isinstance(ip,list):
- ip.append(git_serverip)
- _ip = ','.join('"%s"'%(i) for i in ip)
- else:
- _ip = '"{git_serverip}"'.format(git_serverip=git_serverip)
- repo = '''{{
- "{uid}": {{
- "name": "{name}",
- "path": "{path}",
- "ip": ["127.0.0.1",{ip}],
- "cmd": "{cmd}",
- "user": "{user}",
- "interval": {interval}
- }}
- }}'''.format(uid=uid, name=name, path=path,ip=_ip,user=user,cmd=cmd,interval=interval)
- repos = getconf()
- if uid in repos and not update:
- print ('uuid is exist if you want to update please use --update')
- print (json.dumps({uid:(repos.get(uid))},indent=4))
- print ('----------error---------')
- sys.exit(-1)
- with open(conf_path,'a') as f:
- f.write(json.dumps(json.loads(repo)))
- f.write('\n')
- print (repo)
- def getconf():
- _conf_dict = {}
- with open(conf_path,'r') as f:
- for line in f:
- if line.strip():
- _conf_dict.update(json.loads(line))
- else:
- print ('blank line')
- return _conf_dict
- def check():
- repo_dict = getconf()
- flag = False
- shutil.copy(conf_path,conf_path+'.bak')
- with open(conf_path,'w') as f:
- for k,v in repo_dict.items():
- f.write(json.dumps({k:v}))
- f.write('\n')
- # print(json.dumps(getconf(),indent=4))
- def parse_cmd_args():
- '''处理命令行选项
- '''
- import argparse
- import uuid
- parser = argparse.ArgumentParser(
- prog='add gitconf',
- description='add repo')
- parser.add_argument("--uid", dest='uid', action="store",
- help="repo sync uid")
- parser.add_argument("--update", dest='update', action="store",
- # default= str(uuid.uuid1()), # 不能有默认值
- default=False,
- help="update date")
- parser.add_argument("--cmd", dest='cmd', action="store",
- default= 'git pull origin master', # 不能有默认值
- help=("please use --cmd \"git pull origin master\" "
- "not --cmd 'git pull origin master' .etc "))
- parser.add_argument("--name", action="store",
- dest='name',
- help="repo name")
- parser.add_argument("--path", action="store",
- dest='path',
- help="repo name")
- parser.add_argument('--ip',
- action="store",
- nargs='+',
- default=git_serverip,
- help="allow access ip address")
- parser.add_argument("--user", action="store_true", default='apache',
- dest='user',
- help="user")
- parser.add_argument('--interval',
- action="store",
- default='3',
- dest='interval',
- help='interval default 3s',type=int)
- args = parser.parse_args()
- if not (args.uid and args.name and args.path):
- parser.print_help()
- sys.exit(0)
- return args
- if __name__ == '__main__':
- args = parse_cmd_args()
- cmd = args.cmd
- if args.update:
- print('update data')
- addconf(args.uid, args.name, args.path, ip=args.ip, user=args.user,
- interval=args.interval,cmd=cmd, update=True)
- check()
- else:
- addconf(args.uid, args.name, args.path, ip=args.ip, user=args.user,
- interval=args.interval,cmd=cmd, update=False)
|