def uploads(): #print "799" if request.method == 'POST': file = request.files['file'] path = request.values['path'] searchFfDir = os.path.join(CURRENT_DIR, 'featureFiles'); if file and allowed_file(file.filename): #print "812",file.read() filename = secure_filename(file.filename) if path =="": featureFile_DIR = os.path.join(CURRENT_DIR, 'featureFiles') else: path = path.replace("../","") #print "820----path----", path featureFile_DIR = os.path.join(CURRENT_DIR, path) #print "822------", featureFile_DIR filePath = os.path.join(featureFile_DIR, filename) baseFileName = filename.split(".")[0] fileNameExt = filename.split(os.extsep, 1)[-1] #print "822", fileNameExt timestr = time.strftime("%Y%m%d-%H%M%S") for (dir,subdirs,files) in os.walk(searchFfDir): if filename in files: #print "836", ("Found:", os.path.join(dir, filename)) filename = baseFileName+"_"+str(timestr)+"."+fileNameExt file.save(os.path.join(featureFile_DIR, filename)) return jsonify(status='OK',result='success') else: return jsonify(status='Error',result='Error')
def download_artifact(current_dir, artifact_name): try: path = os.path.join(current_dir, 'artifacts', artifact_name) createdFolderPath = os.path.join(current_dir, 'abot/temp', artifact_name) def _zipdir(url, ziph): length = len(url) for root, dirs, files in os.walk(url): folder = root[length:] for file in files: ziph.write(os.path.join(root, file), os.path.join(folder, file)) zipfilename = artifact_name zipFolderPath = createdFolderPath + '.zip' zipf = zipfile.ZipFile(zipFolderPath, 'w', zipfile.ZIP_DEFLATED) _zipdir(path, zipf) zipf.close() zipSourcePath = zipFolderPath zipDestinationPath = os.path.join(current_dir, 'artifacts/') # To copy the Report folder to Artifacts folder from temp folder subprocess.Popen(["sudo", "cp", zipSourcePath, zipDestinationPath], stdout=subprocess.PIPE) # To delete the Report folder from temp folder subprocess.Popen(["sudo", "rm", "-rf", zipFolderPath], stdout=subprocess.PIPE) result = str(zipfilename) return jsonify(status='OK', result=result) except Exception, e: return jsonify(status='ERROR', message=str(e))
def configFileTree(): try: #BASE_DIR = os.path.join(os.pardir, 'config') BASE_DIR = os.path.join(CURRENT_DIR, 'config') tree_data = _make_config_tree(BASE_DIR)['children'] return jsonify(status='OK',tree_list=tree_data) except Exception,e: return jsonify(status='ERROR',message=str(e))
def newFeatureFileTree(): try: #BASE_DIR = os.path.join(os.pardir, 'featureFiles') BASE_DIR = os.path.join(CURRENT_DIR, 'featureFiles') #BASE_DIR = "static/featureFiles" tree_data = _make_feature_tree(BASE_DIR)['children'] return jsonify(status='OK',tree_list=tree_data) except Exception,e: return jsonify(status='ERROR',message=str(e))
def configs(current_dir): valid_files_ext = ['.properties', '.template'] hidden_dir = ['blue', 'charts', 'js', '__macosx'] try: base_dir = os.path.join(current_dir, 'config') tree_data = _make_tree(base_dir, valid_files_ext, hidden_dir)['children'] tree_data.sort(key=lambda x: x['label'], reverse=False) return jsonify(status='OK', tree_list=tree_data) except Exception, e: return jsonify(status='ERROR', message=str(e))
def home_directory(current_dir): valid_files_ext = ['.pcap', '.log', '.csv'] hidden_dir = ['blue', 'charts', 'js', '__macosx', '.zip'] try: base_dir = os.path.join(current_dir, 'artifacts') tree_data = _make_tree(base_dir, valid_files_ext, hidden_dir)['children'] tree_data.sort(key=lambda x: time.mktime( time.strptime(x['label'].rsplit("-", 1)[0], "%d%b%Y-%H%M%S%p")), reverse=True) return jsonify(status='OK', artifact=tree_data[0]['label']) except Exception, e: return jsonify(status='ERROR', message=str(e))
def newFileTree(): if APPLY and ARTIFACTS_LIST is not None and (len(json.loads(ARTIFACTS_LIST.response[0])['tree_list']) == len(os.listdir(os.path.join(CURRENT_DIR, 'artifacts')))-1): #print "EXISTING DATA" return ARTIFACTS_LIST try: #BASE_DIR = os.path.join(os.pardir, 'artifacts') BASE_DIR = os.path.join(CURRENT_DIR, 'artifacts') tree_data = _make_tree(BASE_DIR)['children'] #tree_data.sort(key=lambda x: time.mktime(time.strptime(x['label'], "%d%b%Y-%H%M%S%p-UTC")), reverse=True) tree_data.sort(key=lambda x: time.mktime(time.strptime(x['label'].rsplit("-", 1)[0], "%d%b%Y-%H%M%S%p")), reverse=True) return jsonify(status='OK',tree_list=tree_data) except Exception,e: return jsonify(status='ERROR',message=str(e))
def downloadArtifactsFolder(): BASE_DIR = os.path.join(CURRENT_DIR, 'artifacts') try: json_data = request.get_json(force=True) path = os.path.join(CURRENT_DIR,'artifacts', json_data['folderName']) createdFolderPath = os.path.join(CURRENT_DIR,'abot/temp', json_data['folderName']) def zipdir(url, ziph): length = len(url) for root, dirs, files in os.walk(url): folder = root[length:] # path without "parent" for file in files: ziph.write(os.path.join(root, file), os.path.join(folder, file)) zipfilename = json_data['folderName'] zipFolderPath = createdFolderPath+".zip"; zipf = zipfile.ZipFile(zipFolderPath, 'w', zipfile.ZIP_DEFLATED) zipdir(path, zipf) zipf.close() zipSourcePath = zipFolderPath zipDestinationPath = os.path.join(CURRENT_DIR,'artifacts/') #To copy the Report folder to Artifacts folder from temp folder process = subprocess.Popen(["sudo", "cp", zipSourcePath , zipDestinationPath], stdout=subprocess.PIPE) #To delete the Report folder from temp folder process = subprocess.Popen(["sudo", "rm", "-rf", zipFolderPath], stdout=subprocess.PIPE) result = str(zipfilename) return jsonify(status='OK',result=result) except Exception,e: return jsonify(status='ERROR',message=str(e))
def upload_feature_files(current_dir, file, path): searchFfDir = os.path.join(current_dir, 'featureFiles') if file and _allowed_file(file.filename): filename = secure_filename(file.filename) if path == "": featureFile_DIR = os.path.join(current_dir, 'featureFiles') else: path = path.replace("../", "") featureFile_DIR = os.path.join(current_dir, path) filePath = os.path.join(featureFile_DIR, filename) baseFileName = filename.split(".")[0] fileNameExt = filename.split(os.extsep, 1)[-1] timestr = time.strftime("%Y%m%d-%H%M%S") for (dir, subdirs, files) in os.walk(searchFfDir): if filename in files: filename = baseFileName + "_" + str( timestr) + "." + fileNameExt file.save(os.path.join(featureFile_DIR, filename)) return jsonify(status='OK', result='success') else: return jsonify(status='Error', result='Error')
def listDirectory(): global ARTIFACTS_LIST if APPLY: ARTIFACTS_LIST = newFileTree() try: json_data = request.json BASE_DIR = os.path.join(CURRENT_DIR, 'artifacts') #BASE_DIR = os.path.join(os.getcwd(), 'static', 'artifacts') if not os.path.exists(BASE_DIR): return abort(404) subdir_names = [x for x in os.listdir(BASE_DIR) if os.path.isdir(os.path.join(BASE_DIR, x))] #subdir_names.sort(key=lambda x: time.mktime(time.strptime(x, "%d%b%Y-%H%M%S%p-UTC")), reverse=True) subdir_names.sort(key=lambda x: time.mktime(time.strptime(x.rsplit("-", 1)[0], "%d%b%Y-%H%M%S%p")), reverse=True) #subdir_names = natsorted(os.listdir(BASE_DIR), reverse=True) result = [] for dir in subdir_names: if (dir != '.') and (dir != '..') and (os.path.isdir(os.path.join(BASE_DIR, dir))): result.append(dir) for file_name in subdir_names: if (file_name != '.') and (file_name != '..') and (not os.path.isdir(os.path.join(BASE_DIR, file_name))): result.append(file_name) #file_path = os.path.join('static', 'artifacts', subdir_names[0], 'report-'+ subdir_names[0], json_data['page']+'.html') #print "127", result return jsonify(status='OK',directories=result) except Exception,e: #return jsonify(status='ERROR',message=str(e)) return
def get_packet_counters(): packet_counters_response = packet_counters() if packet_counters_response is None: return make_response(jsonify({'error': 'Internal Server Error'}), 500) return packet_counters_response, 200
def execute_feature_file(): json_data = request.get_json(force=True) params = json_data['params'] runner = FeatureFileRunner(json_data['params']) runner.start() return make_response(jsonify({'status': 'OK'}), 200)
def is_executing(): execution_status = {'executing': file_service.is_executing()} return make_response(jsonify(status=execution_status), 200)
def internal_server_error(error): return make_response(jsonify({'error': 'Internal Server Error'}), 500)
def editFileContent(): try: #Initialize result = {'file' : '', 'mime' : '', 'message' : '', 'message_css' : '', 'download' : '', 'showDownload' : False, 'containerScroll' : True, 'content' : '', 'keywords' : ''} #json_data = request.json json_data = request.get_json(force=True) #file = json_data['path'] file = os.path.join(CURRENT_DIR, json_data['path'].replace("../","")) fileContent = json_data['content'].replace("<br>", "\n") # cleanr = re.compile('<.*?>') # cleantext = re.sub(cleanr, '', fileContent) #print "584", cleantext # fileContent = cleantext # Providing Read, Write and Execute permission to selected featureFiles exePermissionStr = "sudo " + "find " + file + " -type " + "f " + "-exec " + "chmod " + "ugo+rwx " + "{} " +"\;" #print "569", exePermissionStr ffPermissionChange = subprocess.Popen(exePermissionStr, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True) output, err = ffPermissionChange.communicate() temp = ffPermissionChange.returncode #print "572", temp if temp == 0: with open(file, "w") as f: #print "575" f.write(fileContent) else: print "Write permission not granted to the feature file" mime = MimeTypes() mime_type = mime.guess_type(urllib.pathname2url(file))[0] if mime.guess_type(urllib.pathname2url(file))[0] else 'text/plain' result['file'] = file result['mime'] = mime_type download_html = '' download_html+='<div class="">' download_html+='<a class="btn-download btn btn-lg btn-primary" href="' + file + '" target="_blank"><i class="glyphicon glyphicon-download-alt"></i> Download</a>' download_html+='</div>' result['download'] = download_html supported_application = ['application/xml', 'text/x-php', 'text/plain', 'text/xml', 'text/csv' ] mime = mime_type.split('/') file_mime = mime[0] if mime_type in supported_application: content = '' words = '' file_ext = file.split('.') if file_ext[len(file_ext)-1] == "properties": with open(file) as f: for line in f: content += line elif file_ext[len(file_ext)-1] == "feature": #print "569" content = open(file).read() x = open(file, "r") words = [w for w in x.read().split() if w[0] == "@"] else: content = open(file).read() result['content'] = content result['containerScroll'] = True result['showDownload'] = False result['keywords'] = words elif (file_mime == 'text'): html_content = '' html_content+= '<object data="' + file + '" type="' + mime_type + '" id="fileObject" style="width:100% ">' html_content+='<embed type="' + mime_type + '" src="' + file + '" style="width:100%">' html_content+='</object>' result['content'] = html_content result['containerScroll'] = False result['showDownload'] = False else: html_content = '' html_content+= '<object data="' + file + '" type="' + mime_type + '" id="fileObject" style="width:100% " class="hidden">' html_content+='<embed type="' + mime_type + '" src="' + file + '" style="width:100%">' html_content+='</object>' result['content'] = html_content result['containerScroll'] = False result['showDownload'] = True return jsonify(status='OK',result=result) except Exception,e: return jsonify(status='ERROR',message=str(e))
def edit_file_content(current_dir, path, content): try: result = { 'file': '', 'mime': '', 'message': '', 'message_css': '', 'download': '', 'showDownload': False, 'containerScroll': True, 'content': '', 'keywords': '' } file = os.path.join(current_dir, path.replace('../', '')) fileContent = content.replace("<br>", "\n") exePermissionStr = 'sudo ' + 'find ' + file + ' -type ' + \ 'f ' + '-exec ' + 'chmod ' + 'ugo+rwx ' + '{} ' + '\;' ffPermissionChange = Popen(exePermissionStr, stderr=PIPE, stdout=PIPE, shell=True) ffPermissionChange.communicate() temp = ffPermissionChange.returncode if temp == 0: with open(file, "w") as f: f.write(fileContent) else: print "Write permission not granted to the feature file" mime = MimeTypes() mime_type = mime.guess_type( urllib.pathname2url(file))[0] if mime.guess_type( urllib.pathname2url(file))[0] else 'text/plain' result['file'] = file result['mime'] = mime_type download_html = '' download_html += '<div class="">' download_html += '<a class="btn-download btn btn-lg btn-primary" href="' + file + \ '" target="_blank"><i class="glyphicon glyphicon-download-alt"></i> Download</a>' download_html += '</div>' result['download'] = download_html supported_application = [ 'application/xml', 'text/x-php', 'text/plain', 'text/xml', 'text/csv' ] mime = mime_type.split('/') file_mime = mime[0] if mime_type in supported_application: content = '' words = '' file_ext = file.split('.') if file_ext[len(file_ext) - 1] == "properties": with open(file) as f: for line in f: content += line elif file_ext[len(file_ext) - 1] == "feature": content = open(file).read() x = open(file, "r") words = [w for w in x.read().split() if w[0] == "@"] else: content = open(file).read() result['content'] = content result['containerScroll'] = True result['showDownload'] = False result['keywords'] = words elif (file_mime == 'text'): html_content = '' html_content += '<object data="' + file + '" type="' + \ mime_type + '" id="fileObject" style="width:100% ">' html_content += '<embed type="' + mime_type + \ '" src="' + file + '" style="width:100%">' html_content += '</object>' result['content'] = html_content result['containerScroll'] = False result['showDownload'] = False else: html_content = '' html_content += '<object data="' + file + '" type="' + mime_type + \ '" id="fileObject" style="width:100% " class="hidden">' html_content += '<embed type="' + mime_type + \ '" src="' + file + '" style="width:100%">' html_content += '</object>' result['content'] = html_content result['containerScroll'] = False result['showDownload'] = True return jsonify(status='OK', result=result) except Exception, e: return jsonify(status='ERROR', message=str(e))
def file_content(current_dir, path): try: result = { 'file': '', 'mime': '', 'message': '', 'message_css': '', 'download': '', 'showDownload': False, 'containerScroll': True, 'content': '', 'keywords': '' } file = os.path.join(current_dir, path.replace("../", "")) mime = MimeTypes() mime_type = mime.guess_type( urllib.pathname2url(file))[0] if mime.guess_type( urllib.pathname2url(file))[0] else 'text/plain' result['file'] = file result['mime'] = mime_type download_html = '' download_html += '<div class="">' download_html += '<a class="btn-download btn btn-lg btn-primary" href="' + file + \ '" target="_blank"><i class="glyphicon glyphicon-download-alt"></i> Download</a>' download_html += '</div>' result['download'] = download_html supported_application = [ 'application/xml', 'text/x-php', 'text/plain', 'text/xml', 'text/csv' ] mime = mime_type.split('/') file_mime = mime[0] if mime_type in supported_application: content = '' words = '' file_ext = file.split('.') if file_ext[len(file_ext) - 1] == "properties": with open(file) as f: for line in f: content += line elif file_ext[len(file_ext) - 1] == "feature": content = open(file).read() x = open(file, "r") words = [w for w in x.read().split() if w[0] == "@"] elif file_ext[len(file_ext) - 1] == "csv": with open(file, 'rb') as csvfile: spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|') for row in spamreader: line = ' '.join(row) content += line + "\n" else: content = open(file).read() content = unicode(content, errors='ignore') result['content'] = content result['containerScroll'] = True result['showDownload'] = False result['keywords'] = words elif (file_mime == 'text'): html_content = '' html_content += '<object data="' + file + '" type="' + \ mime_type + '" id="fileObject" style="width:100% ">' html_content += '<embed type="' + mime_type + \ '" src="' + file + '" style="width:100%">' html_content += '</object>' result['content'] = html_content result['containerScroll'] = False result['showDownload'] = False else: html_content = '' html_content += '<object data="' + file + '" type="' + mime_type + \ '" id="fileObject" style="width:100% " class="hidden">' html_content += '<embed type="' + mime_type + \ '" src="' + file + '" style="width:100%">' html_content += '</object>' result['content'] = html_content result['containerScroll'] = False result['showDownload'] = True return jsonify(status='OK', result=result) except Exception, e: return jsonify(status='ERROR', message=str(e))
def bad_request(error): return make_response(jsonify({'error': 'Bad request'}), 400)
def getReportContent(): try: #Initialize result = {'file' : '', 'mime' : '', 'message' : '', 'message_css' : '', 'download' : '', 'showDownload' : False, 'containerScroll' : True, 'content' : '', 'keywords' : ''} json_data = request.get_json(force=True) file = os.path.join(CURRENT_DIR, json_data['path'].replace("../","")) #print "632", file pdfName = file.split("/")[-1].split(".")[0] reportDate = file.split("/")[-3] #print "636", reportDate pdfName = pdfName + "_" + reportDate + ".pdf" #print "639", pdfName BASE_DIR = os.path.join(CURRENT_DIR, 'abot') #print "638", BASE_DIR newFilePath = '' # load the file with open(file) as fp: #print "649" soup = BeautifulSoup(fp) # Remove header and footer from html soup.find(attrs={"id" : "fullwidth_header"}).extract() soup.find(attrs={"class" : "footer"}).extract() for a in soup.findAll('a'): # Delete the link from del a['href'] #print "666", (os.path.isdir("temp")) if not os.path.isdir("temp"): print "670" os.makedirs("temp"); #print(os.path.exists("/home/el/myfile.txt")) tempdir = os.path.join(CURRENT_DIR,'abot/temp') #print "652", tempdir delTempFilesCmd = "rm"+" -rf"+" temp/*.pdf" #print "653", delTempFilesCmd # delete temp folder content delTempFile = subprocess.Popen(delTempFilesCmd, stdout=subprocess.PIPE, shell=True) newFilePath = os.path.join(tempdir, "output.html") with open(newFilePath, "w") as f: #print "684" f.write(str(soup)) f.close() bindir = os.path.join(CURRENT_DIR,'abot/bin') #print "692", bindir # Set execution permission for wkhtmltopdf file # find /opt/lampp/htdocs -type f -exec chmod 777 {} \; exePermissionStr = "find " + bindir + "/wkhtmltopdf" + " -type " + "f " "-exec" + " chmod " + "ugo+x " + "{} " +"\;" #print "697", exePermissionStr binPermission = subprocess.Popen(exePermissionStr, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True) #print "701", binPermission # create pdf file - cat test.html | ./wkhtmltopdf - test.pdf strs = "cat "+newFilePath+" | bin/wkhtmltopdf - "+pdfName process = subprocess.Popen(strs, stdout=subprocess.PIPE, shell=True) #output, err = process.communicate(b"input data that is passed to subprocess' stdin") output, err = process.communicate() rc = process.returncode if rc==0: # move pdf file to temp dir from bas dir - mv 002-sip_register.pdf temp/ mvFile = subprocess.Popen(["mv " + BASE_DIR +"/"+ pdfName + " temp/"], stdout=subprocess.PIPE, shell=True) output, err = mvFile.communicate() mvFileval = mvFile.returncode if mvFileval==0: result['download'] = pdfName result['showDownload'] = True return jsonify(status='OK',result=result) except Exception,e: return
def not_found(error): return make_response(jsonify({'error': 'Not found'}), 404)
def download_report(current_dir, path): try: result = { 'file': '', 'mime': '', 'message': '', 'message_css': '', 'download': '', 'showDownload': False, 'containerScroll': True, 'content': '', 'keywords': '' } file = os.path.join(current_dir, path.replace("../", "")) pdfName = file.split("/")[-1].split(".")[0] reportDate = file.split("/")[-3] pdfName = pdfName + "_" + reportDate + ".pdf" baseDir = os.path.join(current_dir, 'abot') newFilePath = '' # load the file with open(file) as fp: soup = BeautifulSoup(fp, "html.parser") # Remove header and footer from html soup.find(attrs={"id": "fullwidth_header"}).extract() soup.find(attrs={"class": "footer"}).extract() for a in soup.findAll('a'): del a['href'] if not os.path.isdir("temp"): os.makedirs("temp") tempdir = os.path.join(current_dir, 'abot/temp') delTempFilesCmd = "rm" + " -rf" + " temp/*.pdf" # delete temp folder content delTempFile = subprocess.Popen(delTempFilesCmd, stdout=subprocess.PIPE, shell=True) newFilePath = os.path.join(tempdir, "output.html") with open(newFilePath, "w") as f: f.write(str(soup)) f.close() bindir = os.path.join(current_dir, 'abot/bin') # Set execution permission for wkhtmltopdf file exePermissionStr = 'chmod +x ' + bindir + '/wkhtmltopdf; chmod +x ' + bindir + '/wkhtmltoimage' binPermission = subprocess.Popen(exePermissionStr, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True) # create pdf file - cat test.html | ./wkhtmltopdf - test.pdf strs = 'bin/wkhtmltopdf' + ' --load-error-handling ignore ' + \ newFilePath + ' ' + pdfName process = subprocess.Popen(strs, stdout=subprocess.PIPE, shell=True) #output, err = process.communicate(b"input data that is passed to subprocess' stdin") output, err = process.communicate() rc = process.returncode if rc == 0: # move pdf file to temp dir from bas dir - mv 002-sip_register.pdf temp/ mvFile = subprocess.Popen( ["mv " + baseDir + "/" + pdfName + " temp/"], stdout=subprocess.PIPE, shell=True) output, err = mvFile.communicate() mvFileval = mvFile.returncode if mvFileval == 0: result['download'] = pdfName result['showDownload'] = True return jsonify(status='OK', result=result) except Exception: return