pip/tests/local_repos.py

76 lines
2.5 KiB
Python
Raw Normal View History

2010-06-16 12:32:57 +02:00
import os
import subprocess
from pip.vcs import subversion, git, bazaar, mercurial
2011-03-15 20:49:48 +01:00
from pip.backwardcompat import urlretrieve
from tests.test_pip import path_to_url
from tests.pypi_server import PyPIProxy
2010-06-16 12:32:57 +02:00
if hasattr(subprocess, "check_call"):
subprocess_call = subprocess.check_call
else:
subprocess_call = subprocess.call
2010-06-16 12:32:57 +02:00
def _create_initools_repository():
2011-03-19 07:07:53 +01:00
subprocess_call('svnadmin create INITools'.split(), cwd=_get_vcs_folder())
2010-06-16 12:32:57 +02:00
def _dump_initools_repository():
2011-03-15 20:49:48 +01:00
filename, _ = urlretrieve('http://bitbucket.org/hltbra/pip-initools-dump/raw/8b55c908a320/INITools_modified.dump')
initools_folder = os.path.join(_get_vcs_folder(), 'INITools')
devnull = open(os.devnull, 'w')
dump = open(filename)
2011-03-19 07:07:53 +01:00
subprocess_call(['svnadmin', 'load', initools_folder], stdin=dump, stdout=devnull)
dump.close()
devnull.close()
2010-09-14 01:21:32 +02:00
os.remove(filename)
2010-06-16 12:32:57 +02:00
def _create_svn_repository_for_initools():
2010-07-02 14:39:45 +02:00
tests_cache = _get_vcs_folder()
if not os.path.exists(os.path.join(tests_cache, 'INITools')):
2010-06-16 12:32:57 +02:00
_create_initools_repository()
_dump_initools_repository()
def _get_vcs_folder():
2010-07-02 14:39:45 +02:00
folder_name = PyPIProxy.CACHE_PATH
2010-06-16 12:32:57 +02:00
if not os.path.exists(folder_name):
os.mkdir(folder_name)
return folder_name
def _get_vcs_and_checkout_url(remote_repository):
2010-07-02 14:39:45 +02:00
tests_cache = _get_vcs_folder()
2010-06-16 12:32:57 +02:00
vcs_classes = {'svn': subversion.Subversion,
'git': git.Git,
'bzr': bazaar.Bazaar,
'hg': mercurial.Mercurial}
default_vcs = 'svn'
if '+' not in remote_repository:
remote_repository = '%s+%s' % (default_vcs, remote_repository)
vcs, repository_path = remote_repository.split('+', 1)
vcs_class = vcs_classes[vcs]
branch = ''
if vcs == 'svn':
branch = os.path.basename(remote_repository)
repository_name = os.path.basename(remote_repository[:-len(branch)-1]) # remove the slash
else:
repository_name = os.path.basename(remote_repository)
2010-07-02 14:39:45 +02:00
destination_path = os.path.join(tests_cache, repository_name)
2010-06-16 12:32:57 +02:00
if not os.path.exists(destination_path):
vcs_class(remote_repository).obtain(destination_path)
2010-07-02 14:39:45 +02:00
return '%s+%s' % (vcs, path_to_url('/'.join([tests_cache, repository_name, branch])))
2010-06-16 12:32:57 +02:00
def local_checkout(remote_repo):
2010-06-16 12:32:57 +02:00
if remote_repo.startswith('svn'):
_create_svn_repository_for_initools()
return _get_vcs_and_checkout_url(remote_repo)
def local_repo(remote_repo):
return local_checkout(remote_repo).split('+', 1)[1]