1
# -*- coding: UTF-8 -*-
2
"""Fetch seeds from a URL collection or from bzr."""
4
# Copyright (c) 2004, 2005, 2006, 2008 Canonical Ltd.
6
# Germinate is free software; you can redistribute it and/or modify it
7
# under the terms of the GNU General Public License as published by the
8
# Free Software Foundation; either version 2, or (at your option) any
11
# Germinate is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
# General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with Germinate; see the file COPYING. If not, write to the Free
18
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
32
class SeedError(RuntimeError):
35
def _cleanup_bzr_cache(directory):
36
shutil.rmtree(directory, ignore_errors=True)
38
def _open_seed_internal(seed_base, seed_branch, seed_file, bzr=False):
39
seed_path = os.path.join(seed_base, seed_branch)
40
if not seed_path.endswith('/'):
44
if bzr_cache_dir is None:
45
bzr_cache_dir = tempfile.mkdtemp(prefix='germinate-')
46
atexit.register(_cleanup_bzr_cache, bzr_cache_dir)
47
seed_checkout = os.path.join(bzr_cache_dir, seed_branch)
48
if not os.path.isdir(seed_checkout):
49
# https://launchpad.net/products/bzr/+bug/39542
50
if seed_path.startswith('http:'):
52
logging.info("Fetching branch of %s", seed_path)
54
operation = 'checkout --lightweight'
55
logging.info("Checking out %s", seed_path)
56
command = ('bzr %s %s %s' % (operation, seed_path, seed_checkout))
57
status = os.system(command)
59
raise SeedError("Command failed with exit status %d:\n"
60
" '%s'" % (status, command))
61
return open(os.path.join(seed_checkout, seed_file))
63
url = urlparse.urljoin(seed_path, seed_file)
64
logging.info("Downloading %s", url)
65
req = urllib2.Request(url)
66
req.add_header('Cache-Control', 'no-cache')
67
req.add_header('Pragma', 'no-cache')
68
return urllib2.urlopen(req)
70
def open_seed(seed_bases, seed_branches, seed_file, bzr=False):
71
if isinstance(seed_branches, str) or isinstance(seed_branches, unicode):
72
seed_branches = [seed_branches]
76
for base in seed_bases:
77
for branch in seed_branches:
79
fd = _open_seed_internal(base, branch, seed_file, bzr)
82
ssh_match = re.match(r'bzr\+ssh://(?:[^/]*?@)?(.*?)(?:/|$)',
85
seed_ssh_host = ssh_match.group(1)
86
except (OSError, IOError, urllib2.URLError):
93
logging.warning("Could not open %s from checkout of (any of):",
95
for base in seed_bases:
96
for branch in seed_branches:
97
logging.warning(' %s' % os.path.join(base, branch))
99
if seed_ssh_host is not None:
100
logging.error("Do you need to set your user name on %s?",
102
logging.error("Try a section such as this in ~/.ssh/config:")
104
logging.error("Host %s", seed_ssh_host)
105
logging.error(" User YOUR_USER_NAME")
107
logging.warning("Could not open (any of):")
108
for base in seed_bases:
109
for branch in seed_branches:
110
path = os.path.join(base, branch)
111
if not path.endswith('/'):
113
logging.warning(' %s' % urlparse.urljoin(path, seed_file))
114
raise SeedError("Could not open %s" % seed_file)