140 lines
3.7 KiB
Python
140 lines
3.7 KiB
Python
#!/usr/bin/env python3
|
||
# encoding=utf8
|
||
import os,sys
|
||
from optparse import OptionParser
|
||
import subprocess
|
||
|
||
#删除文件
|
||
remove_bin="rm" #删除命令
|
||
remove_arg="-rf" #递归删除
|
||
#打包
|
||
pack_bin='tar' #压缩命令
|
||
pack_arg='zcvf' #压缩参数
|
||
#oss传输
|
||
oss_bin='ossutil64' #oss命令
|
||
oss_copy='cp' #oss复制
|
||
oss_list='ls' #oss查询
|
||
oss_del='rm' #oss删除
|
||
oss_arg='oss://hongko' #oss参数
|
||
|
||
#git
|
||
git_bin='git' #git命令
|
||
git_clone='clone' #git 克隆参数
|
||
git_mirror='--mirror' #git镜像参数
|
||
|
||
"""
|
||
读取任务文件,返回任务列表
|
||
"""
|
||
def read_task_file(filename):
|
||
task_list=[]
|
||
with open(filename,'r') as fd:
|
||
for line in fd:
|
||
task_list.append(line[:-1])
|
||
return task_list
|
||
|
||
"""
|
||
执行rm命令,将文件或者文件夹进行删除
|
||
"""
|
||
def exec_remove(files):
|
||
cmd_args=[]
|
||
cmd_args.append(remove_bin)
|
||
cmd_args.append(remove_arg)
|
||
cmd_args.append(files)
|
||
print(cmd_args)
|
||
if(subprocess.call(cmd_args)==0):
|
||
pass
|
||
cmd_args=[]
|
||
"""
|
||
执行压缩,将传进的文件名进行压缩
|
||
"""
|
||
def exec_package(files):
|
||
package_name=files+'.tar.gz'
|
||
cmd_args=[]
|
||
cmd_args.append(pack_bin)
|
||
cmd_args.append(pack_arg)
|
||
cmd_args.append(package_name)
|
||
cmd_args.append(files)
|
||
print(cmd_args)
|
||
if(subprocess.call(cmd_args)==0):
|
||
pass
|
||
exec_remove(files) #压缩完源文件后,将源文件进行删除
|
||
cmd_args = []
|
||
return package_name
|
||
"""
|
||
执行oss传输查询命令,将
|
||
|
||
"""
|
||
def exec_oss_ls(files):
|
||
cmd_args=[]
|
||
cmd_args.append(oss_bin)
|
||
cmd_args.append(oss_list)
|
||
cmd_args.append(oss_arg)
|
||
print(cmd_args)
|
||
with subprocess.Popen(cmd_args,stdout=subprocess.PIPE) as p:
|
||
p.wait()
|
||
out=str(p.stdout.read(),'utf-8')
|
||
file_list_str=out.split('\n')[1:-4]
|
||
file_list=[]
|
||
for file_line in file_list_str:
|
||
file_str_list=file_line.split(' ')
|
||
file_name=file_str_list[-1]
|
||
file_list.append(file_name)
|
||
return file_list
|
||
pass
|
||
cmd_args = []
|
||
|
||
"""
|
||
执行oss传输查询命令,将
|
||
|
||
"""
|
||
def exec_oss_del(file):
|
||
cmd_args=[]
|
||
cmd_args.append(oss_bin)
|
||
cmd_args.append(oss_del)
|
||
cmd_args.append(file)
|
||
print(cmd_args)
|
||
if(subprocess.call(cmd_args)==0):
|
||
pass
|
||
cmd_args = []
|
||
"""
|
||
执行oss传输命令,将
|
||
"""
|
||
def exec_oss_down(files,path='.'):
|
||
for file in files:
|
||
cmd_args=[]
|
||
cmd_args.append(oss_bin)
|
||
cmd_args.append(oss_copy)
|
||
cmd_args.append(file)
|
||
cmd_args.append(path+'/')
|
||
print(cmd_args)
|
||
if(subprocess.call(cmd_args)==0):
|
||
pass
|
||
exec_oss_del(file) #压缩完源文件后,将源文件进行删除
|
||
cmd_args = []
|
||
"""
|
||
执行git镜像命令,用于镜像传进的url列表
|
||
"""
|
||
|
||
if __name__=="__main__":
|
||
print("this is a gitoss tool")
|
||
parser = OptionParser()
|
||
"""
|
||
这是git oss工具,自动化git拉取仓库列表,
|
||
下载下来的仓库打包通过oss进行传输的工具
|
||
绕过IP被墙的困扰!
|
||
"""
|
||
parser.add_option('-p','--path', type=str, help="路径")
|
||
#parser.add_argument('--config', type=str,default='.gitoss.conf', help="oss配置")
|
||
#subparsers = parser.add_subparsers(help='sub-command help')
|
||
#parser_cmd = subparsers.add_parser('a', help='a help')
|
||
(options, args) = parser.parse_args()
|
||
if(options.path):
|
||
print("options.path: %s"%options.path)
|
||
file_list=exec_oss_ls(options.path)
|
||
exec_oss_down(file_list,options.path)
|
||
#执行git镜像url仓库
|
||
# tasks=read_task_file(args.file)
|
||
# print(tasks)
|
||
# exec_git(tasks)
|
||
pass
|