From 8a74e9c0f3f9fbf5709201719431d0a2b7cb8794 Mon Sep 17 00:00:00 2001 From: 潘志宝 <979469083@qq.com> Date: 星期五, 22 十一月 2024 10:06:07 +0800 Subject: [PATCH] 计划数据图表 --- iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/MpkFileServiceImpl.java | 100 ++++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 88 insertions(+), 12 deletions(-) diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/MpkFileServiceImpl.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/MpkFileServiceImpl.java index f37fa61..3db6b2b 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/MpkFileServiceImpl.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/MpkFileServiceImpl.java @@ -22,7 +22,6 @@ import com.iailab.module.model.mpk.dto.GeneratorCodeHistoryDTO; import com.iailab.module.model.mpk.dto.ModelMethodDTO; import com.iailab.module.model.mpk.dto.MpkFileDTO; -import com.iailab.module.model.mpk.dto.ProjectPackageHistoryDTO; import com.iailab.module.model.mpk.entity.GeneratorCodeHistoryEntity; import com.iailab.module.model.mpk.entity.MpkFileEntity; import com.iailab.module.model.mpk.entity.ProjectPackageHistoryEntity; @@ -130,6 +129,12 @@ entity.setCreateDate(new Date()); insert(entity); modelMethodService.insertList(dto.getModelMethods(), entity.getId()); + + // 替换环境变量MDK_PKGS下的py文件 +// String mdkPkgs = System.getenv("MDK_PKGS"); +// String pyFilePath = mdkPkgs + File.separator + entity.getPyModule().replace(".", File.separator) + File.separator + entity.getPyName() + ".pyd"; +// FileUtil.mkParentDirs(pyFilePath); +// FileUtil.copy(entity.getFilePath(), pyFilePath, true); } @Override @@ -141,6 +146,12 @@ updateById(entity); modelMethodService.deleteModelMethod(entity.getId()); modelMethodService.insertList(dto.getModelMethods(), entity.getId()); + + // 替换环境变量MDK_PKGS下的py文件 +// String mdkPkgs = System.getenv("MDK_PKGS"); +// String pyFilePath = mdkPkgs + File.separator + entity.getPyModule().replace(".", File.separator) + File.separator + entity.getPyName() + ".pyd"; +// FileUtil.mkParentDirs(pyFilePath); +// FileUtil.copy(entity.getFilePath(), pyFilePath, true); } @Override @@ -234,6 +245,11 @@ //生成Jni.h文件 File jniHFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + "Jni.h"); GenUtils.drawTemplate("Jni.h.vm", dataContext, jniHFile); + + // 添加python源文件 + String pyFilePath = dirPath.getAbsolutePath() + File.separator + MdkConstant.ALGS + File.separator + entity.getPyModule().replace(".", File.separator) + File.separator + entity.getPyName() + ".pyd"; + FileUtil.mkParentDirs(pyFilePath); + FileUtil.copy(entity.getFilePath(), pyFilePath, true); // 资源文件 FileUtil.copy(mpkResources + File.separator + "libs", dirPath.getAbsolutePath(), true); @@ -480,26 +496,86 @@ Long tenantId = TenantContextHolder.getTenantId(); // 备份文件 租户隔离 String mpkTenantBakFilePath = mpkBakFilePath + File.separator + tenantId; - - File dir = new File(mpkTenantBakFilePath); - if (!dir.exists()) { - dir.mkdirs(); + File bakDir = new File(mpkTenantBakFilePath); + if (!bakDir.exists()) { + bakDir.mkdirs(); + } + //临时文件夹 + File tempDir = null; + try { + tempDir = Files.createTempDirectory("pyFileTmp").toFile(); + log.info("生成临时文件夹," + tempDir.getAbsolutePath()); + } catch (IOException e) { + throw new RuntimeException("创建临时文件夹异常",e); } String fileName = file.getOriginalFilename(); - String fileSuffix = fileName.substring(fileName.lastIndexOf(".")); - File saveFile = new File(dir.getAbsolutePath() + File.separator + UUID.randomUUID() + fileSuffix); + File pydBakFile = new File(bakDir.getAbsolutePath() + File.separator + UUID.randomUUID() + ".pyd"); - saveFile.createNewFile(); - // 保存 - file.transferTo(saveFile); + String pyName = fileName.substring(0, fileName.lastIndexOf(".")); + try { + // py文件存入临时文件夹 + File saveFile = new File(tempDir.getAbsolutePath() + File.separator + fileName); + saveFile.createNewFile(); + file.transferTo(saveFile); + + // 临时文件夹中生成pyd文件 + // 生成setup.py文件 + createSetUpFile(tempDir.getAbsolutePath(),fileName,pyName); + // 执行cmd命令编译pyd文件 + createPydFile(tempDir.getAbsolutePath()); + + // 临时文件夹中pyd文件移到dir下,修改为uuid.pyd + File pydFile = new File(tempDir.getAbsolutePath() + File.separator + pyName + MdkConstant.PYD_SUFFIX); + if (!pydFile.exists()) { + throw new RuntimeException("编译pyd文件失败!"); + } + log.info("备份pyd文件," + pydBakFile.getAbsolutePath()); + FileUtil.copy(pydFile,pydBakFile,true); + } catch (Exception e) { + throw new RuntimeException("编译pyd文件异常"); + } finally { + if (tempDir.exists()) { + tempDir.delete(); + } + } Map<String, String> result = new HashMap<>(2); - result.put("filePath", saveFile.getAbsolutePath()); - result.put("fileName", fileName); + result.put("filePath", pydBakFile.getAbsolutePath()); + result.put("fileName", pyName); return result; } + private void createPydFile(String dir) { + try { + String command = "python setup.py build_ext --inplace"; + log.info("执行cmd命令编译pyd:" + command); + ProcessBuilder builder = new ProcessBuilder("cmd", "/c", command); + builder.directory(new File(dir)); // 设置工作目录 + builder.redirectErrorStream(true); // 将错误输出和标准输出合并 + Process process = builder.start(); + // 获取命令输出结果 + InputStream inputStream = process.getInputStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "GBK")); // 设置编码为GBK + String line; + while ((line = reader.readLine()) != null) { + log.info(line); + } + // 等待命令执行完成 + process.waitFor(); + } catch (Exception e) { + throw new RuntimeException("执行cmd命令生成dll异常", e); + } + } + + private void createSetUpFile(String dir, String pyFileName, String pyName) { + // 生成setup.py文件 + HashMap<String,Object> map = new HashMap<>(); + map.put("fileName",pyFileName); + map.put("pyName",pyName); + GenUtils.drawTemplate("setup.py.vm",map,new File(dir + File.separator + MdkConstant.SETUP_PY)); + } + @Override public CommonResult<String> publish(Map<String, Object> params) { String historyId = (String) params.get("historyId"); -- Gitblit v1.9.3