tools/read-dependencies tools/read-version: rewrite in python
This just does python rewrites of these tools that were shell or sed or grep. Clearly the user of cloud-init has python, but it turns out that getting sane versions of sed or grep on different unixes is less than simple.
This commit is contained in:
parent
123cffbade
commit
1c99abb054
|
@ -1,32 +1,23 @@
|
||||||
#!/bin/sh
|
#!/usr/bin/env python
|
||||||
|
|
||||||
set -e
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
find_root() {
|
if 'CLOUD_INIT_TOP_D' in os.environ:
|
||||||
local topd
|
topd = os.path.realpath(os.environ.get('CLOUD_INIT_TOP_D'))
|
||||||
if [ -z "${CLOUD_INIT_TOP_D}" ]; then
|
else:
|
||||||
topd=$(cd "$(dirname "${0}")" && cd .. && pwd)
|
topd = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
|
||||||
else
|
|
||||||
topd=$(cd "${CLOUD_INIT_TOP_D}" && pwd)
|
|
||||||
fi
|
|
||||||
[ $? -eq 0 -a -f "${topd}/setup.py" ] || return
|
|
||||||
ROOT_DIR="$topd"
|
|
||||||
}
|
|
||||||
fail() { echo "$0:" "$@" 1>&2; exit 1; }
|
|
||||||
|
|
||||||
if ! find_root; then
|
for fname in ("setup.py", "requirements.txt"):
|
||||||
fail "Unable to locate 'setup.py' file that should " \
|
if not os.path.isfile(os.path.join(topd, fname)):
|
||||||
"exist in the cloud-init root directory."
|
sys.stderr.write("Unable to locate '%s' file that should "
|
||||||
fi
|
"exist in cloud-init root directory." % fname)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
REQUIRES="$ROOT_DIR/requirements.txt"
|
with open(os.path.join(topd, "requirements.txt"), "r") as fp:
|
||||||
|
for line in fp:
|
||||||
|
if not line.strip() or line.startswith("#"):
|
||||||
|
continue
|
||||||
|
sys.stdout.write(line)
|
||||||
|
|
||||||
if [ ! -e "$REQUIRES" ]; then
|
sys.exit(0)
|
||||||
fail "Unable to find 'requirements.txt' file located at '$REQUIRES'"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Filter out comments and empty lines
|
|
||||||
DEPS=$(sed -n -e 's,#.*,,' -e '/./p' "$REQUIRES") &&
|
|
||||||
[ -n "$DEPS" ] ||
|
|
||||||
fail "failed to read deps from '${REQUIRES}'"
|
|
||||||
echo "$DEPS" | sort -d -f
|
|
||||||
|
|
|
@ -1,32 +1,26 @@
|
||||||
#!/bin/sh
|
#!/usr/bin/env python
|
||||||
|
|
||||||
set -e
|
import os
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
|
||||||
find_root() {
|
if 'CLOUD_INIT_TOP_D' in os.environ:
|
||||||
local topd
|
topd = os.path.realpath(os.environ.get('CLOUD_INIT_TOP_D'))
|
||||||
if [ -z "${CLOUD_INIT_TOP_D}" ]; then
|
else:
|
||||||
topd=$(cd "$(dirname "${0}")" && cd .. && pwd)
|
topd = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
|
||||||
else
|
|
||||||
topd=$(cd "${CLOUD_INIT_TOP_D}" && pwd)
|
|
||||||
fi
|
|
||||||
[ $? -eq 0 -a -f "${topd}/setup.py" ] || return
|
|
||||||
ROOT_DIR="$topd"
|
|
||||||
}
|
|
||||||
fail() { echo "$0:" "$@" 1>&2; exit 1; }
|
|
||||||
|
|
||||||
if ! find_root; then
|
for fname in ("setup.py", "ChangeLog"):
|
||||||
fail "Unable to locate 'setup.py' file that should " \
|
if not os.path.isfile(os.path.join(topd, fname)):
|
||||||
"exist in the cloud-init root directory."
|
sys.stderr.write("Unable to locate '%s' file that should "
|
||||||
fi
|
"exist in cloud-init root directory." % fname)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
CHNG_LOG="$ROOT_DIR/ChangeLog"
|
vermatch = re.compile(r"^[0-9]+[.][0-9]+[.][0-9]+:$")
|
||||||
|
|
||||||
if [ ! -e "$CHNG_LOG" ]; then
|
with open(os.path.join(topd, "ChangeLog"), "r") as fp:
|
||||||
fail "Unable to find 'ChangeLog' file located at '$CHNG_LOG'"
|
for line in fp:
|
||||||
fi
|
if vermatch.match(line):
|
||||||
|
sys.stdout.write(line.strip()[:-1] + "\n")
|
||||||
|
break
|
||||||
|
|
||||||
VERSION=$(sed -n '/^[0-9]\+[.][0-9]\+[.][0-9]\+:/ {s/://; p; :a;n; ba; }' \
|
sys.exit(0)
|
||||||
"$CHNG_LOG") &&
|
|
||||||
[ -n "$VERSION" ] ||
|
|
||||||
fail "failed to get version from '$CHNG_LOG'"
|
|
||||||
echo "$VERSION"
|
|
||||||
|
|
Loading…
Reference in New Issue