PythonUtil.java 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 E0401 = "E0401";
  20. public static String E0601 = "E0601";
  21. public static String F = "F";
  22. public static String PASS = "Your code has been rated at 10.00/10";
  23. /**
  24. * @param pythonCode python 代码
  25. * @param disables 忽略指定等级的日志
  26. */
  27. @SneakyThrows
  28. public static String pylint(String pythonCode, String... disables) {
  29. //1 把代码保存成本地文件
  30. String filePath = "/SimulationCloud/Evaluate/test/" + UUID.randomUUID().toString().replace("-", "") + ".py";
  31. FileUtil.writeStringToLocalFile(pythonCode, filePath);
  32. //2 执行命令检查格式
  33. StringBuilder command = new StringBuilder("pylint3 --disable=");
  34. for (String disable : disables) {
  35. command.append(disable).append(",");
  36. }
  37. command.append(" ").append(filePath);
  38. return LinuxUtil.execute(command.toString());
  39. }
  40. }