Created
September 21, 2008 04:01
-
-
Save ishikawa/11835 to your computer and use it in GitHub Desktop.
Using os.spawnv to run and compile files with or without optimization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def compile_python_source(filepath, optimization=False): | |
| """ | |
| Compile a source file to byte-code and write out | |
| the byte-code cache file. The source code is loaded | |
| from the file name *filepath*. The byte-code is cached | |
| in the normal manner (*filepath* + 'c' or 'o' if | |
| optimization is enabled). | |
| """ | |
| from os import spawnv | |
| from sys import executable, platform | |
| args = [executable] | |
| if optimization: | |
| args.append('-O') | |
| args += ['-m', 'py_compile'] | |
| if isinstance(filepath, (tuple, list)): | |
| args.extend(filepath) | |
| else: | |
| args.append(filepath) | |
| if platform == "win32": | |
| args = ['"%s"' % arg for arg in args] | |
| return spawnv(os.P_WAIT, executable, args) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment