#!/usr/bin/python # -*- coding: utf-8 -*- # --------------------------------------------------------------------------------------------------------- # Author : Ulrich Carmesin # Source : gitea.ucarmesin.de # --------------------------------------------------------------------------------------------------------- """ program to clean the workspace : * remove old debug-files """ import os import re import shutil import sys import traceback import basic.program import basic.constants as B import tools.date_tool as date_tool import tools.path_tool as path_tool import tools.job_tool as job_tool LIMIT_DEBUG_FILES = -7 PROGRAM_NAME = "clean_workspace" def startPyJob(job): try: job.m.logDebug("--- start " + PROGRAM_NAME + " ------>>>>") # remove debug-files removeDebugFiles(job) # clean and archive log-files cleanLogFiles(job) job.m.setMsg("Job " + PROGRAM_NAME + " fertig") job.m.logDebug("<<<<<<<<----- " + PROGRAM_NAME + " ------") except Exception as e: job.m.logDebug("+++++++++++++++++++++++++++++++++++++++++++++") job.m.setFatal(str(e)) job.m.logDebug("+++++++++++++++++++++++++++++++++++++++++++++") job.m.logDebug("execpt "+traceback.format_exc()) job.m.logDebug("+++++++++++++++++++++++++++++++++++++++++++++") def removeDebugFiles(job): """ to remove debug-files in any relevant folder :param job: :return: """ job.m.logInfo("# # remove log-files # # #") limit = date_tool.getActdate(date_tool.F_LOG, LIMIT_DEBUG_FILES)[0:8] path = job.conf[B.SUBJECT_PATH][B.ATTR_PATH_DEBUG] cleanFolder(job, path, limit) path = os.path.join(B.HOME_PATH, "temp") cleanFolder(job, path, limit) def cleanFolder(job, path, limit): """ remove all files in the folder with a log-date older than limit :param job: :param path: :param limit: :return: """ date_regex = r"(.*)_(\d{8})_\d{6}" cntRm = 0 cntMv = 0 cntAll = 0 for f in os.listdir(path): cntAll += 1 if re.match(date_regex, f): res = re.search(date_regex, f) fileType = str(res.group(1)) fileDate = str(res.group(2)) if fileType in ["debug", "log", "start_dialog"]: if fileDate >= limit: continue job.m.logInfo("remove " + os.path.join(path, f)) os.remove(os.path.join(path, f)) cntRm += 1 else: fileYear = fileDate[0:4] actYear = date_tool.getActdate(date_tool.F_LOG)[0:4] archivPath = os.path.join(path, fileYear) if fileYear < actYear: if not os.path.exists(archivPath): os.mkdir(archivPath) if not os.path.isdir(archivPath): raise Exception("archiv-folder is not a directory: " + archivPath) shutil.move(os.path.join(path, f), os.path.join(archivPath, f)) cntMv += 1 job.m.setMsg(str(cntRm) + " / " + str(cntAll) + " files removed in " + path) job.m.setMsg(str(cntMv) + " / " + str(cntAll) + " files moved from " + path) def cleanLogFiles(job): """ searches all log-folder in test-documents and remove the oldest log-files except the newest :param job: :return: """ job.m.logInfo("# # clean log-files # # #") limit = date_tool.getActdate(date_tool.F_LOG, LIMIT_DEBUG_FILES)[0:8] path = path_tool.compose_path(job, "{job.par.wsdir}/{log}", None) cleanFolder(job, path, limit) environments = job_tool.select_environment(job, "", "ALL") for env in environments: jobEnv = "" if hasattr(job.par, "environment"): jobEnv = getattr(job.par, "environment") setattr(job.par, "environment", env) path = path_tool.compose_path(job, "{envlog}", None) cleanFolder(job, path, limit) setattr(job.par, "environment", jobEnv) pass if __name__ == '__main__': job = basic.program.Job(PROGRAM_NAME) startPyJob(job)