-
Notifications
You must be signed in to change notification settings - Fork 28
/
run.py
155 lines (139 loc) · 5.05 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# coding:utf-8
# 将程序上传到服务器上执行
import os
import sys
from functools import wraps
import time
# 两个常用的工具函数,装饰器
# 工具函数,在上传到服务器上运行时改变当前目录
def change_dir(func):
@wraps(func)
def change(*args, **kwargs):
oldpath = os.getcwd()
newpath = "/home/code/"
os.chdir(newpath)
r = func(*args, **kwargs)
os.chdir(oldpath)
return r
return change
# 工具函数,计算函数运行时间
def timethis(func):
@wraps(func)
def wrapper(*args, **kwargs):
start = time.perf_counter()
r = func(*args, **kwargs)
end = time.perf_counter()
print('{}.{}的运行时间为 : {}秒'.format(func.__module__, func.__name__, end - start))
return r
return wrapper
# 运行代码前准备
def before_run(user, server):
# 创建输出目录
s = "ssh " + user + "@" + server + " \"sudo mkdir ~/code/output\""
# print("测试4", s)
os.system(s)
# 创建数据目录
s = "ssh " + user + "@" + server + " \"sudo mkdir ~/code/datas\""
# print("测试4", s)
os.system(s)
# 更改目录权限
s = "ssh root@" + server + " -p 2222 \"chown -R 1000:1000 /home/code/output\""
os.system(s)
s = "ssh root@" + server + " -p 2222 \"chown -R 1000:1000 /home/code/datas\""
os.system(s)
# 将本地目录所有文件上传至容器
s = "scp ./* " + user + "@" + server + ":~/code"
# print("测试3", s)
os.system(s)
# 删除md文件,避免影响博客更新
s = "ssh root@" + server + " -p 2222 \"rm /home/code/README.md\""
os.system(s)
# 更改服务器容器里的当前目录
s = "ssh root@" + server + " -p 2222 \"cd /home/code\""
os.system(s)
# 运行完成后的后续操作
def after_run(user, server):
# 看本地是否有输出目录,没有则新建
if os.path.exists("./output") == False:
s = "mkdir ./output"
os.system(s)
# 看本地是否有数据目录,没有则新建
if os.path.exists("./datas") == False:
s = "mkdir ./datas"
os.system(s)
# 改变文件所有者
s = "ssh root@" + server + " -p 2222 \"chown 1000:1000 -R /home/code/*"
# 将代码目录里所有输出文件传回
s = "scp -r " + user +"@" + server + ":~/code/output/* ./output/"
os.system(s)
# 将代码目录里所有数据文件传回
# s = "scp -r " + user +"@" + server + ":~/code/datas/* ./datas/"
# print("测试5", s)
# os.system(s)
# 上传代码至服务器并运行
def run(gpus, user, server):
# 上传本目录所有文件再执行指定文件
if gpus == "all":
before_run(user, server)
# 运行指定代码
s = "ssh root@" + server + " -p 2222 \"python -u /home/code/" + sys.argv[2] + "\""
# print("测试4", s)
print("正在运行代码……\n")
os.system(s)
after_run(user, server)
elif gpus == "copy":
if sys.argv[2] == "up":
before_run(user, server)
elif sys.argv[2] == "down":
after_run(user, server)
elif sys.argv[2] == "up_data":
s = "scp ./datas/* " + user + "@" + server + ":~/code/datas"
os.system(s)
elif sys.argv[2] == "down_data":
s = "scp " + user + "@" + server + ":~/code/datas/* ./datas/"
os.system(s)
else:
print("输入错误,copy后面要跟up、down、up_data或down_data。")
# 用pytest执行单元测试
elif gpus == "test":
before_run(user, server)
# 运行指定代码
arg_len = len(sys.argv)
if arg_len == 3:
s = "ssh root@" + server + " -p 2222 \"/opt/conda/bin/pytest -v /home/code/" + sys.argv[2] + "\""
# elif arg_len == 2:
# s = "ssh root@" + server + " -p 2222 \"/opt/conda/bin/pytest -v\""
else:
print("输入有误!")
return
print("测试4", s)
print("正在测试代码……\n")
os.system(s)
after_run(user, server)
elif gpus == "clean":
# 清除服务器代码目录所有文件
s = "ssh root@" + server + " -p 2222 \"rm -rf /home/code/*\""
# print("测试1", s)
print("清除服务器代码目录上的文件……\n")
os.system(s)
else:
print("输入有误,格式: python run.py all/copy/test filename.py 其中filename.py为要运行/测试的源文件。")
# 主函数
def main():
gpus = sys.argv[1]
# 读取服务器IP地址,自己编辑serverIP.txt去
with open("serverIP.txt", "rt") as f:
server_list = f.readlines()
for s in server_list:
s = s.replace('\n', '').replace('\r', '')
# print(s)
if s[0] != "#":
res = s.split("@")
username = res[0]
server = res[1]
# print("测试", username, server)
# input("按任意键继续")
run(gpus, username, server)
return
if __name__ == "__main__":
main()