Root/scripts/schhist2web

Source at commit 2921bcea47bc9926587d60469c8bbd2e02d906a8 created 13 years 7 months ago.
By Werner Almesberger, New script sanitize-profile to remove glitches from a KiCad profile.
1#!/bin/sh
2#
3# schhist2web - Web-browseable graphical revision history of schematics
4#
5# Written 2010 by Werner Almesberger
6# Copyright 2010 Werner Almesberger
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13
14
15OUTDIR=_out
16THUMB_OPTS="-w 3 -d 60 -c 0.5,0.5,0.5 -n 1,1,0"
17BG_COLOR="f0f0ff"
18FNAME_COLOR="#b0f0ff"
19SEP_COLOR="#000000"
20
21
22shrink()
23{
24    pnmscale -width 120 "$@" || exit
25}
26
27
28pngdiff()
29{
30    # pngdiff preproc outfile arg ...
31    pp="$1"
32    of="$2"
33    shift 2
34    if ! PATH=$PATH:`dirname $0`/ppmdiff ppmdiff "$@" "$out/_tmp"; then
35    rm -f "$out/_tmp"
36    return 1
37    fi
38    $pp "$out/_tmp" | pnmtopng >"$of"
39    rm "$out/_tmp"
40}
41
42
43usage()
44{
45    cat <<EOF 2>&1
46usage: $0 [-c cache-dir] [-n] [-S] [top-dir] [top-schem] [out-dir]
47
48  top-dir top-level directory of the git archive (default: locate it)
49  top-schem root sheet of the schematics (default: locate it in top-dir)
50  out-dir output directory (default: $OUTDIR)
51  -c cache-dir cache directory (default: same as out-dir)
52  -n don't use previous cache content (rebuild the cache)
53  -S sanitize KiCad profile
54EOF
55    exit 1
56}
57
58
59no_cache=false
60sanitize=
61
62while true; do
63    case "$1" in
64    -n) no_cache=true
65    shift;;
66    -c) [ -z "$1" ] && usage
67    cache="$1"
68    shift 2;;
69    -S) sanitize=-S
70    shift;;
71    -*) usage;;
72    *) break;;
73    esac
74done
75
76if [ ! -z "$1" -a -d "$1/.git" ]; then
77    dir="$1"
78    shift
79else
80    dir=.
81    while [ ! -d $dir/.git ]; do
82    if [ $dir -ef $dir/.. ]; then
83        echo "no .git/ directory found in hierarchy" 1>&2
84        exit 1
85    fi
86    dir=$dir/..
87    done
88    echo "found top-dir: $dir" 1>&2
89fi
90
91if [ ! -z "$1" -a -f "$dir/$1" -a \
92  -f "$dir"/`dirname "$1"`/`basename "$1" .sch`.pro ]; then
93    sch="$1"
94    shift
95else
96    for n in "$dir"/*.sch; do
97    [ -f `dirname "$n"`/`basename "$n" .sch`.pro ] || continue
98    if [ ! -z "$sch" ]; then
99        echo "multiple choices for top-level .sch file" 1>&2
100        exit 1
101    fi
102    sch="$n"
103    done
104    if [ -z "$sch" -o "$sch" = "$dir/*.sch" ]; then
105    echo "no candidate for top-level .sch file found" 1>&2
106    exit 1
107    fi
108    echo "found root sheet: $sch" 1>&2
109fi
110
111if [ ! -z "$1" ] && [ ! -e "$1" ] || [ -d "$1" -a ! -d "$1"/.git ]; then
112    out="$1"
113    shift
114else
115    out=$OUTDIR
116fi
117[ -z "$cache" ] && cache="$out"
118
119[ -z "$1" ] || usage
120
121PATH=`dirname "$0"`:"$PATH"
122first=`gitenealogy "$dir" "$sch" | sed '$s/ .*//p;d'`
123schname=`gitenealogy "$dir" "$sch" | sed '$s/^.* //p;d'`
124
125rm -rf "$out/diff_*" "$out/thumb_*" "$out/names"
126$no_cache && rm -rf "$cache"
127mkdir -p "$out/names"
128mkdir -p "$cache"
129
130head=
131for n in $first `cd "$dir" && git rev-list --reverse $first..HEAD`; do
132    ( cd "$dir" && git show --pretty=format:'' --name-only $n; ) |
133      egrep -q '\.sch$|\.pro$|\.lib$' || continue
134    echo Processing $n
135    new=`gitenealogy "$dir" "$sch" | sed "/^$n /s///p;d"`
136    if [ ! -z "$new" ]; then
137    echo Name change $schname to $new 1>&2
138    schname="$new"
139    fi
140    trap "rm -rf \"$cache/ppm_$n\" \"$cache/fat_$n\"" 0
141    if [ ! -d "$cache/ppm_$n" ]; then
142    rm -rf "$cache/ppm_$n"
143    mkdir "$cache/ppm_$n"
144    gitsch2ppm $sanitize -k "$dir" "$schname" $n "$cache/ppm_$n" || exit
145    gitsch2ppm -c -w 500 "$dir" "$schname" $n "$cache/fat_$n" || exit
146    fi
147    for m in "$cache/ppm_$n/"*; do
148    [ "$m" = "$cache/ppm_$n/*" ] && break
149    touch "$out/names/"`basename "$m" .ppm`
150    done
151    trap 0
152    head=$n
153done
154
155if [ -z "$head" ]; then
156    echo "no usable head found" 2>&1
157    exit 1
158fi
159
160ppmmake '#e0e0e0' 5 20 | pnmtopng >"$out"/unchanged.png
161
162index="$out/index.html"
163{
164    cat <<EOF
165<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
166<HTML>
167EOF
168    if [ ! -z "$SCHHIST_TITLE" ]; then
169    echo "<TITLE>$SCHHIST_TITLE</TITLE>"
170    fi
171    echo "<BODY>"
172    if [ ! -z "$SCHHIST_TITLE" ]; then
173    echo "<H1>"
174    [ -z "$SCHHIST_HOME_URL" ] || echo "<A href=\"$SCHHIST_HOME_URL\">"
175    echo "$SCHHIST_TITLE"
176    [ -z "$SCHHIST_HOME_URL" ] || echo "</A>"
177    echo "</H1>"
178    fi
179    cat <<EOF
180<TABLE bgcolor="$BG_COLOR" callpadding=1>
181<TR bgcolor="$FNAME_COLOR">
182EOF
183    for m in `ls -1 "$out/names"`; do
184    echo "<TD><B>$m</B>"
185    done
186} >"$index"
187
188next="$head"
189for n in `cd "$dir" && git rev-list $first..HEAD~1` $first; do
190    [ -d "$cache/ppm_$n" ] || continue
191    empty=true
192    s="<TR><TR>"
193    mkdir -p "$out/diff_$next" "$out/thumb_$next"
194    for m in `ls -1 "$out/names"`; do
195    a="$cache/ppm_$n/$m.ppm"
196    fat_a="$cache/fat_$n/$m.ppm"
197    b="$cache/ppm_$next/$m.ppm"
198    fat_b="$cache/fat_$next/$m.ppm"
199    diff="$out/diff_$next/$m.png"
200    thumb="$out/thumb_$next/$m.png"
201
202    if [ -f "$a" -a -f "$b" ]; then
203        s="$s<TD align=\"center\" valign=\"middle\">"
204        if ! pngdiff cat "$diff" "$a" "$b"; then
205        s="$s<IMG src=\"unchanged.png\""
206        continue
207        fi
208        pngdiff shrink "$thumb" -f $THUMB_OPTS "$fat_a" "$fat_b" \
209          "$a" "$b" || exit
210    elif [ -f "$a" ]; then
211        s="$s<TD>"
212        pngdiff cat "$diff" -f -c 1,0,0 "$a" "$a" || exit
213        pngdiff shrink "$thumb" -f -c 1,0,0 $THUMB_OPTS "$fat_a" "$fat_a" \
214          || exit
215    elif [ -f "$b" ]; then
216        s="$s<TD>"
217        pngdiff cat "$diff" -f -c 0,1,0 "$b" "$b" || exit
218        pngdiff shrink "$thumb" -f -c 0,1,0 $THUMB_OPTS "$fat_b" "$fat_b" \
219          || exit
220    else
221        s="$s<TD>"
222        continue
223    fi
224    echo "$s" >>"$index"
225    s=
226    empty=false
227    echo "<A href=\"diff_$next/$m.png\"><IMG src=\"thumb_$next/$m.png\"></A>" >>"$index"
228    done
229    if ! $empty; then
230    (
231        cat <<EOF
232$s<TD valign="middle">
233<TABLE bgcolor="$SEP_COLOR" cellspacing=0 width="100%"><TR><TD></TABLE>
234EOF
235    mkdir -p "$out/diff_$next" "$out/thumb_$next"
236        echo "<PRE>"
237        ( cd "$dir" && git log --pretty=short $next~1..$next; ) |
238          if [ -z "$SCHHIST_COMMIT_TEMPLATE" ]; then
239        cat
240          else
241        url=`echo "$SCHHIST_COMMIT_TEMPLATE" | sed "s/{}/$next/g"`
242        sed "s|^commit |<A href=\"$url\">commit</a> |"
243          fi |
244          sed '/^<.*>commit</n;s/&/&amp;/g;s/</\&lt;/g;s/>/\&gt;/g'
245        echo "</PRE>"
246    ) >>"$index"
247    fi
248    next=$n
249done
250
251cat <<EOF >>"$index"
252</TABLE>
253<HR>
254`date -u '+%F %X'` UTC
255</BODY>
256</HTML>
257EOF
258

Archive Download this file



interactive