python编写开发更新数据后台脚本

  

需求:
1,复制/数据/web/s1并把s1改成对应的开服的区服
2,将s1目录里面的。py文件里面的s1字符全部替换成对应的开服的区服id,并给a.py写权限
3,将对应sn目录里面的插入。日志。txt清空
4,把定时任务中的/5 * python/数据/web/s1/y祝辞祝辞/数据/web/s1/插入。日志复制成对应的区服并且追加写入crond

  
 <代码> # !/usr/bin/env python
  utf - 8编码:
  进口操作系统
  进口再保险
  进口shutil副本
  
  copy_web_service='/数据/web/s1 '
  dest_web_service='/数据/web/s '
  all_web_service='/数据/web/'
  crontab_file='/数据/更新/crontab.txt '
  openservice_num='/数据/更新/open_num.txt '
  
  #获取开服数字并拷贝一个新目录出来
  def copy_newservice ():
  全球open_num
  all_service=组()
  alredy_open_service=组()
  如果不是os.path.exists (openservice_num):
  打印(' % s开服数文件不存在,请检查' % openservice_num)
  退出(1)
  张开('/数据/更新/open_num.txt ', ' r ') f:
  文件在f.readlines ():
  num=file.strip ()
  num的范围(1,int (num) + 1):
  all_service.add (num)
  服务在os.listdir (all_web_service):
  如果re.match (\ d +”,服务):
  alredy_open_service.add (int(服务[1:]))
  cha_num=alredy_open_service ^ all_service
  open_num=列表(cha_num)
  如果不是os.path.exists (copy_web_service):
  打印(' % s目录不存在,脚本退出“% copy_web_service)
  退出(1)
  其他:
  open_num num的:
  dest_dir=dest_web_service + str (num)
  如果不是os.path.exists (dest_dir):
  copy.copytree (copy_web_service dest_dir)
  
  #修改a.py脚本区服名字三处
  def modify_script ():
  脚本="
  open_num num的:
  新=s + str (num) + '/'
  dest_dir=dest_web_service + str (num)
  如果不是os.path.exists (dest_dir +/a.py):
  打印(' % s脚本不存在,脚本退出“% (dest_dir + '/a.py '))
  退出(1)
  张开(dest_dir +/a.py) f:
  线的f.readlines ():
  如果line.find (str (s1/)):
  行=re.sub (s1/,新的线)
  脚本+=行
  其他:
  脚本+=行
  张开(dest_dir +/a.py ', ' w ') f:
  f.write(脚本)
  脚本="
  
  #清空do.txt和insert.log文件
  def clear_file ():
  香港="
  file_list=['/insert.log ', '/do.txt ']
  open_num num的:
  在file_list文件:
  dest_dir=dest_web_service + str (num)
  如果不是os.path.exists (dest_dir +文件):
  打印(' % s文件不存在,脚本退出“% (dest_dir +文件))
  其他:
  张开(dest_dir +文件,“w”) f:
  f.write(香港)
  
  #创建定时任务
  def crontab ():
  如果不是os.path.exists (crontab_file):
  打印(' % s文件不存在,请检查' % crontab_file)
  open_num num的:
  任务=' */5 * * * *的python/数据/web/s + str (num) + '/a。py祝辞祝辞/数据/web/s ' + str (num) + '/insert.log \ n '
  张开(crontab_file a) f:
  f.write(任务)
  打印(033 ' s \ [31 m % s 033 \ [0 m服已完成操作…' % num)
  操作系统。系统(的crontab % s % crontab_file)
  
  if __name__==癬_main__”:
  copy_newservice ()
  modify_script ()
  clear_file ()
  crontab()  

python编写开发更新数据后台脚本