summaryrefslogtreecommitdiff
path: root/developer/deken
blob: d4daaa71554a0901cc7626827ad87209be8c818b (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env sh

# Ensure this file is executable via `chmod a+x deken`, then place it
# somewhere on your ${PATH}, like ~/bin. The rest of Deken will self-install
# on first run into the ~/.deken/ directory.

# Much of this code is pilfered from Clojure's Leiningen tool

script="$0"
script_args=$*

##############################################################################
# variable declarations
##############################################################################

export DEKEN_VERSION="0.9.16"

: "${DEKEN_HOME:=${HOME}/.deken}"
: "${DEKEN_GIT_BRANCH:=main}"
: "${DEKEN_BASE_URL:=https://raw.githubusercontent.com/pure-data/deken/${DEKEN_GIT_BRANCH}/developer}"
# allow the user to override the default python
: "${PYTHON_BIN:=python3}"

export DEKEN_HOME

VIRTUALENV_URL="https://bootstrap.pypa.io/virtualenv/@pyversion@/virtualenv.pyz"

case "$(uname -s)" in
    CYGWIN*)
        DEKEN_HOME=$(cygpath -w "${DEKEN_HOME}")
        ;;
    *)
        ;;
esac

# This needs to be defined before we call HTTP_CLIENT below
if [ "${HTTP_CLIENT}" = "" ]; then
    if which curl >/dev/null; then
        if [ -n "${https_proxy}" ]; then
            CURL_PROXY="-x ${https_proxy}"
        fi
        HTTP_CLIENT="curl ${CURL_PROXY} -f -L -o"
    else
        HTTP_CLIENT="wget -O"
    fi
fi

if test -z "${systeminstalled}"; then
 # if this script resides in /usr/ and there's a deken.hy relative to it in the
 # share/ folder, we consider it system-installed
 if test -z "${0##/usr/*}" && test -e "${0%/*}/../share/deken/deken.hy"; then
   systeminstalled=true
 else
   systeminstalled=false
 fi
fi

if ${systeminstalled}; then
    DEKEN_HOME="${0%/*}/../share/deken"
fi

: "${DEKEN_HY:=${DEKEN_HOME}/deken.hy}"
: "${DEKENHY:=${DEKEN_HOME}/virtualenv/bin/hy}"
: "${SYSTEMHY:=$(command -v hy3 hy | head -1)}"
: "${SYSTEMHY:=$(which hy3 hy 2>/dev/null| head -1)}"

##############################################################################
# helper functions
##############################################################################

error() {
    echo "$@" 1>&2
}

countdown() {
  _countdown_t=$1
  _countdown_t=$((_countdown_t))
  while [ $_countdown_t -gt 0 ]; do
     printf "\r%2d" $((_countdown_t))
     sleep 1
     _countdown_t=$((_countdown_t-1))
  done
  printf "\r  \r"
  unset _countdown_t
}

fetch_file() {
  _fetch_file_dst=${1#file://}
  _fetch_file_src=${2#file://}
  if test -e "${_fetch_file_src}"; then
     cp -v "${_fetch_file_src}" "${_fetch_file_dst}"
  else
     ${HTTP_CLIENT} "${_fetch_file_dst}" "${_fetch_file_src}"
  fi
  unset _fetch_file_dst
  unset _fetch_file_src
}

uninstall_deken() {
    if ${systeminstalled}; then
      # on Debian we disallow uninstalling
      error "Uninstalling is disabled for system-provided deken!"
      error "Instead, use your package manager to remove deken."
      exit 1
    fi
    error "I'm going to uninstall myself and my dependencies from ${DEKEN_HOME} now."
    error "Feel free to Ctrl-C now if you don't want to do this."
    countdown 5
    error "Uninstalling deken."
    rm -rf "${DEKEN_HOME}" "$0"
    exit 0
}

bail_install() {
    error "Self-installation of Deken failed."
    error "Please paste any errors in the bug tracker at https://github.com/pure-data/deken/issues"
    # remove all traces of our attempts to install.
    rm -rf "${DEKEN_HOME}"
    # bail from this script.
    exit 1
}
bail_install_msg() {
    error "$@"
    bail_install
}

tell_reinstall() {
cat <<EOF
You can run 'deken install --self' or 'deken upgrade --self' anytime to
 re-install (or upgrade) your Deken installation.
EOF

}

bail_requirements() {
    rm -f "${DEKEN_HOME}/requirements.txt"
    cat >/dev/stderr <<EOF
Installation of requirements failed.
You probably should install the following packages first:
 - 'python3-dev'
 - 'libffi-dev'
 - 'libssl-dev'
EOF
    tell_reinstall >/dev/stderr
    exit 1
}

bail_upgrade() {
    if ${systeminstalled}; then
      # on Debian we don't want the user to run upgrades themselves
      return
    fi
    cat >/dev/stderr <<EOF
It seems your version of deken is out of sync.
($script has version ${DEKEN_VERSION}, but your installation is $1)
EOF
    tell_reinstall >/dev/stderr
    echo >/dev/stderr
}

install_virtualenv() {
  if which virtualenv >/dev/null; then
    virtualenv --system-site-packages "${DEKEN_HOME}/virtualenv"
  else
    _install_virtualenv_pyversion=$("${PYTHON_BIN}" -c "import sys; print('%s.%s' % (sys.version_info.major, sys.version_info.minor))")
    _install_virtualenv_url=$(echo "${VIRTUALENV_URL}" | sed -e "s|@pyversion@|${_install_virtualenv_pyversion}|g")
    echo "Downloading & installing Virtualenv for ${_install_virtualenv_pyversion} using ${_install_virtualenv_url}"
    rm -rf "${DEKEN_HOME}/virtualenv.pyz"
    mkdir -p "${DEKEN_HOME}"
    fetch_file "${DEKEN_HOME}/virtualenv.pyz" "${_install_virtualenv_url}" && \
            "${PYTHON_BIN}" "${DEKEN_HOME}/virtualenv.pyz" "${DEKEN_HOME}/virtualenv"
    rm -rf "${DEKEN_HOME}/virtualenv.pyz"
    unset _install_virtualenv_pyversion
    unset _install_virtualenv_url
  fi

    [ -d "${DEKEN_HOME}/virtualenv" ] || \
            bail_install
}

install_deken() {
    if ${systeminstalled}; then
       # on Debian, we can skip installation
       return
    fi

    which "${PYTHON_BIN}" >/dev/null || \
        bail_install_msg "Oops, no Python found! You need Python3 to run Deken: ${PYTHON_BIN}
You can specify an alternative Python interpreter via the PYTHON_BIN envvar"
    error "This is your first time running deken on this machine."
    error "I'm going to install myself and my dependencies into ${DEKEN_HOME} now."
    error "Feel free to Ctrl-C now if you don't want to do this."
    countdown 3
    error "Installing deken."

    mkdir -p "${DEKEN_HOME}"
    [ -e "${DEKEN_HOME}/requirements.txt" ] || (\
        ( echo "Fetching Python requirements file: ${DEKEN_BASE_URL}/requirements.txt" && \
        fetch_file "${DEKEN_HOME}/requirements.txt" "${DEKEN_BASE_URL}/requirements.txt" ) || bail_install)
    [ -e "${DEKEN_HOME}/requirements.txt" ] || bail_install
    [ -e "${DEKEN_HY}" ] || (\
        ( echo "Fetching main hylang file: ${DEKEN_BASE_URL}/deken.hy" && \
        fetch_file "${DEKEN_HY}" "${DEKEN_BASE_URL}/deken.hy" ) || bail_install)
    [ -e "${DEKEN_HY}" ] || bail_install
    [ -d "${DEKEN_HOME}/virtualenv" ] || install_virtualenv
    if ! [ -x "${DEKENHY}" ]; then
        echo "Installing deken library dependencies."
        "${DEKEN_HOME}/virtualenv/bin/pip" install -r "${DEKEN_HOME}/requirements.txt" || bail_requirements
    fi
    echo "${DEKEN_VERSION}" > "${DEKEN_HOME}/version.txt"
}

upgrade_deken() {
    if ${systeminstalled}; then
      # on Debian we disallow upgrading
      error "Direct upgrading is disabled for system-provided deken!"
      error "Instead, use your package manager to install newer versions."
      exit 1
    fi
    # first upgrade this script itself
    echo "Upgrading ${script}."
    fetch_file "${DEKEN_HOME}/.upgrade-deken" "${DEKEN_BASE_URL}/deken" || ( error "Error upgrading deken"; exit 1; )
    if diff -q "${DEKEN_HOME}/.upgrade-deken" "$script" >/dev/null; then
        # launcher script is already up-to-date
        rm "${DEKEN_HOME}/.upgrade-deken"
    else
        if test -w "${script}"; then
            cat "${DEKEN_HOME}/.upgrade-deken" > "${script}"
            rm "${DEKEN_HOME}/.upgrade-deken"
            error "The deken-installer has changed."
            error "Please re-run the last command"
            error ""
            error "Hint: ${script} ${script_args}"
            exit
	else
            rm "${DEKEN_HOME}/.upgrade-deken"
	    error "Unable to update '${script}', proceeding anyhow..."
        fi
    fi
    # next upgrade our dependencies
    for f in requirements.txt deken.hy
    do
        echo "Fetching ${f} file: ${DEKEN_BASE_URL}/${f}"
        fetch_file "${DEKEN_HOME}/.upgrade-${f}" "${DEKEN_BASE_URL}/${f}" || ( error "Error upgrading ${f}"; exit 1; )
        mv "${DEKEN_HOME}/.upgrade-${f}" "${DEKEN_HOME}/${f}"
    done
    # finally update the python dependencies
    "${DEKEN_HOME}/virtualenv/bin/pip" install -r "${DEKEN_HOME}/requirements.txt" || bail_requirements

    echo "${DEKEN_VERSION}" > "${DEKEN_HOME}/version.txt"
    echo "Successfully upgraded."
}

tryrun_deken() {
    if ${systeminstalled}; then
       "${SYSTEMHY}" "${DEKEN_HY}" "$@"
       exit $?
    fi

    # check if the 'deken' script and the actual implementation match
    _tryrun_deken_version=$(cat "${DEKEN_HOME}/version.txt" 2>/dev/null)
    if test -n "${_tryrun_deken_version}" && test "x${_tryrun_deken_version}" != "x${DEKEN_VERSION}"; then
        bail_upgrade "${_tryrun_deken_version}"
    fi
    unset _tryrun_deken_version

    if [ ! -x "${DEKENHY}" ]; then
       error "Unable to find '${DEKENHY}'"
       error "Try running '$0 install --self' or '$0 upgrade --self'"
       exit 1
    fi
    if [ ! -e "${DEKEN_HY}" ]; then
       error "Unable to find '${DEKEN_HY}'"
       error "Try running '$0 install --self' or '$0 upgrade --self'"
       exit 1
    fi

    "${DEKENHY}" "${DEKEN_HY}" "$@"
}

##############################################################################
# here starts the code
##############################################################################


# catch 'uninstall --self' early, so we don't run into the "installed" checks
if [ $# -eq 2  ] && [ "$1" = "uninstall" ] && [ "$2" = "--self" ]; then
  uninstall_deken
  exit
fi

if [ $# -eq 1  ] && [ "$1" = "--version" ]; then
  echo "${DEKEN_VERSION}"
  exit
fi


if [ "$(id -u)" -eq 0 ] && [ "${DEKEN_ROOT}" = "" ]; then
    error "WARNING: You're currently running as root; probably by accident."
    error "Press Control-C to abort or Enter to continue as root."
    error "Set DEKEN_ROOT=yes to disable this warning."
    read -r _
fi

# make sure we are deployed
[ -d "${DEKEN_HOME}" ] || install_deken

# last check to make sure we can bootstrap
[ -d "${DEKEN_HOME}" ] || bail_install

# catch the special "upgrade" command
if [ $# -eq 2  ] && [ "$2" = "--self" ]; then
	case "$1" in
		install)
			install_deken
			exit
			;;
		update|upgrade)
			upgrade_deken
			exit
			;;
	esac
fi

# run the real deken command with args passed through
tryrun_deken "$@"