aboutsummaryrefslogtreecommitdiff
path: root/lang/python/python-package-install.sh
blob: 4e7fb7af5b9cf3982bd9e527bb2a0e84478d225c (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
#!/bin/sh

log() {
	printf '%s\n' "$*"
}

error() {
	printf 'Error: %s\n' "$*" >&2
}

path_exists() {
	local dir="$1"
	local path="$2"

	[ -n "$(find "$dir"/$path -print -quit 2>/dev/null)" ]
}

file_dir_chmod() {
	local dir="$1"
	local path="$2"
	local file_mode="$3"
	local dir_mode="$4"
	shift; shift; shift; shift;

	if [ -n "$file_mode" ]; then
		find "$dir"/$path -type f "$@" -exec chmod "$file_mode" -- '{}' +
	fi

	if [ -n "$dir_mode" ]; then
		find "$dir"/$path -type d "$@" -exec chmod "$dir_mode" -- '{}' +
	fi
}

src="$1"
dest="$2"
filespec="$3"

if [ -z "$src" ]; then
	error "Missing source directory"
	exit 1
fi
if [ -z "$dest" ]; then
	error "Missing destination directory"
	exit 1
fi

while IFS='|' read -r cmd path file_mode dir_mode; do

	# trim whitespace

	cmd="${cmd#"${cmd%%[![:space:]]*}"}"
	cmd="${cmd%"${cmd##*[![:space:]]}"}"

	path="${path#"${path%%[![:space:]]*}"}"
	path="${path%"${path##*[![:space:]]}"}"

	file_mode="${file_mode#"${file_mode%%[![:space:]]*}"}"
	file_mode="${file_mode%"${file_mode##*[![:space:]]}"}"

	dir_mode="${dir_mode#"${dir_mode%%[![:space:]]*}"}"
	dir_mode="${dir_mode%"${dir_mode##*[![:space:]]}"}"


	if [ -z "$cmd" ] || [ "$cmd" != "${cmd#\#}" ]; then
		continue
	fi

	if [ -z "$path" ]; then
		error "Missing path for \"$cmd\""
		exit 1
	fi

	case "$cmd" in
	+)
		log "Copying: \"$path\""

		if ! path_exists "$src" "$path"; then
			error "\"$src/$path\" not found"
			exit 1
		fi

		dir="${path%/*}"
		mkdir -p "$dest/$dir"
		cp -fpR "$src"/$path "$dest/$dir/"

		file_dir_chmod "$dest" "$path" "$file_mode" "$dir_mode"
		;;

	-)
		log "Removing: \"$path\""

		if ! path_exists "$dest" "$path"; then
			error "\"$dest/$path\" not found"
			exit 1
		fi

		rm -fR -- "$dest"/$path
		;;

	=)
		log "Setting recursive permissions \"${file_mode:-(none)}\"/\"${dir_mode:-(none)}\" on \"$path\""

		if ! path_exists "$dest" "$path"; then
			error "\"$dest/$path\" not found"
			exit 1
		fi

		if [ -z "$file_mode$dir_mode" ]; then
			error "Missing recursive permissions for \"$path\""
			exit 1
		fi

		file_dir_chmod "$dest" "$path" "$file_mode" "$dir_mode"
		;;

	==)
		log "Setting permissions \"${file_mode:-(none)}\"/\"${dir_mode:-(none)}\" on \"$path\""

		if ! path_exists "$dest" "$path"; then
			error "\"$dest/$path\" not found"
			exit 1
		fi

		if [ -z "$file_mode$dir_mode" ]; then
			error "Missing permissions for \"$path\""
			exit 1
		fi

		file_dir_chmod "$dest" "$path" "$file_mode" "$dir_mode" -maxdepth 0
		;;

	*)
		error "Unknown command \"$cmd\""
		exit 1
		;;
	esac

done << EOF
$filespec
EOF