12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- package api.common.util;
- import lombok.SneakyThrows;
- import lombok.extern.slf4j.Slf4j;
- import java.util.UUID;
- @Slf4j
- public class PythonUtil {
- /*
- C:惯例,违反了编码风格标准
- R:重构,代码非常糟糕
- W:警告,某些 Python 特定的问题
- E:错误,很可能是代码中的错误
- F:致命错误,阻止 Pylint 进一步运行的错误
- */
- public static String LINE_TOO_LONG = "C0301";
- public static String C = "C";
- public static String R = "R";
- public static String W = "W";
- public static String E = "E";
- public static String F = "F";
- public static String PASS = "Your code has been rated at 10.00/10";
- /**
- * @param pythonCode python 代码
- * @param disables 忽略指定等级的日志
- */
- @SneakyThrows
- public static String pylint(String pythonCode, String... disables) {
- //1 把代码保存成本地文件
- String filePath = "/tmp/" + UUID.randomUUID().toString().replace("-", "") + ".py";
- FileUtil.writeStringToLocalFile(pythonCode, filePath);
- //2 执行命令检查格式
- StringBuilder command = new StringBuilder("pylint --disable=");
- for (String disable : disables) {
- command.append(disable).append(",");
- }
- command.append(" ").append(filePath);
- return LinuxUtil.execute(command.toString());
- }
- }
|