python3调用windows dos命令的例子

(编辑:jimmy 日期: 2024/11/13 浏览:2)

最近游戏项目在多个国家上线,每个国家都对应两份儿svn目录(一份是本地策划目录,一份是线上目录)。于是乎维护变得很烦躁。需要先更新本地策划svn目录,然后把更新的文件拷贝到对应的线上目录,然后提交线上svn目录,然后维护服务器。多个国家就要重复多次类似的更新,拷贝,提交的操作,还要格外注意不能手抖,出现少复制的错误。这种重复的操作很适合写一个工具来完成。

于是考虑使用python来写这个工具,最基本的操作就是使用python调用svn命令。因为windows安装svn后是没有svn命令行的,所以首先需要下载Apache-Subversion,这是svn命令行工具,解压后设置环境变量。

python调用dos命令,使用下面的方法:

os.popen(command)

此方法会阻塞,直到执行完成,当然也会返回执行的结果。如果你使用chcp设置过windows命令行的代码页,比如曾经使用:chcp 65001。那么在执行python程序的时候就可能产生gbk编码的错误,解决此问题需要重新使用:chcp 936来设置命令行代码页就OK了。

完整代码如下:

import os
import shutil

'''

循环执行每个一个资源目录,每个目录的执行过程如下:

  1:更新线上目录
  2:更新本地目录
  3:把本地目录的修改同步到线上目录
  4:提交线上目录

'''

#本地svn目录
local_r_en = "server/trunk/resource"
local_r_ar = "server/resource_ar"
local_r_fr = "server/resource_fr"
local_r_ge = "server/resource_ge"
local_r_ita = "server/resource_ita"
local_r_ru = "server/resource_ru"
local_r_sp = "server/resource_sp"
local_r_tr = "server/resource_tr"
local_r_wp_en = "server/resource_wp"

#线上svn目录
online_r_en = "serverOL/resource_en"
online_r_ar = "serverOL/resource_ar"
online_r_fr = "serverOL/resource_fr"
online_r_ge = "serverOL/resource_ge"
online_r_ita = "serverOL/resource_ita"
online_r_ru = "serverOL/resource_ru"
online_r_sp = "serverOL/resource_sp"
online_r_tk = "serverOL/resource_tk"
online_r_wp_en = "serverOL/resource_wp"


#不需要更新的服务器,请把下面对应的资源目录注释掉
recource_dict = {}
recource_dict[local_r_en] = online_r_en       #美国
# recource_dict[local_r_ar] = online_r_ar      #阿拉伯
# recource_dict[local_r_fr] = online_r_fr      #法国
# recource_dict[local_r_ge] = online_r_ge      #德国
# recource_dict[local_r_ita] = online_r_ita     #意大利
# recource_dict[local_r_ru] = online_r_ru      #俄罗斯
# recource_dict[local_r_sp] = online_r_sp      #西班牙
# recource_dict[local_r_tr] = online_r_tk      #土耳其
# recource_dict[local_r_wp] = online_r_wp    #wp



'''
复制文件到目的文件
'''
def copyFile(src,dst):
  #目的目录
  dst_dir = dst[:dst.rfind("/")]

  #如果目录不存在则创建目录,如果文件存在则删除
  if not os.path.exists(dst_dir):
    os.makedirs(dst_dir)
  elif os.path.exists(dst):
    os.remove(dst)

  #复制新文件
  shutil.copyfile(src, dst)
#end method copyFile

'''
删除文件
'''
def removeFile(dst):
  if os.path.exists(dst):
    os.remove(dst)
#end method removeFile


'''
打印命令结果
'''
def svnResult(result):
  for line in result:
    line = line.replace("\n","")
    print(line)
#end method svnResult


'''
svn命令执行
'''
def svnSimpleExecute(command):
  print("",command)
  result = os.popen(command).readlines()
  svnResult(result)
#end method svnExecute


'''
svn提交目录
'''
def svnCommit(dst):
  command = "svn status "+dst
  print("",command)
  command_result = os.popen(command).readlines()
  if len(command_result) <= 2:
    print("没有需要添加,删除和提交的内容")
    return

  for line in command_result:
    #去掉换行符
    line = line.replace("\n","")
    print(line)
    line = line.replace(" ","")
    firstChar = line[0:1]
    filePath = line[1:]
    #命令
    command_add = "svn add "+filePath
    command_ci = "svn commit -m \"svn tools commit,更新维护。\" "+filePath
    command_rm = "svn delete "+filePath

    if firstChar == "":#ADD命令
      svnSimpleExecute(command_add)
      svnSimpleExecute(command_ci)
    elif firstChar in "ADM":#COMMIT 命令
      svnSimpleExecute(command_ci)
    elif firstChar == "!": #删除命令
      svnSimpleExecute(command_rm)
      svnSimpleExecute(command_ci)
    else:
      print("Unkonw.......",line)
#end method svnCommit


'''
解析获得对应的本地文件和线上文件路径
'''
def parseLine(line,local_dir,online_dir):
  line = line.replace("'","")
  line = line.replace("\\","/")
  line = line.replace(local_dir,"")
  pos = line.find("/")
  return local_dir + line[pos:],online_dir + line[pos:]
#end method parseLine


#循环资源目录
for local_dir in recource_dict:
  online_dir = recource_dict[local_dir]

  #更新线上资源
  command_online = "svn up " + online_dir
  svnSimpleExecute(command_online)

  #更新本地资源
  command_local = "svn up " + local_dir
  print("",command_local)

  lines = os.popen(command_local).readlines()
  if len(lines) <= 2:
    print("没有更新内容")
    continue

  #去掉头和尾部内容
  lines.pop(-1)
  lines.pop(0)

  for line in lines:
    #去掉换行符
    line = line.replace("\n","")
    print(line)
    #解析文件对应目录
    localFilePath,onlineFilePath = parseLine(line,local_dir,online_dir)

    if line.startswith("Restored") or line.startswith("A") or line.startswith("U"):
      print("复制文件:",localFilePath,"到",onlineFilePath) 
      copyFile(localFilePath,onlineFilePath)
    elif line.startswith("D"):
      #删除
      print("删除文件:",onlineFilePath)
      removeFile(onlineFilePath)

  #提交线上目录
  svnCommit(online_dir)

这下维护起来就简单多了,世界瞬间清净了。

以上这篇python3调用windows dos命令的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。