~gary/zc.buildout/python-support

« back to all changes in this revision

Viewing changes to src/zc/buildout/testing.py

  • Committer: Gary Poster
  • Date: 2010-03-19 14:24:54 UTC
  • mfrom: (535.1.26 gary-8)
  • Revision ID: gary.poster@canonical.com-20100319142454-p5currsgehvuy28i
merge buildout trunk with system python support branches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
import subprocess
29
29
import sys
30
30
import tempfile
 
31
import textwrap
31
32
import threading
32
33
import time
33
34
import urllib2
109
110
    e.close()
110
111
    return result
111
112
 
 
113
def call_py(interpreter, cmd, flags=None):
 
114
    if sys.platform == 'win32':
 
115
        args = ['"%s"' % arg for arg in (interpreter, flags, cmd) if arg]
 
116
        args.insert(-1, '"-c"')
 
117
        return system('"%s"' % ' '.join(args))
 
118
    else:
 
119
        cmd = repr(cmd)
 
120
        return system(
 
121
            ' '.join(arg for arg in (interpreter, flags, '-c', cmd) if arg))
 
122
 
112
123
def get(url):
113
124
    return urllib2.urlopen(url).read()
114
125
 
120
131
    args = [zc.buildout.easy_install._safe_arg(arg)
121
132
            for arg in args]
122
133
    args.insert(0, '-q')
123
 
    args.append(dict(os.environ, PYTHONPATH=setuptools_location))
 
134
    env = dict(os.environ)
 
135
    if executable == sys.executable:
 
136
        env['PYTHONPATH'] = setuptools_location
 
137
    # else pass an executable that has setuptools! See testselectingpython.py.
 
138
    args.append(env)
124
139
 
125
140
    here = os.getcwd()
126
141
    try:
139
154
def bdist_egg(setup, executable, dest):
140
155
    _runsetup(setup, executable, 'bdist_egg', '-d', dest)
141
156
 
 
157
def sys_install(setup, dest):
 
158
    _runsetup(setup, sys.executable, 'install', '--install-purelib', dest,
 
159
              '--record', os.path.join(dest, '__added_files__'),
 
160
              '--single-version-externally-managed')
 
161
 
142
162
def find_python(version):
143
163
    e = os.environ.get('PYTHON%s' % version)
144
164
    if e is not None:
206
226
        time.sleep(0.01)
207
227
    raise ValueError('Timed out waiting for: '+label)
208
228
 
 
229
def get_installer_values():
 
230
    """Get the current values for the easy_install module.
 
231
 
 
232
    This is necessary because instantiating a Buildout will force the
 
233
    Buildout's values on the installer.
 
234
 
 
235
    Returns a dict of names-values suitable for set_installer_values."""
 
236
    names = ('default_versions', 'download_cache', 'install_from_cache',
 
237
             'prefer_final', 'include_site_packages',
 
238
             'allowed_eggs_from_site_packages', 'use_dependency_links',
 
239
             'allow_picked_versions', 'always_unzip'
 
240
            )
 
241
    values = {}
 
242
    for name in names:
 
243
        values[name] = getattr(zc.buildout.easy_install, name)()
 
244
    return values
 
245
 
 
246
def set_installer_values(values):
 
247
    """Set the given values on the installer."""
 
248
    for name, value in values.items():
 
249
        getattr(zc.buildout.easy_install, name)(value)
 
250
 
 
251
def make_buildout(executable=None):
 
252
    """Make a buildout that uses this version of zc.buildout."""
 
253
    # Create a basic buildout.cfg to avoid a warning from buildout.
 
254
    open('buildout.cfg', 'w').write(
 
255
        "[buildout]\nparts =\n"
 
256
        )
 
257
    # Get state of installer defaults so we can reinstate them (instantiating
 
258
    # a Buildout will force the Buildout's defaults on the installer).
 
259
    installer_values = get_installer_values()
 
260
    # Use the buildout bootstrap command to create a buildout
 
261
    config = [
 
262
        ('buildout', 'log-level', 'WARNING'),
 
263
        # trick bootstrap into putting the buildout develop egg
 
264
        # in the eggs dir.
 
265
        ('buildout', 'develop-eggs-directory', 'eggs'),
 
266
        ]
 
267
    if executable is not None:
 
268
        config.append(('buildout', 'executable', executable))
 
269
    zc.buildout.buildout.Buildout(
 
270
        'buildout.cfg', config,
 
271
        user_defaults=False,
 
272
        ).bootstrap([])
 
273
    # Create the develop-eggs dir, which didn't get created the usual
 
274
    # way due to the trick above:
 
275
    os.mkdir('develop-eggs')
 
276
    # Reinstate the default values of the installer.
 
277
    set_installer_values(installer_values)
 
278
 
209
279
def buildoutSetUp(test):
210
280
 
211
281
    test.globs['__tear_downs'] = __tear_downs = []
212
282
    test.globs['register_teardown'] = register_teardown = __tear_downs.append
213
283
 
214
 
    prefer_final = zc.buildout.easy_install.prefer_final()
 
284
    installer_values = get_installer_values()
215
285
    register_teardown(
216
 
        lambda: zc.buildout.easy_install.prefer_final(prefer_final)
 
286
        lambda: set_installer_values(installer_values)
217
287
        )
218
288
 
219
289
    here = os.getcwd()
259
329
    sample = tmpdir('sample-buildout')
260
330
 
261
331
    os.chdir(sample)
262
 
 
263
 
    # Create a basic buildout.cfg to avoid a warning from buildout:
264
 
    open('buildout.cfg', 'w').write(
265
 
        "[buildout]\nparts =\n"
266
 
        )
267
 
 
268
 
    # Use the buildout bootstrap command to create a buildout
269
 
    zc.buildout.buildout.Buildout(
270
 
        'buildout.cfg',
271
 
        [('buildout', 'log-level', 'WARNING'),
272
 
         # trick bootstrap into putting the buildout develop egg
273
 
         # in the eggs dir.
274
 
         ('buildout', 'develop-eggs-directory', 'eggs'),
275
 
         ]
276
 
        ).bootstrap([])
277
 
 
278
 
 
279
 
 
280
 
    # Create the develop-eggs dir, which didn't get created the usual
281
 
    # way due to the trick above:
282
 
    os.mkdir('develop-eggs')
 
332
    make_buildout()
283
333
 
284
334
    def start_server(path):
285
335
        port, thread = _start_server(path, name=path)
287
337
        register_teardown(lambda: stop_server(url, thread))
288
338
        return url
289
339
 
 
340
    def make_py(initialization=''):
 
341
        """Returns paths to new executable and to its site-packages.
 
342
        """
 
343
        buildout = tmpdir('executable_buildout')
 
344
        site_packages_dir = os.path.join(buildout, 'site-packages')
 
345
        mkdir(site_packages_dir)
 
346
        old_wd = os.getcwd()
 
347
        os.chdir(buildout)
 
348
        make_buildout()
 
349
        # Normally we don't process .pth files in extra-paths.  We want to
 
350
        # in this case so that we can test with setuptools system installs
 
351
        # (--single-version-externally-managed), which use .pth files.
 
352
        initialization = (
 
353
            ('import sys\n'
 
354
             'import site\n'
 
355
             'known_paths = set(sys.path)\n'
 
356
             'site_packages_dir = %r\n'
 
357
             'site.addsitedir(site_packages_dir, known_paths)\n'
 
358
            ) % (site_packages_dir,)) + initialization
 
359
        initialization = '\n'.join(
 
360
            '  ' + line for line in initialization.split('\n'))
 
361
        install_develop(
 
362
            'zc.recipe.egg', os.path.join(buildout, 'develop-eggs'))
 
363
        install_develop(
 
364
            'z3c.recipe.scripts', os.path.join(buildout, 'develop-eggs'))
 
365
        write('buildout.cfg', textwrap.dedent('''\
 
366
            [buildout]
 
367
            parts = py
 
368
 
 
369
            [py]
 
370
            recipe = z3c.recipe.scripts
 
371
            interpreter = py
 
372
            initialization =
 
373
            %(initialization)s
 
374
            extra-paths = %(site-packages)s
 
375
            eggs = setuptools
 
376
            ''') % {
 
377
                'initialization': initialization,
 
378
                'site-packages': site_packages_dir})
 
379
        system(os.path.join(buildout, 'bin', 'buildout'))
 
380
        os.chdir(old_wd)
 
381
        return (
 
382
            os.path.join(buildout, 'bin', 'py'), site_packages_dir)
 
383
 
290
384
    test.globs.update(dict(
291
385
        sample_buildout = sample,
292
386
        ls = ls,
297
391
        tmpdir = tmpdir,
298
392
        write = write,
299
393
        system = system,
 
394
        call_py = call_py,
300
395
        get = get,
301
396
        cd = (lambda *path: os.chdir(os.path.join(*path))),
302
397
        join = os.path.join,
305
400
        start_server = start_server,
306
401
        buildout = os.path.join(sample, 'bin', 'buildout'),
307
402
        wait_until = wait_until,
 
403
        make_py = make_py
308
404
        ))
309
405
 
310
 
    zc.buildout.easy_install.prefer_final(prefer_final)
311
 
 
312
406
def buildoutTearDown(test):
313
407
    for f in test.globs['__tear_downs']:
314
408
        f()