Python 实现开机自动更新svn
每天早上到公司首先要做的事情就是打开Outlook收邮件、firefox恢复昨天打开的网页、登录即时通信工具、更新项目svn到最新版本、看新闻,最后是开始干活。
由于工作空间项目比较多,大约有十几个项目,所以每次都要手动更新,感觉比较麻烦。就想通过python写一个脚本来实现开机自动给我更新,这样就不用我一个一个来手动更新了。因为之前没用过python所以就去求助Google,经过一天的努力终于搞定了,下面把python脚本代码贴出来,供有需要的同学参考,相互学习哈:-D
1 #coding=utf8 2 3 import subprocess 4 import sys 5 import string 6 import time 7 8 class Log: 9 logfile = ''; 10 def config(self, _logfile):#调用这个方法实现判断日志文件位置 11 if _logfile != '': 12 self.logfile = _logfile; 13 else: 14 self.logfile = 'run.log'; 15 16 def log(self, _msg):#调用此方法实现保存记录到文件中去 17 now = time.time() 18 strtime = str(time.localtime(now)[0]) + '-' + str(time.localtime(now)[1]) \ 19 + '-' + str(time.localtime(now)[2]) \ 20 + ' ' + str(time.localtime(now)[3]) \ 21 + ':' + str(time.localtime(now)[4]) \ 22 + ':' + str(time.localtime(now)[5]); 23 msg = strtime + ' ' + _msg + '\n'; 24 try: 25 logfilehandle = open(self.logfile, 'a+');#返回一个文件句柄的 26 logfilehandle.write(msg);#write file 27 logfilehandle.close(); 28 except: 29 exit(0); 30 31 def daemonize(): 32 run('asset1.0_tank\\') 33 run('mysohu1.0\\') 34 run('mysohu1.0_html\\') 35 run('mysohu1.5_html\\') 36 run('mysohu1.6_product\\') 37 run('suc_home\\') 38 run('suc-home-branches\\') 39 run('suc-person\\') 40 run('suc-person-branches\\') 41 run('sucux_branch\\') 42 run('sucux_trunk\\') 43 run('toolbar\\') 44 45 def run(path): 46 log = Log() 47 logfile = 'D:\\workspace\\svn-up.log' 48 dir = 'D:\\workspace\\' 49 subprocess.call('svn cleanup ' + dir + path) 50 cur = path.replace('\\', '') 51 print (cur + ' 已更新') 52 subprocess.call('svn up ' + dir + path) 53 print ('=============================') 54 #记录更新的日志 55 log.config(logfile) 56 log.log(cur + ' 已更新') 57 if cur == 'toolbar': 58 log.log('================================') 59 60 daemonize()
由于我自己电脑系统是win7所以这里的python代码只能在windows平台运行,要想运行这个脚本需要以下工具支持:
1. python
2. Slik SVN
首先需要配置好环境,然后把代码中的项目路劲改成自己的,最后把该脚本文件放到 开始 -》 启动 里面就可以了,每次开机后会自动运行该python脚本。