File: _py.sh

package info (click to toggle)
twextpy 1%3A0.1~git20161216.0.b90293c-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,724 kB
  • sloc: python: 20,458; sh: 742; makefile: 5
file content (159 lines) | stat: -rw-r--r-- 4,031 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
154
155
156
157
158
159
# -*- sh-basic-offset: 2 -*-
##
# Copyright (c) 2005-2016 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##

find_cmd () {
  local cmd="$1"; shift;

  local path="$(type "${cmd}" 2>/dev/null | sed "s|^${cmd} is \(a tracked alias for \)\{0,1\}||")";

  if [ -z "${cmd}" ]; then
    return 1;
  fi;

  echo "${path}";
}

# Echo the major.minor version of the given Python interpreter.

py_version () {
  local python="$1"; shift;
  echo "$("${python}" -c "from distutils.sysconfig import get_python_version; print get_python_version()")";
}

#
# Test if a particular python interpreter is available, given the full path to
# that interpreter.
#
try_python () {
  local python="$1"; shift;

  if [ -z "${python}" ]; then
    return 1;
  fi;

  if ! type "${python}" > /dev/null 2>&1; then
    return 1;
  fi;

  local py_version="$(py_version "${python}")";
  if [ "$(echo "${py_version}" | sed 's|\.||')" -lt "25" ]; then
    return 1;
  fi;
  return 0;
}


#
# Detect which version of Python to use, then print out which one was detected.
#
# This will prefer the python interpreter in the PYTHON environment variable.
# If that's not found, it will check for "python2.7" and "python", looking for
# each in your PATH.
#
detect_python_version () {
  local v;
  local p;
  for v in "2.7" ""
  do
    for p in         \
      "${PYTHON:=}"  \
      "python${v}"   \
      ;
    do
      if p="$(find_cmd "${p}")"; then
        if try_python "${p}"; then
          echo "${p}";
          return 0;
        fi;
      fi;
    done;
  done;
  return 1;
}


#
# Compare version numbers
#
cmp_version () {
  local  v="$1"; shift;
  local mv="$1"; shift;

  local vh;
  local mvh;
  local result;

  while true; do
     vh="${v%%.*}"; # Get highest-order segment
    mvh="${mv%%.*}";

    if [ "${vh}" -gt "${mvh}" ]; then
      result=1;
      break;
    fi;

    if [ "${vh}" -lt "${mvh}" ]; then
      result=0;
      break;
    fi;

    if [ "${v}" = "${v#*.}" ]; then
      # No dots left, so we're ok
      result=0;
      break;
    fi;

    if [ "${mv}" = "${mv#*.}" ]; then
      # No dots left, so we're not gonna match
      result=1;
      break;
    fi;

     v="${v#*.}";
    mv="${mv#*.}";
  done;

  return ${result};
}


#
# Detect which python to use, and store it in the 'python' variable, as well as
# setting up variables related to version and build configuration.
#
init_py () {
  # First, detect the appropriate version of Python to use, based on our version
  # requirements and the environment.  Note that all invocations of python in
  # our build scripts should therefore be '"${python}"', not 'python'; this is
  # important on systems with older system pythons (2.4 or earlier) with an
  # alternate install of Python, or alternate python installation mechanisms
  # like virtualenv.
  bootstrap_python="$(detect_python_version)";

  # Set the $PYTHON environment variable to an absolute path pointing at the
  # appropriate python executable, a standard-ish mechanism used by certain
  # non-distutils things that need to find the "right" python.  For instance,
  # the part of the PostgreSQL build process which builds pl_python.  Note that
  # detect_python_version, above, already honors $PYTHON, so if this is already
  # set it won't be stomped on, it will just be re-set to the same value.
  export PYTHON="$(find_cmd ${bootstrap_python})";

  if [ -z "${bootstrap_python:-}" ]; then
    echo "No suitable python found. Python 2.6 or 2.7 is required.";
    exit 1;
  fi;
}