Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
erp5
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Lukas Niegsch
erp5
Commits
cfa27232
Commit
cfa27232
authored
May 25, 2020
by
Jérome Perrin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core: use mypy to check python code ( WIP experiment )
parent
39fd2694
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
75 additions
and
18 deletions
+75
-18
product/ERP5/bootstrap/erp5_core/ExtensionTemplateItem/portal_components/extension.erp5.PythonCodeUtils.py
...eItem/portal_components/extension.erp5.PythonCodeUtils.py
+69
-17
product/ERP5/bootstrap/erp5_core/ExtensionTemplateItem/portal_components/extension.erp5.PythonCodeUtils.xml
...Item/portal_components/extension.erp5.PythonCodeUtils.xml
+6
-1
No files found.
product/ERP5/bootstrap/erp5_core/ExtensionTemplateItem/portal_components/extension.erp5.PythonCodeUtils.py
View file @
cfa27232
...
...
@@ -5,12 +5,15 @@ from Products.ERP5Type.Utils import checkPythonSourceCode
logger
=
logging
.
getLogger
(
'extension.erp5.PythonCodeUtils'
)
import
erp5.portal_type
import
typing
def
checkPythonSourceCodeAsJSON
(
self
,
data
,
REQUEST
=
None
):
#xtype: (erp5.portal_type.ERP5Site, str, str) -> str
"""
Check Python source suitable for Source Code Editor and return a JSON object
"""
import
json
# XXX data is encoded as json, because jQuery serialize lists as []
if
isinstance
(
data
,
basestring
):
data
=
json
.
loads
(
data
)
...
...
@@ -77,45 +80,94 @@ def checkPythonSourceCodeAsJSON(self, data, REQUEST=None):
reporter
=
Reporter
()
pyflakes
.
api
.
check
(
code
,
script_name
,
reporter
)
logger
.
info
(
'pyflake checked %d lines in %.2f'
,
len
(
code
.
splitlines
()),
time
.
time
()
-
start
)
'pyflakes checked %d lines in %.2f'
,
len
(
code
.
splitlines
()),
time
.
time
()
-
start
)
message_list
=
reporter
.
message_list
import
lib2to3.refactor
import
lib2to3.pgen2.parse
refactoring_tool
=
lib2to3
.
refactor
.
RefactoringTool
(
fixer_names
=
(
'lib2to3.fixes.fix_except'
,
))
refactoring_tool
=
lib2to3
.
refactor
.
RefactoringTool
(
fixer_names
=
(
'lib2to3.fixes.fix_except'
,))
old_code
=
code
.
decode
(
'utf-8'
)
try
:
new_code
=
unicode
(
refactoring_tool
.
refactor_string
(
old_code
,
script_name
))
except
lib2to3
.
pgen2
.
parse
.
ParseError
as
e
:
message
,
(
row
,
column
)
=
e
.
context
message_list
.
append
(
dict
(
row
=
row
,
column
=
column
,
type
=
'E'
,
text
=
message
))
message_list
.
append
(
dict
(
row
=
row
,
column
=
column
,
type
=
'E'
,
text
=
message
))
else
:
if
new_code
!=
old_code
:
i
=
0
for
new_line
,
old_line
in
zip
(
new_code
.
splitlines
(),
old_code
.
splitlines
()):
for
new_line
,
old_line
in
zip
(
new_code
.
splitlines
(),
old_code
.
splitlines
()):
i
+=
1
print
(
'new_line'
,
new_line
,
'old_line'
,
old_line
)
print
(
'new_line'
,
new_line
,
'old_line'
,
old_line
)
if
new_line
!=
old_line
:
message_list
.
append
(
dict
(
row
=
i
,
column
=
0
,
type
=
'W'
,
text
=
u'-{}
\
n
+{}'
.
format
(
old_line
,
new_line
)))
dict
(
row
=
i
,
column
=
0
,
type
=
'W'
,
text
=
u'-{}
\
n
+{}'
.
format
(
old_line
,
new_line
)))
# import pdb; pdb.set_trace()
pylint_message_list
=
[]
if
1
:
if
0
:
start
=
time
.
time
()
pylint_message_list
=
checkPythonSourceCode
(
code
,
data
.
get
(
'portal_type'
))
logger
.
info
(
'pylint checked %d lines in %.2f'
,
len
(
code
.
splitlines
()),
time
.
time
()
-
start
)
'pylint checked %d lines in %.2f'
,
len
(
code
.
splitlines
()),
time
.
time
()
-
start
)
message_list
=
pylint_message_list
import
subprocess
start
=
time
.
time
()
mypy_process
=
subprocess
.
Popen
(
[
"/srv/slapgrid/slappart3/srv/runner/software/7ded4ab7e8ec62a2b1ad4312c472eeea/parts/python-language-server/bin/mypy"
,
"--python-version=2.7"
,
"--allow-redefinition"
,
"--allow-untyped-globals"
,
"--ignore-missing-imports"
,
# XXX
# "--check-untyped-defs",
"--show-error-codes"
,
"-c"
,
code
,
# "--booom"
],
env
=
{
'MYPYPATH'
:
'/tmp/ahaha/'
,
'MYPY_CACHE_DIR'
:
'/tmp/ahaha/.mypy_cache/'
,
},
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
mypy_out
,
mypy_err
=
mypy_process
.
communicate
()
logger
.
info
(
'mypy checked %d lines in %.2f'
,
len
(
code
.
splitlines
()),
time
.
time
()
-
start
)
#import pdb
# pdb.set_trace()
# '<string>:9: error: Type signature has too few arguments\nFound 1 error in 1 file (checked 1 source file)\n'
for
line
in
mypy_out
.
splitlines
():
try
:
filename
,
line_number
,
error_type
,
message
=
line
.
split
(
':'
,
3
)
except
ValueError
as
e
:
logger
.
info
(
"oops %s / %s"
,
e
,
line
)
else
:
if
filename
==
'<string>'
:
message_list
.
append
(
dict
(
row
=
int
(
line_number
),
column
=
0
,
type
=
'E'
,
text
=
"mypy: "
+
message
,
))
for
message_dict
in
message_list
:
if
is_script
:
message_dict
[
'row'
]
=
message_dict
[
'row'
]
-
2
...
...
product/ERP5/bootstrap/erp5_core/ExtensionTemplateItem/portal_components/extension.erp5.PythonCodeUtils.xml
View file @
cfa27232
...
...
@@ -45,7 +45,12 @@
<item>
<key>
<string>
text_content_warning_message
</string>
</key>
<value>
<tuple/>
<tuple>
<string>
W:145, 12: Unused variable \'mypy_err\' (unused-variable)
</string>
<string>
W:156, 29: Unused variable \'error_type\' (unused-variable)
</string>
<string>
W: 8, 0: Unused import erp5.portal_type (unused-import)
</string>
<string>
W: 9, 0: Unused import typing (unused-import)
</string>
</tuple>
</value>
</item>
<item>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment