Debian Codegraph @

configure

raw
1 #!/usr/bin/env bash
2 #
3 # Copyright (c) nexB Inc. and others. All rights reserved.
4 # SPDX-License-Identifier: Apache-2.0
5 # See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6 # See https://github.com/aboutcode-org/ for support or download.
7 # See https://aboutcode.org for more information about nexB OSS projects.
8 #
9
10 set -e
11 #set -x
12
13 ################################
14 # A configuration script to set things up:
15 # create a virtualenv and install or update thirdparty packages.
16 # Source this script for initial configuration
17 # Use configure --help for details
18 #
19 # NOTE: please keep in sync with Windows script configure.bat
20 #
21 # This script will search for a virtualenv.pyz app in etc/thirdparty/virtualenv.pyz
22 # Otherwise it will download the latest from the VIRTUALENV_PYZ_URL default
23 ################################
24 CLI_ARGS=$1
25
26 ################################
27 # Defaults. Change these variables to customize this script
28 ################################
29
30 # Requirement arguments passed to pip and used by default or with --dev.
31 REQUIREMENTS="--editable . --constraint requirements.txt"
32 DEV_REQUIREMENTS="--editable .[testing] --constraint requirements.txt --constraint requirements-dev.txt"
33 DOCS_REQUIREMENTS="--editable .[docs] --constraint requirements.txt"
34
35 # where we create a virtualenv
36 VIRTUALENV_DIR=venv
37
38 # Cleanable files and directories to delete with the --clean option
39 CLEANABLE="build dist venv .cache .eggs"
40
41 # extra arguments passed to pip
42 PIP_EXTRA_ARGS=" "
43
44 # the URL to download virtualenv.pyz if needed
45 VIRTUALENV_PYZ_URL=https://bootstrap.pypa.io/virtualenv.pyz
46 ################################
47
48
49 ################################
50 # Current directory where this script lives
51 CFG_ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
52 CFG_BIN_DIR=$CFG_ROOT_DIR/$VIRTUALENV_DIR/bin
53
54
55 ################################
56 # Install with or without and index. With "--no-index" this is using only local wheels
57 # This is an offline mode with no index and no network operations
58 # NO_INDEX="--no-index "
59 NO_INDEX=""
60
61
62 ################################
63 # Thirdparty package locations and index handling
64 # Find packages from the local thirdparty directory if present
65 THIRDPARDIR=$CFG_ROOT_DIR/thirdparty
66 if [[ "$(echo $THIRDPARDIR/*.whl)x" != "$THIRDPARDIR/*.whlx" ]]; then
67 PIP_EXTRA_ARGS="$NO_INDEX --find-links $THIRDPARDIR"
68 fi
69
70
71 ################################
72 # Set the quiet flag to empty if not defined
73 if [[ "$CFG_QUIET" == "" ]]; then
74 CFG_QUIET=" "
75 fi
76
77
78 ################################
79 # Find a proper Python to run
80 # Use environment variables or a file if available.
81 # Otherwise the latest Python by default.
82 find_python() {
83 if [[ "$PYTHON_EXECUTABLE" == "" ]]; then
84 # check for a file named PYTHON_EXECUTABLE
85 if [ -f "$CFG_ROOT_DIR/PYTHON_EXECUTABLE" ]; then
86 PYTHON_EXECUTABLE=$(cat "$CFG_ROOT_DIR/PYTHON_EXECUTABLE")
87 else
88 PYTHON_EXECUTABLE=python3
89 fi
90 fi
91 }
92
93
94 ################################
95 create_virtualenv() {
96 # create a virtualenv for Python
97 # Note: we do not use the bundled Python 3 "venv" because its behavior and
98 # presence is not consistent across Linux distro and sometimes pip is not
99 # included either by default. The virtualenv.pyz app cures all these issues.
100
101 VENV_DIR="$1"
102 if [ ! -f "$CFG_BIN_DIR/python" ]; then
103
104 mkdir -p "$CFG_ROOT_DIR/$VENV_DIR"
105
106 if [ -f "$CFG_ROOT_DIR/etc/thirdparty/virtualenv.pyz" ]; then
107 VIRTUALENV_PYZ="$CFG_ROOT_DIR/etc/thirdparty/virtualenv.pyz"
108 else
109 VIRTUALENV_PYZ="$CFG_ROOT_DIR/$VENV_DIR/virtualenv.pyz"
110 wget -O "$VIRTUALENV_PYZ" "$VIRTUALENV_PYZ_URL" 2>/dev/null || curl -o "$VIRTUALENV_PYZ" "$VIRTUALENV_PYZ_URL"
111 fi
112
113 $PYTHON_EXECUTABLE "$VIRTUALENV_PYZ" \
114 --wheel embed --pip embed --setuptools embed \
115 --seeder pip \
116 --never-download \
117 --no-periodic-update \
118 --no-vcs-ignore \
119 $CFG_QUIET \
120 "$CFG_ROOT_DIR/$VENV_DIR"
121 fi
122 }
123
124
125 ################################
126 install_packages() {
127 # install requirements in virtualenv
128 # note: --no-build-isolation means that pip/wheel/setuptools will not
129 # be reinstalled a second time and reused from the virtualenv and this
130 # speeds up the installation.
131 # We always have the PEP517 build dependencies installed already.
132
133 "$CFG_BIN_DIR/pip" install \
134 --upgrade \
135 --no-build-isolation \
136 $CFG_QUIET \
137 $PIP_EXTRA_ARGS \
138 $1
139 }
140
141
142 ################################
143 cli_help() {
144 echo An initial configuration script
145 echo " usage: ./configure [options]"
146 echo
147 echo The default is to configure for regular use. Use --dev for development.
148 echo
149 echo The options are:
150 echo " --clean: clean built and installed files and exit."
151 echo " --dev: configure the environment for development."
152 echo " --help: display this help message and exit."
153 echo
154 echo By default, the python interpreter version found in the path is used.
155 echo Alternatively, the PYTHON_EXECUTABLE environment variable can be set to
156 echo configure another Python executable interpreter to use. If this is not
157 echo set, a file named PYTHON_EXECUTABLE containing a single line with the
158 echo path of the Python executable to use will be checked last.
159 set +e
160 exit
161 }
162
163
164 ################################
165 clean() {
166 # Remove cleanable file and directories and files from the root dir.
167 echo "* Cleaning ..."
168 for cln in $CLEANABLE;
169 do rm -rf "${CFG_ROOT_DIR:?}/${cln:?}";
170 done
171 set +e
172 exit
173 }
174
175
176 ################################
177 # Main command line entry point
178 CFG_REQUIREMENTS=$REQUIREMENTS
179
180 # We are using getopts to parse option arguments that start with "-"
181 while getopts :-: optchar; do
182 case "${optchar}" in
183 -)
184 case "${OPTARG}" in
185 help ) cli_help;;
186 clean ) find_python && clean;;
187 dev ) CFG_REQUIREMENTS="$DEV_REQUIREMENTS";;
188 docs ) CFG_REQUIREMENTS="$DOCS_REQUIREMENTS";;
189 esac;;
190 esac
191 done
192
193
194 PIP_EXTRA_ARGS="$PIP_EXTRA_ARGS"
195
196 find_python
197 create_virtualenv "$VIRTUALENV_DIR"
198 install_packages "$CFG_REQUIREMENTS"
199 . "$CFG_BIN_DIR/activate"
200
201
202 set +e