forked from dashingsoft/pyarmor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbug.py
More file actions
130 lines (107 loc) · 4.48 KB
/
bug.py
File metadata and controls
130 lines (107 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
#############################################################
# #
# Copyright @ 2024 - Dashingsoft corp. #
# All rights reserved. #
# #
# Pyarmor #
# #
# Version: 8.5.1 - #
# #
#############################################################
#
#
# @File: cli/bug.py
#
# @Author: Jondy Zhao (pyarmor@163.com)
#
# @Create Date: Tue Mar 12 15:19:41 CST 2024
#
import logging
from string import Template
BUG_FILE, LOG_FILE = 'pyarmor.report.bug', '.pyarmor/pyarmor.debug.log'
BUG_TEMPLATE = Template('''[Bug] $title
### Full command options and console output
$cmdline
$tracelog
### Traceback
$tb
''')
UNUSED_ENABLE_DEBUG_HINTS = '''something is wrong
*=============================================================*
* Please enable debug option `-d` to run it again *
* pyarmor -d gen options ... *
* *
* Then check console log to find more information *
* *
* Please also check *
* https://pyarmor.readthedocs.io/en/latest/questions.html *
*=============================================================*
'''
UNUSED_SOLUTION_HINTS = Template('''something is wrong
*=============================================================*
* Please check *
* https://pyarmor.readthedocs.io/en/latest/questions.html *
* or run `pyarmor man` to find solutions quickly *
* *
* It's recommand to report issue by `pyarmor man` in order *
* to provide necessary information, and avoid dupcliated *
* issues or unclear question. *
*=============================================================*
''')
ENABLE_DEBUG_HINTS = 'please enable debug option `-d` to run it again'
SOLUTION_HINTS = Template('''something is wrong
*=============================================================*
* Please check console log to find out what's wrong *
* *
* If still not solved, please find solutions by *
* https://pyarmor.readthedocs.io/en/latest/questions.html *
*=============================================================*
''')
def generate_bug_report(e, logfile=LOG_FILE, output=BUG_FILE):
'Generate file `pyarmor.report.bug` in current path'
from os.path import exists
from sys import argv
from traceback import format_exc
if exists(logfile):
with open(logfile, 'r') as f:
lines = f.readlines()
n = len(lines)
if n > 128:
tracelog = ''.join(lines[:100] + ['...\n'] + lines[n-20:n])
else:
tracelog = ''.join(lines[:n])
else:
tracelog = Template(
'TODO: no found logfile "$logfile", please paste console log '
'here manually').substitute(logfile=logfile)
title = '%s: %s' % (type(e).__name__, str(e))
with open(output, 'w') as f:
f.write(BUG_TEMPLATE.substitute(
title=title,
cmdline=' '.join(argv),
tracelog=tracelog,
tb=format_exc()
))
def find_solutions(e):
'''Print quick solutions according to exception
If not enable debug, tell user try `pyarmor -d cmd...`
If it raises CliError, print possible solutions
For unknown error, print FAQ page link and `pyarmor man`
Pyarmor Man is designed to help Pyarmor users to learn
and use Pyarmor by web-ui, to find solution quickly when
something is wrong, to report bugs and ask questions in
standard form in order to save both Pyarmor team's and
Pyarmor users' time.
'''
logger = logging.getLogger()
if logger.getEffectiveLevel() > logging.DEBUG:
logger.error(ENABLE_DEBUG_HINTS)
return
logger.info('generate bug file "%s"', BUG_FILE)
generate_bug_report(e)
logger.error(SOLUTION_HINTS.substitute(bugfile=BUG_FILE))
if __name__ == '__main__':
pass