2017-07-03 69 views
1

我似乎无法破解如何更改setup.py自定义命令中的目录。我的python模块需要opencv,它需要从源代码创建。setup.py自定义命令中的相对路径'不能cd到文件夹'

一切顺利通过CMake

['cmake','-Hopencv',"-Bopencv/build"], 

...

-- Configuring done 
-- Generating done 
-- Build files have been written to: /root/DeepMeerkat/tests/prediction/opencv/build 

未来,我只想CD到生成文件夹,并-j4。但setup.py拒绝看到该文件夹​​。我试过OpenCV的CD /建造,我从/根/ DeepMeerkat /测试/预测试过/ OpenCV的/建造

,但我总是得到

Running command: ['cd', '/root/DeepMeerkat/tests/prediction/opencv/build'] 
error: [Errno 2] No such file or directory 

这只是混乱。

目录肯定存在

[email protected]:~/DeepMeerkat/tests/prediction# /root/DeepMeerkat/tests/prediction/opencv/build/ 
bash: /root/DeepMeerkat/tests/prediction/opencv/build/: Is a directory 

据我知道你需要从目标目录内作出的Makefile。

完整的脚本:

import subprocess 
from distutils.command.build import build as _build 
import setuptools 

class build(_build): 
    sub_commands = _build.sub_commands + [('CustomCommands', None)] 

class CustomCommands(setuptools.Command): 

    def initialize_options(self): 
    pass 

    def finalize_options(self): 
    pass 

    def RunCustomCommand(self, command_list): 
    print('Running command: %s' % command_list) 
    p = subprocess.Popen(
     command_list, 
     stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 
    stdout_data, _ = p.communicate() 
    print('Command output: %s' % stdout_data) 
    if p.returncode != 0: 
     raise RuntimeError(
      'Command %s failed: exit code: %s' % (command_list, p.returncode)) 

    def run(self): 
    for command in CUSTOM_COMMANDS: 
     self.RunCustomCommand(command) 

CUSTOM_COMMANDS = [ 

    #Get cmake and git 
    ['apt-get', 'update', '-y'], 
    ['apt-get', 'install', '-y', 'cmake', 'git'], 
    ['git','clone', 'https://github.com/Itseez/opencv.git', '--depth', '1'], 
    ['git','clone', 'https://github.com/Itseez/opencv_contrib.git', '--depth', '1'], 
    ['mkdir', 'opencv/build'], 
    ['cmake','-Hopencv',"-Bopencv/build"], 
    ['cd','/root/DeepMeerkat/tests/prediction/opencv/build'], 
    ['make', '-j4'], 
    ['make', 'install'], 
    ['ldconfig']]  

REQUIRED_PACKAGES = ['numpy'] 

setuptools.setup(
    name='DeepMeerkat', 
    version='0.0.1', 
    description='Running MotionMeerkat in the Cloud', 
    install_requires=REQUIRED_PACKAGES, 
    packages=setuptools.find_packages(), 
    cmdclass={'build': build, 'CustomCommands': CustomCommands}) 
+2

只是不要让'-C DIR ...'。 – arrowd

回答

0

每个子在接下来的子单独运行,所以当运行时,它开始在用户的主目录。

尝试:

cd somedir && some_command

或类似:

subprocess.Popen(
    command_list, 
    stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, 
    cwd=working_dir)