PythonUtil.java 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package api.common.util;
  2. import lombok.SneakyThrows;
  3. import lombok.extern.slf4j.Slf4j;
  4. import java.util.UUID;
  5. @Slf4j
  6. public class PythonUtil {
  7. /*
  8. C:惯例,违反了编码风格标准
  9. R:重构,代码非常糟糕
  10. W:警告,某些 Python 特定的问题
  11. E:错误,很可能是代码中的错误
  12. F:致命错误,阻止 Pylint 进一步运行的错误
  13. */
  14. public static String LINE_TOO_LONG = "C0301";
  15. public static String C = "C";
  16. public static String R = "R";
  17. public static String W = "W";
  18. public static String E = "E";
  19. public static String F = "F";
  20. public static String PASS = "Your code has been rated at 10.00/10";
  21. /**
  22. * @param pythonCode python 代码
  23. * @param disables 忽略指定等级的日志
  24. */
  25. @SneakyThrows
  26. public static String pylint(String pythonCode, String... disables) {
  27. //1 把代码保存成本地文件
  28. String filePath = "/tmp/" + UUID.randomUUID().toString().replace("-", "") + ".py";
  29. FileUtil.writeStringToLocalFile(pythonCode, filePath);
  30. //2 执行命令检查格式
  31. StringBuilder command = new StringBuilder("pylint --disable=");
  32. for (String disable : disables) {
  33. command.append(disable).append(",");
  34. }
  35. command.append(" ").append(filePath);
  36. return LinuxUtil.execute(command.toString());
  37. }
  38. }