confapi.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # @File : confapi.py
  4. # @Author: becivells
  5. #@Contact : becivells@gmail.com
  6. # @Date : 2017/12/6
  7. #@Software : PyCharm
  8. # @Desc :
  9. import os
  10. import sys
  11. import json
  12. import shutil
  13. import argparse
  14. BASE_DIR = os.path.dirname(os.path.realpath(__file__))
  15. conf_path = BASE_DIR + os.sep + 'git.conf'
  16. git_serverip = '123.206.177.125'
  17. def addconf(uid,name,path,ip=None,user='apache',interval=3,cmd='git pull origin master',update=False):
  18. if isinstance(ip,list):
  19. ip.append(git_serverip)
  20. _ip = ','.join('"%s"'%(i) for i in ip)
  21. else:
  22. _ip = '"{git_serverip}"'.format(git_serverip=git_serverip)
  23. repo = '''{{
  24. "{uid}": {{
  25. "name": "{name}",
  26. "path": "{path}",
  27. "ip": ["127.0.0.1",{ip}],
  28. "cmd": "{cmd}",
  29. "user": "{user}",
  30. "interval": {interval}
  31. }}
  32. }}'''.format(uid=uid, name=name, path=path,ip=_ip,user=user,cmd=cmd,interval=interval)
  33. repos = getconf()
  34. if uid in repos and not update:
  35. print ('uuid is exist if you want to update please use --update')
  36. print (json.dumps({uid:(repos.get(uid))},indent=4))
  37. print ('----------error---------')
  38. sys.exit(-1)
  39. with open(conf_path,'a') as f:
  40. f.write(json.dumps(json.loads(repo)))
  41. f.write('\n')
  42. print (repo)
  43. def getconf():
  44. _conf_dict = {}
  45. with open(conf_path,'r') as f:
  46. for line in f:
  47. if line.strip():
  48. _conf_dict.update(json.loads(line))
  49. else:
  50. print ('blank line')
  51. return _conf_dict
  52. def check():
  53. repo_dict = getconf()
  54. flag = False
  55. shutil.copy(conf_path,conf_path+'.bak')
  56. with open(conf_path,'w') as f:
  57. for k,v in repo_dict.items():
  58. f.write(json.dumps({k:v}))
  59. f.write('\n')
  60. # print(json.dumps(getconf(),indent=4))
  61. def parse_cmd_args():
  62. '''处理命令行选项
  63. '''
  64. import argparse
  65. import uuid
  66. parser = argparse.ArgumentParser(
  67. prog='add gitconf',
  68. description='add repo')
  69. parser.add_argument("--uid", dest='uid', action="store",
  70. help="repo sync uid")
  71. parser.add_argument("--update", dest='update', action="store",
  72. # default= str(uuid.uuid1()), # 不能有默认值
  73. default=False,
  74. help="update date")
  75. parser.add_argument("--cmd", dest='cmd', action="store",
  76. default= 'git pull origin master', # 不能有默认值
  77. help=("please use --cmd \"git pull origin master\" "
  78. "not --cmd 'git pull origin master' .etc "))
  79. parser.add_argument("--name", action="store",
  80. dest='name',
  81. help="repo name")
  82. parser.add_argument("--path", action="store",
  83. dest='path',
  84. help="repo name")
  85. parser.add_argument('--ip',
  86. action="store",
  87. nargs='+',
  88. default=git_serverip,
  89. help="allow access ip address")
  90. parser.add_argument("--user", action="store_true", default='apache',
  91. dest='user',
  92. help="user")
  93. parser.add_argument('--interval',
  94. action="store",
  95. default='3',
  96. dest='interval',
  97. help='interval default 3s',type=int)
  98. args = parser.parse_args()
  99. if not (args.uid and args.name and args.path):
  100. parser.print_help()
  101. sys.exit(0)
  102. return args
  103. if __name__ == '__main__':
  104. args = parse_cmd_args()
  105. cmd = args.cmd
  106. if args.update:
  107. print('update data')
  108. addconf(args.uid, args.name, args.path, ip=args.ip, user=args.user,
  109. interval=args.interval,cmd=cmd, update=True)
  110. check()
  111. else:
  112. addconf(args.uid, args.name, args.path, ip=args.ip, user=args.user,
  113. interval=args.interval,cmd=cmd, update=False)