import traceback import pipes import subprocess from functions.mwDB import mwDB def get_target_files(domain=False): """ タグ埋め込み対象ファイルリストの取得 Paramerts --------- ctrid : str コンテナID domain : str Webサイト分析が有効になっているドメイン Returns ------- target_files : list タグ埋め込み対象ファイルリスト """ try: target_files = [] db = mwDB(domain) sql = "SELECT setting_value FROM web_setting WHERE setting_name='autorecover'" res = db.fetchone(sql) if int(res[0]): sql = "SELECT filepath FROM web_target_path" res = db.fetch(sql) for r in res: target_files.append(r[0]) db.close() return target_files except Exception as e: print(traceback(e)) return target_files def insert_tag(targetfile): """ 対象ファイルにアクセス解析タグを挿入する Paramerts --------- targetfile : str 対象ファイル Returns ------- 処理結果 成功の場合True エラーの場合False """ try: esc_target = pipes.quote(targetfile) tag_str = ''\ '' p_cmd = "if ! grep '" + grep_str + "' " + esc_target + \ "; then sudo sed -i -e '1,/<\/head>/ s/<\/head>/" + \ tag_str + "/g' " + esc_target + "; fi" result = subprocess.Popen(p_cmd.strip(), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() for line in result: if "No such file or directory" in line.decode(): return False elif "Permission denied" in line.decode(): return False return True except Exception as e: print(traceback(e)) return False