通过Java ProcessBuilder激活virtualenv

时间:2018-06-18 18:53:37

标签: java multithreading bash unix processbuilder

尝试通过以下代码以编程方式激活Python的 virtualenv 时获取以下信息:

java.io.IOException: Cannot run program "." (in directory "/Users/simeon.../..../reporting"): error=13, Permission denied
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
    at VirtualEnvCreateCmdTest.runCommandInDirectory(VirtualEnvCreateCmdTest.java:30)
    at VirtualEnvCreateCmdTest.createVirtEnv(VirtualEnvCreateCmdTest.java:61)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at ......
Caused by: java.io.IOException: error=13, Permission denied
    at java.lang.UNIXProcess.forkAndExec(Native Method)
    at java.lang.UNIXProcess.<init>(UNIXProcess.java:247)
    at java.lang.ProcessImpl.start(ProcessImpl.java:134)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
    ... 25 more

代码:

public class VirtualEnvCreateCmdTest {

    private final static Logger LOG = LoggerFactory.getLogger(VirtualEnvCreateCmdTest.class);

    private void runCommandInDirectory(String path,String ... command) throws Throwable
    {

        LOG.info("Running command '"+String.join(" ",command)+"' in path '"+path+"'");


        ProcessBuilder builder = new ProcessBuilder(command)
                .directory(new File(path))
                .inheritIO();

        Process pr = builder.start();

        final String failureMsg = format("Failed to run '%s' in path '%s'.  Got exit code: %d", join(" ",command), path, pr.exitValue());
        LOG.info("prepared message {}: ", failureMsg);

        if(!pr.waitFor(120, TimeUnit.SECONDS))
        {
            throw new Exception(failureMsg);

        }


        int output = IOUtils.copy(pr.getInputStream(), System.out);

        int exitCode=pr.exitValue();

        if(exitCode!=0)
            throw new Exception(failureMsg);

    }

    @Test
    public void createVirtEnv()  throws Throwable {

        String path = "/Users/simeon/.../reporting";
        String [] commands = new String[]{".", "activate"};
        //String [] commands = new String [] {"/bin/bash", "-c", ". /Users/simeon/..../venv2.7/bin/activate"};
        runCommandInDirectory(path, commands);

    }

更改文件的权限似乎不起作用:

chmod u+x ./bin/activate

通过/bin/bash执行此操作仍然无效,但有不同的错误。

同时,以下命令行正常工作:

. /Users/simeon/.../venv2.7/bin/activate

如何从Java中调用python virtualenv activate命令的任何示例?

===有效:=====

以下结果为我工作:

    private void runDjangoMigrate() throws Throwable {

        final String REPORTING_PROJECT_LOCATION = "/Users/simeon.../.../reporting";
        final String UNIX_SHELL_LOCATION = "/bin/bash";
        final String PYTHON_VIRTUALENV_ACTIVATOR_COMMAND =". /Users/simeon.../.../venv2.7/bin/activate;";
        final String PYTHON_VIRTUALENV_ACTIVATOR_COMMAND =". " + PYTHON_VIRTUALENV_ACTIVATE_SCRIPT_LOCATION + ";"; 
        final String DJANGO_MANAGE_MODULE = " manage.py";

        final String[] DJANGO_MIGRATE_COMMAND = new String[] { UNIX_SHELL_LOCATION, "-c", PYTHON_VIRTUALENV_ACTIVATOR_COMMAND
                + PYTHON_INTERPRETER + DJANGO_MANAGE_MODULE + " migrate --noinput --fake-initial" };

        runCommandInDirectory(REPORTING_PROJECT_LOCATION, DJANGO_MIGRATE_COMMAND);


}


    private void runCommandInDirectory(String path, String... command) throws Throwable {

        LOG.info(format("Running '%s' command in path '%s'", join(" ",command),path));

        ProcessBuilder builder = new ProcessBuilder(command).directory(new File(path)).inheritIO();

        Process pr = null;

        pr = builder.start();

        IOUtils.copy(pr.getInputStream(), System.out);  

        boolean terminated = pr.waitFor(SPAWNED_PYTHON_PROCESS_TTL_SEC, SECONDS);

        int exitCode = -1;

        if (terminated) {

            exitCode = pr.exitValue();
        }

        if (exitCode != 0) {

            final String failureMsg = format("Failed to run '%s' in path '%s'.  Got exit code: %d", join(" ", command),
                    path, pr.exitValue());

            throw new Exception(failureMsg);
        }

    }

并产生以下预期输出:

[java] System check identified some issues:
 [java]
 [java] WARNINGS:
 [java] ?: (urls.W005) URL namespace 'admin' isn't unique. You may not be able to reverse all URLs in this namespace
 [java] Building permissions...
 [java] Operations to perform:
 [java]   Apply all migrations: admin, auth, contenttypes, sessions
 [java] Running migrations:
 [java]   No migrations to apply.
 [java] System check identified some issues:
 [java]
 [java] WARNINGS:
 [java] ?: (urls.W005) URL namespace 'admin' isn't unique. You may not be able to reverse all URLs in this namespace
 [java] Building permissions...
 [java] User exists, exiting normally due to --preserve
 [java] System check identified some issues:
 [java]

1 个答案:

答案 0 :(得分:1)

您可以在bash(可能是zsh)和Python中激活Python虚拟环境,但不能在Java或任何其他环境中激活。要理解的主要是虚拟环境激活不会产生一些系统魔力 - 激活脚本只是改变当前环境,而Python虚拟环境准备改变shell或Python,但没有别的。< / p>

就Java而言,这意味着当你调用runCommandInDirectory()它运行一个新shell时,新shell会短暂激活虚拟环境,但是shell退出并且所有“激活”虚拟环境的更改都消失了

这反过来意味着如果您需要在虚拟环境中运行一些shell或Python命令,则必须在每个runCommandInDirectory()调用中激活环境:

String [] commands1 = new String [] {"/bin/bash", "-c", ". /Users/simeon/..../venv2.7/bin/activate; script1.sh"};
runCommandInDirectory(path, commands)

String [] commands2 = new String [] {"/bin/bash", "-c", ". /Users/simeon/..../venv2.7/bin/activate; script2.sh"};
runCommandInDirectory(path, commands)

对于Python脚本,它更简单,因为您可以从环境中运行python并自动激活环境:

String [] commands = new String [] {"/Users/simeon/..../venv2.7/bin/python", "script.py"};
runCommandInDirectory(path, commands)