code_check.py 995 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import json
  2. import os
  3. import tempfile
  4. from typing import Optional
  5. from fastapi import APIRouter
  6. from fastapi import Depends
  7. from app import schemas
  8. from utils.sx_time import sxtimeit
  9. from utils.sx_web import web_try
  10. from pylint import epylint
  11. import sqlfluff
  12. router = APIRouter(
  13. prefix="/jpt/code_check",
  14. tags=["code_check-代码校验"],
  15. )
  16. @router.post("/")
  17. @web_try()
  18. @sxtimeit
  19. def code_check(code: str, code_type: str):
  20. res = []
  21. if code_type == "python":
  22. file = tempfile.NamedTemporaryFile(delete=False, mode='w')
  23. file.write(code)
  24. file.close()
  25. options = ' '.join([
  26. file.name,
  27. '--output-format', 'json',
  28. ])
  29. (lint_stdout, lint_stderr) = epylint.py_run(return_std=True, command_options=options)
  30. os.remove(file.name)
  31. res = json.loads(lint_stdout.getvalue())
  32. else:
  33. lint_out = sqlfluff.lint(code,'ansi')
  34. res = [l for l in lint_out if l['code'] == 'PRS']
  35. return res