apkbuild-fixer 5.37 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 81
		# The command above might replace the $pkgname in
		# '$pkgname-$pkgver.tar.gz::' which should not be
		# replaced, so re-add it if that is the case
		sed -r -i "{$l s|$pkgname-\\\$pkgver(.*)::|\$pkgname-\$pkgver\1::|g }" "$1"
82 83 84
	done
}

Leo's avatar
Leo committed
85 86 87
for apkbuild; do
	if [ -f "$apkbuild" ]; then

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

Leo's avatar
Leo committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107
	# 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 | \
108 109
						 sort -r | \
						 tr '\n' ' ')"
110 111 112 113

	remove_braces="$(echo "$violations" | \
					 grep -F '[AL32]' | \
					 awk -F: '{print $4" "$6}' | \
114
					 sort -r)"
Leo's avatar
Leo committed
115 116
	
	[ -n "$rewrite_functions" ] && rewrite_function "$apkbuild" "$rewrite_functions"
117
	[ -n "$remove_braces" ] && remove_braces "$apkbuild" "$remove_braces"
Leo's avatar
Leo committed
118 119 120 121 122
	
	# Remove || return 1 (might delete lines)
	return_1_lines="$(echo "$violations" | \
					  grep -F '[AL2]' | \
					  cut -d : -f4 | \
123 124
					  sort -r | \
					  tr '\n' ' ')"
Leo's avatar
Leo committed
125 126 127 128 129 130 131 132 133 134 135 136
	
	[ -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 | \
137 138
					   sort -r | \
					   tr '\n' ' ')"
Leo's avatar
Leo committed
139 140 141 142 143 144 145 146
	
	# 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
147

Leo's avatar
Leo committed
148 149 150 151
	# Remove lines with default builddir (will certainly remove lines)
	removable_lines="$(echo "$violations" | \
					   grep '\[AL[1]\]'| \
					   cut -d : -f4 | \
152 153
					   sort -r | \
					   tr '\n' ' ')"
Leo's avatar
Leo committed
154 155 156 157 158 159 160 161 162 163 164 165
	
	[ -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 | \
166 167
						 sort -r | \
						 tr '\n' ' ')"
Leo's avatar
Leo committed
168

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

	# Replace $pkgname
	pkgname_in_source="$(echo "$violations" | \
						 grep '\[AL29\]' | \
						 cut -d : -f4 | \
175 176
						 sort -r | \
						 tr '\n' ' ')"
Leo's avatar
Leo committed
177 178 179

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

Leo's avatar
Leo committed
180 181
	fi
done