python编写开发更新数据后台脚本
admin
2023-07-24 02:02:13
0

需求:
1、复制 /data/web/s1 并把s1改成对应的开服的区服
2、将s1目录里面的a.py文件里面的s1字符 全部替换成对应的开服的区服id,并给a.py写权限
3、将对应sn目录里面的insert.log do.txt 清空
4、把定时任务中的/5 * python /data/web/s1/a.py >> /data/web/s1/insert.log 复制成对应 的区服 并且追加写入crond

#!/usr/bin/env python
#coding:utf-8
import os
import re
import shutil as copy

copy_web_service = '/data/web/s1'
dest_web_service = '/data/web/s'
all_web_service = '/data/web/'
crontab_file = '/data/update/crontab.txt'
openservice_num = '/data/update/open_num.txt'

#获取开服数字并拷贝一个新目录出来
def copy_newservice():
    global open_num
    all_service = set()
    alredy_open_service = set()
    if not os.path.exists(openservice_num):
        print('%s开服数文件不存在,请检查' % openservice_num)
        exit(1)
    with open('/data/update/open_num.txt','r') as f:
        for file in f.readlines():
            num = file.strip()
    for nums in range(1,int(num)+1):
        all_service.add(nums)
    for service in os.listdir(all_web_service):
        if re.match('s\d+',service):
            alredy_open_service.add(int(service[1:]))
    cha_num = alredy_open_service^all_service
    open_num = list(cha_num)
    if not os.path.exists(copy_web_service):
        print('%s目录不存在,脚本退出' % copy_web_service)
        exit(1)
    else:
        for nums in open_num:
            dest_dir = dest_web_service + str(nums)
            if not os.path.exists(dest_dir):
                copy.copytree(copy_web_service,dest_dir)

#修改a.py脚本区服名字三处
def modify_script():
    script = ''
    for nums in open_num:
        new = 's' + str(nums) + '/'
        dest_dir = dest_web_service + str(nums)
        if not os.path.exists(dest_dir+'/a.py'):
            print('%s脚本不存在,脚本退出' % (dest_dir+'/a.py'))
            exit(1)
        with open(dest_dir+'/a.py') as f:
            for line in f.readlines():
                if line.find(str('s1/')):
                    line = re.sub('s1/',new,line)
                    script += line
                else:
                     script += line
        with open(dest_dir+'/a.py','w') as f:
            f.write(script)
            script = ''

#清空do.txt和insert.log文件
def clear_file():
    kong = ''
    file_list = ['/insert.log','/do.txt']
    for nums in open_num:
        for files in file_list:
            dest_dir = dest_web_service + str(nums)
            if not os.path.exists(dest_dir+files):
                print('%s文件不存在,脚本退出' % (dest_dir+files))
            else:
                with open(dest_dir+files,'w') as f:
                    f.write(kong)

#创建定时任务
def crontab():
    if not os.path.exists(crontab_file):
        print('%s文件不存在,请检查' % crontab_file)
    for nums in open_num:
        task = '*/5 * * * * python /data/web/s' + str(nums) + '/a.py >> /data/web/s' + str(nums) + '/insert.log\n'
        with open(crontab_file,'a') as f:
            f.write(task)
        print('s\033[31m%s\033[0m服已完成操作...' % nums)
    os.system('crontab %s' % crontab_file)

if __name__ == '__main__':
    copy_newservice()
    modify_script()
    clear_file()
    crontab()

相关内容

热门资讯

对话小湃科技 | 家庭智能体3... 近日,深圳小湃科技有限公司以“破界新生 AI护万家”为主题召开品牌战略升级大会,“小湃有钳”Claw...
委内瑞拉不到一天已发生138次... 新华社加拉加斯6月25日电(记者田睿 许咏政)委内瑞拉全国代表大会主席豪尔赫·罗德里格斯25日表示,...
2026年财富中国科技50强出... 【CNMO科技消息】近日,CNMO科技注意到,2026年财富中国科技50强出炉,华为投资控股有限公司...
2026中国移动“牵手计划”全... 6月25日,在2026上海世界移动通信大会期间,中国移动成功举办“牵手计划”(hi-H Progra...
厦钨新能获得实用新型专利授权:... 证券之星消息,根据天眼查APP数据显示厦钨新能(688778)新获得一项实用新型专利授权,专利名为“...
2026年大学生AI影像旗舰手... OPPO数码全家桶 随着2026年高考结束,准大学生们即将开启新的人生篇章,一款功能强大、体验出色的...
原创 为... 文昌航天发射场的301号工位上,一道烈焰划破长空。 2月11日11时00分,长征十号运载火箭点火升空...
共筑健康驿站新生态 第六届星辰... 2026年6月15日至18日,第六届医药流通贸易大会暨第六届医药零售业大会(简称“第六届星辰会”)在...
一个智谱,相当于半个阿里巴巴? “年亏损47亿的智谱,市值跻身万亿港元阵营。 “这个估值能睡得着觉吗?”对于智谱的市值,不少市场人士...
原创 小... 小米新机量越来越多,比如小米17T系列、REDMI K90至尊版等,可谓是新机连连。其中小米17全系...