Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev 1.9.0 bug fix #635

Merged
merged 10 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Mapper
public interface UserDepartmentInfoMapper {

void insertUser(UserDepartmentInfo user);

@Transactional(rollbackFor = Exception.class)
int batchInsertUsers(@Param("userDepartmentInfos") List<UserDepartmentInfo> userDepartmentInfos);

void updateUser(UserDepartmentInfo user);

UserDepartmentInfo selectUser(@Param("userName") String userName);

@Transactional(rollbackFor = Exception.class)
void deleteUser();

List<UserDepartmentInfo> selectAllUsers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import java.nio.file.Paths;
import java.text.MessageFormat;
import java.util.*;
import java.util.stream.Collectors;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
Expand Down Expand Up @@ -1466,7 +1467,7 @@ public Message pythonUpload(
List<String> pythonModules = FilesystemUtils.getInstallRequestPythonModules(file);
String dependencies = "";
if (CollectionUtils.isNotEmpty(pythonModules)) {
dependencies = String.join(",", pythonModules);
dependencies = pythonModules.stream().distinct().collect(Collectors.joining(","));
String errorMsg = FilesystemUtils.checkModuleFile(pythonModules, username);
if (StringUtils.isNotBlank(errorMsg)) {
return Message.error("部分依赖未加载,请检查并重新上传依赖包,依赖信息:" + errorMsg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.StringJoiner;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -120,12 +117,12 @@ private static String getRootPath(InputStream inputStream, String folder) throws
while ((entry = tarInput.getNextTarEntry()) != null) {
if (entry.isDirectory()
&& entry.getName().endsWith(FsPath.SEPARATOR + folder + FsPath.SEPARATOR)) {
return entry.getName();
return entry.getName().replace(folder + FsPath.SEPARATOR, "");
}
if (entry.getName().contains(FsPath.SEPARATOR + folder + FsPath.SEPARATOR)) {
String delimiter = FsPath.SEPARATOR + folder + FsPath.SEPARATOR;
int delimiterIndex = entry.getName().indexOf(delimiter);
return entry.getName().substring(0, delimiterIndex + delimiter.length());
return entry.getName().substring(0, delimiterIndex);
}
}
} catch (Exception e) {
Expand Down Expand Up @@ -199,7 +196,7 @@ public static String checkModuleFile(List<String> pythonModules, String username
String exec =
Utils.exec(
(new String[] {
"python",
"python3",
Configuration.getLinkisHome() + "/admin/" + "check_modules.py",
module
}));
Expand Down Expand Up @@ -234,7 +231,7 @@ public static List<String> getInstallRequestPythonModules(MultipartFile file) th
new TarArchiveInputStream(new GzipCompressorInputStream(file.getInputStream()))) {
TarArchiveEntry entry;
while ((entry = tarInput.getNextTarEntry()) != null) {
if (entry.getName().endsWith("setup.py")) {
if (entry.getName().endsWith("setup.py") || entry.getName().endsWith("pyproject.toml")) {
findSetup = 1;
String content = IOUtils.toString(tarInput, StandardCharsets.UTF_8);
modules = extractDependencies(content);
Expand All @@ -245,31 +242,49 @@ public static List<String> getInstallRequestPythonModules(MultipartFile file) th
throw WorkspaceExceptionManager.createException(80039, e.getMessage());
}
if (findSetup == 0) {
throw WorkspaceExceptionManager.createException(80040, "setup.py");
throw WorkspaceExceptionManager.createException(80040, "setup.py or pyproject.toml");
}
}
return modules;
}

public static List<String> extractDependencies(String content) {
String trim =
content
.replaceAll("#.*?\\n", "")
.replaceAll("\\n", "")
.replaceAll("'", "")
.replaceAll(" ", "")
.trim();
List<String> modules = new ArrayList<>();
Pattern pattern = Pattern.compile("install_requires=\\[(.*?)\\]", Pattern.DOTALL);
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
String requirements = matcher.group(1);
String[] packages = requirements.split(",");
for (String pkg : packages) {
pkg = pkg.replaceAll("#.*?\\n", "").replaceAll("\\n", "").replaceAll("'", "").trim();
for (String operator : OPERATORS) {
if (pkg.contains(operator)) {
String[] parts = pkg.split(operator);
pkg = parts[0].trim();
}
}
if (StringUtils.isNotBlank(pkg)) {
modules.add(pkg);
String moduleStr = "";
Matcher setupMatcher =
Pattern.compile("install_requires=\\[(.*?)\\]", Pattern.DOTALL).matcher(trim);
if (setupMatcher.find()) {
moduleStr = setupMatcher.group(1);
}
Matcher pyprojectMatcher =
Pattern.compile("dependencies=\\[(.*?)\\]", Pattern.DOTALL).matcher(trim);
if (pyprojectMatcher.find()) {
moduleStr = pyprojectMatcher.group(1);
}
String[] packages = moduleStr.split(",");
for (String pkg : packages) {
pkg =
pkg.replaceAll("#.*?\\n", "")
.replaceAll("\\n", "")
.replaceAll("'", "")
.replace("\"", "")
.trim();
for (String operator : OPERATORS) {
if (pkg.contains(operator)) {
String[] parts = pkg.split(operator);
pkg = parts[0].trim();
}
}
if (StringUtils.isNotBlank(pkg)) {
modules.add(pkg);
}
}
return modules;
}
Expand Down
Loading