app-crypt/glep63-check: Bump to v8
[gentoo.git] / eclass / mercurial.eclass
1 # Copyright 1999-2014 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: mercurial.eclass
5 # @MAINTAINER:
6 # Christoph Junghans <junghans@gentoo.org>
7 # @AUTHOR:
8 # Next gen author: Krzysztof Pawlik <nelchael@gentoo.org>
9 # Original author: Aron Griffis <agriffis@gentoo.org>
10 # @BLURB: This eclass provides generic mercurial fetching functions
11 # @DESCRIPTION:
12 # This eclass provides generic mercurial fetching functions. To fetch sources
13 # from mercurial repository just set EHG_REPO_URI to correct repository URI. If
14 # you need to share single repository between several ebuilds set EHG_PROJECT to
15 # project name in all of them.
16
17 inherit eutils
18
19 EXPORT_FUNCTIONS src_unpack
20
21 DEPEND="dev-vcs/mercurial"
22
23 # @ECLASS-VARIABLE: EHG_REPO_URI
24 # @DESCRIPTION:
25 # Mercurial repository URI.
26
27 # @ECLASS-VARIABLE: EHG_REVISION
28 # @DESCRIPTION:
29 # Create working directory for specified revision, defaults to default.
30 #
31 # EHG_REVISION is passed as a value for --updaterev parameter, so it can be more
32 # than just a revision, please consult `hg help revisions' for more details.
33 : ${EHG_REVISION:="default"}
34
35 # @ECLASS-VARIABLE: EHG_STORE_DIR
36 # @DESCRIPTION:
37 # Mercurial sources store directory. Users may override this in /etc/portage/make.conf
38 [[ -z "${EHG_STORE_DIR}" ]] && EHG_STORE_DIR="${PORTAGE_ACTUAL_DISTDIR:-${DISTDIR}}/hg-src"
39
40 # @ECLASS-VARIABLE: EHG_PROJECT
41 # @DESCRIPTION:
42 # Project name.
43 #
44 # This variable default to $PN, but can be changed to allow repository sharing
45 # between several ebuilds.
46 [[ -z "${EHG_PROJECT}" ]] && EHG_PROJECT="${PN}"
47
48 # @ECLASS-VARIABLE: EGIT_CHECKOUT_DIR
49 # @DESCRIPTION:
50 # The directory to check the hg sources out to.
51 #
52 # EHG_CHECKOUT_DIR=${S}
53
54 # @ECLASS-VARIABLE: EHG_QUIET
55 # @DESCRIPTION:
56 # Suppress some extra noise from mercurial, set it to 'ON' to be quiet.
57 : ${EHG_QUIET:="OFF"}
58 [[ "${EHG_QUIET}" == "ON" ]] && EHG_QUIET_CMD_OPT="--quiet"
59
60 # @ECLASS-VARIABLE: EHG_CONFIG
61 # @DESCRIPTION:
62 # Extra config option to hand to hg clone/pull
63
64 # @ECLASS-VARIABLE: EHG_CLONE_CMD
65 # @DESCRIPTION:
66 # Command used to perform initial repository clone.
67 [[ -z "${EHG_CLONE_CMD}" ]] && EHG_CLONE_CMD="hg clone ${EHG_CONFIG:+--config ${EHG_CONFIG}} ${EHG_QUIET_CMD_OPT} --pull --noupdate"
68
69 # @ECLASS-VARIABLE: EHG_PULL_CMD
70 # @DESCRIPTION:
71 # Command used to update repository.
72 [[ -z "${EHG_PULL_CMD}" ]] && EHG_PULL_CMD="hg pull ${EHG_CONFIG:+--config ${EHG_CONFIG}} ${EHG_QUIET_CMD_OPT}"
73
74 # @ECLASS-VARIABLE: EHG_OFFLINE
75 # @DESCRIPTION:
76 # Set this variable to a non-empty value to disable the automatic updating of
77 # a mercurial source tree. This is intended to be set outside the ebuild by
78 # users.
79 EHG_OFFLINE="${EHG_OFFLINE:-${EVCS_OFFLINE}}"
80
81 # @FUNCTION: mercurial_fetch
82 # @USAGE: [repository_uri] [module] [sourcedir]
83 # @DESCRIPTION:
84 # Clone or update repository.
85 #
86 # If repository URI is not passed it defaults to EHG_REPO_URI, if module is
87 # empty it defaults to basename of EHG_REPO_URI, sourcedir defaults to
88 # EHG_CHECKOUT_DIR, which defaults to S.
89
90 mercurial_fetch() {
91         debug-print-function ${FUNCNAME} "${@}"
92
93         has "${EAPI:-0}" 0 1 2 && ! use prefix && EPREFIX=
94
95         EHG_REPO_URI=${1-${EHG_REPO_URI}}
96         [[ -z "${EHG_REPO_URI}" ]] && die "EHG_REPO_URI is empty"
97
98         local module="${2-$(basename "${EHG_REPO_URI}")}"
99         local sourcedir="${3:-${EHG_CHECKOUT_DIR:-${S}}}"
100
101         # Should be set but blank to prevent using $HOME/.hgrc
102         export HGRCPATH=
103
104         # Check ${EHG_STORE_DIR} directory:
105         addwrite "$(dirname "${EHG_STORE_DIR}")" || die "addwrite failed"
106         if [[ ! -d "${EHG_STORE_DIR}" ]]; then
107                 mkdir -p "${EHG_STORE_DIR}" || die "failed to create ${EHG_STORE_DIR}"
108                 chmod -f g+rw "${EHG_STORE_DIR}" || \
109                         die "failed to chown ${EHG_STORE_DIR}"
110         fi
111
112         # Create project directory:
113         mkdir -p "${EHG_STORE_DIR}/${EHG_PROJECT}" || \
114                 die "failed to create ${EHG_STORE_DIR}/${EHG_PROJECT}"
115         chmod -f g+rw "${EHG_STORE_DIR}/${EHG_PROJECT}" || \
116                 echo "Warning: failed to chmod g+rw ${EHG_PROJECT}"
117         pushd "${EHG_STORE_DIR}/${EHG_PROJECT}" > /dev/null || \
118                 die "failed to cd to ${EHG_STORE_DIR}/${EHG_PROJECT}"
119
120         # Clone/update repository:
121         if [[ ! -d "${module}" ]]; then
122                 einfo "Cloning ${EHG_REPO_URI} to ${EHG_STORE_DIR}/${EHG_PROJECT}/${module}"
123                 ${EHG_CLONE_CMD} "${EHG_REPO_URI}" "${module}" || {
124                         rm -rf "${module}"
125                         die "failed to clone ${EHG_REPO_URI}"
126                 }
127         elif [[ -z "${EHG_OFFLINE}" ]]; then
128                 einfo "Updating ${EHG_STORE_DIR}/${EHG_PROJECT}/${module} from ${EHG_REPO_URI}"
129                 pushd "${module}" > /dev/null || die "failed to cd to ${module}"
130                 ${EHG_PULL_CMD} "${EHG_REPO_URI}" || die "update failed"
131                 popd > /dev/null || die
132         fi
133         popd > /dev/null || die
134
135         # Checkout working copy:
136         einfo "Creating working directory in ${sourcedir} (target revision: ${EHG_REVISION})"
137         mkdir -p "${sourcedir}" || die "failed to create ${sourcedir}"
138         hg clone \
139                 ${EHG_QUIET_CMD_OPT} \
140                 --updaterev="${EHG_REVISION}" \
141                 ${EHG_CONFIG:+--config ${EHG_CONFIG}} \
142                 "${EHG_STORE_DIR}/${EHG_PROJECT}/${module}" \
143                 "${sourcedir}" || die "hg clone failed"
144         # An exact revision helps a lot for testing purposes, so have some output...
145         # id           num  branch
146         # fd6e32d61721 6276 default
147         local HG_REVDATA=($(hg identify -b -i "${sourcedir}"))
148         export HG_REV_ID=${HG_REVDATA[0]}
149         local HG_REV_BRANCH=${HG_REVDATA[1]}
150         einfo "Work directory: ${sourcedir} global id: ${HG_REV_ID} (was ${EHG_REVISION} branch: ${HG_REV_BRANCH}"
151 }
152
153 # @FUNCTION: mercurial_bootstrap
154 # @INTERNAL
155 # @DESCRIPTION:
156 # Internal function that runs bootstrap command on unpacked source.
157 mercurial_bootstrap() {
158         debug-print-function ${FUNCNAME} "$@"
159
160         # @ECLASS-VARIABLE: EHG_BOOTSTRAP
161         # @DESCRIPTION:
162         # Command to be executed after checkout and clone of the specified
163         # repository.
164         if [[ ${EHG_BOOTSTRAP} ]]; then
165                 pushd "${S}" > /dev/null
166                 einfo "Starting bootstrap"
167
168                 if [[ -f ${EHG_BOOTSTRAP} ]]; then
169                         # we have file in the repo which we should execute
170                         debug-print "${FUNCNAME}: bootstraping with file \"${EHG_BOOTSTRAP}\""
171
172                         if [[ -x ${EHG_BOOTSTRAP} ]]; then
173                                 eval "./${EHG_BOOTSTRAP}" \
174                                         || die "${FUNCNAME}: bootstrap script failed"
175                         else
176                                 eerror "\"${EHG_BOOTSTRAP}\" is not executable."
177                                 eerror "Report upstream, or bug ebuild maintainer to remove bootstrap command."
178                                 die "\"${EHG_BOOTSTRAP}\" is not executable"
179                         fi
180                 else
181                         # we execute some system command
182                         debug-print "${FUNCNAME}: bootstraping with commands \"${EHG_BOOTSTRAP}\""
183
184                         eval "${EHG_BOOTSTRAP}" \
185                                 || die "${FUNCNAME}: bootstrap commands failed"
186                 fi
187
188                 einfo "Bootstrap finished"
189                 popd > /dev/null
190         fi
191 }
192
193 # @FUNCTION: mercurial_src_unpack
194 # @DESCRIPTION:
195 # The mercurial src_unpack function, which will be exported.
196 function mercurial_src_unpack {
197         debug-print-function ${FUNCNAME} "$@"
198
199         mercurial_fetch
200         mercurial_bootstrap
201 }