|
| 1 | +import re |
| 2 | +import json |
| 3 | +import os |
| 4 | +import sys |
| 5 | + |
| 6 | +from django.views.generic import TemplateView |
| 7 | + |
| 8 | +from community.views import get_header_and_footer |
| 9 | +from community.git import ( |
| 10 | + get_org_name, |
| 11 | + get_owner, |
| 12 | + get_deploy_url, |
| 13 | + get_upstream_deploy_url |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class BuildLogsView(TemplateView): |
| 18 | + template_name = 'build_logs.html' |
| 19 | + |
| 20 | + def copy_build_logs_json(self, ci_build_jsons): |
| 21 | + """ |
| 22 | + Copy the build logs detailed JSON file from ./_site directory to |
| 23 | + ./static and ./public directories |
| 24 | + :param ci_build_jsons: A dict of directories path |
| 25 | + :return: A boolean, whether the build file is copied |
| 26 | + """ |
| 27 | + if os.path.isfile(ci_build_jsons['public_path']): |
| 28 | + if sys.platform == 'linux': |
| 29 | + os.popen('cp {} {}'.format( |
| 30 | + ci_build_jsons['site_path'], |
| 31 | + ci_build_jsons['public_path'])) |
| 32 | + os.popen('cp {} {}'.format( |
| 33 | + ci_build_jsons['site_path'], |
| 34 | + ci_build_jsons['static_path'])) |
| 35 | + else: |
| 36 | + os.popen('copy {} {}'.format( |
| 37 | + ci_build_jsons['site_path'], |
| 38 | + ci_build_jsons['public_path'])) |
| 39 | + os.popen('copy {} {}'.format( |
| 40 | + ci_build_jsons['site_path'], |
| 41 | + ci_build_jsons['static_path'])) |
| 42 | + return True |
| 43 | + return False |
| 44 | + |
| 45 | + def create_and_copy_build_logs_json(self, logs, level_specific_logs): |
| 46 | + """ |
| 47 | + Create a build logs detailed json file in ./_site directory and copy |
| 48 | + that file in the ./static and ./public/static directories |
| 49 | + :param logs: A list of all lines in build log file |
| 50 | + :param level_specific_logs: A dict containing logs divided in their |
| 51 | + respective categories |
| 52 | + :return: A boolean, whether the files were copied or not |
| 53 | + """ |
| 54 | + ci_build_jsons = { |
| 55 | + 'site_path': './_site/ci-build-detailed-logs.json', |
| 56 | + 'public_path': './public/static/ci-build-detailed-logs.json', |
| 57 | + 'static_path': './static/ci-build-detailed-logs.json' |
| 58 | + } |
| 59 | + with open(ci_build_jsons['site_path'], 'w+') as build_logs_file: |
| 60 | + data = { |
| 61 | + 'logs': logs, |
| 62 | + 'logs_level_Specific': level_specific_logs |
| 63 | + } |
| 64 | + json.dump(data, build_logs_file, indent=4) |
| 65 | + return self.copy_build_logs_json(ci_build_jsons) |
| 66 | + |
| 67 | + def get_build_logs(self, log_file_path): |
| 68 | + """ |
| 69 | + :param log_file_path: build logs file path |
| 70 | + :return: a tuple of two where the first element in tuple refers to |
| 71 | + a list of build logs in the file, and the second element is a dict |
| 72 | + which categorizes the build logs into 5 categories - INFO, DEBUG, |
| 73 | + WARNING, ERROR nad CRITICAL |
| 74 | + """ |
| 75 | + log_lines = [] |
| 76 | + log_level_specific_lines = { |
| 77 | + 'INFO': [], |
| 78 | + 'DEBUG': [], |
| 79 | + 'WARNING': [], |
| 80 | + 'ERROR': [], |
| 81 | + 'CRITICAL': [] |
| 82 | + } |
| 83 | + with open(log_file_path) as log_file: |
| 84 | + previous_found_level = None |
| 85 | + for line in log_file: |
| 86 | + log_lines.append(line) |
| 87 | + levels = re.findall(r'\[[A-Z]+]', line) |
| 88 | + if levels: |
| 89 | + level = levels[0] |
| 90 | + level = previous_found_level = level[1:-1] |
| 91 | + log_level_specific_lines[level].append(line) |
| 92 | + elif previous_found_level: |
| 93 | + log_level_specific_lines[previous_found_level].append( |
| 94 | + line) |
| 95 | + return log_lines, log_level_specific_lines |
| 96 | + |
| 97 | + def check_build_logs_stored(self): |
| 98 | + """ |
| 99 | + Check whether the build logs json file is copied to _site and public |
| 100 | + directories or not |
| 101 | + :return: A Boolean |
| 102 | + """ |
| 103 | + log_file_path = './_site/community.log' |
| 104 | + log_file_exists = os.path.isfile(log_file_path) |
| 105 | + if log_file_exists: |
| 106 | + logs, level_specific_logs = self.get_build_logs(log_file_path) |
| 107 | + return self.create_and_copy_build_logs_json(logs, |
| 108 | + level_specific_logs) |
| 109 | + return False |
| 110 | + |
| 111 | + def get_build_info(self): |
| 112 | + """ |
| 113 | + Get the information about build, like who deployed the website i.e. |
| 114 | + owner, name of the organization or user etc. |
| 115 | + :return: A dict having information about build related details |
| 116 | + """ |
| 117 | + data = { |
| 118 | + 'Org name': get_org_name(), |
| 119 | + 'Owner': get_owner(), |
| 120 | + 'Deploy URL': get_deploy_url(), |
| 121 | + } |
| 122 | + try: |
| 123 | + data['Upstream deploy URL'] = get_upstream_deploy_url() |
| 124 | + except RuntimeError: |
| 125 | + data['Upstream deploy URL'] = 'Not found' |
| 126 | + return data |
| 127 | + |
| 128 | + def get_context_data(self, **kwargs): |
| 129 | + context = super().get_context_data(**kwargs) |
| 130 | + context = get_header_and_footer(context) |
| 131 | + context['build_info'] = self.get_build_info() |
| 132 | + context['logs_stored'] = self.check_build_logs_stored() |
| 133 | + return context |
0 commit comments