Record a self updating program written by yourself

Posted by robin105 on Mon, 16 Dec 2019 22:41:35 +0100

update.py

# -*- coding: UTF-8 -*-
import sys
import subprocess

update = open(u"upgrade.bat", 'w')

bat_list = u"choice /t 3 /d y /n >nul\n"
bat_list += u'chcp 65001\n'
bat_list += u'xcopy \\\\storage\\CG core\\performance appraisal\\PerformancePlugin\\TimeLog\\autoUpdate\\perUpdate\\*.* "%~dp0" ' \
            u'/e/s/d/y\n'
bat_list += u'md "C:\\cgteamwork\\python"\n'
bat_list += u'xcopy \\\\storage\\CG core\\performance appraisal\\PerformancePlugin\\TimeLog\\autoUpdate\\python\\*.* ' \
            u'"C:\\cgteamwork\\python\\" /e/s/d/y\n'
bat_list += u'"C:\cgteamwork\python\python.exe" "%~dp0copy.py"\n'
bat_list += u'"C:\cgteamwork\python\python.exe" "%~dp0winshellcopy.py"\n'
bat_list += u'"C:\cgteamwork\python\python.exe" "%~dp0desktop.py"\n'
bat_list += u'chcp 437\n'
bat_list += u'schtasks /delete /tn autoTimeLog /f\n'
bat_list += u'schtasks /create /tn autoTimeLog /sc daily /st 17:45:00 /tr "C:\Performance210\Performance.exe"\n'
bat_list += u'schtasks /query /xml /tn autoTimeLog > sch_autoTimeLog.xml\n'
bat_list += u'"C:\cgteamwork\python\python.exe" "%~dp0rexml.py"\n'
bat_list += u'schtasks /create /tn autoTimeLog /xml sch_autoTimeLog.xml /F\n'
bat_list += u'chcp 65001\n'
bat_list += u'del "%~dp0copy.py"\n'
bat_list += u'del "%~dp0desktop.py"\n'
bat_list += u'del "%~dp0rexml.py"\n'
bat_list += u'del sch_autoTimeLog.xml\n'
bat_list += u'del "%~dp0winshellcopy.py"\n'
bat_list += u'cd C:\Performance210\"\n'
bat_list += u'start Performance.exe\n'
bat_list += u'del %0\n'

update.write(bat_list.encode('utf-8'))
update.close()
subprocess.Popen(u"upgrade.bat")
sys.exit()

The main task is to automatically update and then record several points that need attention:

  1. start Performance.exe the start here is to make the program start the same as double-click, otherwise there may be problems in starting performance.exe directly
  2. schtasks /query /xml /tn autoTimeLog > sch_autoTimeLog.xml
    "C:\cgteamwork\python\python.exe" "%~dp0rexml.py"
    schtasks /create /tn autoTimeLog /xml sch_autoTimeLog.xml /F
    It is necessary to set the starting position in the auto start plan so as not to cause any problems. The rmxml.py code will be pasted out. Because of the coding problem of exported xml, the processing here is a bit of a mess
  3. chcp 65001 and chcp 437 are used to avoid coding errors when bat files are started. The first one is for utf-8, and the second one is for schtasks cannot run in Chinese bat environment
  4. Choice / T 3 / D Y / N > nul is to run the update script after the end of the current program, delaying 3 seconds to prevent conflicts

rmxml.py

import os, re

fo = open(u"sch_autoTimeLog.xml", u"r")
data = fo.read()
# dir_curr = os.getcwd() + '\\Performance'
dir_curr = "C:\\Performance210"
# print data
data = re.sub(r'</Command>', r'</Command>\n\n      <WorkingDirectory>' + dir_curr + r'</WorkingDirectory>', data)
# print data
fo.close()
fo = open(u"sch_autoTimeLog.xml", u"w")
fo.write(data)
fo.close()

desktop.py

import winshell
import os

# aim_path = os.getcwd()
# aim_path += "\\Performance"
aim_path = "C:\\Performance210"

target=aim_path+"\\Performance.exe"
title="time_log"
origin=aim_path

s = os.path.basename(target)
fname = os.path.splitext(s)[0]
winshell.CreateShortcut(
    Path=os.path.join(winshell.desktop(), fname + '.lnk'),
    # Path=os.path.join(os.getcwd(), fname + '.lnk'),
    Target=target,
    StartIn=origin,
    Icon=(target, 0),
    Description=title)

winshellcopy.py

#! /usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import shutil
import subprocess
import re
import xml.etree.ElementTree as ET

def my_copy_file(p_src_file, p_dst_file):
    if not os.path.isfile(p_src_file):
        return
    else:
        f_path, f_name = os.path.split(p_dst_file)  # Separating file names and paths
        if not os.path.exists(f_path):
            os.makedirs(f_path)  # Create path
        shutil.copyfile(p_src_file, p_dst_file)  # Copy file

xml_file_path = u"\\\\storage\\CG core\\performance appraisal\\PerformancePlugin\\TimeLog\\autoUpdate\\winshell.xml"
aim_path = u"C:\\cgteamwork\\python\\Lib\\site-packages"

if os.path.exists(xml_file_path):
    tree = ET.parse(xml_file_path)
    root = tree.getroot()
    full_path_list = []
    for each_item in root[0][0].findall('full_path_ele'):
        full_path_list.append(each_item.text)

    aim_path_list = []
    for each_item in root[0][1].findall('aim_path_ele'):
        aim_path_list.append(aim_path + each_item.text)

    for I in range(0, len(full_path_list)):
        my_copy_file(full_path_list[I], aim_path_list[I])

copy.py

#! /usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import shutil
import subprocess
import re
import xml.etree.ElementTree as ET

def my_copy_file(p_src_file, p_dst_file):
    if not os.path.isfile(p_src_file):
        return
    else:
        f_path, f_name = os.path.split(p_dst_file)  # Separating file names and paths
        if not os.path.exists(f_path):
            os.makedirs(f_path)  # Create path
        shutil.copyfile(p_src_file, p_dst_file)  # Copy file

xml_file_path = u"\\\\storage\\CG core\\performance appraisal\\PerformancePlugin\\TimeLog\\autoUpdate\\updateInfo.xml"
# aim_path = os.getcwd()
# aim_path += "\\Performance"
aim_path = "C:\\Performance210"

if os.path.exists(xml_file_path):
    tree = ET.parse(xml_file_path)
    root = tree.getroot()
    full_path_list = []
    for each_item in root[0][0].findall('full_path_ele'):
        full_path_list.append(each_item.text)

    aim_path_list = []
    for each_item in root[0][1].findall('aim_path_ele'):
        aim_path_list.append(aim_path + each_item.text)

    for I in range(0, len(full_path_list)):
        my_copy_file(full_path_list[I], aim_path_list[I])

 

Topics: Python xml