0

我想从 mayas python 解释器中下载并安装 setup_tools、easy_install 和 tinydb。

查看下面代码中的 setup()。

我已经很接近了,但是运行 ez-setup.py 的系统命令似乎没有将 easy_install 包下载到 mayas site_packages 目录,这很奇怪,因为相同的命令在 shell 中完美运行。

因此系统调用如下所示:/Applications/Autodesk/maya2016/Maya.app/Contents/bin/mayapy /Users/paxtongerrish/downloads/ez_setup.py

我将 mayas python 解释器指向 ez_setup.py

当我将此命令打入 shell 时,它会将 setup_tools 下载到 mayas python site_packages 目录...太棒了!:D

但是..我需要从 mayas python 解释器内部发生这一切,并且从 os.system 或 subprocess.call 调用时它不起作用

谢谢!导入 os 导入 sys 导入 urllib2 导入子进程

setup_tools_address = 'https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py'
downloads_directory = '%s/downloads' % os.getenv('HOME')

if not os.path.exists(downloads_directory):
    os.makedirs(downloads_directory)

def setup():
    url = setup_tools_address
    file_path = '%s/%s' % (downloads_directory, url.split('/')[-1])
    maya_py = maya_py_path()
    for p in download_url(url, file_path):
        print p
    system_command = '%s %s' % (maya_py, file_path)
    print '----- sys command---  (only works in shell)------\n'
    print system_command
    print '\n----------------------------------------------\n'
    #This system command works from shell, but not from python.... Maybe superuser thing??
    #os.system(system_command)
    #sub process doesnt work either

    p = subprocess.Popen([maya_py, file_path], shell=True, stdout=subprocess.PIPE)
    print '--->>'
    p.wait()
    for i in p.stdout.readline():
        sys.stdout.flush()
        print i
    add_eggs()
    from setuptools.command import easy_install
    easy_install.main(['tinydb'])
    add_eggs()
    import tinydb

def download_url(url, file_path, block_size=2056):
    request = urllib2.urlopen(url)
    file_size = int(request.info()['Content-Length'])
    if not file_path:
        file_path = '%s/%s' % (os.getenv('HOME'), url.split("/")[-1])
    downloaded_chunk = 0
    with open(file_path, "wb") as f:
        while downloaded_chunk < file_size:
            chunk = request.read(block_size)
            downloaded_chunk += len(chunk)
            f.write(chunk)
            progress = float(downloaded_chunk) / file_size * 100
            yield progress
        print("\nDownload Complete.")

def maya_app_path():
    appName = 'Maya'
    if sys.platform == 'win32':
        appName = 'Maya.exe'
    for p in sys.path:
        app_path = '%s/%s' % (p.replace('\\','/') , appName)
        if os.path.exists(app_path):
            return app_path

def maya_py_path():
    file_name = 'mayapy'
    if sys.platform == 'win32':
        file_name = 'mayapy.exe'
        return '%s\\%s' % (os.path.dirname(maya_app_path().replace('/','\\')), file_name.replace('/','\\'))
    return '%s/bin/%s' % (os.path.dirname(os.path.dirname(maya_app_path())), file_name)

def get_site_packages_directory():
    for p in sys.path:
        if p.endswith('site-packages'):
            return p

def add_eggs():
    site_packages_directory = get_site_packages_directory()
    for item in os.listdir(site_packages_directory):
        if item.endswith('.egg'):
            sys.path.append('%s/%s' % (site_packages_directory, item))
4

0 回答 0