prefix.eclass: introduce hprefixify and prefixify_ro
authorBenda Xu <heroxbd@gentoo.org>
Thu, 21 Jul 2016 01:40:18 +0000 (10:40 +0900)
committerBenda Xu <heroxbd@gentoo.org>
Thu, 21 Jul 2016 01:41:29 +0000 (10:41 +0900)
Bug: 583740

eclass/prefix.eclass

index 1f3c4b0c4a5b9db6368bd2147678763cd8f9c77e..c0675cd37032626117f87db9610f36b53fce0c28 100644 (file)
@@ -48,5 +48,83 @@ eprefixify() {
        return 0
 }
 
+# @FUNCTION: hprefixify
+# @USAGE: [ -w <line match> ] [-e <extended regex> ] <list of files>
+# @DESCRIPTION:
+# Tries a set of heuristics to prefixify the given files. Dies if no
+# arguments are given, a file does not exist, or changing a file failed.
+#
+# Additional extended regular expression can be passed by -e or
+# environment variable PREFIX_EXTRA_REGEX.  The default heuristics can
+# be constrained to lines that match a sed expression passed by -w or
+# environment variable PREFIX_LINE_MATCH.
+# @EXAMPLE:
+# Only prefixify the 30th line,
+#   hprefixify -w 30 configure
+# Only prefixify lines that contain "PATH",
+#   hprefixify -w "/PATH/" configure
+# Also delete all the /opt/gnu search paths,
+#   hprefixify -e "/\/opt\/gnu/d" configure
+hprefixify() {
+       use prefix || return 0
+
+       local PREFIX_EXTRA_REGEX PREFIX_LINE_MATCH xl=() x
+       while [[ $# -gt 0 ]]; do
+               case $1 in
+                       -e)
+                               PREFIX_EXTRA_REGEX="$2"
+                               shift
+                               ;;
+                       -w)
+                               PREFIX_LINE_MATCH="$2"
+                               shift
+                               ;;
+                       *)
+                               xl+=( "$1" )
+                               ;;
+               esac
+               shift
+       done
+
+       [[ ${#xl[@]} -lt 1 ]] && die "at least one file operand is required"
+       einfo "Adjusting to prefix ${EPREFIX:-/}"
+       for x in "${xl[@]}" ; do
+               if [[ -e ${x} ]] ; then
+                       ebegin "  ${x##*/}"
+                       sed -r \
+                               -e "${PREFIX_LINE_MATCH}s,([^[:alnum:]}\)\.])/(usr|lib(|[onx]?32|n?64)|etc|bin|sbin|var|opt),\1${EPREFIX}/\2,g" \
+                               -e "${PREFIX_EXTRA_REGEX}" \
+                               -i "${x}"
+                       eend $? || die "failed to prefixify ${x}"
+               else
+                       die "${x} does not exist"
+               fi
+       done
+}
 
+# @FUNCTION: prefixify_ro
+# @USAGE: prefixify_ro <file>.
+# @DESCRIPTION:
+# prefixify a read-only file.
+# copies the files to ${T}, prefixies it, echos the new file.
+# @EXAMPLE:
+# doexe "$(prefixify_ro "${FILESDIR}"/fix_libtool_files.sh)"
+# epatch "$(prefixify_ro "${FILESDIR}"/${PN}-4.0.2-path.patch)"
+prefixify_ro() {
+       if [[ -e $1 ]] ; then
+               local f=${1##*/}
+               cp "$1" "${T}" || die "failed to copy file"
+               local x="${T}"/${f}
+               # redirect to stderr because stdout is used to
+               # return the prefixified file.
+               if grep -qs @GENTOO_PORTAGE_EPREFIX@ "${x}" ; then
+                       eprefixify "${T}"/${f} 1>&2
+               else
+                       hprefixify "${T}"/${f} 1>&2
+               fi
+               echo "${x}"
+       else
+               die "$1 does not exist"
+       fi
+}
 # vim: tw=72: