dev-python/josepy: 1.1.0 cleanup
[gentoo.git] / eclass / bzr.eclass
1 # Copyright 1999-2019 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: bzr.eclass
5 # @MAINTAINER:
6 # Ulrich Müller <ulm@gentoo.org>
7 # @AUTHOR:
8 # Jorge Manuel B. S. Vicetto <jmbsvicetto@gentoo.org>
9 # Mark Lee <bzr-gentoo-overlay@lazymalevolence.com>
10 # Ulrich Müller <ulm@gentoo.org>
11 # Christian Faulhammer <fauli@gentoo.org>
12 # @SUPPORTED_EAPIS: 2 3 4 5 6 7
13 # @BLURB: generic fetching functions for the Bazaar VCS
14 # @DESCRIPTION:
15 # The bzr.eclass provides functions to fetch and unpack sources from
16 # repositories of the Bazaar distributed version control system.
17 # The eclass was originally derived from git.eclass.
18 #
19 # Note: Just set EBZR_REPO_URI to the URI of the branch and src_unpack()
20 # of this eclass will export the branch to ${WORKDIR}/${P}.
21
22 EBZR="bzr.eclass"
23
24 PROPERTIES+=" live"
25
26 if [[ ${EBZR_REPO_URI%%:*} = sftp ]]; then
27         DEPEND=">=dev-vcs/bzr-2.6.0[sftp]"
28 else
29         DEPEND=">=dev-vcs/bzr-2.6.0"
30 fi
31
32 case ${EAPI:-0} in
33         2|3|4|5|6) ;;
34         7) BDEPEND="${DEPEND}"; DEPEND="" ;;
35         *) die "${EBZR}: EAPI ${EAPI:-0} is not supported" ;;
36 esac
37
38 EXPORT_FUNCTIONS src_unpack
39
40 # @ECLASS-VARIABLE: EBZR_STORE_DIR
41 # @DESCRIPTION:
42 # The directory to store all fetched Bazaar live sources.
43 : ${EBZR_STORE_DIR:=${PORTAGE_ACTUAL_DISTDIR:-${DISTDIR}}/bzr-src}
44
45 # @ECLASS-VARIABLE: EBZR_UNPACK_DIR
46 # @DESCRIPTION:
47 # The working directory where the sources are copied to.
48 : ${EBZR_UNPACK_DIR:=${WORKDIR}/${P}}
49
50 # @ECLASS-VARIABLE: EBZR_INIT_REPO_CMD
51 # @DESCRIPTION:
52 # The Bazaar command to initialise a shared repository.
53 : ${EBZR_INIT_REPO_CMD:="bzr init-repository --no-trees"}
54
55 # @ECLASS-VARIABLE: EBZR_FETCH_CMD
56 # @DESCRIPTION:
57 # The Bazaar command to fetch the sources.
58 : ${EBZR_FETCH_CMD:="bzr branch --no-tree"}
59
60 # @ECLASS-VARIABLE: EBZR_UPDATE_CMD
61 # @DESCRIPTION:
62 # The Bazaar command to update the sources.
63 : ${EBZR_UPDATE_CMD:="bzr pull --overwrite-tags"}
64
65 # @ECLASS-VARIABLE: EBZR_EXPORT_CMD
66 # @DESCRIPTION:
67 # The Bazaar command to export a branch.
68 : ${EBZR_EXPORT_CMD:="bzr export"}
69
70 # @ECLASS-VARIABLE: EBZR_CHECKOUT_CMD
71 # @DESCRIPTION:
72 # The Bazaar command to checkout a branch.
73 : ${EBZR_CHECKOUT_CMD:="bzr checkout --lightweight -q"}
74
75 # @ECLASS-VARIABLE: EBZR_REVNO_CMD
76 # @DESCRIPTION:
77 # The Bazaar command to list a revision number of the branch.
78 : ${EBZR_REVNO_CMD:="bzr revno"}
79
80 # @ECLASS-VARIABLE: EBZR_OPTIONS
81 # @DEFAULT_UNSET
82 # @DESCRIPTION:
83 # The options passed to the fetch and update commands.
84
85 # @ECLASS-VARIABLE: EBZR_REPO_URI
86 # @DEFAULT_UNSET
87 # @REQUIRED
88 # @DESCRIPTION:
89 # The repository URI for the source package.
90 #
91 # Note: If the ebuild uses an sftp:// URI, then the eclass will depend
92 # on dev-vcs/bzr[sftp].
93
94 # @ECLASS-VARIABLE: EBZR_INITIAL_URI
95 # @DEFAULT_UNSET
96 # @DESCRIPTION:
97 # The URI used for initial branching of the source repository.  If this
98 # variable is set, the initial branch will be cloned from the location
99 # specified, followed by a pull from ${EBZR_REPO_URI}.  This is intended
100 # for special cases, e.g. when download from the original repository is
101 # slow, but a fast mirror exists but may be out of date.
102 #
103 # Normally, this variable needs not be set.
104
105 # @ECLASS-VARIABLE: EBZR_PROJECT
106 # @DESCRIPTION:
107 # The project name of your ebuild.  Normally, the branch will be stored
108 # in the ${EBZR_STORE_DIR}/${EBZR_PROJECT} directory.
109 #
110 # If EBZR_BRANCH is set (see below), then a shared repository will be
111 # created in that directory, and the branch will be located in
112 # ${EBZR_STORE_DIR}/${EBZR_PROJECT}/${EBZR_BRANCH}.
113 : ${EBZR_PROJECT:=${PN}}
114
115 # @ECLASS-VARIABLE: EBZR_BRANCH
116 # @DEFAULT_UNSET
117 # @DESCRIPTION:
118 # The directory where to store the branch within a shared repository,
119 # relative to ${EBZR_STORE_DIR}/${EBZR_PROJECT}.
120 #
121 # This variable should be set if there are several live ebuilds for
122 # different branches of the same upstream project.  The branches can
123 # then share the same repository in EBZR_PROJECT, which will save both
124 # data traffic volume and disk space.
125 #
126 # If there is only a live ebuild for one single branch, EBZR_BRANCH
127 # needs not be set.  In this case, the branch will be stored in a
128 # stand-alone repository directly in EBZR_PROJECT.
129
130 # @ECLASS-VARIABLE: EBZR_REVISION
131 # @DEFAULT_UNSET
132 # @DESCRIPTION:
133 # Revision to fetch, defaults to the latest
134 # (see http://bazaar-vcs.org/BzrRevisionSpec or bzr help revisionspec).
135
136 # @ECLASS-VARIABLE: EBZR_OFFLINE
137 # @DESCRIPTION:
138 # Set this variable to a non-empty value to disable automatic updating
139 # of a bzr source tree.  This is intended to be set outside the ebuild
140 # by users.
141 : ${EBZR_OFFLINE=${EVCS_OFFLINE}}
142
143 # @ECLASS-VARIABLE: EVCS_UMASK
144 # @DEFAULT_UNSET
145 # @DESCRIPTION:
146 # Set this variable to a custom umask.  This is intended to be set by
147 # users.  By setting this to something like 002, it can make life easier
148 # for people who do development as non-root (but are in the portage
149 # group), and then switch over to building with FEATURES=userpriv.
150 # Or vice-versa.  Shouldn't be a security issue here as anyone who has
151 # portage group write access already can screw the system over in more
152 # creative ways.
153
154 # @ECLASS-VARIABLE: EBZR_WORKDIR_CHECKOUT
155 # @DEFAULT_UNSET
156 # @DESCRIPTION:
157 # If this variable is set to a non-empty value, EBZR_CHECKOUT_CMD will
158 # be used instead of EBZR_EXPORT_CMD to copy the sources to WORKDIR.
159
160 # @FUNCTION: bzr_initial_fetch
161 # @USAGE: <repository URI> <branch directory>
162 # @DESCRIPTION:
163 # Internal function, retrieves the source code from a repository for the
164 # first time, using ${EBZR_FETCH_CMD}.
165 bzr_initial_fetch() {
166         local repo_uri=$1 branch_dir=$2
167
168         if [[ -n "${EBZR_OFFLINE}" ]]; then
169                 ewarn "EBZR_OFFLINE cannot be used when there is no local branch yet."
170         fi
171
172         # fetch branch
173         einfo "bzr branch start -->"
174         einfo "   repository: ${repo_uri} => ${branch_dir}"
175
176         ${EBZR_FETCH_CMD} ${EBZR_OPTIONS} "${repo_uri}" "${branch_dir}" \
177                 || die "${EBZR}: can't branch from ${repo_uri}"
178 }
179
180 # @FUNCTION: bzr_update
181 # @USAGE: <repository URI> <branch directory>
182 # @DESCRIPTION:
183 # Internal function, updates the source code from a repository, using
184 # ${EBZR_UPDATE_CMD}.
185 bzr_update() {
186         local repo_uri=$1 branch_dir=$2
187
188         if [[ -n "${EBZR_OFFLINE}" ]]; then
189                 einfo "skipping bzr pull -->"
190                 einfo "   repository: ${repo_uri}"
191         else
192                 # update branch
193                 einfo "bzr pull start -->"
194                 einfo "   repository: ${repo_uri}"
195
196                 pushd "${branch_dir}" > /dev/null \
197                         || die "${EBZR}: can't chdir to ${branch_dir}"
198                 ${EBZR_UPDATE_CMD} ${EBZR_OPTIONS} "${repo_uri}" \
199                         || die "${EBZR}: can't pull from ${repo_uri}"
200                 popd > /dev/null || die "${EBZR}: popd failed"
201         fi
202 }
203
204 # @FUNCTION: bzr_fetch
205 # @DESCRIPTION:
206 # Wrapper function to fetch sources from a Bazaar repository with
207 # bzr branch or bzr pull, depending on whether there is an existing
208 # working copy.
209 bzr_fetch() {
210         local repo_dir branch_dir
211         local save_sandbox_write=${SANDBOX_WRITE} save_umask
212
213         [[ -n ${EBZR_REPO_URI} ]] || die "${EBZR}: EBZR_REPO_URI is empty"
214
215         if [[ ! -d ${EBZR_STORE_DIR} ]] ; then
216                 addwrite /
217                 mkdir -p "${EBZR_STORE_DIR}" \
218                         || die "${EBZR}: can't mkdir ${EBZR_STORE_DIR}"
219                 SANDBOX_WRITE=${save_sandbox_write}
220         fi
221
222         pushd "${EBZR_STORE_DIR}" > /dev/null \
223                 || die "${EBZR}: can't chdir to ${EBZR_STORE_DIR}"
224
225         repo_dir=${EBZR_STORE_DIR}/${EBZR_PROJECT}
226         branch_dir=${repo_dir}${EBZR_BRANCH:+/${EBZR_BRANCH}}
227
228         if [[ -n ${EVCS_UMASK} ]]; then
229                 save_umask=$(umask)
230                 umask "${EVCS_UMASK}" || die
231         fi
232         addwrite "${EBZR_STORE_DIR}"
233
234         if [[ ! -d ${branch_dir}/.bzr ]]; then
235                 if [[ ${repo_dir} != "${branch_dir}" && ! -d ${repo_dir}/.bzr ]]; then
236                         einfo "creating shared bzr repository: ${repo_dir}"
237                         ${EBZR_INIT_REPO_CMD} "${repo_dir}" \
238                                 || die "${EBZR}: can't create shared repository"
239                 fi
240
241                 if [[ -z ${EBZR_INITIAL_URI} ]]; then
242                         bzr_initial_fetch "${EBZR_REPO_URI}" "${branch_dir}"
243                 else
244                         # Workaround for faster initial download. This clones the
245                         # branch from a fast server (which may be out of date), and
246                         # subsequently pulls from the slow original repository.
247                         bzr_initial_fetch "${EBZR_INITIAL_URI}" "${branch_dir}"
248                         if [[ ${EBZR_REPO_URI} != "${EBZR_INITIAL_URI}" ]]; then
249                                 EBZR_UPDATE_CMD="${EBZR_UPDATE_CMD} --remember --overwrite" \
250                                         EBZR_OFFLINE="" \
251                                         bzr_update "${EBZR_REPO_URI}" "${branch_dir}"
252                         fi
253                 fi
254         else
255                 bzr_update "${EBZR_REPO_URI}" "${branch_dir}"
256         fi
257
258         # Restore sandbox environment and umask
259         SANDBOX_WRITE=${save_sandbox_write}
260         if [[ -n ${save_umask} ]]; then
261                 umask "${save_umask}" || die
262         fi
263
264         cd "${branch_dir}" || die "${EBZR}: can't chdir to ${branch_dir}"
265
266         # Save revision number in environment. #311101
267         export EBZR_REVNO=$(${EBZR_REVNO_CMD})
268
269         if [[ -n ${EBZR_WORKDIR_CHECKOUT} ]]; then
270                 einfo "checking out ..."
271                 ${EBZR_CHECKOUT_CMD} ${EBZR_REVISION:+-r ${EBZR_REVISION}} \
272                         . "${EBZR_UNPACK_DIR}" || die "${EBZR}: checkout failed"
273         else
274                 einfo "exporting ..."
275                 ${EBZR_EXPORT_CMD} ${EBZR_REVISION:+-r ${EBZR_REVISION}} \
276                         "${EBZR_UNPACK_DIR}" . || die "${EBZR}: export failed"
277         fi
278         einfo \
279                 "revision ${EBZR_REVISION:-${EBZR_REVNO}} is now in ${EBZR_UNPACK_DIR}"
280
281         popd > /dev/null || die "${EBZR}: popd failed"
282 }
283
284 # @FUNCTION: bzr_src_unpack
285 # @DESCRIPTION:
286 # Default src_unpack(), calls bzr_fetch.
287 bzr_src_unpack() {
288         bzr_fetch
289 }