2012-10-28 34 views
5

Tôi đang viết game trong python 2.7 và muốn viết "bootstrap" của môi trường phát triển trò chơi của tôi, sau đó gọi shovel. Nếu virtualenvwrapper không được phát hiện, tôi sẽ sử dụng virtualenv bootstrap solution. Tuy nhiên, nếu phát hiện thấy virtualenvwrapper , tôi muốn sử dụng nó thay thế.Scripting virtualenvwrapper mkvirtualenv

Vấn đề là các hàm vỏ ảo nội tuyến ảo không được thừa hưởng bởi tập lệnh khởi động của tôi. Theo như tôi biết, các quy tắc đó hoạt động như "mkvirtualenv NotOrion". Kể từ biến môi trường "VIRTUALENVWRAPPER_VIRTUALENV" bộ (trong trường hợp của tôi, từ macports: /opt/local/bin/virtualenv-2.7), tôi đã cố gắng sử dụng nó trực tiếp thay vì:

#!/usr/bin/env bash 

# Name your first "bootstrap" environment: 
ENV_NAME=NotOrion 
# Options for your first environment: 
ENV_OPTS='--no-site-packages --distribute' 

unset PYTHONDONTWRITEBYTECODE 

function create_virtualenvwrapper_venv { 
    echo "installing into virtualenvwrapper directory" 
    cd $WORKON_HOME 
    $VIRTUALENVWRAPPER_VIRTUALENV $ENV_OPTS $ENV_NAME 
    cd - 
    #mkvirtualenv $ENV_NAME 
    #workon $ENV_NAME 
} 

function create_standalone_venv { 
    # not run/snipped 
} 

if [ -z "$VIRTUALENVWRAPPER_VIRTUALENV" ]; then 
    create_standalone_venv 
else 
    create_virtualenvwrapper_venv 
fi 

pip install shovel 
shovel help 

kịch bản bootstrap của tôi kết thúc cài đặt cái xẻng. Tuy nhiên chạy xẻng (ví dụ như dòng cuối cùng) tạo ra cảnh báo:

/Users/me/.virtualenvs/NotOrion/bin/shovel:25: UserWarning: Module argparse was already imported from /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.pyc, but /Users/me/.virtualenvs/NotOrion/lib/python2.7/site-packages is being added to sys.path 
import pkg_resources 
# normal shovel output snipped 

Vì vậy, có thể bằng cách nào đó gọi "mkvirtualenv" từ tập lệnh? Nếu không, tôi có thể chạy cái gì khác từ tập lệnh của tôi có cùng tác dụng không nhưng không có tạo cảnh báo không?

+0

Vấn đề chính xác tương tự, viết tập lệnh khởi động. Cảm ơn bạn! – cloudrave

Trả lời

11

Kịch bản của bạn sẽ có thể làm:

# 'which' will print absolute path to virtualenvwrapper.sh 
source `which virtualenvwrapper.sh` 

tôi sử dụng đối với một số kịch bản triển khai.

+0

Có vẻ như macports virtualenvwrapper không cung cấp "virtualenvwrapper.sh". Nó có các biến thể khác như "virtualenvwrapper.sh-2.7", vì vậy có vẻ như tôi không thể tin vào điều này. – EdwardTeach

+0

Ah, vâng. Nếu bạn sử dụng MacPorts bạn đang sắp xếp của khó khăn đối phó với tất cả những thay đổi không được hỗ trợ mà họ thực hiện. Điều này đặc biệt khó chịu vì không có lý do gì cho nó - một cài đặt virtualenv duy nhất có thể xử lý nhiều phiên bản Python khác nhau (tôi có một cài đặt duy nhất được sử dụng cho Python 2.5, 2.6, 2.7, 3.2, 3.3, PyPy và Jython) –

+0

. theo dõi để hoàn thành kịch bản bootstrap của tôi; Tôi sẽ dán phần còn thiếu ở trên và chấp nhận câu trả lời này. – EdwardTeach

2

Dường như không có cách "chuẩn" để thực hiện việc này. Vì vậy, tôi đã xem xét thủ công các địa điểm khác nhau. Lộn xộn, nhưng dường như là cách duy nhất:

function find_virtualenvwrapper { 
    # no consistent way to find 'virtualenvwrapper.sh', so try various methods 
    # is it directly available in the path? 
    virtualenvwrapper_path=$(which virtualenvwrapper.sh) 
    if [ $? -eq 0 ]; then 
     return 
    fi 
    # nope; how about something that looks like it in our path? 
    # http://stackoverflow.com/questions/948008/linux-command-to-list-all-available-commands-and-aliases 
    virtualenvwrapper_cmd=$(compgen -ac | grep -i 'virtualenvwrapper\.sh' | sort | uniq | head -1) 
    if [ -n "$virtualenvwrapper_cmd" ]; then 
     virtualenvwrapper_path=$(which $virtualenvwrapper_cmd) 
     if [ $? -eq 0 ]; then 
     return 
     fi 
    fi 
    # still not; Debubuntu puts it in /etc/bash_completion.d 
    virtualenvwrapper_path='/etc/bash_completion.d/virtualenvwrapper' 
    if [ -e "$virtualenvwrapper_path" ]; then 
     return 
    fi 
    # any other methods to find virtualenvwrapper can be added here 
    echo "unable to find virtualenvwrapper.sh or anything that looks like it" 
    exit 1 
}