summaryrefslogtreecommitdiff
path: root/eclass/vcs-snapshot.eclass
blob: 1b7299b92a3b14412dcd2105e67473be097fa20a (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
# Copyright 1999-2022 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

# @ECLASS: vcs-snapshot.eclass
# @MAINTAINER:
# mgorny@gentoo.org
# @SUPPORTED_EAPIS: 6 7 8
# @BLURB: support eclass for unpacking VCS snapshot tarballs
# @DESCRIPTION:
# THIS ECLASS IS NOT NECESSARY FOR MODERN GITHUB AND GITLAB SNAPSHOTS.
# THEIR DIRECTORY STRUCTURE IS ENTIRELY PREDICTABLE, SO UPDATE YOUR
# EBUILD TO USE /ARCHIVE/ URI AND SET S IF NECESSARY.
#
# This eclass provides a convenience src_unpack() which does unpack all
# the tarballs in SRC_URI to locations matching their (local) names,
# discarding the original parent directory.
#
# The typical use case are VCS tag snapshots coming from BitBucket
# (but not GitHub or GitLab).  They have hash appended to the directory
# name which makes extracting them a painful experience.  But if you are
# using a SRC_URI arrow to rename them (which quite likely you have to
# do anyway), vcs-snapshot will just extract them into matching
# directories.
#
# Please note that this eclass handles only tarballs (.tar, .tar.gz,
# .tar.bz2 & .tar.xz).  For any other file format (or suffix) it will
# fall back to regular unpack.  Support for additional formats may be
# added in the future if necessary.
#
# @EXAMPLE:
#
# @CODE
# EAPI=7
# inherit vcs-snapshot
#
# SRC_URI="
#    https://bitbucket.org/foo/bar/get/${PV}.tar.bz2 -> ${P}.tar.bz2
#    https://bitbucket.org/foo/bar-otherstuff/get/${PV}.tar.bz2
#        -> ${P}-otherstuff.tar.bz2"
# @CODE
#
# and however the tarballs were originally packed, all files will appear
# in ${WORKDIR}/${P} and ${WORKDIR}/${P}-otherstuff respectively.

case ${EAPI} in
	6) inherit eqawarn ;;
	7|8) ;;
	*) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
esac

if [[ ! ${_VCS_SNAPSHOT_ECLASS} ]]; then
_VCS_SNAPSHOT_ECLASS=1

# @FUNCTION: vcs-snapshot_src_unpack
# @DESCRIPTION:
# Extract all the archives from ${A}. The .tar, .tar.gz, .tar.bz2
# and .tar.xz archives will be unpacked to directories matching their
# local names. Other archive types will be passed down to regular
# unpack.
vcs-snapshot_src_unpack() {
	debug-print-function ${FUNCNAME} "${@}"

	local renamed_any=
	local f

	for f in ${A}
	do
		case "${f}" in
			*.tar|*.tar.gz|*.tar.bz2|*.tar.xz)
				local destdir=${WORKDIR}/${f%.tar*}

				debug-print "${FUNCNAME}: unpacking ${f} to ${destdir}"

				local l topdirs=()
				while read -r l; do
					topdirs+=( "${l}" )
				done < <(tar -t -f "${DISTDIR}/${f}" | cut -d/ -f1 | sort -u)
				if [[ ${#topdirs[@]} -gt 1 ]]; then
					eerror "The archive ${f} contains multiple or no top directory."
					eerror "It is impossible for vcs-snapshot to unpack this correctly."
					eerror "Top directories found:"
					local d
					for d in "${topdirs[@]}"; do
						eerror "    ${d}"
					done
					die "${FUNCNAME}: Invalid directory structure in archive ${f}"
				fi
				[[ ${topdirs[0]} != ${f%.tar*} ]] && renamed_any=1

				mkdir "${destdir}" || die
				# -o (--no-same-owner) to avoid restoring original owner
				einfo "Unpacking ${f}"
				tar -C "${destdir}" -x -o --strip-components 1 \
					-f "${DISTDIR}/${f}" || die
				;;
			*)
				debug-print "${FUNCNAME}: falling back to unpack for ${f}"

				# fall back to the default method
				unpack "${f}"
				;;
		esac
	done

	if [[ ! ${renamed_any} ]]; then
		local w=eerror
		[[ ${EAPI} == 6 ]] && w=eqawarn
		"${w}" "${FUNCNAME} did not find any archives that needed renaming."
		"${w}" "Please verify that its usage is really necessary, and remove"
		"${w}" "the inherit if it is not."

		[[ ${w} == eerror ]] && die "${FUNCNAME}: Unnecessary usage detected"
	fi
}

fi

EXPORT_FUNCTIONS src_unpack