| 1 | #!/bin/bash |
|---|
| 2 | |
|---|
| 3 | ## |
|---|
| 4 | # Copyright (c) 2005-2007 Apple Inc. All rights reserved. |
|---|
| 5 | # |
|---|
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 7 | # you may not use this file except in compliance with the License. |
|---|
| 8 | # You may obtain a copy of the License at |
|---|
| 9 | # |
|---|
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 11 | # |
|---|
| 12 | # Unless required by applicable law or agreed to in writing, software |
|---|
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 15 | # See the License for the specific language governing permissions and |
|---|
| 16 | # limitations under the License. |
|---|
| 17 | ## |
|---|
| 18 | |
|---|
| 19 | #PATH |
|---|
| 20 | #PYTHONPATH |
|---|
| 21 | |
|---|
| 22 | daemonize=""; |
|---|
| 23 | username=""; |
|---|
| 24 | groupname=""; |
|---|
| 25 | configfile=""; |
|---|
| 26 | twistdpath="$(type -p twistd)"; |
|---|
| 27 | plugin_name="caldav"; |
|---|
| 28 | service_type=""; |
|---|
| 29 | profile=""; |
|---|
| 30 | twistd_reactor=""; |
|---|
| 31 | child_reactor="" |
|---|
| 32 | |
|---|
| 33 | py_version () |
|---|
| 34 | { |
|---|
| 35 | local python="$1"; shift |
|---|
| 36 | echo "$("${python}" -c "from distutils.sysconfig import get_python_version; print get_python_version()")"; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | try_python () |
|---|
| 40 | { |
|---|
| 41 | local python="$1"; shift |
|---|
| 42 | |
|---|
| 43 | if [ -z "${python}" ]; then return 1; fi; |
|---|
| 44 | |
|---|
| 45 | if ! type "${python}" > /dev/null 2>&1; then return 1; fi; |
|---|
| 46 | local py_version="$(py_version "${python}")"; |
|---|
| 47 | if [ "${py_version/./}" -lt "24" ]; then return 1; fi; |
|---|
| 48 | |
|---|
| 49 | return 0; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | for v in "" "2.5"; do |
|---|
| 53 | for p in \ |
|---|
| 54 | "${PYTHON:=}" \ |
|---|
| 55 | "python${v}" \ |
|---|
| 56 | "/usr/local/bin/python${v}" \ |
|---|
| 57 | "/usr/local/python/bin/python${v}" \ |
|---|
| 58 | "/usr/local/python${v}/bin/python${v}" \ |
|---|
| 59 | "/opt/bin/python${v}" \ |
|---|
| 60 | "/opt/python/bin/python${v}" \ |
|---|
| 61 | "/opt/python${v}/bin/python${v}" \ |
|---|
| 62 | "/Library/Frameworks/Python.framework/Versions/${v}/bin/python" \ |
|---|
| 63 | "/opt/local/bin/python${v}" \ |
|---|
| 64 | "/sw/bin/python${v}" \ |
|---|
| 65 | ; |
|---|
| 66 | do |
|---|
| 67 | if try_python "${p}"; then python="${p}"; break; fi; |
|---|
| 68 | done; |
|---|
| 69 | if [ -n "${python:-}" ]; then break; fi; |
|---|
| 70 | done; |
|---|
| 71 | |
|---|
| 72 | if [ -z "${python:-}" ]; then |
|---|
| 73 | echo "No suitable python found."; |
|---|
| 74 | exit 1; |
|---|
| 75 | fi; |
|---|
| 76 | |
|---|
| 77 | usage () |
|---|
| 78 | { |
|---|
| 79 | program="$(basename "$0")"; |
|---|
| 80 | |
|---|
| 81 | if [ "${1--}" != "-" ]; then echo "$@"; echo; fi; |
|---|
| 82 | |
|---|
| 83 | echo "Usage: ${program} [-hX] [-u username] [-g groupname] [-T twistd] [-t type] [-f caldavd.plist] [-p statsfile]"; |
|---|
| 84 | echo "Options:"; |
|---|
| 85 | echo " -h Print this help and exit"; |
|---|
| 86 | echo " -X Do not daemonize"; |
|---|
| 87 | echo " -u User name to run as"; |
|---|
| 88 | echo " -g Group name to run as"; |
|---|
| 89 | echo " -f Configuration file to read"; |
|---|
| 90 | echo " -T Path to twistd binary"; |
|---|
| 91 | echo " -t Process type (Master, Slave or Combined)"; |
|---|
| 92 | echo " -p Path to the desired pstats file."; |
|---|
| 93 | echo " -R The Twisted Reactor to run [${reactor}]"; |
|---|
| 94 | |
|---|
| 95 | if [ "${1-}" == "-" ]; then return 0; fi; |
|---|
| 96 | exit 64; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | while getopts 'hXu:g:f:T:P:t:p:R:' option; do |
|---|
| 100 | case "${option}" in |
|---|
| 101 | '?') usage; ;; |
|---|
| 102 | 'h') usage -; exit 0; ;; |
|---|
| 103 | 'X') daemonize="-n"; ;; |
|---|
| 104 | 'f') configfile="-f ${OPTARG}"; ;; |
|---|
| 105 | 'T') twistdpath="${OPTARG}"; ;; |
|---|
| 106 | 'u') username="-u ${OPTARG}"; ;; |
|---|
| 107 | 'g') groupname="-g ${OPTARG}"; ;; |
|---|
| 108 | 'P') plugin_name="${OPTARG}"; ;; |
|---|
| 109 | 't') service_type="-o ProcessType=${OPTARG}"; ;; |
|---|
| 110 | 'p') profile="-o Profiling/Enabled=True -o Profiling/BaseDirectory=${OPTARG}"; ;; |
|---|
| 111 | 'R') twistd_reactor="--reactor=${OPTARG}"; child_reactor="-o Twisted/reactor=${OPTARG}"; ;; |
|---|
| 112 | esac; |
|---|
| 113 | done; |
|---|
| 114 | |
|---|
| 115 | shift $((${OPTIND} - 1)); |
|---|
| 116 | |
|---|
| 117 | if [ $# != 0 ]; then usage "Unrecognized arguments:" "$@"; fi; |
|---|
| 118 | |
|---|
| 119 | export PYTHONPATH |
|---|
| 120 | |
|---|
| 121 | echo exec "${python}" "${twistdpath}" "${twistd_reactor}" ${daemonize} ${username} ${groupname} "${plugin_name}" ${configfile} ${service_type} ${profile} "${child_reactor}"; |
|---|
| 122 | |
|---|
| 123 | exec "${python}" "${twistdpath}" ${twistd_reactor} ${daemonize} ${username} ${groupname} "${plugin_name}" ${configfile} ${service_type} ${profile} ${child_reactor}; |
|---|