2016-03-28 8 views
1

Ich muss project und lib Verzeichnis zu meinem virtualenv im ansible playbook hinzufügen. Ich bin die Installation von Abhängigkeiten wie folgen aus:ansible: Hinzufügen eines projektspezifischen Pfades zu virtualenv

- pip: 
    name: virtualenv 

- pip: 
    requirements: "{{project_dir}}requirements/development.txt" 
    virtualenv: "{{ virtualenv_path }}" 
    state: latest 

Auf der Entwicklungsmaschine ich dies mit tun kann:

$ add2virtualenv project 
$ add2virtualenv lib 

aber ich nicht wan't virtualenvwrapper auf dem Server zu installieren. Also, was ist die idiomatische Möglichkeit, virtualenv mit Ansible in diesem Fall richtig einzurichten?

Antwort

0

Sie können den folgenden Shell-Skript anstelle von add2virtualenv dass doing this:

Fügen die angegebenen Verzeichnisse auf den Python-Pfad für die gegenwärtig aktive virtualenv.

Syntax:

add2virtualenv directory1 directory2 ...

Manchmal ist es wünschenswert installierten Pakete zu teilen, die nicht in das System site-packages Verzeichnis sind und die sollte nicht in jedem virtualenv installiert. Eine mögliche Lösung ist die Symlink der Quelle in die Umgebung Site-Packages-Verzeichnis, aber es ist auch einfach zusätzliche Verzeichnisse zum PYTHONPATH hinzufügen, indem Sie sie in eine .pth Datei in Site-Pakete mit add2virtualenv.

Check out the source for a big project, such as Django. 
Run: add2virtualenv path_to_source. 
Run: add2virtualenv. 
A usage message and list of current “extra” paths is printed. 

Die Verzeichnisnamen auf eine Pfaddatei _virtualenv_path_extensions.pth in dem site-packages-Verzeichnis für die Umgebung mit dem Namen hinzugefügt werden.

Shell-Skript:

# Path management for packages outside of the virtual env. 
# Based on a contribution from James Bennett and Jannis Leidel. 
# 
# add2virtualenv directory1 directory2 ... 
# 
# Adds the specified directories to the Python path for the 
# currently-active virtualenv. This will be done by placing the 
# directory names in a path file named 
# "virtualenv_path_extensions.pth" inside the virtualenv's 
# site-packages directory; if this file does not exist, it will be 
# created first. 
# 
#:help:add2virtualenv: add directory to the import path 
function add2virtualenv { 
    virtualenvwrapper_verify_workon_home || return 1 
    virtualenvwrapper_verify_active_environment || return 1 

    site_packages="`virtualenvwrapper_get_site_packages_dir`" 

    if [ ! -d "${site_packages}" ] 
    then 
     echo "ERROR: currently-active virtualenv does not appear to have a site-packages directory" >&2 
     return 1 
    fi 

    # Prefix with _ to ensure we are loaded as early as possible, 
    # and at least before easy_install.pth. 
    path_file="$site_packages/_virtualenv_path_extensions.pth" 

    if [ "$*" = "" ] 
    then 
     echo "Usage: add2virtualenv dir [dir ...]" 
     if [ -f "$path_file" ] 
     then 
      echo 
      echo "Existing paths:" 
      cat "$path_file" | grep -v "^import" 
     fi 
     return 1 
    fi 

    remove=0 
    if [ "$1" = "-d" ] 
    then 
     remove=1 
     shift 
    fi 

    if [ ! -f "$path_file" ] 
    then 
     echo "import sys; sys.__plen = len(sys.path)" > "$path_file" || return 1 
     echo "import sys; new=sys.path[sys.__plen:]; del sys.path[sys.__plen:]; p=getattr(sys,'__egginsert',0); sys.path[p:p]=new; sys.__egginsert = p+len(new)" >> "$path_file" || return 1 
    fi 

    for pydir in "[email protected]" 
    do 
     absolute_path="$(virtualenvwrapper_absolutepath "$pydir")" 
     if [ "$absolute_path" != "$pydir" ] 
     then 
      echo "Warning: Converting \"$pydir\" to \"$absolute_path\"" 1>&2 
     fi 

     if [ $remove -eq 1 ] 
     then 
      sed -i.tmp "\:^$absolute_path$: d" "$path_file" 
     else 
      sed -i.tmp '1 a\ 
'"$absolute_path"' 
' "$path_file" 
     fi 
     rm -f "${path_file}.tmp" 
    done 
    return 0 
} 
Verwandte Themen