source: isenes/mpl_buildout/trunk/recipes/numpy_venv.py @ 5446

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/isenes/mpl_buildout/trunk/recipes/numpy_venv.py@5446
Revision 5446, 4.2 KB checked in by spascoe, 10 years ago (diff)

small bug fix

Line 
1"""
2A recipe to install numpy into a virtualenv environment.
3
4"""
5
6import logging, os, zc.buildout
7from zc.buildout.easy_install import install, Installer
8import pkg_resources
9import subprocess
10from glob import glob
11import shutil
12
13from recipe import BaseRecipe
14
15class NumpyVenv(BaseRecipe):
16
17    def __init__(self, buildout, name, options):
18        self.name, self.options = name, options
19        self.log = logging.getLogger(self.name)
20
21        #!TODO: support buildout.python option
22        options['executable'] = buildout['buildout']['executable']
23        options['virtualenv'] = os.path.join(
24            buildout['buildout']['parts-directory'],
25            name
26            )
27        options['eggs-directory'] = buildout['buildout']['eggs-directory']
28        options['download-cache'] = buildout['buildout']['download-cache']
29
30        if 'eggs' not in options:
31            options['eggs'] = ''
32
33    def install(self):
34        """
35        The numpy egg is built with the buildout infrastructure and then
36        installed into a virtualenv.  This allows the latest numpy to be
37        built and used for further distribution building even if an older
38        version of numpy is already installed globally.
39       
40        """
41        path = self.options['virtualenv']
42
43        # First install numpy using buildout
44        install(['numpy'], self.options['eggs-directory'])
45
46        # Find the egg
47        e = pkg_resources.Environment([self.options['eggs-directory']])
48        dist = e['numpy'][0]
49                                     
50        # Add the egg to the virtualenv
51        self.options['eggs'] = '%s\n%s' % (self.options['eggs'],
52                                           dist.location)
53        self.create_virtualenv(path)
54
55        # Install MPL using the numpy virtualenv
56        self.install_mpl()
57
58        # Install basemap
59        self.install_basemap()
60
61        return path
62
63
64    def install_mpl(self):
65        venv = self.options['virtualenv']
66        venv_python = os.path.join(venv, 'bin', 'python')
67       
68        install(['matplotlib'], self.options['eggs-directory'],
69                links=[self.options['download-cache']],
70                executable = venv_python)
71
72    def install_basemap(self):
73        """
74        Use the numpy virtualenv to get basemap, unpack it,
75        install the embedded geos and build the egg.
76
77        @warning: This method uses internal zc.buildout calls that are
78            subject to change.
79           
80        """
81        venv = self.options['virtualenv']
82        venv_python = os.path.join(venv, 'bin', 'python')
83        venv_easy_install = os.path.join(venv, 'bin', 'easy_install')
84       
85        # Download the source of basemap using zc.buildout
86        i = Installer(venv, 
87                      links = self.options['download-cache'],
88                      executable = venv_python)
89        dist, avail = i._satisfied(pkg_resources.Requirement.parse('basemap'),
90                                 source=True)
91        assert(dist==None)
92
93        basemap_dist_dir = os.path.join(venv, 'basemap') 
94        if os.path.exists(basemap_dist_dir):
95            #!TODO: check version
96            pass
97        else:
98            # Unpack into the venv
99            status = subprocess.call([venv_easy_install, '-eb', venv,
100                                  '-f', avail.location, 'basemap'])
101            if status != 0:
102                raise zc.buildout.UserError('Failed to unpack basemap distribution')
103        geos_install = self.install_geos()
104        os.environ['GEOS_DIR'] = geos_install
105
106        install(['basemap'], self.options['eggs-directory'],
107                links=[basemap_dist_dir],
108                executable = venv_python)
109
110    def install_geos(self):
111        venv = self.options['virtualenv']
112        venv_python = os.path.join(venv, 'bin', 'python')
113
114        geos_dir = glob(os.path.join(self.options['virtualenv'], 'basemap', 'geos-*'))[0]
115        geos_install = os.path.join(self.options['virtualenv'], 'geos')
116        status = subprocess.call(['cd %s ; ./configure --prefix=%s ; make install' % (geos_dir, geos_install)], shell=True)
117        if status != 0:
118            raise zc.buildout.UserError('GEOS install failed')
119
120        return geos_install
121   
122    def update(self):
123        pass
124
125   
Note: See TracBrowser for help on using the repository browser.