|
| 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 get_build_info(self): |
| 21 | + data = { |
| 22 | + 'Org name': get_org_name(), |
| 23 | + 'Owner': get_owner(), |
| 24 | + 'Deploy URL': get_deploy_url(), |
| 25 | + } |
| 26 | + try: |
| 27 | + upstream_deploy_url = get_upstream_deploy_url() |
| 28 | + data['Upstream deploy URL'] = upstream_deploy_url |
| 29 | + except RuntimeError: |
| 30 | + data['Upstream deploy URL'] = 'Not found' |
| 31 | + return data |
| 32 | + |
| 33 | + def get_build_logs(self, log_file_path): |
| 34 | + log_lines = [] |
| 35 | + log_level_specific_lines = { |
| 36 | + 'INFO': [], |
| 37 | + 'DEBUG': [], |
| 38 | + 'WARNING': [], |
| 39 | + 'ERROR': [], |
| 40 | + 'CRITICAL': [] |
| 41 | + } |
| 42 | + with open(log_file_path) as log_file: |
| 43 | + previous_found_level = None |
| 44 | + for line in log_file: |
| 45 | + log_lines.append(line) |
| 46 | + levels = re.findall(r'\[[A-Z]+]', line) |
| 47 | + if levels: |
| 48 | + level = levels[0] |
| 49 | + level = previous_found_level = level[1:-1] |
| 50 | + log_level_specific_lines[level].append(line) |
| 51 | + elif previous_found_level: |
| 52 | + log_level_specific_lines[previous_found_level].append( |
| 53 | + line) |
| 54 | + return log_lines, log_level_specific_lines |
| 55 | + |
| 56 | + def create_and_copy_build_logs_json(self, logs, level_specific_logs): |
| 57 | + ci_build_jsons = { |
| 58 | + 'site_path': './_site/ci-build-detailed-logs.json', |
| 59 | + 'public_path': './public/static/ci-build-detailed-logs.json', |
| 60 | + 'static_path': './static/ci-build-detailed-logs.json' |
| 61 | + } |
| 62 | + with open(ci_build_jsons['site_path'], 'w+') as build_logs_file: |
| 63 | + data = { |
| 64 | + 'logs': logs, |
| 65 | + 'logs_level_Specific': level_specific_logs |
| 66 | + } |
| 67 | + json.dump(data, build_logs_file, indent=4) |
| 68 | + return self.copy_build_logs_json(ci_build_jsons) |
| 69 | + |
| 70 | + def copy_build_logs_json(self, ci_build_jsons): |
| 71 | + if os.path.isfile(ci_build_jsons['public_path']): |
| 72 | + if sys.platform == 'linux': |
| 73 | + os.popen('cp {} {}'.format( |
| 74 | + ci_build_jsons['site_path'], |
| 75 | + ci_build_jsons['public_path'])) |
| 76 | + os.popen('cp {} {}'.format( |
| 77 | + ci_build_jsons['site_path'], |
| 78 | + ci_build_jsons['static_path'])) |
| 79 | + else: |
| 80 | + os.popen('copy {} {}'.format( |
| 81 | + ci_build_jsons['site_path'], |
| 82 | + ci_build_jsons['public_path'])) |
| 83 | + os.popen('copy {} {}'.format( |
| 84 | + ci_build_jsons['site_path'], |
| 85 | + ci_build_jsons['static_path'])) |
| 86 | + return True |
| 87 | + return False |
| 88 | + |
| 89 | + def check_build_logs_stored(self): |
| 90 | + log_file_path = './_site/community.log' |
| 91 | + log_file_exists = os.path.isfile(log_file_path) |
| 92 | + if log_file_exists: |
| 93 | + logs, level_specific_logs = self.get_build_logs(log_file_path) |
| 94 | + return self.create_and_copy_build_logs_json(logs, |
| 95 | + level_specific_logs) |
| 96 | + return False |
| 97 | + |
| 98 | + def get_context_data(self, **kwargs): |
| 99 | + context = super().get_context_data(**kwargs) |
| 100 | + context = get_header_and_footer(context) |
| 101 | + context['build_info'] = self.get_build_info() |
| 102 | + context['logs_stored'] = self.check_build_logs_stored() |
| 103 | + return context |
0 commit comments