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