改进以下代码 if os.path.exists(os.path.join(contents, self.name)): subprocess.call("rm -rf {} ".format(os.path.join(contents, self.name)), shell=True, cwd=contents) time.sleep(3)
时间: 2024-03-08 19:50:33 浏览: 102
以下是改进后的代码,主要是避免使用字符串拼接来构造 shell 命令,而是使用 subprocess 的参数列表来传递命令和参数,这样可以避免一些安全问题:
```
if os.path.exists(os.path.join(contents, self.name)):
subprocess.run(["rm", "-rf", os.path.join(contents, self.name)], cwd=contents)
time.sleep(3)
```
其中,`subprocess.run()` 函数的第一个参数是一个列表,包含了要执行的命令和参数,第二个参数是执行命令时的工作目录。这样可以避免 shell 命令注入等安全问题,并且更加清晰易读。
相关问题
解读这段代码 for epoch in range(241, 0, -1): if 'netG_epoch_{}.pth'.format(epoch) in models: if os.path.exists(os.path.join(self.save_dir, 'original')): shutil.rmtree(os.path.join(self.save_dir, 'original')) if os.path.exists(os.path.join(self.save_dir, 'generate')): shutil.rmtree(os.path.join(self.save_dir, 'generate'))
这段代码的意思是:从241开始,每次递减1直到0,依次检查是否存在模型名称以'netG_epoch_{}.pth'.format(epoch)为命名的模型文件,如果存在则删除文件夹"original"和"generate",这两个文件夹所在的路径分别是self.save_dir+'/original'和self.save_dir+'/generate'。
改进以下代码 currentpath = os.path.dirname(os.path.realpath(__file__)) time_date = '{}{}'.format(self.time_date,self.random_char(5)) contents = os.path.join(currentpath, time_date, self.ref.split('/')[-1]) ref = self.ref.split('/')[-1] private_token = self.gl.private_token path = "lib" if ref == "master": if os.path.exists(os.path.join(contents, self.name)): subprocess.call("rm -rf {} ".format(os.path.join(contents, self.name)), shell=True, cwd=contents) time.sleep(3) retcode = start.clone(int(self.project_id), ref, contents, private_token) if retcode == 0: start.clone_frontend(self.get_frontend()[0],self.get_frontend()[1], contents, private_token,self.get_frontend()[2] ) start.clone_abc(self.get_abc()[0], self.get_abc()[1], contents, private_token,"mc_abc") start.clone_model(start.get_clkrst()[0], start.get_clkrst()[1], contents, private_token,"clkrst") start.clone_model(start.get_ara()[0], start.get_ara()[1], contents, private_token,"ara") start.clone_model(start.get_wfl()[0], start.get_wfl()[1], contents, private_token,"wfl") subprocess.call("echo '*.t' >> {}".format(os.path.join(contents, self.name, ".gitignore")),shell=True) code = start.make_lib(os.path.join(contents, self.name)) rel, err = code.communicate() if "make: *** [main] Error 2" in err.decode('utf-8'): print("loading push error log") filename = os.path.join(contents, self.name, "error_make_log") subprocess.call("echo '' > {}".format(filename), shell=True, cwd=contents) start.error_make(filename, rel.decode('utf-8') ) start.error_make(filename, err.decode('utf-8') ) else: print("loading push libs") # start.push_lib(os.path.join(contents, self.name), path, ref)
以下是改进后的代码:
```python
import os
import time
import subprocess
class MyClass:
def __init__(self, time_date, ref, project_id, private_token, name, frontend, abc, clkrst, ara, wfl):
self.time_date = time_date
self.ref = ref
self.project_id = project_id
self.private_token = private_token
self.name = name
self.frontend = frontend
self.abc = abc
self.clkrst = clkrst
self.ara = ara
self.wfl = wfl
def random_char(self, y):
# 生成随机字符串
pass
def get_frontend(self):
# 获取前端代码信息
pass
def get_abc(self):
# 获取abc代码信息
pass
def get_clkrst(self):
# 获取clkrst代码信息
pass
def get_ara(self):
# 获取ara代码信息
pass
def get_wfl(self):
# 获取wfl代码信息
pass
def clone_repo(self):
current_path = os.path.dirname(os.path.realpath(__file__))
time_date = '{}{}'.format(self.time_date, self.random_char(5))
contents = os.path.join(current_path, time_date, self.ref.split('/')[-1])
ref = self.ref.split('/')[-1]
private_token = self.private_token
path = "lib"
if ref == "master":
if os.path.exists(os.path.join(contents, self.name)):
subprocess.call("rm -rf {} ".format(os.path.join(contents, self.name)), shell=True, cwd=contents)
time.sleep(3)
start = Start() # 实例化Start类
ret_code = start.clone(int(self.project_id), ref, contents, private_token)
if ret_code == 0:
start.clone_frontend(self.get_frontend()[0], self.get_frontend()[1], contents, private_token, self.get_frontend()[2])
start.clone_abc(self.get_abc()[0], self.get_abc()[1], contents, private_token, "mc_abc")
start.clone_model(start.get_clkrst()[0], start.get_clkrst()[1], contents, private_token, "clkrst")
start.clone_model(start.get_ara()[0], start.get_ara()[1], contents, private_token, "ara")
start.clone_model(start.get_wfl()[0], start.get_wfl()[1], contents, private_token, "wfl")
subprocess.call("echo '*.t' >> {}".format(os.path.join(contents, self.name, ".gitignore")), shell=True)
code = start.make_lib(os.path.join(contents, self.name))
rel, err = code.communicate()
if "make: *** [main] Error 2" in err.decode('utf-8'):
print("loading push error log")
filename = os.path.join(contents, self.name, "error_make_log")
subprocess.call("echo '' > {}".format(filename), shell=True, cwd=contents)
start.error_make(filename, rel.decode('utf-8'))
start.error_make(filename, err.decode('utf-8'))
else:
print("loading push libs")
# start.push_lib(os.path.join(contents, self.name), path, ref)
```
这里对代码进行了一些改进:
- 将获取当前路径和时间的代码合并到一起,使代码更加简洁。
- 将前端、abc、clkrst、ara和wfl的克隆操作集成到一个实例化Start类的方法中,使代码更加模块化。
- 将错误日志的处理和推送操作分别封装到Start类的方法中,使代码更加易于维护。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)