summaryrefslogtreecommitdiff
path: root/xib.sh
blob: 9c97a74a6632633a6d331397284bf5bab9cd72ef (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/bin/sh

[ -f /usr/lib/colors.sh ] && . /usr/lib/colors.sh
[ -f /usr/lib/glyphs.sh ] && . /usr/lib/glyphs.sh
[ -f /usr/lib/xilib.sh ] && . /usr/lib/xilib.sh

xib_dir="/var/lib/xib"
build_profile="/etc/xib_profile.conf"

priv_key="xi.pem"

buildfiles="/home/david/docs/proj/xilinux/buildfiles"
#buildfiles="$xib_dir/buildfiles"
seen="$xib_dir/seen"
logs="$xib_dir/logs"
chroot="$xib_dir/chroot"
stage="$xib_dir/stage"
logs="$xib_dir/logs"
local_repo="$xib_dir/repo"
keychain="$xib_dir/keychain"

quickfail=true

usage () {
    cat << EOF
${LIGHT_RED}Usage: ${RED}xib [option] [package]
${BLUE}Avaiable Options:
    ${BLUE}-d
        ${LIGHT_CYAN}daemon; run as a daemon, automatically rebuilding all packages
    ${BLUE}-p
        ${LIGHT_CYAN}publish; publish packages in the stage to the repo
${RESET}
EOF
}

# publish any packages in the stage directory to the repo
#
publish_package () {
    local xipkgs=$(ls $stage/*.xipkg)
    local packageslist="$local_repo/packages.list"
    local depsgraph="$local_repo/deps.graph"
    [ ! -d "$local_repo" ] && mkdir -p "$local_repo"
    [ ! -f "$packageslist" ] && touch $packageslist
    [ ! -f "$depsgraph" ] && touch $depsgraph

    for xipkg in $xipkgs; do
        local name=$(basename $xipkg ".xipkg")
        local filecount=$(tar -tvvf $xipkg | grep -cv ^d)
        local checksum=$(sha512sum $xipkg | awk '{ print $1 }')
        local size=$(stat -t $xipkg | cut -d" " -f2)
        local deps=$(grep "^DEPS=" $xipkg.info | sed -rn 's/DEPS=(.*)/\1/p' | tr '\n' ' ')

        sed -i "s/^$name.xipkg .*$//" $packageslist
        echo $name.xipkg $checksum $size $filecount  >> $packageslist

        sed -i "s/^$name: .*$//" $depsgraph
        echo "$name: $deps" >> $depsgraph

        [ -f $stage/$name.xipkg ] && mv $stage/$name.xipkg $local_repo/$name.xipkg
        [ -f $stage/$name.xipkg.info ] && mv $stage/$name.xipkg.info $local_repo/$name.xipkg.info
        [ -f $stage/$name.xibuild ] && mv $stage/$name.xibuild $local_repo/$name.xibuild
        [ -f $logs/$name.log ] && cp $logs/$name.log $local_repo/$name.log
    done

    [ ! -d "$seen" ] && mkdir -p $seen
    for s in $stage/*.xibsum; do
        s=$(basename $s)
        mv $stage/$s $seen/$s
    done
}

# get root package from package name
#
#   get_package_build [name]
#
get_package_build () {
    local buildfile=$(find $buildfiles/repo -name "$1.xibuild" | head -1)
    echo ${buildfile%/*}
}

# list all packages available
#
list_all () {
    ls -1 $buildfiles/repo/
}

# get package file from package name
#
get_package_file () {
    find $local_repo/ -name "$1.xipkg" | head -1
}

# build a single package using xibuild
#
# build_package [build directory]
#
build_package () {
    local name=$(basename $1)

    rm -rf $stage
    mkdir -p $stage

    [ ! -d "$logs" ] && mkdir -p $logs
    rm -f $logs/$name.log
    touch $logs/$name.log

    xibuild -v \
        -C $1 \
        -o $stage \
        -r $chroot \
        -l $logs/$name.log \
        -k $keychain/$priv_key \
        -p $build_profile \
        || return 1

    get_buildfiles_hash $1 > $stage/$name.xibsum
}

# installs a package
#  
#   package_install [name] [xipkg file]
#
package_install () {
    local name=$1
    local xipkg=$2
    [ -f $xipkg ] &&
        xipkg -qlny -r $3 install $xipkg && printf "${PASS}${CHECKMARK}\n" || printf "${NEUTRAL}${CHECKMARK}\n"
}

# get the direct make dependencies of a single package
#
#   get_deps [name]
#
get_deps () {
    local package=$(get_package_build $1)
    [ -d $package ] && 
             sed -rn "s/^MAKEDEPS=\"(.*)\"/\1/p" $package/$1.xibuild
}

# list dependencies of a list of packages
# 
#   list_deps [dependencies]
#
list_deps () {
    local deps=""
    while [ "$#" != "0" ]; do
        # pop a value from the args
        local package=$1

        #only add if not already added
        echo ${deps} | grep -q "\b$package\b" || deps="$deps $package"

        for dep in $(get_deps $package); do
            # if not already checked
            echo $@ | grep -qv "\b$dep\b" && set -- $@ $dep
        done
        shift
    done
    echo "$deps" 
}

# check if a package build dir actually requires any building
#
#   is_meta [xibuild dir]
#
is_meta () {
    local package=$1
    local name=${package#${package%/*}/}
    local src=$(sed -rn "s/^SOURCE=\"(.*)\"/\1/p" $package/$name.xibuild | head -1)
    [ -z "$src" ]
}

# get the checksum of a whole package's buildfiles
#
#   get_buildfiles_hash [xibuild dir]
#
get_buildfiles_hash () {
    cat $1/*.xibuild | sha512sum | cut -d' ' -f1
}

# build a single package
#
#   xib_single [name]
#
xib_single () {
    local name=$1
    local package=$(get_package_build $name)
    local deps=$(get_deps $name)
    local missing=""

    for dep in $deps; do
        [ -e "$chroot/var/lib/xipkg/installed/$dep" ] || {
            pkgfile=$(get_package_file $dep)
                    [ "${#pkgfile}" = "0" ] && missing="$missing $dep"
            printf "${LIGHT_GREEN}+${LIGHT_CYAN}install $dep"
            package_install $dep $(get_package_file $dep) $chroot
        }
    done

    [ "${#missing}" != "0" ] && {
        printf "${RED}$name depends on these packages to be build before: ${LIGHT_RED}$missing\n"
        return 1
    }

    build_package $package \
        && publish_package \
        && {
            [ -e "$chroot/var/lib/xipkg/installed/$name" ] && {
                xi -r $chroot -nqyl remove $name
        } || true
    }
}

# build all packages
#
xib_all () {
    for name in $(build_order $(list_all)); do

        package=$(get_package_build $name)
        [ "${#package}" != 0 ] && [ -d "$package" ] && {
            local package_name="$(basename ${package#/*})"
            xibsum=$(get_buildfiles_hash $package)
            [ -f "$seen/$package_name.xibsum" ] && \
                [ "$(cat "$seen/$package_name.xibsum")" = "$xibsum" ] && {
                printf "${BLUE}$package_name${LIGHT_BLUE}...already built!\n"
            } || {
                xib_single $name || {
                    $quickfail && return 0
                }
            }
            true

        } || {
            printf "${RED} could not find package for $name in $package $RESET\n"
        }
    done
}

# return the order that packages should be built
# sorted topologically from dependencies
#
#   build_order [packages...]
#
build_order () {
    for pkg in $@; do 
        for dep in $(get_deps $pkg); do
            echo $pkg $dep
        done
    done | tsort | reverse_lines
}

# main loop of the xib daemon
#
xibd () {
    quickfail=false
    while true; do
        cd $buildfiles
        git pull
        cd $xib_dir
        xib_all
        sleep 5
    done
}

[ "$#" = 0 ] && {
    xib_all
} || {
    case "$1" in 
        "-d")
            xibd;;
        "-p")
            publish_package
            ;;
        "-h")
            usage
            ;;
        *)
        for x in $@; do
            xib_single $x
        done
        ;;
    esac
}