From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 258A8138334 for ; Sun, 22 Sep 2019 06:02:31 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id BB114E09A8; Sun, 22 Sep 2019 06:02:26 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 59896E0999 for ; Sun, 22 Sep 2019 06:02:26 +0000 (UTC) Received: from pomiot (c134-66.icpnet.pl [85.221.134.66]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: mgorny) by smtp.gentoo.org (Postfix) with ESMTPSA id 0CE4534B470; Sun, 22 Sep 2019 06:02:23 +0000 (UTC) Message-ID: <5483370b6a3a2d2db7d2a65389bcbb060b8cccf0.camel@gentoo.org> Subject: Re: [gentoo-dev] [PATCH 2/2] go-module-vendor.eclass: new eclass for go modules that do not vendor From: =?UTF-8?Q?Micha=C5=82_G=C3=B3rny?= To: gentoo-dev@lists.gentoo.org Cc: William Hubbs Date: Sun, 22 Sep 2019 08:02:19 +0200 In-Reply-To: <20190921221032.10777-3-williamh@gentoo.org> References: <20190921221032.10777-1-williamh@gentoo.org> <20190921221032.10777-3-williamh@gentoo.org> Organization: Gentoo Content-Type: multipart/signed; micalg="pgp-sha512"; protocol="application/pgp-signature"; boundary="=-HU0R1d7dFttEBniwqnST" User-Agent: Evolution 3.32.4 Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-dev@lists.gentoo.org Reply-to: gentoo-dev@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply MIME-Version: 1.0 X-Archives-Salt: ba75af75-a10a-4be2-a02c-2abf360c8b87 X-Archives-Hash: 29f4b40996d00b6580eb39a9feed4f81 --=-HU0R1d7dFttEBniwqnST Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sat, 2019-09-21 at 17:10 -0500, William Hubbs wrote: > This eclass adds a src_unpack function that supports the EGO_VENDOR > variable for vendoring modules. > --- > eclass/go-module-vendor.eclass | 133 +++++++++++++++++++++++++++++++++ > 1 file changed, 133 insertions(+) > create mode 100644 eclass/go-module-vendor.eclass >=20 > diff --git a/eclass/go-module-vendor.eclass b/eclass/go-module-vendor.ecl= ass > new file mode 100644 > index 00000000000..af9007df411 > --- /dev/null > +++ b/eclass/go-module-vendor.eclass > @@ -0,0 +1,133 @@ > +# Copyright 2019 gentoo authors > +# Distributed under the terms of the GNU General Public License v2 > + > +# @ECLASS: go-module-vendor.eclass > +# @MAINTAINER: > +# William Hubbs > +# @SUPPORTED_EAPIS: 7 > +# @BLURB: Eclass for building software written in the go > +# programming language that uses go modules and does not vendor. > +# @DESCRIPTION: > +# This eclass provides a src_unpack function which supports vendoring > +# dependencies for software written in the go programming language that > +# uses go modules. > +# > +# You will know the software you are packaging uses modules because > +# it will have files named go.sum and go.mod in its top-level source > +# directory. If it does not have these files, use the golang-* eclasses. > +# > +# If there is also a directory named vendor in the top level source dire= ctory > +# of your package, use the golang-module eclass instead of this one. > +# > +# This eclass provides a src_unpack function which unpacks the=20 > +# first tarball mentioned in SRC_URI to the usual location and unpacks > +# any modules mentioned in EGO_VENDOR to ${S}/vendor. > +# > +# Please note that this eclass currently handles only tarballs > +# (.tar.gz), but support for more formats may be added in the future. > +# > +# Since Go programs are statically linked, it is important that your ebu= ild's > +# LICENSE=3D setting includes the licenses of all statically linked > +# dependencies. So please make sure it is accurate. > +# > +# @EXAMPLE: > +# > +# @CODE > +# EGO_VENDOR=3D( > +# "github.com/xenolf/lego 6cac0ea7d8b28c889f709ec7fa92e92b82f490dd" > +# "golang.org/x/crypto 453249f01cfeb54c3d549ddb75ff152ca243f9d8 github.c= om/golang/crypto" > +# ) > +# > +# inherit go-module-vendor > +# > +# SRC_URI=3D"https://github.com/example/${PN}/archive/v${PV}.tar.gz -> $= {P}.tar.gz > +# ${EGO_VENDOR_URI}" > +# @CODE > +# > +# The above example will extract the tarball to ${S} and > +# extract the contents of EGO_VENDOR to ${S}/vendor. > + > +inherit go-module > + > +case ${EAPI:-0} in > + 7) ;; > + *) die "${ECLASS} API in EAPI ${EAPI} not yet established." > +esac > + > +if [[ -z ${_GO_MODULE_VENDOR} ]]; then > + > +_GO_MODULE_VENDOR=3D1 > + > +EXPORT_FUNCTIONS src_unpack > + > +# @ECLASS-VARIABLE: EGO_VENDOR You want @REQUIRED here. > +# @DESCRIPTION: > +# This variable contains a list of vendored packages. > +# The items of this array are strings that contain the > +# import path and the git commit hash for a vendored package. > +# If the import path does not start with github.com, the third argument > +# can be used to point to a github repository. > + > +declare -arg EGO_VENDOR > + > +_go-module-vendor_set_vendor_uri() { I think you ought to check whether EGO_VENDOR_URI is non-empty here, and error out if it's empty. This will be important in catching people accidentally defining it after 'inherit'. Another thing worth considering is to actually permit defining it anywhere. If you replaced EGO_VENDOR_URI with a function, user would only need to define it prior to SRC_URI, e.g.: inherit go-module-vendor # ... EGO_VENDOR_URI=3D(...) SRC_URI=3D"... $(ego_vendor_uri)" But it's up to you. I personally feel like very long lists prior to inherit are inconvenient for the reader. > + EGO_VENDOR_URI=3D > + local lib > + for lib in "${EGO_VENDOR[@]}"; do > + lib=3D(${lib}) > + if [[ -n ${lib[2]} ]]; then > + EGO_VENDOR_URI+=3D" https://${lib[2]}/archive/${lib[1]}.tar.gz -> ${l= ib[2]//\//-}-${lib[1]}.tar.gz" > + else > + EGO_VENDOR_URI+=3D" https://${lib[0]}/archive/${lib[1]}.tar.gz -> ${l= ib[0]//\//-}-${lib[1]}.tar.gz" > + fi > + done > + readonly EGO_VENDOR_URI > +} > + > +_go-module-vendor_set_vendor_uri > +unset -f _go-module-vendor_set_vendor_uri > + > +_go-module-vendor_dovendor() { > + local VENDOR_PATH=3D$1 VENDORPN=3D$2 TARBALL=3D$3 Common convention is to use lowercase names for local variables. > + rm -fr "${VENDOR_PATH}/${VENDORPN}" || die > + mkdir -p "${VENDOR_PATH}/${VENDORPN}" || die > + tar -C "${VENDOR_PATH}/${VENDORPN}" -x --strip-components 1\ Add space before `\`. > + -f "${DISTDIR}/${TARBALL}" || die > +} > + > +# @FUNCTION: go-module-vendor_src_unpack > +# @DESCRIPTION: > +# Extract the first archive from ${A} to ${S}, then extract > +# any sources mentioned in ${EGO_VENDOR} to ${S}/vendor. > +go-module-vendor_src_unpack() { > + local lib vendor_path x > + set -- ${A} > + x=3D"$1" > + mkdir -p "${S}" || die > + tar -C "${S}/" -x --strip-components 1 \ > + -f "${DISTDIR}/${x}" || die It's probably a bad idea to hardcode the 'first argument' logic. My suggestion would be to match all ${A} against ${EGO_VENDOR}, and unpack those that aren't present in the latter. > + > + if [[ -d "${S}/vendor" ]]; then > + eerror "Upstream for ${P}.ebuild vendors dependencies." > + die "This ebuild should inherit go-module.eclass" > + fi I'm sorry but all things said, there might be a valid use case to permit this after all -- if packager wants to upgrade vendored dependencies e.g. to fix a security problem. > + > + if [[ -n "${EGO_VENDOR}" ]]; then Empty EGO_VENDOR should probably be an error here. > + vendor_path=3D"${S}/vendor" > + mkdir -p "${vendor_path}" || die > + for lib in "${EGO_VENDOR[@]}"; do > + lib=3D(${lib}) > + if [[ -n ${lib[2]} ]]; then Sounds like you could simplify that by: [[ -z ${lib[2]} ]] && set -- "${lib[@]}" "${lib[0]}" and then leaving only one branch of the 'if'. You could also use readable variable names instead of [0], [1], [2]. And then, you could inline _go-module_dovendor since it has only one call site now and has all nice variable names already. > + einfo "Vendoring ${lib[0]} ${lib[2]//\//-}-${lib[1]}.tar.gz" > + _go-module_dovendor "${vendor_path}" ${lib[0]} \ > + ${lib[2]//\//-}-${lib[1]}.tar.gz > + else > + einfo "Vendoring ${lib[0]} ${lib[0]//\//-}-${lib[1]}.tar.gz" > + _go-module_dovendor "${vendor_path}" ${lib[0]} \ > + ${lib[0]//\//-}-${lib[1]}.tar.gz > + fi > + done > + fi > +} > + > +fi --=20 Best regards, Micha=C5=82 G=C3=B3rny --=-HU0R1d7dFttEBniwqnST Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNATURE----- iQGTBAABCgB9FiEEx2qEUJQJjSjMiybFY5ra4jKeJA4FAl2HDmxfFIAAAAAALgAo aXNzdWVyLWZwckBub3RhdGlvbnMub3BlbnBncC5maWZ0aGhvcnNlbWFuLm5ldEM3 NkE4NDUwOTQwOThEMjhDQzhCMjZDNTYzOUFEQUUyMzI5RTI0MEUACgkQY5ra4jKe JA6BZwgAy8Rk4wgnnVgqkopVqj5SckXRAOaqYuIQ3+QKq1QG6KtDkdIjJIKNij0X LNSTC7tk6igN9JkmvZQLRg05UAFzsVtRlTUvX4ZjU5ID1LrVWdOGD8xSQV0p1cue gOvkwzwA3ax5o9W2aaXhjQsv6RewfbveyKVBlWEGrbrwR8CWa8tBXPGxp7+80D/N 6C3bfNJTRViguowqu/ZgOT116ukL5mPb5Te/iAEucoNmkM+7MAjT5MehflfzFTXy iypGqri5rnHSww6HBqgt/bUrPI9VhMQBUjN8XktOk4yp7pJ9cLi1NLXTsbNCP5/9 lUvOTQIi/Bu92z3WaejQeFA2ZEfpLg== =5Ujm -----END PGP SIGNATURE----- --=-HU0R1d7dFttEBniwqnST--