dev-qt/qttest: stable 5.14.2 for ppc, bug #719732
[gentoo.git] / eclass / vcs-snapshot.eclass
1 # Copyright 1999-2019 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: vcs-snapshot.eclass
5 # @MAINTAINER:
6 # mgorny@gentoo.org
7 # @SUPPORTED_EAPIS: 0 1 2 3 4 5 6 7
8 # @BLURB: support eclass for unpacking VCS snapshot tarballs
9 # @DESCRIPTION:
10 # THIS ECLASS IS NOT NECESSARY FOR MODERN GITHUB AND GITLAB SNAPSHOTS.
11 # THEIR DIRECTORY STRUCTURE IS ENTIRELY PREDICTABLE, SO UPDATE YOUR
12 # EBUILD TO USE /ARCHIVE/ URI AND SET S IF NECESSARY.
13 #
14 # This eclass provides a convenience src_unpack() which does unpack all
15 # the tarballs in SRC_URI to locations matching their (local) names,
16 # discarding the original parent directory.
17 #
18 # The typical use case are VCS tag snapshots coming from BitBucket
19 # (but not GitHub or GitLab).  They have hash appended to the directory
20 # name which makes extracting them a painful experience.  But if you are
21 # using a SRC_URI arrow to rename them (which quite likely you have to
22 # do anyway), vcs-snapshot will just extract them into matching
23 # directories.
24 #
25 # Please note that this eclass handles only tarballs (.tar, .tar.gz,
26 # .tar.bz2 & .tar.xz).  For any other file format (or suffix) it will
27 # fall back to regular unpack.  Support for additional formats may be
28 # added in the future if necessary.
29 #
30 # @EXAMPLE:
31 #
32 # @CODE
33 # EAPI=7
34 # inherit vcs-snapshot
35 #
36 # SRC_URI="
37 #    https://bitbucket.org/foo/bar/get/${PV}.tar.bz2 -> ${P}.tar.bz2
38 #    https://bitbucket.org/foo/bar-otherstuff/get/${PV}.tar.bz2
39 #        -> ${P}-otherstuff.tar.bz2"
40 # @CODE
41 #
42 # and however the tarballs were originally packed, all files will appear
43 # in ${WORKDIR}/${P} and ${WORKDIR}/${P}-otherstuff respectively.
44
45 case ${EAPI:-0} in
46         0|1|2|3|4|5|6|7) ;;
47         *) die "vcs-snapshot.eclass API in EAPI ${EAPI} not yet established."
48 esac
49
50 EXPORT_FUNCTIONS src_unpack
51
52 # @FUNCTION: vcs-snapshot_src_unpack
53 # @DESCRIPTION:
54 # Extract all the archives from ${A}. The .tar, .tar.gz, .tar.bz2
55 # and .tar.xz archives will be unpacked to directories matching their
56 # local names. Other archive types will be passed down to regular
57 # unpack.
58 vcs-snapshot_src_unpack() {
59         debug-print-function ${FUNCNAME} "${@}"
60
61         local renamed_any=
62         local f
63
64         for f in ${A}
65         do
66                 case "${f}" in
67                         *.tar|*.tar.gz|*.tar.bz2|*.tar.xz)
68                                 local destdir=${WORKDIR}/${f%.tar*}
69
70                                 debug-print "${FUNCNAME}: unpacking ${f} to ${destdir}"
71
72                                 local l topdirs=()
73                                 while read -r l; do
74                                         topdirs+=( "${l}" )
75                                 done < <(tar -t -f "${DISTDIR}/${f}" | cut -d/ -f1 | sort -u)
76                                 if [[ ${#topdirs[@]} -gt 1 ]]; then
77                                         eerror "The archive ${f} contains multiple or no top directory."
78                                         eerror "It is impossible for vcs-snapshot to unpack this correctly."
79                                         eerror "Top directories found:"
80                                         local d
81                                         for d in "${topdirs[@]}"; do
82                                                 eerror "    ${d}"
83                                         done
84                                         die "${FUNCNAME}: Invalid directory structure in archive ${f}"
85                                 fi
86                                 [[ ${topdirs[0]} != ${f%.tar*} ]] && renamed_any=1
87
88                                 mkdir "${destdir}" || die
89                                 # -o (--no-same-owner) to avoid restoring original owner
90                                 einfo "Unpacking ${f}"
91                                 tar -C "${destdir}" -x -o --strip-components 1 \
92                                         -f "${DISTDIR}/${f}" || die
93                                 ;;
94                         *)
95                                 debug-print "${FUNCNAME}: falling back to unpack for ${f}"
96
97                                 # fall back to the default method
98                                 unpack "${f}"
99                                 ;;
100                 esac
101         done
102
103         if [[ ! ${renamed_any} ]]; then
104                 local w=eerror
105                 [[ ${EAPI} == [0123456] ]] && w=eqawarn
106                 "${w}" "${FUNCNAME} did not find any archives that needed renaming."
107                 "${w}" "Please verify that its usage is really necessary, and remove"
108                 "${w}" "the inherit if it is not."
109
110                 [[ ${w} == eerror ]] && die "${FUNCNAME}: Unnecessary usage detected"
111         fi
112 }