gawel / gp.recipe.pip
zc.buildout recipe for pip
Clone this repository (size: 42.9 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/gawel/gprecipepip/
| commit 26: | bb64c1a6789d |
| parent 25: | 98686aecd967 |
| branch: | default |
avoid duplicated intallation by buildout
Changed (Δ734 bytes):
raw changeset »
gp/recipe/pip/__init__.py (45 lines added, 64 lines removed)
Up to file-list gp/recipe/pip/__init__.py:
1 |
1 |
# -*- coding: utf-8 -*- |
2 |
2 |
"""Recipe pip""" |
3 |
import pkg_resources |
|
4 |
import zc.buildout |
|
3 |
5 |
import zc.buildout.easy_install |
4 |
6 |
from zc.recipe.egg import Scripts |
5 |
7 |
from subprocess import call |
| … | … | @@ -25,12 +27,9 @@ def get_executable(part_dir): |
25 |
27 |
break |
26 |
28 |
return executable |
27 |
29 |
|
28 |
class Recipe( |
|
30 |
class Recipe(Scripts): |
|
29 |
31 |
"""zc.buildout recipe""" |
30 |
32 |
|
31 |
def __init__(self, buildout, name, options): |
|
32 |
self.buildout, self.name, self.options = buildout, name, options |
|
33 |
||
34 |
33 |
def pip_install(self, part_dir, build_dir, src_dir, extra_args): |
35 |
34 |
print 'pip install %s' % ' '.join(extra_args) |
36 |
35 |
|
| … | … | @@ -101,21 +100,23 @@ class Recipe(object): |
101 |
100 |
if code != 0: |
102 |
101 |
raise RuntimeError('An error occur during pip installation. See %s-log.txt' % self.name) |
103 |
102 |
|
104 |
def |
|
103 |
def working_set(self, extra=()): |
|
104 |
options = self.options |
|
105 |
||
105 |
106 |
part_dir = join(self.buildout['buildout']['parts-directory'], 'pip') |
106 |
107 |
if 'virtualenv' in self.buildout['buildout']: |
107 |
108 |
part_dir = self.buildout['buildout']['virtualenv'] |
108 |
if 'virtualenv' in self.options: |
|
109 |
part_dir = self.options['virtualenv'] |
|
109 |
if 'virtualenv' in options: |
|
110 |
part_dir = options['virtualenv'] |
|
110 |
111 |
|
111 |
112 |
build_dir = join(part_dir, 'build') |
112 |
113 |
if 'build-directory' in self.buildout['buildout']: |
113 |
114 |
build_dir = self.buildout['buildout']['build-directory'] |
114 |
if 'build-directory' in self.options: |
|
115 |
build_dir = self.options['build-directory'] |
|
115 |
if 'build-directory' in options: |
|
116 |
build_dir = options['build-directory'] |
|
116 |
117 |
|
117 |
src_dir = self.options.get('sources-directory', |
|
118 |
join(self.buildout['buildout']['directory'], 'src')) |
|
118 |
src_dir = options.get('sources-directory', |
|
119 |
join(self.buildout['buildout']['directory'], 'src')) |
|
119 |
120 |
|
120 |
121 |
# get buildout versions |
121 |
122 |
versions_option = self.buildout['buildout'].get('versions') |
| … | … | @@ -130,12 +131,12 @@ class Recipe(object): |
130 |
131 |
self.pip_install(part_dir, build_dir, src_dir, []) |
131 |
132 |
|
132 |
133 |
# VSC |
133 |
editables = to_list( |
|
134 |
editables = to_list(options.get('editables', '')) |
|
134 |
135 |
for e in editables: |
135 |
136 |
self.pip_install(part_dir, build_dir, src_dir, ['-e', e]) |
136 |
137 |
|
137 |
138 |
# packages / bundles. add version if needed |
138 |
installs = to_list( |
|
139 |
installs = to_list(options.get('install', '')) |
|
139 |
140 |
for i in installs: |
140 |
141 |
for k in buildout_versions: |
141 |
142 |
if i.endswith(k): |
| … | … | @@ -147,68 +148,48 @@ class Recipe(object): |
147 |
148 |
|
148 |
149 |
# retrieve venv's site-packages and executable |
149 |
150 |
executable = get_executable(part_dir) |
151 |
assert os.path.isfile(executable) |
|
152 |
options['executable'] = executable |
|
153 |
||
150 |
154 |
site_packages = glob.glob(join(part_dir, 'lib', '*', 'site-packages'))[0] |
151 |
if site_packages not in sys.path: |
|
152 |
sys.path.insert(0, site_packages) |
|
153 |
||
154 |
# retrieve versions from egg-info |
|
155 |
egg_infos = glob.glob(join(site_packages, '*.egg-info')) |
|
156 |
eggs = [] |
|
157 |
versions = [] |
|
158 |
for info in egg_infos: |
|
159 |
info = os.path.basename(info) |
|
160 |
name, ver, r = info.split('-', 2) |
|
161 |
# only add eggs available in install |
|
162 |
for i in installs: |
|
163 |
if name in i: |
|
164 |
eggs.append(name) |
|
165 |
versions.append((name, ver)) |
|
166 |
versions = dict(versions) |
|
167 |
||
168 |
# update with buildout versions |
|
169 |
versions.update(buildout_versions) |
|
170 |
||
171 |
zc.buildout.easy_install.default_versions(versions) |
|
172 |
||
173 |
# set eggs directory and executable |
|
174 |
_options = self.buildout['buildout'].copy() |
|
175 |
self.buildout['buildout']['newest'] = 'false' |
|
176 |
self.buildout['buildout']['executable'] = executable |
|
177 |
||
178 |
# clean copy of options |
|
179 |
options = {} |
|
180 |
for k in self.options: |
|
181 |
|
|
155 |
assert os.path.isdir(site_packages) |
|
182 |
156 |
|
183 |
157 |
# move .egg-link from site-packages and append develop eggs to eggs list |
184 |
158 |
dev_eggs_dir = self.buildout['buildout']['develop-eggs-directory'] |
185 |
159 |
for filename in glob.glob(join(site_packages, '*.egg-link')): |
186 |
160 |
name = os.path.basename(filename) |
187 |
eggs.append(name.replace('.egg-link', '')) |
|
188 |
161 |
os.rename(filename, join(dev_eggs_dir, name)) |
189 |
162 |
|
190 |
# add eggs found in venv to eggs option |
|
191 |
options['eggs'] = '\n'.join(eggs) + '\n' + self.options.get('eggs', '') |
|
163 |
# This came from zc.recipe.egg but we need to add venv's WorkingSet |
|
164 |
ws = workink_set=pkg_resources.WorkingSet([site_packages]) |
|
192 |
165 |
|
193 |
# add VCS path as extra_paths |
|
194 |
extra_paths = glob.glob(join(src_dir, '*')) |
|
166 |
kw = {} |
|
167 |
always_unzip = options.get('unzip') |
|
168 |
if always_unzip is not None: |
|
169 |
if always_unzip not in ('true', 'false'): |
|
170 |
raise zc.buildout.UserError("Invalid value for unzip, %s" |
|
171 |
% always_unzip) |
|
172 |
kw['always_unzip'] = always_unzip == 'true' |
|
195 |
173 |
|
196 |
# get eggs already in sys path and add them to extra_paths |
|
197 |
eggs_dir = self.buildout['buildout']['eggs-directory'] |
|
198 |
|
|
174 |
distributions = to_list(options.get('eggs', '')) |
|
175 |
ws = zc.buildout.easy_install.install( |
|
176 |
distributions, |
|
177 |
options['eggs-directory'], |
|
178 |
links = self.links, |
|
179 |
index = self.index, |
|
180 |
executable = executable, |
|
181 |
path=[options['develop-eggs-directory']], |
|
182 |
newest=self.buildout['buildout'].get('newest') == 'true', |
|
183 |
allow_hosts=self.allow_hosts, |
|
184 |
working_set=ws, |
|
185 |
**kw) |
|
199 |
186 |
|
200 |
# run the recipe |
|
201 |
egg = Scripts(self.buildout, self.name, options) |
|
202 |
|
|
187 |
distributions.extend([d.project_name for d in ws]) |
|
203 |
188 |
|
204 |
# restore options |
|
205 |
for k in _options: |
|
206 |
self.buildout['buildout'][k] = _options[k] |
|
207 |
versions = self.buildout['buildout'].get('versions') |
|
208 |
if versions: |
|
209 |
zc.buildout.easy_install.default_versions(dict(self.buildout[versions])) |
|
189 |
return distributions, ws |
|
210 |
190 |
|
211 |
return tuple() |
|
212 |
191 |
|
213 |
update = install |
|
214 |
||
192 |
def install(self): |
|
193 |
if self.buildout['buildout']['offline'] == 'true': |
|
194 |
return () |
|
195 |
return Scripts.install(self) |
