|
| 1 | +import re |
| 2 | +import json |
| 3 | +import os |
| 4 | +from django.views.generic import TemplateView |
| 5 | + |
| 6 | +from community.views import get_header_and_footer |
| 7 | +from community.git import (get_org_name, |
| 8 | + get_owner, |
| 9 | + get_deploy_url, |
| 10 | + get_upstream_deploy_url) |
| 11 | + |
| 12 | + |
| 13 | +class BuildLogsView(TemplateView): |
| 14 | + template_name = 'build_logs.html' |
| 15 | + |
| 16 | + def get_build_info(self): |
| 17 | + data = { |
| 18 | + 'Org name': get_org_name(), |
| 19 | + 'Owner': get_owner(), |
| 20 | + 'Deploy URL': get_deploy_url(), |
| 21 | + } |
| 22 | + try: |
| 23 | + upstream_deploy_url = get_upstream_deploy_url() |
| 24 | + data['Upstream deploy URL'] = upstream_deploy_url |
| 25 | + except RuntimeError: |
| 26 | + data['Upstream deploy URL'] = 'Not found' |
| 27 | + return dict(data) |
| 28 | + |
| 29 | + def get_build_logs(self): |
| 30 | + log_lines = [] |
| 31 | + log_level_specific_lines = { |
| 32 | + 'INFO': [], |
| 33 | + 'DEBUG': [], |
| 34 | + 'WARNING': [], |
| 35 | + 'ERROR': [], |
| 36 | + 'CRITICAL': [] |
| 37 | + } |
| 38 | + log_file_path = './_site/community.log' |
| 39 | + log_file_exists = os.path.isfile(log_file_path) |
| 40 | + if log_file_exists: |
| 41 | + with open(log_file_path) as log_file: |
| 42 | + previous_found_level = None |
| 43 | + for line in log_file: |
| 44 | + log_lines.append(line) |
| 45 | + levels = re.findall(r'\[[A-Z]+]', line) |
| 46 | + if levels: |
| 47 | + level = levels[0] |
| 48 | + level = previous_found_level = level[1:-1] |
| 49 | + log_level_specific_lines[level].append(line) |
| 50 | + elif previous_found_level: |
| 51 | + log_level_specific_lines[previous_found_level].append( |
| 52 | + line) |
| 53 | + with open('./_site/ci-build-detailed-logs.json', |
| 54 | + 'w+') as build_logs_file: |
| 55 | + data = { |
| 56 | + 'logs': log_lines, |
| 57 | + 'logs_level_Specific': log_level_specific_lines |
| 58 | + } |
| 59 | + json.dump(data, build_logs_file, indent=4) |
| 60 | + return True |
| 61 | + else: |
| 62 | + return False |
| 63 | + |
| 64 | + def get_context_data(self, **kwargs): |
| 65 | + context = super().get_context_data(**kwargs) |
| 66 | + context = get_header_and_footer(context) |
| 67 | + context['build_info'] = self.get_build_info() |
| 68 | + context['build_logs_stored'] = self.get_build_logs() |
| 69 | + return context |
0 commit comments