Commit b76ead2b authored by Wallen姚文辉's avatar Wallen姚文辉

测试用例文件系统

parent 8e1e230b
from flask import Blueprint, request, jsonify, session,send_file
import time
import subprocess
import os
from manager.tools import get_config
apptool = Blueprint("apptool",__name__,url_prefix='/apptool')
bath_path=get_config()['other']['casehome']
def strftime(type,str):
'''
type 1-包含年 0-不包含
'''
a=type and time.strptime(str, "%b-%d-%Y") or time.strptime("Jun-17 18:58", "%b-%d %H:%M")
return time.strftime("%m-%d %H:%M:%S", a)
k=lambda x:':' in x and strftime(0,x) or strftime(1,x)
@apptool.route("/case/filelist",methods=["GET"])
def filelist():
path=request.args.get("path")
a=subprocess.getoutput('cd %s && ls -l' % os.path.join(bath_path,path))
b=a.split('\n')[1:]
for i in range(len(b)):
b[i]=b[i].split(maxsplit=8)
s=list(map(lambda x:{'floder':x[0][0]=='d' and True or False,'size':x[4],'update': k(x[5]+'-'+x[6]+(':' in x[7] and ' '+x[7] or '-'+x[7])),'name':x[8]},b))
return jsonify({"code": 200, "message": "请求成功", "data": s}),200
@apptool.route("/case/create",methods=["POST"])
def createFloder():
path=request.json.get("path")
name=request.json.get("name")
type_=request.json.get("type")
if '../' in path and type_=='floder':
return jsonify({"code": 403, "message": "目录名违法"}),403
a=subprocess.getoutput('cd %s && ls -l' % os.path.join(bath_path,path))
b=a.split('\n')[1:]
for i in range(len(b)):
b[i]=b[i].split()
s=list(map(lambda x:{'floder':x[0][0]=='d' and True or False,'size':x[4],'update': k(x[5]+'-'+x[6]+(':' in x[7] and ' '+x[7] or '-'+x[7])),'name':x[8]},b))
for each in s:
if each.get("name")==name and each.get("floder") and type_=='floder':
return jsonify({"code": 503, "message": "该目录已存在"}),503
elif each.get("name")==name and each.get("file") and type_=='file':
return jsonify({"code": 503, "message": "该文件已存在"}),503
type_=='floder' and subprocess.getoutput(f'cd {os.path.join(bath_path,path)} && mkdir "{name}"') or subprocess.getoutput(f'cd {os.path.join(bath_path,path)} && touch "{name}"')
return jsonify({"code": 200, "message": "创建成功"}),200
@apptool.route("/case/rename",methods=["POST"])
def rename():
path=request.json.get("path")
oldname=request.json.get("oldname")
newname=request.json.get("newname")
a=subprocess.getoutput(f'cd {os.path.join(bath_path,path)} && mv "{oldname}" "{newname}"')
if not a:
return jsonify({"code": 200, "message": "修改成功"}),200
else:
return jsonify({"code": 503, "message":a}),503
@apptool.route("/case/move",methods=["POST"])
def move():
bath_path='/home/yaowenhui/test_case'
path=request.json.get("path")
name=request.json.get("name")
targetPath=request.json.get("targetPath")
if '../' in path or '../' in targetPath:
return jsonify({"code": 403, "message": "目录名违法"}),403
if os.path.join(bath_path,path).endswith('/'):
p=os.path.join(bath_path,path)+'"'+name+'"'
else:
p=os.path.join(bath_path,path)+'/"'+name+'"'
print(f'mv {p} {os.path.join(bath_path,targetPath)}')
a=subprocess.getoutput(f'mv {p} {os.path.join(bath_path,targetPath)}')
if not a:
return jsonify({"code": 200, "message": "修改成功"}),200
else:
if ("are the same file"):
return jsonify({"code": 503, "message":"改路径已存在相同文件"}),503
return jsonify({"code": 503, "message":a}),503
@apptool.route("/case/delete",methods=["POST"])
def delete():
path=request.json.get("path")
name=request.json.get("name")
a=subprocess.getoutput(f'cd {os.path.join(bath_path,path)} && rm -rf "{name}"')
if not a:
return jsonify({"code": 200, "message": "删除成功"}),200
else:
return jsonify({"code": 503, "message":a}),503
@apptool.route('/case/upload',methods=["POST"])
def upload():
path=request.form.get("path")
if 'file' not in request.files:
return jsonify({"code": 503, "message": "请选择文件"}),503
file = request.files['file']
if file:
# subprocess.getoutput(f'cd {os.path.join(bath_path,path)} && touch "{file.filename}"')
if os.path.join(bath_path,path).endswith('/'):
file_path=os.path.join(bath_path,path)+file.filename
else:
file_path=os.path.join(bath_path,path)+"/"+file.filename
file.save(file_path)
return jsonify({"code": 200, "message": "上传成功","data":{"filename":file.filename}}),200
@apptool.route('/case/download', methods=['GET'])
def download_file():
path=request.args.get("path")
name=request.args.get("name")
if os.path.join(bath_path,path).endswith('/'):
file_path=os.path.join(bath_path,path)+name
else:
file_path=os.path.join(bath_path,path)+"/"+name
print(222)
return send_file(file_path, as_attachment=True)
\ No newline at end of file
......@@ -17,7 +17,7 @@ class Config(object):
@server.app.before_request
def token():
token = request.headers.get("token")
release_path = ["/user/login", "/user/logout",'notoken']
release_path = ["/user/login", "/user/logout",'notoken','download','upload']
next_=False
for i in release_path:
if i in request.path:
......@@ -76,6 +76,9 @@ if __name__ == '__main__':
from controller.notoken import *
server.app.register_blueprint(notoken)
from controller.appUitest import *
server.app.register_blueprint(apptool)
# server.app.run(host="0.0.0.0", port=8000, debug=True)
server.socketio.run(server.app, host="0.0.0.0", port=8000, debug=True,allow_unsafe_werkzeug=True)
......
import subprocess
import time
import os
def test(path):
bath_path='~'
def strftime(type,str):
'''
type 1-包含年 0-不包含
'''
a=type and time.strptime(str, "%b-%d-%Y") or time.strptime("Jun-17 18:58", "%b-%d %H:%M")
return time.strftime("%m-%d %H:%M:%S", a)
k=lambda x:':' in x and strftime(0,x) or strftime(1,x)
a=subprocess.getoutput('cd %s && ls -l' % os.path.join(bath_path,path))
b=a.split('\n')[1:]
for i in range(len(b)):
b[i]=b[i].split()
s=list(map(lambda x:{'floder':x[0][0]=='d' and True or False,'size':x[4],'update': k(x[5]+'-'+x[6]+(':' in x[7] and ' '+x[7] or '-'+x[7])),'name':x[8]},b))
return s
print(test('project-other/source'))
# timeArray=time.strptime("Jun-17 18:58", "%b-%d %H:%M")
# otherStyleTime = time.strftime("%m-%d %H:%M:%S", timeArray)
# print(otherStyleTime)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment