aboutsummaryrefslogtreecommitdiffstats
path: root/do.sh
blob: a6267ba6c721fb96ca4a05f993eb057927056df3 (plain) (blame)
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
#!/bin/sh

REPOROOT="$(git rev-parse --show-toplevel)"
SCRIPTPATH="$(dirname $(readlink -f "$0"))"
cd "$SCRIPTPATH"

makefile="
all: service/.copyts

service/.copyts: src/*
	bash do.sh cleansrc src service/src
	touch service/.copyts
"

shopt -s expand_aliases
alias pushd="pushd &>/dev/null"
alias popd="popd &>/dev/null"

if [ "$1" == "compose" ]; then
	# ensure built service files are up to date
	make --file <(echo "$makefile")

	# forward commands to compose
	pushd service
	docker-compose ${@:2}
	popd
elif [ "$1" == "cleansrc" ]; then
	if [ $# -lt 3 ]; then
		echo "USAGE: do.sh cleansrc <SRC> <DST>"
		exit 0
	fi

	# copy files
	src="$2"
	dst="$3"
	[ -e "$dst" ] && rm -rf "$dst"
	mkdir -p "$dst"
	cp -r "$src"/{*.c,*.h,Makefile,msgs,.gitignore} "$dst"

	# strip comments
	find "$dst" | while read path; do
		if [ -f "$path" ]; then
			if [ ! -z $(echo "$path" | grep '.[hc]$') ]; then
				sed -i -e 's/^\s*\/\*.*\*\/\s*$//g' "$path"     # remove /* */ style comments
				sed -i -e 's/\s*\/\*.*\*\/\s*/ /g' "$path"      # remove /* */ style comments
				sed -i -e 's/\s*\/\/.*//g' "$path"              # remove // style comments
				sed -i -e ':a;N;$!ba;s/\n\{3,\}/\n\n/g' "$path" # collapse multiple newlines
				sed -i -e 's/fprintf(\s*stderr\s*,\s*/printf(/g' "$path" # replace fprintf stderr
			elif [ "$(basename "$path")" == "Makefile" ]; then
				sed -i -e 's/\s*#.*//g' "$path"                 # remove # style comments
				sed -i -e ':a;N;$!ba;s/\n\{3,\}/\n\n/g' "$path" # collapse multiple newlines
			fi
		fi
	done

	# apply patches if requested
	if [ ! -z "$PATCHED" ]; then
		pushd "$dst"
		git apply "$REPOROOT/src/patches/"*.diff
		popd
	fi
elif [ "$1" == "test" ]; then
	SRCDIR="$PWD/service/src" DATADIR="$PWD/service/data" bash "tests/test.sh" ${@:2}
elif [ "$1" == "make" ]; then
	make -C service/src
elif [ "$1" == "ci-test" ]; then
	cd "$REPOROOT"

	docker-compose -f service/docker-compose.yml up --build -d -V
	docker-compose -f checker/docker-compose.yml up --build -d -V

	if [ -z "$ADDRESS" ]; then
		echo "Specify your local network address via the ADDRESS var"
		exit 1
	fi
	export ENOCHECKER_TEST_CHECKER_ADDRESS=$ADDRESS
	export ENOCHECKER_TEST_CHECKER_PORT=9091
	export ENOCHECKER_TEST_SERVICE_ADDRESS=$ADDRESS
	export ENOCHECKER_TEST_SERVICE_PORT=9090
	enochecker_test

	docker-compose -f service/docker-compose.yml logs --no-color --tail=1000 > /tmp/ci-test-service.log
	docker-compose -f checker/docker-compose.yml logs --no-color --tail=1000 > /tmp/ci-test-checker.log

	docker-compose -f service/docker-compose.yml down
	docker-compose -f checker/docker-compose.yml down
elif [ "$1" == "host-socat" ]; then
	RESULTDIR=service/data/uploads socat -x -v -T180 -s TCP-LISTEN:9090,nodelay,reuseaddr,fork \
		EXEC:./run-proxy.sh,raw,pty,echo=0,stderr
elif [ "$1" == "checker-local" ]; then
	cd checker
	if [ -z "$(docker ps | grep stldoctor-mongo)" ]; then
		docker-compose down -v
		docker-compose up -d stldoctor-mongo
	fi

	export MONGO_ENABLED=1
	export MONGO_HOST=localhost
	export MONGO_PORT=27017
	export MONGO_USER=stldoctor_checker
	export MONGO_PASSWORD=stldoctor_checker

	python3 -u src/checker.py $@
elif [ "$1" == "host-ncat" ]; then
	RESULTDIR=service/data/uploads ncat -v -k -l -p 9090 \
		-m 200 --no-shutdown -w10s -i180s -c "./run-proxy.sh"
elif [ "$1" == "parse-log" ]; then
	python3 -c '
#!/usr/bin/env python3

import jsons, sys

for l in open(sys.argv[1]).read().split("\n"):
	if "##ENOLOGMESSAGE" not in l: continue
	l = l.split("##ENOLOGMESSAGE ", 1)[1]
	jmsg = jsons.loads(l)
	print("[[ %s #%s ]] " % (jmsg["method"], jmsg["variantId"]), end="")
	print(jmsg["message"] + "\n--------")
	' "$2"
else
	echo "USAGE: do.sh (compose|test|cleansrc) [args..]"
	echo "EXAMPLES:"
	echo "    do.sh compose up --build       # setup src and start the container"
	echo "    do.sh test stl-upload test.stl # test the upload of STL files"
	echo "    do.sh make                     # build local binary in src"
	echo "    do.sh cleansrc <src> <dst>     # post-process source files for release"
	echo "                                   # if \$PATCHED is set, patches are applied"
fi