~aelkner/schooltool/schooltool.lyceum.journal_october_release

« back to all changes in this revision

Viewing changes to bootstrap.py

  • Committer: Ignas Mikalajūnas
  • Date: 2008-01-14 23:18:29 UTC
  • Revision ID: ignas@pov.lt-20080114231829-qhhxzokl8tppirru
Update lyceum checkout into a buildout.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2006 Zope Corporation and Contributors.
 
4
# All Rights Reserved.
 
5
#
 
6
# This software is subject to the provisions of the Zope Public License,
 
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
 
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
 
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 
11
# FOR A PARTICULAR PURPOSE.
 
12
#
 
13
##############################################################################
 
14
"""Bootstrap a buildout-based project
 
15
 
 
16
Simply run this script in a directory containing a buildout.cfg.
 
17
The script accepts buildout command-line options, so you can
 
18
use the -c option to specify an alternate configuration file.
 
19
 
 
20
"""
 
21
import os, shutil, sys, tempfile, urllib2
 
22
 
 
23
join = os.path.join
 
24
py_version = 'python%s.%s' % (sys.version_info[0], sys.version_info[1])
 
25
 
 
26
def mkdir(path):
 
27
    if not os.path.exists(path):
 
28
        print 'Creating %s' % path
 
29
        os.makedirs(path)
 
30
 
 
31
def symlink(src, dest):
 
32
    if not os.path.exists(dest):
 
33
        os.symlink(src, dest)
 
34
    else:
 
35
        print 'Symlink %s already exists' % dest
 
36
 
 
37
 
 
38
def rmtree(dir):
 
39
    if os.path.exists(dir):
 
40
        print 'Deleting tree %s' % dir
 
41
        shutil.rmtree(dir)
 
42
 
 
43
def make_exe(fn):
 
44
    if os.name == 'posix':
 
45
        oldmode = os.stat(fn).st_mode & 07777
 
46
        newmode = (oldmode | 0555) & 07777
 
47
        os.chmod(fn, newmode)
 
48
 
 
49
def make_virtual_python():
 
50
    if os.name != 'posix':
 
51
        print "This script only works on Unix-like platforms, sorry."
 
52
        return
 
53
 
 
54
    lib_dir = join('python', 'lib', py_version)
 
55
    inc_dir = join('python', 'include', py_version)
 
56
    bin_dir = join('python', 'bin')
 
57
 
 
58
    if sys.executable.startswith(bin_dir):
 
59
        print 'Please use the *system* python to run this script'
 
60
        return
 
61
 
 
62
    mkdir('python')
 
63
    prefix = sys.prefix
 
64
    mkdir(lib_dir)
 
65
    stdlib_dir = join(prefix, 'lib', py_version)
 
66
    for fn in os.listdir(stdlib_dir):
 
67
        if fn != 'site-packages':
 
68
            symlink(join(stdlib_dir, fn), join(lib_dir, fn))
 
69
 
 
70
    mkdir(join(lib_dir, 'site-packages'))
 
71
 
 
72
    mkdir(inc_dir)
 
73
    stdinc_dir = join(prefix, 'include', py_version)
 
74
    for fn in os.listdir(stdinc_dir):
 
75
        symlink(join(stdinc_dir, fn), join(inc_dir, fn))
 
76
 
 
77
    if sys.exec_prefix != sys.prefix:
 
78
        exec_dir = join(sys.exec_prefix, 'lib', py_version)
 
79
        for fn in os.listdir(exec_dir):
 
80
            symlink(join(exec_dir, fn), join(lib_dir, fn))
 
81
 
 
82
    mkdir(bin_dir)
 
83
    print 'Copying %s to %s' % (sys.executable, bin_dir)
 
84
    py_executable = join(bin_dir, 'python')
 
85
    if sys.executable != py_executable:
 
86
        shutil.copyfile(sys.executable, py_executable)
 
87
        make_exe(py_executable)
 
88
 
 
89
 
 
90
if __name__ == "__main__":
 
91
    if sys.executable != os.path.abspath('python/bin/python'):
 
92
        make_virtual_python()
 
93
        sys.exit(os.spawnve(
 
94
                os.P_WAIT, 'python/bin/python',
 
95
                ['python/bin/python'] + sys.argv, os.environ))
 
96
 
 
97
    tmpeggs = tempfile.mkdtemp()
 
98
 
 
99
    ez = {}
 
100
    exec urllib2.urlopen('http://peak.telecommunity.com/dist/ez_setup.py'
 
101
                         ).read() in ez
 
102
    ez['use_setuptools'](to_dir=tmpeggs, download_delay=0)
 
103
 
 
104
    import pkg_resources
 
105
 
 
106
    cmd = 'from setuptools.command.easy_install import main; main()'
 
107
    if sys.platform == 'win32':
 
108
        cmd = '"%s"' % cmd # work around spawn lamosity on windows
 
109
 
 
110
    ws = pkg_resources.working_set
 
111
    assert os.spawnle(
 
112
        os.P_WAIT, sys.executable, sys.executable,
 
113
        '-c', cmd, '-mqNxd', tmpeggs, 'zc.buildout',
 
114
        dict(os.environ,
 
115
             PYTHONPATH=
 
116
             ws.find(pkg_resources.Requirement.parse('setuptools')).location
 
117
             ),
 
118
        ) == 0
 
119
 
 
120
    ws.add_entry(tmpeggs)
 
121
    ws.require('zc.buildout')
 
122
    import zc.buildout.buildout
 
123
    zc.buildout.buildout.main(sys.argv[1:] + ['bootstrap'])
 
124
    shutil.rmtree(tmpeggs)