aboutsummaryrefslogtreecommitdiff
path: root/pbuilder-runhooks
blob: 8e96e950f5bc419f975676a28c8abf7bab6b86ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#! /bin/bash
#==========================================================================
# Execute any hooks required for this segment
#
# Title:         pbuilder-runhooks
# Description    User hooks for pbuilder package
# Programmed by: Dale Amon <amon@vnl.com>

# $HOOKDIR is set in /etc/pbuilderrc and contains a list of executable
# programs or scripts. The type is only limited by what you expect will
# be available in your chrooted environment after debootstrap has done
# an initial install. The functions must be named in the format:
# <prefex><digit><digit><descriptive name>. Currently only X is defined;
# scripts prefixed with X will be run just before the chroot environment
# is exited and the base.tgz file is created or updated.

# $BUILDPLACE is as used in pbuilder. When already inside chroot, this
# routine can be called with $BUILDPLACE='' (from pdebuild-internal)

# TODO:  * Are there any other executable backup types we should
#          filtered besides *~ and *.bak?
#
#==========================================================================

hooks=tmp/hooks

#==========================================================================
# Set up fresh chroot'd hooks tmp script directory
#

function loadhooks () {
    if [ -z "$HOOKDIR" ]; then
        return ;
    fi
    if [ -d "$BUILDPLACE/$hooks" ]; then
        rm -rf "$BUILDPLACE/$hooks"
    fi
    if [ -d "$HOOKDIR" ]; then
        mkdir -p "$BUILDPLACE/$hooks"
        if ! cp -aL "$HOOKDIR/"* "$BUILDPLACE/$hooks"; then
            log.w "no hooks found in the hookdir '$HOOKDIR'"
        fi
    else
        log.w "hookdir $HOOKDIR does not exist, skipping"
    fi
}

#--------------------------------------------------------------------------
# Tidy up after ourselves. (Anything we leave behind ends up in base.tgz)
#

function unloadhooks () {
    if [ -z "$HOOKDIR" ]; then
        return ;
    fi
    rm -rf "$BUILDPLACE/$hooks"
}

#--------------------------------------------------------------------------
# Execute every script found in the chroot'd target directory. We only
# test for whether a file is executable because we have no idea what
# the user had put in their dist. If they want PL/1 and ADA on the base
# dist or have decided to use emacslisp for everything, it's their
# problem.
#
# Args:    Required prefix on hook fn name
# Returns: none
#

function executehooks () {
    local prefix="$1"
    if [ -z "$HOOKDIR" ]; then
        return
    fi
    for fn in "$BUILDPLACE/$hooks/$prefix"[0-9][0-9]* ; do
       case "$fn" in
           "$BUILDPLACE/$hooks/$prefix"'[0-9][0-9]*')
               log.w "no hooks of type ${prefix} found -- ignoring"
               ;;
           *~)
               log.w "skipping an editor backup file $fn"
               ;;
           *.bak)
               log.w "skipping a backup file $fn"
               ;;
           *)
               if [ -x "$fn" ]; then
                   log.i "user script $fn starting"
                   BUILDDIR="$BUILDDIR" \
                       $CHROOTEXEC "/$hooks/$(basename "$fn")"
                   log.i "user script $fn finished"
               else
                   if [ -f "$fn" ]; then
                       filetype=$(basename "$fn" )
                       log.w "execute priv not set on file $filetype, not executing."
                   else
                       # Should it reach here ? This case should be caught in the above case.
                       log.w "no hooks of type ${prefix} found -- internal error in logic"
                   fi
               fi
               ;;
       esac
    done
}

#--------------------------------------------------------------------------