/[gentoo-x86]/eclass/eutils.eclass
Gentoo

Diff of /eclass/eutils.eclass

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.363 Revision 1.372
1# Copyright 1999-2011 Gentoo Foundation 1# Copyright 1999-2011 Gentoo Foundation
2# Distributed under the terms of the GNU General Public License v2 2# Distributed under the terms of the GNU General Public License v2
3# $Header: /var/cvsroot/gentoo-x86/eclass/eutils.eclass,v 1.363 2011/09/12 20:44:01 mgorny Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/eutils.eclass,v 1.372 2011/12/14 17:36:18 vapier Exp $
4 4
5# @ECLASS: eutils.eclass 5# @ECLASS: eutils.eclass
6# @MAINTAINER: 6# @MAINTAINER:
7# base-system@gentoo.org 7# base-system@gentoo.org
8# @BLURB: many extra (but common) functions that are used in ebuilds 8# @BLURB: many extra (but common) functions that are used in ebuilds
13# home rather than having multiple ebuilds implementing the same thing. 13# home rather than having multiple ebuilds implementing the same thing.
14# 14#
15# Due to the nature of this eclass, some functions may have maintainers 15# Due to the nature of this eclass, some functions may have maintainers
16# different from the overall eclass! 16# different from the overall eclass!
17 17
18if [[ ${___ECLASS_ONCE_EUTILS} != "recur -_+^+_- spank" ]] ; then
19___ECLASS_ONCE_EUTILS="recur -_+^+_- spank"
20
18inherit multilib portability 21inherit multilib portability user
19 22
20DESCRIPTION="Based on the ${ECLASS} eclass" 23DESCRIPTION="Based on the ${ECLASS} eclass"
21 24
22if has "${EAPI:-0}" 0 1 2; then 25if has "${EAPI:-0}" 0 1 2; then
23 26
70# implementation if available. Reuses PORTAGE_ELOG_CLASSES as set by the dev 73# implementation if available. Reuses PORTAGE_ELOG_CLASSES as set by the dev
71# profile. 74# profile.
72if ! declare -F eqawarn >/dev/null ; then 75if ! declare -F eqawarn >/dev/null ; then
73 eqawarn() { 76 eqawarn() {
74 has qa ${PORTAGE_ELOG_CLASSES} && ewarn "$@" 77 has qa ${PORTAGE_ELOG_CLASSES} && ewarn "$@"
78 :
75 } 79 }
76fi 80fi
77 81
78# @FUNCTION: ecvs_clean 82# @FUNCTION: ecvs_clean
79# @USAGE: [list of dirs] 83# @USAGE: [list of dirs]
335 local STDERR_TARGET="${T}/${patchname}.out" 339 local STDERR_TARGET="${T}/${patchname}.out"
336 if [[ -e ${STDERR_TARGET} ]] ; then 340 if [[ -e ${STDERR_TARGET} ]] ; then
337 STDERR_TARGET="${T}/${patchname}-$$.out" 341 STDERR_TARGET="${T}/${patchname}-$$.out"
338 fi 342 fi
339 343
340 printf "***** %s *****\n\n" "${patchname}" > "${STDERR_TARGET}" 344 printf "***** %s *****\nPWD: %s\n\n" "${patchname}" "${PWD}" > "${STDERR_TARGET}"
341 345
342 # Decompress the patch if need be 346 # Decompress the patch if need be
343 local count=0 347 local count=0
344 local PATCH_TARGET 348 local PATCH_TARGET
345 if [[ -n ${PIPE_CMD} ]] ; then 349 if [[ -n ${PIPE_CMD} ]] ; then
529 TMPDIR="${topdir}" mktemp -dt tmp.XXXXXXXXXX 533 TMPDIR="${topdir}" mktemp -dt tmp.XXXXXXXXXX
530 fi 534 fi
531 fi 535 fi
532} 536}
533 537
534# @FUNCTION: egetent
535# @USAGE: <database> <key>
536# @MAINTAINER:
537# base-system@gentoo.org (Linux)
538# Joe Jezak <josejx@gmail.com> (OS X)
539# usata@gentoo.org (OS X)
540# Aaron Walker <ka0ttic@gentoo.org> (FreeBSD)
541# @DESCRIPTION:
542# Small wrapper for getent (Linux),
543# nidump (< Mac OS X 10.5), dscl (Mac OS X 10.5),
544# and pw (FreeBSD) used in enewuser()/enewgroup()
545egetent() {
546 case ${CHOST} in
547 *-darwin[678])
548 case "$2" in
549 *[!0-9]*) # Non numeric
550 nidump $1 . | awk -F":" "{ if (\$1 ~ /^$2\$/) {print \$0;exit;} }"
551 ;;
552 *) # Numeric
553 nidump $1 . | awk -F":" "{ if (\$3 == $2) {print \$0;exit;} }"
554 ;;
555 esac
556 ;;
557 *-darwin*)
558 local mytype=$1
559 [[ "passwd" == $mytype ]] && mytype="Users"
560 [[ "group" == $mytype ]] && mytype="Groups"
561 case "$2" in
562 *[!0-9]*) # Non numeric
563 dscl . -read /$mytype/$2 2>/dev/null |grep RecordName
564 ;;
565 *) # Numeric
566 local mykey="UniqueID"
567 [[ $mytype == "Groups" ]] && mykey="PrimaryGroupID"
568 dscl . -search /$mytype $mykey $2 2>/dev/null
569 ;;
570 esac
571 ;;
572 *-freebsd*|*-dragonfly*)
573 local opts action="user"
574 [[ $1 == "passwd" ]] || action="group"
575
576 # lookup by uid/gid
577 if [[ $2 == [[:digit:]]* ]] ; then
578 [[ ${action} == "user" ]] && opts="-u" || opts="-g"
579 fi
580
581 pw show ${action} ${opts} "$2" -q
582 ;;
583 *-netbsd*|*-openbsd*)
584 grep "$2:\*:" /etc/$1
585 ;;
586 *)
587 type -p nscd >& /dev/null && nscd -i "$1"
588 getent "$1" "$2"
589 ;;
590 esac
591}
592
593# @FUNCTION: enewuser
594# @USAGE: <user> [uid] [shell] [homedir] [groups] [params]
595# @DESCRIPTION:
596# Same as enewgroup, you are not required to understand how to properly add
597# a user to the system. The only required parameter is the username.
598# Default uid is (pass -1 for this) next available, default shell is
599# /bin/false, default homedir is /dev/null, there are no default groups,
600# and default params sets the comment as 'added by portage for ${PN}'.
601enewuser() {
602 case ${EBUILD_PHASE} in
603 unpack|compile|test|install)
604 eerror "'enewuser()' called from '${EBUILD_PHASE}()' which is not a pkg_* function."
605 eerror "Package fails at QA and at life. Please file a bug."
606 die "Bad package! enewuser is only for use in pkg_* functions!"
607 esac
608
609 # get the username
610 local euser=$1; shift
611 if [[ -z ${euser} ]] ; then
612 eerror "No username specified !"
613 die "Cannot call enewuser without a username"
614 fi
615
616 # lets see if the username already exists
617 if [[ -n $(egetent passwd "${euser}") ]] ; then
618 return 0
619 fi
620 einfo "Adding user '${euser}' to your system ..."
621
622 # options to pass to useradd
623 local opts=
624
625 # handle uid
626 local euid=$1; shift
627 if [[ -n ${euid} && ${euid} != -1 ]] ; then
628 if [[ ${euid} -gt 0 ]] ; then
629 if [[ -n $(egetent passwd ${euid}) ]] ; then
630 euid="next"
631 fi
632 else
633 eerror "Userid given but is not greater than 0 !"
634 die "${euid} is not a valid UID"
635 fi
636 else
637 euid="next"
638 fi
639 if [[ ${euid} == "next" ]] ; then
640 for ((euid = 101; euid <= 999; euid++)); do
641 [[ -z $(egetent passwd ${euid}) ]] && break
642 done
643 fi
644 opts="${opts} -u ${euid}"
645 einfo " - Userid: ${euid}"
646
647 # handle shell
648 local eshell=$1; shift
649 if [[ ! -z ${eshell} ]] && [[ ${eshell} != "-1" ]] ; then
650 if [[ ! -e ${ROOT}${eshell} ]] ; then
651 eerror "A shell was specified but it does not exist !"
652 die "${eshell} does not exist in ${ROOT}"
653 fi
654 if [[ ${eshell} == */false || ${eshell} == */nologin ]] ; then
655 eerror "Do not specify ${eshell} yourself, use -1"
656 die "Pass '-1' as the shell parameter"
657 fi
658 else
659 for shell in /sbin/nologin /usr/sbin/nologin /bin/false /usr/bin/false /dev/null ; do
660 [[ -x ${ROOT}${shell} ]] && break
661 done
662
663 if [[ ${shell} == "/dev/null" ]] ; then
664 eerror "Unable to identify the shell to use, proceeding with userland default."
665 case ${USERLAND} in
666 GNU) shell="/bin/false" ;;
667 BSD) shell="/sbin/nologin" ;;
668 Darwin) shell="/usr/sbin/nologin" ;;
669 *) die "Unable to identify the default shell for userland ${USERLAND}"
670 esac
671 fi
672
673 eshell=${shell}
674 fi
675 einfo " - Shell: ${eshell}"
676 opts="${opts} -s ${eshell}"
677
678 # handle homedir
679 local ehome=$1; shift
680 if [[ -z ${ehome} ]] || [[ ${ehome} == "-1" ]] ; then
681 ehome="/dev/null"
682 fi
683 einfo " - Home: ${ehome}"
684 opts="${opts} -d ${ehome}"
685
686 # handle groups
687 local egroups=$1; shift
688 if [[ ! -z ${egroups} ]] ; then
689 local oldifs=${IFS}
690 local defgroup="" exgroups=""
691
692 export IFS=","
693 for g in ${egroups} ; do
694 export IFS=${oldifs}
695 if [[ -z $(egetent group "${g}") ]] ; then
696 eerror "You must add group ${g} to the system first"
697 die "${g} is not a valid GID"
698 fi
699 if [[ -z ${defgroup} ]] ; then
700 defgroup=${g}
701 else
702 exgroups="${exgroups},${g}"
703 fi
704 export IFS=","
705 done
706 export IFS=${oldifs}
707
708 opts="${opts} -g ${defgroup}"
709 if [[ ! -z ${exgroups} ]] ; then
710 opts="${opts} -G ${exgroups:1}"
711 fi
712 else
713 egroups="(none)"
714 fi
715 einfo " - Groups: ${egroups}"
716
717 # handle extra and add the user
718 local oldsandbox=${SANDBOX_ON}
719 export SANDBOX_ON="0"
720 case ${CHOST} in
721 *-darwin*)
722 ### Make the user
723 if [[ -z $@ ]] ; then
724 dscl . create /users/${euser} uid ${euid}
725 dscl . create /users/${euser} shell ${eshell}
726 dscl . create /users/${euser} home ${ehome}
727 dscl . create /users/${euser} realname "added by portage for ${PN}"
728 ### Add the user to the groups specified
729 local oldifs=${IFS}
730 export IFS=","
731 for g in ${egroups} ; do
732 dscl . merge /groups/${g} users ${euser}
733 done
734 export IFS=${oldifs}
735 else
736 einfo "Extra options are not supported on Darwin yet"
737 einfo "Please report the ebuild along with the info below"
738 einfo "eextra: $@"
739 die "Required function missing"
740 fi
741 ;;
742 *-freebsd*|*-dragonfly*)
743 if [[ -z $@ ]] ; then
744 pw useradd ${euser} ${opts} \
745 -c "added by portage for ${PN}" \
746 die "enewuser failed"
747 else
748 einfo " - Extra: $@"
749 pw useradd ${euser} ${opts} \
750 "$@" || die "enewuser failed"
751 fi
752 ;;
753
754 *-netbsd*)
755 if [[ -z $@ ]] ; then
756 useradd ${opts} ${euser} || die "enewuser failed"
757 else
758 einfo " - Extra: $@"
759 useradd ${opts} ${euser} "$@" || die "enewuser failed"
760 fi
761 ;;
762
763 *-openbsd*)
764 if [[ -z $@ ]] ; then
765 useradd -u ${euid} -s ${eshell} \
766 -d ${ehome} -c "Added by portage for ${PN}" \
767 -g ${egroups} ${euser} || die "enewuser failed"
768 else
769 einfo " - Extra: $@"
770 useradd -u ${euid} -s ${eshell} \
771 -d ${ehome} -c "Added by portage for ${PN}" \
772 -g ${egroups} ${euser} "$@" || die "enewuser failed"
773 fi
774 ;;
775
776 *)
777 if [[ -z $@ ]] ; then
778 useradd -r ${opts} \
779 -c "added by portage for ${PN}" \
780 ${euser} \
781 || die "enewuser failed"
782 else
783 einfo " - Extra: $@"
784 useradd -r ${opts} "$@" \
785 ${euser} \
786 || die "enewuser failed"
787 fi
788 ;;
789 esac
790
791 if [[ ! -e ${ROOT}/${ehome} ]] ; then
792 einfo " - Creating ${ehome} in ${ROOT}"
793 mkdir -p "${ROOT}/${ehome}"
794 chown ${euser} "${ROOT}/${ehome}"
795 chmod 755 "${ROOT}/${ehome}"
796 fi
797
798 export SANDBOX_ON=${oldsandbox}
799}
800
801# @FUNCTION: enewgroup
802# @USAGE: <group> [gid]
803# @DESCRIPTION:
804# This function does not require you to understand how to properly add a
805# group to the system. Just give it a group name to add and enewgroup will
806# do the rest. You may specify the gid for the group or allow the group to
807# allocate the next available one.
808enewgroup() {
809 case ${EBUILD_PHASE} in
810 unpack|compile|test|install)
811 eerror "'enewgroup()' called from '${EBUILD_PHASE}()' which is not a pkg_* function."
812 eerror "Package fails at QA and at life. Please file a bug."
813 die "Bad package! enewgroup is only for use in pkg_* functions!"
814 esac
815
816 # get the group
817 local egroup="$1"; shift
818 if [ -z "${egroup}" ]
819 then
820 eerror "No group specified !"
821 die "Cannot call enewgroup without a group"
822 fi
823
824 # see if group already exists
825 if [[ -n $(egetent group "${egroup}") ]]; then
826 return 0
827 fi
828 einfo "Adding group '${egroup}' to your system ..."
829
830 # options to pass to useradd
831 local opts=
832
833 # handle gid
834 local egid="$1"; shift
835 if [ ! -z "${egid}" ]
836 then
837 if [ "${egid}" -gt 0 ]
838 then
839 if [ -z "`egetent group ${egid}`" ]
840 then
841 if [[ "${CHOST}" == *-darwin* ]]; then
842 opts="${opts} ${egid}"
843 else
844 opts="${opts} -g ${egid}"
845 fi
846 else
847 egid="next available; requested gid taken"
848 fi
849 else
850 eerror "Groupid given but is not greater than 0 !"
851 die "${egid} is not a valid GID"
852 fi
853 else
854 egid="next available"
855 fi
856 einfo " - Groupid: ${egid}"
857
858 # handle extra
859 local eextra="$@"
860 opts="${opts} ${eextra}"
861
862 # add the group
863 local oldsandbox="${SANDBOX_ON}"
864 export SANDBOX_ON="0"
865 case ${CHOST} in
866 *-darwin*)
867 if [ ! -z "${eextra}" ];
868 then
869 einfo "Extra options are not supported on Darwin/OS X yet"
870 einfo "Please report the ebuild along with the info below"
871 einfo "eextra: ${eextra}"
872 die "Required function missing"
873 fi
874
875 # If we need the next available
876 case ${egid} in
877 *[!0-9]*) # Non numeric
878 for ((egid = 101; egid <= 999; egid++)); do
879 [[ -z $(egetent group ${egid}) ]] && break
880 done
881 esac
882 dscl . create /groups/${egroup} gid ${egid}
883 dscl . create /groups/${egroup} passwd '*'
884 ;;
885
886 *-freebsd*|*-dragonfly*)
887 case ${egid} in
888 *[!0-9]*) # Non numeric
889 for ((egid = 101; egid <= 999; egid++)); do
890 [[ -z $(egetent group ${egid}) ]] && break
891 done
892 esac
893 pw groupadd ${egroup} -g ${egid} || die "enewgroup failed"
894 ;;
895
896 *-netbsd*)
897 case ${egid} in
898 *[!0-9]*) # Non numeric
899 for ((egid = 101; egid <= 999; egid++)); do
900 [[ -z $(egetent group ${egid}) ]] && break
901 done
902 esac
903 groupadd -g ${egid} ${egroup} || die "enewgroup failed"
904 ;;
905
906 *)
907 # We specify -r so that we get a GID in the system range from login.defs
908 groupadd -r ${opts} ${egroup} || die "enewgroup failed"
909 ;;
910 esac
911 export SANDBOX_ON="${oldsandbox}"
912}
913
914# @FUNCTION: edos2unix 538# @FUNCTION: edos2unix
915# @USAGE: <file> [more files ...] 539# @USAGE: <file> [more files ...]
916# @DESCRIPTION: 540# @DESCRIPTION:
917# A handy replacement for dos2unix, recode, fixdos, etc... This allows you 541# A handy replacement for dos2unix, recode, fixdos, etc... This allows you
918# to remove all of these text utilities from DEPEND variables because this 542# to remove all of these text utilities from DEPEND variables because this
919# is a script based solution. Just give it a list of files to convert and 543# is a script based solution. Just give it a list of files to convert and
920# they will all be changed from the DOS CRLF format to the UNIX LF format. 544# they will all be changed from the DOS CRLF format to the UNIX LF format.
921edos2unix() { 545edos2unix() {
922 echo "$@" | xargs sed -i 's/\r$//' 546 [[ $# -eq 0 ]] && return 0
547 sed -i 's/\r$//' -- "$@" || die
923} 548}
924 549
925# Make a desktop file ! 550# Make a desktop file !
926# Great for making those icons in kde/gnome startmenu ! 551# Great for making those icons in kde/gnome startmenu !
927# Amaze your friends ! Get the women ! Join today ! 552# Amaze your friends ! Get the women ! Join today !
1461 eerror "Unknown filetype \"${filetype}\" ?" 1086 eerror "Unknown filetype \"${filetype}\" ?"
1462 false 1087 false
1463 ;; 1088 ;;
1464 esac 1089 esac
1465 assert "failure unpacking (${filetype}) makeself ${shrtsrc} ('${ver}' +${skip})" 1090 assert "failure unpacking (${filetype}) makeself ${shrtsrc} ('${ver}' +${skip})"
1466}
1467
1468# @FUNCTION: check_license
1469# @USAGE: [license]
1470# @DESCRIPTION:
1471# Display a license for user to accept. If no license is
1472# specified, then ${LICENSE} is used.
1473check_license() {
1474 local lic=$1
1475 if [ -z "${lic}" ] ; then
1476 lic="${PORTDIR}/licenses/${LICENSE}"
1477 else
1478 if [ -e "${PORTDIR}/licenses/${lic}" ] ; then
1479 lic="${PORTDIR}/licenses/${lic}"
1480 elif [ -e "${PWD}/${lic}" ] ; then
1481 lic="${PWD}/${lic}"
1482 elif [ -e "${lic}" ] ; then
1483 lic="${lic}"
1484 fi
1485 fi
1486 local l="`basename ${lic}`"
1487
1488 # here is where we check for the licenses the user already
1489 # accepted ... if we don't find a match, we make the user accept
1490 local alic
1491 eshopts_push -o noglob # so that bash doesn't expand "*"
1492 for alic in ${ACCEPT_LICENSE} ; do
1493 if [[ ${alic} == ${l} ]]; then
1494 eshopts_pop
1495 return 0
1496 fi
1497 done
1498 eshopts_pop
1499 [ ! -f "${lic}" ] && die "Could not find requested license ${lic}"
1500
1501 local licmsg=$(emktemp)
1502 cat <<-EOF > ${licmsg}
1503 **********************************************************
1504 The following license outlines the terms of use of this
1505 package. You MUST accept this license for installation to
1506 continue. When you are done viewing, hit 'q'. If you
1507 CTRL+C out of this, the install will not run!
1508 **********************************************************
1509
1510 EOF
1511 cat ${lic} >> ${licmsg}
1512 ${PAGER:-less} ${licmsg} || die "Could not execute pager (${PAGER}) to accept ${lic}"
1513 einfon "Do you accept the terms of this license (${l})? [yes/no] "
1514 read alic
1515 case ${alic} in
1516 yes|Yes|y|Y)
1517 return 0
1518 ;;
1519 *)
1520 echo;echo;echo
1521 eerror "You MUST accept the license to continue! Exiting!"
1522 die "Failed to accept license"
1523 ;;
1524 esac
1525} 1091}
1526 1092
1527# @FUNCTION: cdrom_get_cds 1093# @FUNCTION: cdrom_get_cds
1528# @USAGE: <file on cd1> [file on cd2] [file on cd3] [...] 1094# @USAGE: <file on cd1> [file on cd2] [file on cd3] [...]
1529# @DESCRIPTION: 1095# @DESCRIPTION:
2026 case ${opt} in 1592 case ${opt} in
2027 -a) return $(( r != 0 )) ;; 1593 -a) return $(( r != 0 )) ;;
2028 -o) return $(( r == $# )) ;; 1594 -o) return $(( r == $# )) ;;
2029 esac 1595 esac
2030} 1596}
1597
1598# @FUNCTION: in_iuse
1599# @USAGE: <flag>
1600# @DESCRIPTION:
1601# Determines whether the given flag is in IUSE. Strips IUSE default prefixes
1602# as necessary.
1603#
1604# Note that this function should not be used in the global scope.
1605in_iuse() {
1606 debug-print-function ${FUNCNAME} "${@}"
1607 [[ ${#} -eq 1 ]] || die "Invalid args to ${FUNCNAME}()"
1608
1609 local flag=${1}
1610 local liuse=( ${IUSE} )
1611
1612 has "${flag}" "${liuse[@]#[+-]}"
1613}
1614
1615# @FUNCTION: use_if_iuse
1616# @USAGE: <flag>
1617# @DESCRIPTION:
1618# Return true if the given flag is in USE and IUSE.
1619#
1620# Note that this function should not be used in the global scope.
1621use_if_iuse() {
1622 in_iuse $1 || return 1
1623 use $1
1624}
1625
1626# @FUNCTION: usex
1627# @USAGE: <USE flag> [true output] [false output] [true suffix] [false suffix]
1628# @DESCRIPTION:
1629# If USE flag is set, echo [true output][true suffix] (defaults to "yes"),
1630# otherwise echo [false output][false suffix] (defaults to "no").
1631usex() { use "$1" && echo "${2-yes}$4" || echo "${3-no}$5" ; } #382963
1632
1633check_license() { die "you no longer need this as portage supports ACCEPT_LICENSE itself"; }
1634
1635fi

Legend:
Removed from v.1.363  
changed lines
  Added in v.1.372

  ViewVC Help
Powered by ViewVC 1.1.20