denim / denim / pip.py

# -*- encoding:utf8 -*-
import os
from fabric.api import env, put, require
from denim import paths, scm, utils

__all__ = ('install_requirements', )


def install_requirements(revision=None, path_to_requirements=None,
                         upgrade=True, use_sudo=True, user=None,
                         path_to_log=None):
    """
    Install requirements with PIP.

    To install into a virtualenv use the :ref:`vitualenv.activate` context
    manager.

    :path_to_requirements: path to requirements file; default is:
        `*deploy_path*/app/*revision*/requirements.txt`
    :upgrade: when installing requirements fetch updates.
    :revision: A specific revision name.

    """
    if not path_to_requirements:
        path_to_requirements = paths.package_path(revision, 'requirements.txt')
    parameters = ['install']
    if env.has_key('proxy'):
        parameters.append('--proxy=%s' % env.proxy)
    if upgrade:
        parameters.append('--upgrade')
    if not path_to_log:
        path_to_log = paths.deploy_path('var/pip.log')
    parameters.append('-r %s' % path_to_requirements)
    utils.run_as('pip ' + ' '.join(parameters), use_sudo, user)


def create_bundle_from_revision(revision, bundle_file=None):
    """
    Create pip bundle from requirements file.

    :param revision: Revision to bundle from.
    :param bundle_file: Name of bundle file; defaults to project name.

    Create bundle uses the following process to determine the what packages
    are to be bundled:

     - use `path_to_requirements`
     - use requirements.txt from app/requirements.txt@revision

    """
    require('project_name')

    if not bundle_file:
        bundle_file = paths.local_working_path(file_name='%s.pybundle' % revision)

    path_to_requirements = paths.local_working_path(file_name='%s-requirements.txt' % revision)
    scm.export_file(revision, 'app/requirements.txt', path_to_requirements)

    utils.local('pip bundle -r %s %s' % (path_to_requirements, bundle_file))
    return bundle_file


def install_bundle(bundle_file, use_sudo=True, user=None):
    """
    Install pip bundle.

    To install into a virtualenv use the :ref:`vitualenv.activate` context
    manager.

    :param bundle_file: path to bundle file

    """
    file_name = os.path.basename(bundle_file)
    remote_bundle = paths.join_paths('/tmp/', file_name)
    put(bundle_file, remote_bundle)
    utils.run_as('pip install %s' % remote_bundle, use_sudo, user)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.