apkbuild-fixer 5.16 KB
Newer Older
Leo's avatar
Leo committed
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
#!/bin/sh
remove_line() {
	[ -z "$1" ] || [ -z "$2" ] && return 0
	# path to file first, lines as a whitespace delimited string second
	for l in $2; do
		sed -i "${l}d" "$1"
		[ -z "$3" ] && return 0
		# While the line we get is not empty, remove it, this serves to
		# remove any follow-up lines after we remove the first one.
		# We certainly don't want stray empty lines
		while ! sed -n "${l}p" "$1" | grep -q '.'; do
			sed -i "${l}d" "$1"
		done
	done
}

remove_return_1() {
	[ -z "$1" ] || [ -z "$2" ] && return 0
	# path to file first, lines as a whitespace delimited string second
	for l in $2; do
		prev_line=$(( l - 1 )) # The line before the one we should delete

		# Check if the line we were given contains only || return 1
		if sed -n "${l}p" "$1" | grep -q '^[[:blank:]]*|| return 1[[:blank:]]*$'; then
			# If that is true then delete the line outright, no point in keeping
			# an empty line around
			sed -i "${l}d" "$1"

			# Remove backslash of previous line, this is to deal with cases
			# where return is in a line of its own and the command is in the
			# previous line, commonly used in autoconf scripts, as an example:
			# autoconf \
			#	--prefix=/usr \
			#	|| return 1
			sed -i "${prev_line}s/\\\\$//g" "$1"
		else
			# Remove the || return 1 from the specific line
			sed -i "${l}s/|| return 1//g" "$1"
		fi

	done
}

# Rename _builddir to builddir, both the variable in the global scope
# and any other occurrences
rename__builddir() {
	sed -i 's/_builddir/builddir/g' "$1"
}

# Self-explanatory, remove any whitespace after ending text
remove_trailing_whitespace() {
	sed -i 's/[[:space:]]\+$//' "$1"
}

# Given a file $1 and lines $2, rewrite the function which is composed of
# letters and numbers
rewrite_function() {
	[ -z "$1" ] || [ -z "$2" ] && return 0
	for l in $2; do
		sed -ri " {$l s|^([A-Za-z0-9]*).*|\\1() {|g }" "$1"
	done
}

64
65
66
67
68
69
# Remove braces from a variable in a line
remove_braces() {
	[ -z "$1" ] || [ -z "$2" ] && return 0
	printf "%s\\n" "$2" | while read -r line variable; do
		# Make variable without braces
		unbraced_variable="$(printf "%s\\n" "$variable" | sed 's|[{}]||g')"
Leo's avatar
Leo committed
70
71
72
73
74
75
76
77
		sed -i "{$line s|$variable|$unbraced_variable|g }" "$1"
	done
}

remove_pkgname_from_source() {
	[ -z "$1" ] || [ -z "$2" ] && return 0
	for l in $2; do
		sed -i "{$l s|\$pkgname|$pkgname|g }" "$1"
78
79
80
	done
}

Leo's avatar
Leo committed
81
82
83
for apkbuild; do
	if [ -f "$apkbuild" ]; then

Leo's avatar
Leo committed
84
85
86
87
88
89
	# Source the apkbuild, required to get pkgname
	srcdir="" . "$apkbuild" || {
		echo "Failed to source APKBUILD in '$apkbuild'" ;
		exit 1;
	}

Leo's avatar
Leo committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
	# Remove trailing whitespace
	remove_trailing_whitespace "$apkbuild"
	
	# Before running apkbuild-lint, perform rename on builddir
	grep -q "^_builddir=" "$apkbuild" && rename__builddir "$apkbuild"
	
	# Get all the violations
	violations="$(apkbuild-lint "$apkbuild")"
	[ -z "$violations" ] && return 0
	
	# Rewrite functions (no chance of deleting line)
	rewrite_functions="$(echo "$violations" | \
						 grep -E '\[AL(10|11)\]'| \
						 cut -d : -f4 | \
						 sort -rV | \
						 paste -sd ' ')"
106
107
108
109
110

	remove_braces="$(echo "$violations" | \
					 grep -F '[AL32]' | \
					 awk -F: '{print $4" "$6}' | \
					 sort -rV)"
Leo's avatar
Leo committed
111
112
	
	[ -n "$rewrite_functions" ] && rewrite_function "$apkbuild" "$rewrite_functions"
113
	[ -n "$remove_braces" ] && remove_braces "$apkbuild" "$remove_braces"
Leo's avatar
Leo committed
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
	
	# Remove || return 1 (might delete lines)
	return_1_lines="$(echo "$violations" | \
					  grep -F '[AL2]' | \
					  cut -d : -f4 | \
					  sort -rV | \
					  paste -sd ' ')"
	
	[ -n "$return_1_lines" ] && remove_return_1 "$apkbuild" "$return_1_lines"
	
	# Get all violations again now that lines were deleted and it can potentially cause lines
	# to be misdeleted
	violations="$(apkbuild-lint "$apkbuild")"
	[ -z "$violations" ] && return 0
	
	# Remove lines with empty variables, greedily (will certainly remove lines)
	removable_lines="$(echo "$violations" | \
					   grep -E '\[AL(5|13)\]'| \
					   cut -d : -f4 | \
					   sort -rV | \
					   paste -sd ' ')"
	
	# Third argument enables greedyness
	[ -n "$removable_lines" ] && remove_line "$apkbuild" "$removable_lines" greedy
	
	# Get all violations again now that lines were deleted and it can potentially cause lines
	# to be misdeleted
	violations="$(apkbuild-lint "$apkbuild")"
	[ -z "$violations" ] && return 0
	
	# Remove lines with default builddir (will certainly remove lines)
	removable_lines="$(echo "$violations" | \
					   grep '\[AL[1]\]'| \
					   cut -d : -f4 | \
					   sort -rV | \
					   paste -sd ' ')"
	
	[ -n "$removable_lines" ] && remove_line "$apkbuild" "$removable_lines"
	
	# Get all violations again now that lines were deleted and it can potentially cause lines
	# to be misdeleted
	violations="$(apkbuild-lint "$apkbuild")"
	[ -z "$violations" ] && return 0
	
	# Rewrite functions
	rewrite_functions="$(echo "$violations" | \
						 grep -E '\[AL(10|11)\]'| \
						 cut -d : -f4 | \
						 sort -rV | \
						 paste -sd ' ')"
Leo's avatar
Leo committed
164

Leo's avatar
Leo committed
165
	[ -n "$rewrite_functions" ] && rewrite_function "$apkbuild" "$rewrite_functions"
Leo's avatar
Leo committed
166
167
168
169
170
171
172
173
174
175

	# Replace $pkgname
	pkgname_in_source="$(echo "$violations" | \
						 grep '\[AL29\]' | \
						 cut -d : -f4 | \
						 sort -rV | \
						 paste -sd ' ')"

	[ -n "$pkgname_in_source" ] && remove_pkgname_from_source "$apkbuild" "$pkgname_in_source"

Leo's avatar
Leo committed
176
177
	fi
done