Skip to content

Commit 2ffb877

Browse files
committed
python: Rework filespec install script
* Support wildcards in install (`+`) paths * Add fourth parameter to set directory permissions If file permissions are given (third parameter), these will now apply to files only. * Add non-recursive set permissions command (`==`) * Be more strict about filespec format Blank lines and lines starting with `#` will be ignored. Other errors (unknown command, missing path parameter, etc.) will cause the script to exit. * Be more strict about ensuring paths exist for all commands * Avoid spawning subshells This also removes outdated filespec paths in the python3 package; these paths delete files that are no longer present. Signed-off-by: Jeffery To <[email protected]>
1 parent 329f9a1 commit 2ffb877

2 files changed

Lines changed: 149 additions & 54 deletions

File tree

Lines changed: 134 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,140 @@
11
#!/bin/sh
2-
set -e
3-
4-
process_filespec() {
5-
local src_dir="$1"
6-
local dst_dir="$2"
7-
local filespec="$3"
8-
echo "$filespec" | (
9-
IFS='|'
10-
while read fop fspec fperm; do
11-
local fop=`echo "$fop" | tr -d ' \t\n'`
12-
if [ "$fop" = "+" ]; then
13-
if [ ! -e "${src_dir}${fspec}" ]; then
14-
echo "File not found '${src_dir}${fspec}'"
15-
exit 1
16-
fi
17-
dpath=`dirname "$fspec"`
18-
if [ -z "$fperm" ]; then
19-
dperm=`stat -c "%a" ${src_dir}${dpath}`
20-
fi
21-
mkdir -p -m$dperm ${dst_dir}${dpath}
22-
echo "copying: '$fspec'"
23-
cp -fpR ${src_dir}${fspec} ${dst_dir}${dpath}/
24-
if [ -n "$fperm" ]; then
25-
chmod -R $fperm ${dst_dir}${fspec}
26-
fi
27-
elif [ "$fop" = "-" ]; then
28-
echo "removing: '$fspec'"
29-
rm -fR ${dst_dir}${fspec}
30-
elif [ "$fop" = "=" ]; then
31-
echo "setting permissions: '$fperm' on '$fspec'"
32-
chmod -R $fperm ${dst_dir}${fspec}
33-
fi
34-
done
35-
)
2+
3+
log() {
4+
printf '%s\n' "$*"
5+
}
6+
7+
error() {
8+
printf 'Error: %s\n' "$*" >&2
9+
}
10+
11+
path_exists() {
12+
local dir="$1"
13+
local path="$2"
14+
15+
[ -n "$(find "$dir"/$path -print -quit 2>/dev/null)" ]
16+
}
17+
18+
file_dir_chmod() {
19+
local dir="$1"
20+
local path="$2"
21+
local file_mode="$3"
22+
local dir_mode="$4"
23+
shift; shift; shift; shift;
24+
25+
if [ -n "$file_mode" ]; then
26+
find "$dir"/$path -type f "$@" -exec chmod "$file_mode" -- '{}' +
27+
fi
28+
29+
if [ -n "$dir_mode" ]; then
30+
find "$dir"/$path -type d "$@" -exec chmod "$dir_mode" -- '{}' +
31+
fi
3632
}
3733

38-
src_dir="$1"
39-
dst_dir="$2"
34+
src="$1"
35+
dest="$2"
4036
filespec="$3"
4137

42-
process_filespec "$src_dir" "$dst_dir" "$filespec" || {
43-
echo "process filespec error-ed"
38+
if [ -z "$src" ]; then
39+
error "Missing source directory"
4440
exit 1
45-
}
41+
fi
42+
if [ -z "$dest" ]; then
43+
error "Missing destination directory"
44+
exit 1
45+
fi
46+
47+
while IFS='|' read -r cmd path file_mode dir_mode; do
48+
49+
# trim whitespace
50+
51+
cmd="${cmd#"${cmd%%[![:space:]]*}"}"
52+
cmd="${cmd%"${cmd##*[![:space:]]}"}"
53+
54+
path="${path#"${path%%[![:space:]]*}"}"
55+
path="${path%"${path##*[![:space:]]}"}"
56+
57+
file_mode="${file_mode#"${file_mode%%[![:space:]]*}"}"
58+
file_mode="${file_mode%"${file_mode##*[![:space:]]}"}"
59+
60+
dir_mode="${dir_mode#"${dir_mode%%[![:space:]]*}"}"
61+
dir_mode="${dir_mode%"${dir_mode##*[![:space:]]}"}"
62+
63+
64+
if [ -z "$cmd" ] || [ "$cmd" != "${cmd#\#}" ]; then
65+
continue
66+
fi
67+
68+
if [ -z "$path" ]; then
69+
error "Missing path for \"$cmd\""
70+
exit 1
71+
fi
72+
73+
case "$cmd" in
74+
+)
75+
log "Copying: \"$path\""
76+
77+
if ! path_exists "$src" "$path"; then
78+
error "\"$src/$path\" not found"
79+
exit 1
80+
fi
81+
82+
dir="${path%/*}"
83+
mkdir -p "$dest/$dir"
84+
cp -fpR "$src"/$path "$dest/$dir/"
85+
86+
file_dir_chmod "$dest" "$path" "$file_mode" "$dir_mode"
87+
;;
88+
89+
-)
90+
log "Removing: \"$path\""
91+
92+
if ! path_exists "$dest" "$path"; then
93+
error "\"$dest/$path\" not found"
94+
exit 1
95+
fi
96+
97+
rm -fR -- "$dest"/$path
98+
;;
99+
100+
=)
101+
log "Setting recursive permissions \"${file_mode:-(none)}\"/\"${dir_mode:-(none)}\" on \"$path\""
102+
103+
if ! path_exists "$dest" "$path"; then
104+
error "\"$dest/$path\" not found"
105+
exit 1
106+
fi
107+
108+
if [ -z "$file_mode$dir_mode" ]; then
109+
error "Missing recursive permissions for \"$path\""
110+
exit 1
111+
fi
112+
113+
file_dir_chmod "$dest" "$path" "$file_mode" "$dir_mode"
114+
;;
115+
116+
==)
117+
log "Setting permissions \"${file_mode:-(none)}\"/\"${dir_mode:-(none)}\" on \"$path\""
118+
119+
if ! path_exists "$dest" "$path"; then
120+
error "\"$dest/$path\" not found"
121+
exit 1
122+
fi
123+
124+
if [ -z "$file_mode$dir_mode" ]; then
125+
error "Missing permissions for \"$path\""
126+
exit 1
127+
fi
128+
129+
file_dir_chmod "$dest" "$path" "$file_mode" "$dir_mode" -maxdepth 0
130+
;;
131+
132+
*)
133+
error "Unknown command \"$cmd\""
134+
exit 1
135+
;;
136+
esac
137+
138+
done << EOF
139+
$filespec
140+
EOF

lang/python/python3/Makefile

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,15 @@ define Py3BasePackage
121121
PYTHON3_PACKAGES_DEPENDS+=$(1)
122122
endif
123123
PYTHON3_LIB_FILES_DEL+=$(2)
124-
define Py3Package/$(1)/filespec
125-
ifneq ($(2),)
126-
$(subst $(space),$(newline),$(foreach lib_file,$(2),+|$(lib_file)))
127-
-|/usr/lib/python$(PYTHON3_VERSION)/*/test
128-
-|/usr/lib/python$(PYTHON3_VERSION)/*/tests
129-
endif
130-
endef
124+
ifeq ($(2),)
125+
Py3Package/$(1)/filespec=
126+
else
127+
define Py3Package/$(1)/filespec
128+
$(foreach lib_file,$(2),
129+
+|$(lib_file)
130+
)
131+
endef
132+
endif
131133
Py3Package/$(1)/install?=:
132134
endef
133135

@@ -240,24 +242,22 @@ PYTHON3_LIB_FILES_DEL+=$(PYTHON3_BASE_LIB_FILES)
240242

241243
define Py3Package/python3-base/filespec
242244
+|/usr/bin/python$(PYTHON3_VERSION)
243-
$(subst $(space),$(newline),$(foreach lib_file,$(PYTHON3_BASE_LIB_FILES),+|$(lib_file)))
245+
$(foreach lib_file,$(PYTHON3_BASE_LIB_FILES),
246+
+|$(lib_file)
247+
)
244248
endef
245249

246250
define Py3Package/python3-light/filespec
247251
+|/usr/lib/python$(PYTHON3_VERSION)
248252
-|/usr/lib/python$(PYTHON3_VERSION)/distutils/cygwinccompiler.py
249-
-|/usr/lib/python$(PYTHON3_VERSION)/distutils/command/wininst*
250253
-|/usr/lib/python$(PYTHON3_VERSION)/idlelib
251254
-|/usr/lib/python$(PYTHON3_VERSION)/tkinter
252255
-|/usr/lib/python$(PYTHON3_VERSION)/turtledemo
253-
-|/usr/lib/python$(PYTHON3_VERSION)/lib-dynload/_test*.so
254-
-|/usr/lib/python$(PYTHON3_VERSION)/pdb.doc
255-
-|/usr/lib/python$(PYTHON3_VERSION)/test
256256
-|/usr/lib/python$(PYTHON3_VERSION)/webbrowser.py
257-
-|/usr/lib/python$(PYTHON3_VERSION)/*/test
258-
-|/usr/lib/python$(PYTHON3_VERSION)/*/tests
259257
-|/usr/lib/python$(PYTHON3_VERSION)/_osx_support.py
260-
$(subst $(space),$(newline),$(foreach lib_file,$(PYTHON3_LIB_FILES_DEL),-|$(lib_file)))
258+
$(foreach lib_file,$(filter /usr/lib/python$(PYTHON3_VERSION)/%,$(PYTHON3_LIB_FILES_DEL)),
259+
-|$(lib_file)
260+
)
261261
endef
262262

263263
define Package/libpython3/install

0 commit comments

Comments
 (0)