File: configure

package info (click to toggle)
zope2.13 2.13.22-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, sid
  • size: 38,644 kB
  • ctags: 38,805
  • sloc: python: 196,395; xml: 90,515; ansic: 24,121; sh: 916; makefile: 333; perl: 37
file content (153 lines) | stat: -rw-r--r-- 5,855 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ---
# Configuration for the Zope2.x Makefile.
# 
# Author:       Michael Mulich
# Copyright:    2010 by Penn State University
# Organization: WebLion Group, Penn State University
# License:      GPL, see LICENSE for more detail
# ---
import os
import sys

assert sys.version_info >= (2, 7), "Python >= 2.7 is required"

# XXX Would much rather use argparse here, but it's not available in Python 2.7
from optparse import OptionParser
from distutils.sysconfig import get_python_lib
from subprocess import Popen, PIPE

MAKEFILE_IN = os.path.abspath('Makefile.in')
MAKEFILE = MAKEFILE_IN[:-3]
INDEX = 'http://download.zope.org/Zope2/index/%s/'

# TODO Parse the version information from version.txt
VERSIONS = {'name': 'Zope2',
            'major_minor_version': '2.13',
            'point_version': '21',
            'pip_version': '0.8.1',
            }
complete_version = '%s.%s' % (VERSIONS['major_minor_version'],
                                   VERSIONS['point_version'])
VERSIONS['index'] = INDEX % complete_version

site_pkgs = os.path.abspath(get_python_lib())
script_loc = os.path.abspath(os.path.join(sys.exec_prefix, 'bin'))
default_values = {'python_exec': sys.executable,
                  'lib_dest': site_pkgs,
                  'script_dest': script_loc,
                  'pth_dest': site_pkgs,
                  }

def store_abs_path(option, opt_str, value, parser):
    setattr(parser.values, option.dest, os.path.abspath(value))

def store_prefix_info(option, opt_str, value, parser):
    """Given the prefix, find three values:
    1) The Python executable to use
    2) The site-packages directory
    3) The scripts/binaries directory

    Returns a tuple with the above values.
    """
    prefix = os.path.abspath(value)
    # XXX We are making an assumption that the python version used to configure
    # the project is the same version used to build it.
    python_version = '.'.join([str(i) for i in sys.version_info[:2]])
    python_str = 'python'
    prefixed_site_pkgs = os.path.join(prefix, 'lib', python_str)
    prefixed_bin_dir = os.path.join(prefix, 'bin')
    python_exec = os.path.join(prefixed_bin_dir, python_str)
    lib_dest = prefixed_site_pkgs
    script_dest = prefixed_bin_dir
    # Note: We don't find the pth destination because its destination is the
    # same as the libraries themselves.
    value_to_store = (python_exec, lib_dest, script_dest)
    setattr(parser.values, option.dest, value_to_store)

def find_location_from_python_exec(executable_path):
    """Given the location of the python executable, find three values:
    1) The Python executable itself
    2) The site-packages directory
    3) the scripts/binaries directiory

    Returns a tuple with the above values.
    """
    executable_path = os.path.realpath(executable_path)
    # Check the python version first.
    version_cmd = "import sys; assert sys.version_info >= (2, 7)"
    p = Popen([executable_path, '-c', version_cmd])
    if p.returncode > 0:
        raise RuntimeError("The given python executable does not meet the "
                           "version (>= 2.7).")
    # Find the locations in question.
    locations_cmd = ["import os, sys",
                     "from distutils.sysconfig import get_python_lib",
                     "print((sys.executable, os.path.abspath(get_python_lib()), os.path.abspath(os.path.join(sys.exec_prefix, 'bin')),))"]
    locations_cmd = ';'.join(locations_cmd)
    p = Popen([executable_path, '-c', locations_cmd], stdin=PIPE, stdout=PIPE)
    stdout, stderr = p.communicate()
    result = eval(stdout)
    return result


class IgnoreUnknownOptionParser(OptionParser):
    """An override of optparse.OptionParser that allows unknown options
    to pass through without error."""

    def error(self, msg):
        if msg.startswith('no such option:'):
            pass
        else:
            OptionParser.error(self, msg)


usage = "usage: %prog [--with-python | --prefix]"
parser = IgnoreUnknownOptionParser(usage=usage)
parser.add_option('--with-python', dest='python',
                  type='string', nargs=1,
                  action='callback', callback=store_abs_path,
                  help="Location of the Python executable to use.",
                  metavar='PYTHON')
parser.add_option('--prefix', dest='prefix',
                  type='string', nargs=1,
                  action='callback', callback=store_prefix_info,
                  default=tuple(),
                  help="Prefix for the installation.",
                  metavar='PREFIX')

def main():
    (options, args) = parser.parse_args()

    in_vars = VERSIONS

    if options.prefix or options.python:
        parts = options.prefix or find_location_from_python_exec(options.python)
        python_exec, lib_dest, script_dest = parts
        in_vars.update({'python_exec': python_exec,
                        'lib_dest': lib_dest,
                        'script_dest': script_dest,
                        'pth_dest': lib_dest,
                        })
    else:
        in_vars.update(default_values)

    print('-' * 80)
    print("Zope2 final destinations:")
    print("  libraries -> %s" % in_vars['lib_dest'])
    print("  scripts -> %s" % in_vars['script_dest'])
    print("  pth file -> %s" % in_vars['pth_dest'])
    print('-' * 80)

    with open(MAKEFILE_IN, 'r') as f_in:
        with open(MAKEFILE, 'w') as f_out:
            f_out.write(f_in.read() % in_vars)
    print("Configuration complete. You may now run:  make && make install")

if __name__ == '__main__':
    if not os.path.exists(MAKEFILE_IN):
        raise RuntimeError("Can't find the Makefile.in in the current "
                           "working directory. Please make sure you are "
                           "executing from the project's root location.")
    main()