summaryrefslogtreecommitdiff
path: root/contrib/get-bug-attachments
blob: 80205bafff05b7c42afde670916684325599ccd4 (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
#!/bin/bash
#
# get-bug-attachments
# Morgan Deters <mdeters@cs.nyu.edu>
# Wed, 26 Sep 2012 09:40:10 -0400
#

if [ $# -lt 1 ]; then
  echo "usage: `basename $0` bugids.." >&2
  exit 1
fi

while [ $# -gt 0 ]; do

bugid="$1"
shift

function webcat {
  if which wget &>/dev/null; then
    wget -O - "$1"
  elif which curl &>/dev/null; then
    curl "$1"
  else
    echo "Please install wget or curl." >&2;
    exit 1
  fi
}

function webget {
  if which wget &>/dev/null; then
    tmpfile="$(mktemp get_bug_attach.$$.XXXXXXXX)"
    filename="$(wget -qS -O "$tmpfile" "$1" 2>&1 | grep -i 'Content-disposition: attachment' | sed 's,.*filename="\(.*\)".*,\1,')"
    ext="$(echo "$filename" | sed 's,.*\.\(.*\),\1,')"
    if [ -e "$2.$ext" ] && ! diff -q "$tmpfile" "$2.$ext" &>/dev/null; then
      c=a
      while [ -e "$2$c.$ext" ] && ! diff -q "$tmpfile" "$2$c.$ext" &>/dev/null; do
        c=$(echo $c | tr a-y b-z)
      done
      mkdir -p "$(dirname "$2")"
      mv "$tmpfile" "$2$c.$ext"
      echo "$2$c.$ext"
    else
      mkdir -p "$(dirname "$2")"
      mv "$tmpfile" "$2.$ext"
      echo "$2.$ext"
    fi
  elif which curl &>/dev/null; then
    tmpfile="$(mktemp get_bug_attach.$$.XXXXXXXX)"
    filename="$(curl --head "$1" 2>&1 | grep -i 'Content-disposition: attachment' | sed 's,.*filename="\(.*\)".*,\1,')"
    curl "$1" >"$tmpfile" 2>/dev/null
    ext="$(echo "$filename" | sed 's,.*\.\(.*\),\1,')"
    if [ -e "$2.$ext" ] && ! diff -q "$tmpfile" "$2.$ext" &>/dev/null; then
      c=a
      while [ -e "$2$c.$ext" ] && ! diff -q "$tmpfile" "$2$c.$ext" &>/dev/null; do
        c=$(echo $c | tr a-y b-z)
      done
      mkdir -p "$(dirname "$2")"
      mv "$tmpfile" "$2$c.$ext"
      echo "$2$c.$ext"
    else
      mkdir -p "$(dirname "$2")"
      mv "$tmpfile" "$2.$ext"
      echo "$2.$ext"
    fi
  else
    echo "Please install wget or curl." >&2;
    exit 1
  fi
}

count=0
for attachment in $(\
  webcat "http://cvc4.cs.nyu.edu/bugs/show_bug.cgi?id=$bugid" 2>/dev/null \
  | grep ' href="attachment.cgi?id=[0-9][0-9]*' \
  | sed 's,.* href="attachment.cgi?id=\([0-9][0-9]*\).*,\1,' \
  | sort -nu); do

  let count++
  printf "%-30s " "Downloading attachment $attachment..."
  webget "http://cvc4.cs.nyu.edu/bugs/attachment.cgi?id=$attachment" "test/regress/regress0/bug$bugid"

done

if [ $count -eq 0 ]; then
  echo "There are no bug attachments for bug #$bugid."
else
  s=s
  [ $count -eq 1 ] && s=
  echo "Downloaded $count bug attachment$s for bug #$bugid."
fi

done
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback