Fix the Requires() examples by adding a sleep command to the
[scons.git] / bin / install_python.py
1 #!/usr/bin/env python
2 #
3 # A script for unpacking and installing different historic versions of
4 # Python in a consistent manner for side-by-side development testing.
5 #
6 # This was written for a Linux system (specifically Ubuntu) but should
7 # be reasonably generic to any POSIX-style system with a /usr/local
8 # hierarchy.
9
10 import getopt
11 import os
12 import shutil
13 import sys
14
15 from Command import CommandRunner, Usage
16
17 all_versions = [
18     #'1.5.2',   # no longer available at python.org
19     '2.0.1',
20     '2.1.3',
21     '2.2',
22     '2.3.7',
23     '2.4.5',
24     #'2.5.2',
25     '2.6',
26 ]
27
28 def main(argv=None):
29     if argv is None:
30         argv = sys.argv
31
32     all = False
33     downloads_dir = 'Downloads'
34     downloads_url = 'http://www.python.org/ftp/python'
35     sudo = 'sudo'
36     prefix = '/usr/local'
37
38     short_options = 'ad:hnp:q'
39     long_options = ['all', 'help', 'no-exec', 'prefix=', 'quiet']
40
41     helpstr = """\
42 Usage:  install_python.py [-ahnq] [-d DIR] [-p PREFIX] [VERSION ...]
43
44   -a, --all                     Install all SCons versions.
45   -d DIR, --downloads=DIR       Downloads directory.
46   -h, --help                    Print this help and exit
47   -n, --no-exec                 No execute, just print command lines
48   -p PREFIX, --prefix=PREFIX    Installation prefix.
49   -q, --quiet                   Quiet, don't print command lines
50 """
51
52     try:
53         try:
54             opts, args = getopt.getopt(argv[1:], short_options, long_options)
55         except getopt.error, msg:
56             raise Usage(msg)
57
58         for o, a in opts:
59             if o in ('-a', '--all'):
60                 all = True
61             elif o in ('-d', '--downloads'):
62                 downloads_dir = a
63             elif o in ('-h', '--help'):
64                 print helpstr
65                 sys.exit(0)
66             elif o in ('-n', '--no-exec'):
67                 CommandRunner.execute = CommandRunner.do_not_execute
68             elif o in ('-p', '--prefix'):
69                 prefix = a
70             elif o in ('-q', '--quiet'):
71                 CommandRunner.display = CommandRunner.do_not_display
72     except Usage, err:
73         sys.stderr.write(str(err.msg) + '\n')
74         sys.stderr.write('use -h to get help\n')
75         return 2
76
77     if all:
78         if args:
79             msg = 'install-scons.py:  -a and version arguments both specified'
80             sys.stderr.write(msg)
81             sys.exit(1)
82
83         args = all_versions
84
85     cmd = CommandRunner()
86
87     for version in args:
88         python = 'Python-' + version
89         tar_gz = os.path.join(downloads_dir, python + '.tgz')
90         tar_gz_url = os.path.join(downloads_url, version, python + '.tgz')
91
92         if (version.startswith('1.5') or
93             version.startswith('1.6') or
94             version.startswith('2.0')):
95
96             configureflags = '--with-threads'
97
98         else:
99
100             configureflags = ''
101
102         cmd.subst_dictionary(locals())
103
104         if not os.path.exists(tar_gz):
105             if not os.path.exists(downloads_dir):
106                 cmd.run('mkdir %(downloads_dir)s')
107             cmd.run('wget -O %(tar_gz)s %(tar_gz_url)s')
108
109         cmd.run('tar zxf %(tar_gz)s')
110
111         cmd.run('cd %(python)s')
112
113         if (version.startswith('1.6') or
114             version.startswith('2.0')):
115
116             def edit_modules_setup_in():
117                 content = open('Modules/Setup.in', 'r').read()
118                 content = content.replace('\n#zlib', '\nzlib')
119                 open('Modules/Setup.in', 'w').write(content)
120
121             display = 'ed Modules/Setup.in <<EOF\ns/^#zlib/zlib/\nw\nq\nEOF\n'
122             cmd.run((edit_modules_setup_in,), display)
123
124         cmd.run('./configure --prefix=%(prefix)s %(configureflags)s 2>&1 | tee configure.out')
125         cmd.run('make 2>&1 | tee make.out')
126         cmd.run('%(sudo)s make install')
127
128         cmd.run('%(sudo)s rm -f %(prefix)s/bin/{idle,pydoc,python,python-config,smtpd.py}')
129
130         cmd.run('cd ..')
131
132         cmd.run((shutil.rmtree, python), 'rm -rf %(python)s')
133
134 if __name__ == "__main__":
135     sys.exit(main())
136
137 # Local Variables:
138 # tab-width:4
139 # indent-tabs-mode:nil
140 # End:
141 # vim: set expandtab tabstop=4 shiftwidth=4: