1

I have the following exec commands in my gradle.build file.

exec {
        workingDir System.getProperty("user.dir")
        commandLine 'python3.6', 'buildscript.py'
    }
exec {
    workingDir System.getProperty("user.dir")
    commandLine 'python3.6', '-m', 'virtualenv', 'env'
}


exec {
    workingDir System.getProperty("user.dir")
    commandLine 'source', 'env/bin/activate'
}

exec {
    workingDir System.getProperty("user.dir")
    commandLine 'pip3.6', 'install', 'pybuilder'
}

exec {
    workingDir System.getProperty("user.dir")
    commandLine 'pyb', '-E', 'env', '-X'
}

These are all within a build task that runs when executing gradle build. Theoretically this should run a script I created that creates all of the files necessary for building my python program, it should then create a virtual environment, activate it, install pybuilder to it, and then run pybuilder. However, the command:

exec {
        workingDir System.getProperty("user.dir")
        commandLine 'source', 'env/bin/activate'
    }

Seems to be failing. It claims that the directory/file does not exist despite it working via command line. I'm not sure why this is the case. The whole point of this is to force Pybuilder to install my programs dependencies to the virtual environment I create. pyb -E env should technically be activating the virtual environment for me, but for whatever reason it's not installing my dependencies to that virtual environment. On our Jenkins node this is a problem as we don't want these installed globally, not to mention, I don't have root user privileges anyways.

Any help would be greatly greatly appreciated. If you know of another way to get Pybuilder to work correctly, that would be equally good.

CrizR
  • 688
  • 1
  • 6
  • 26

3 Answers3

5

Calling source on activate is just doing shell magic to wire up variables. You don't need to always call activate (and in this case probably can't). Instead, you should prefix your pip3.6 and pyb commands with env/bin to call the binaries directly. Thus, it would be

exec {
   workingDir System.getProperty("user.dir")
   commandLine 'env/bin/pip3.6', 'install', 'pybuilder'
}

exec {
   workingDir System.getProperty("user.dir")
   commandLine 'env/bin/pyb', '-E', 'env', '-X'
}
FuriousGeorge
  • 4,561
  • 5
  • 29
  • 52
2

Temporary solution: I ended up creating a little shell script to create and activate the virtual environment. I then executed that script from gradle.

CrizR
  • 688
  • 1
  • 6
  • 26
2

Firstly you should create your environment like this. So your environment will create

exec {
    workingDir System.getProperty("user.dir")
    commandLine 'python', '-m', 'virtualenv', 'env'
}

Now You should activate your environment in this way and execute the command(e.g PyBuilder)

For Windows:

exec {
    workingDir System.getProperty("user.dir")
    commandLine 'cmd','activate','env','&&','pip','install','pybuilder'
}

For Shell:

exec {
        workingDir System.getProperty("user.dir")
        commandLine 'source','env/bin/activate','env','&&','pip','install','pybuilder'
    }