0

I have python file, for example, named blah.py, that I would like to have compiled then placed into another folder using cmake. Right now, I am capable of doing this with the following code in my cmake file:

ADD_CUSTOM_TARGET(output ALL /usr/bin/python -m py_compile src/blah.py
                  COMMAND /bin/mv src/blah.pyc build VERBATIM)

This is on ubuntu 12.04. This code works as intended; the only problem is that the python file is being compiled in the source directory, then being put in the build directory.

However, I can't assume that this src directory will have read AND write privileges, meaning what I need to do is combine these two commands into one (compile the python file and place the compiled python file into my build directory, instead of compiling it in the src directory then moving it)

I'm sure there must be some way I could be using to specify where I would like this compiled code to be placed, but I can't find any. Help would be greatly appreciated! :)

EDIT: This link may have a solution..not sure:

Can compiled bytecode files (.pyc) get generated in different directory?

Community
  • 1
  • 1
user1553248
  • 1,184
  • 2
  • 19
  • 33
  • possible duplicate of [Can compiled bytecode files (.pyc) get generated in different directory?](http://stackoverflow.com/questions/611967/can-compiled-bytecode-files-pyc-get-generated-in-different-directory) – tripleee Sep 05 '12 at 19:40
  • 2
    @tripleee: Its not a duplicate only because the OP wants to know how to call this as a shell command. Makes the question a bit different – jdi Sep 05 '12 at 19:58

1 Answers1

3

I was typing out this answer, and then looked at your edited link. This same answer is given in one of the unaccepted answers: https://stackoverflow.com/a/611995/496445

import py_compile
py_compile.compile('/path/to/source/code.py', cfile='/path/to/build/code.pyc')

To call this via a basic shell command you can format it like this:

python -c "import py_compile; py_compile.compile('/path/to/source/code.py', cfile='/path/to/build/code.pyc')"
Community
  • 1
  • 1
jdi
  • 90,542
  • 19
  • 167
  • 203