Source

django-autodeps / autodeps / abstract.py

Full commit
# This file is part of django-autodeps.
#
# django-autodeps is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# django-autodeps is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# django-autodeps.  If not, see <http://www.gnu.org/licenses/>.

import os, sys
import logging
import urlparse

from .exceptions import MissingDependency

# The logger to be used by all backends
logger = logging.getLogger('autodeps')
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())

class VersionControl(object):
    """
    The root class to be used for implementing RCS backends
    """
    def __init__(self, url, root, app_name='', pathtomodule='', revision=''):
        self.url = url
        self.root = root
        self.revision = revision
        self.pathtomodule = pathtomodule

        # App_name is the name of the RCS module but also the name of the
        # directory where it will be downloaded
        if not app_name:
            self.app_name = os.path.basename((urlparse.urlparse(url)[2]).rstrip('/'))
        else:
            self.app_name = app_name

        # The dir where the software is going to be downloaded
        self.dl_path = os.path.join(root,
                                    self.app_name)

        # The RCS module dir
        self.checkout_path = os.path.join(self.dl_path,
                                          self.app_name)

        # The path to add the PYTHONPATH so that python will find it
        self.python_path = os.path.join(self.dl_path,
                                        self.pathtomodule)
    
    def __repr__(self):
        return "<VersionControl: %s>" % self.app_name
    
    def add_to_python_path(self, position):
        """
        Add this module to the PYTHONPATH
        """
        if not os.path.exists(self.checkout_path):
            raise MissingDependency(self.app_name)
        sys.path.insert(position, self.python_path)