process_zip.sh 2.98 KB
Newer Older
1 2 3 4
#!/bin/bash


shopt -s extglob
lwc-tester committed
5
export PYTHONPATH="$PYTHONPATH:$(pwd)"
6 7

function run() {
8 9 10
  SUBMISSION="$1"
  TEMPLATE="$2"
  DESTDIR="$3"
11

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
  if [[ ! -d "templates/$TEMPLATE" ]]; then
    echo "Template '$TEMPLATE' does not exist"
    exit 1
  fi

  if [[ -e "$DESTDIR" ]]; then
    echo "Destination directory '$DESTDIR' already exists"
    exit 1
  fi

  TMPDIR=$(mktemp -d -t build-XXXXXXXXXX)

  if [[ ! -d "$TMPDIR" ]]; then
    echo "Failed to create a temporary build directory"
    exit 1
  fi

  echo "Template is '$TEMPLATE', destination directory is '$DESTDIR', temporary directory is '$TMPDIR'"

  mkdir -p $DESTDIR
  echo "Compiling for template '$TEMPLATE' in directory '$TMPDIR'"
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

  TEMPLATE_PATH="templates/$TEMPLATE"

  TEMPLATE_COMMIT=$(git rev-list -1 HEAD -- "$TEMPLATE_PATH")
  if [ -z "$TEMPLATE_COMMIT" ]; then
    echo "Could not retrieve the git commit of the template"
    exit 1
  fi

  TEMPLATE_TIMESTAMP=$(git show -s --format=%ct "$TEMPLATE_COMMIT")
  if [ -z "$TEMPLATE_TIMESTAMP" ]; then
    echo "Could not retrieve the git commit date of the template"
    exit 1
  fi

  ./compile_all.py -s $SUBMISSION -t $TEMPLATE_PATH -b "$TMPDIR"
49
  
50 51
  for cipher in $TMPDIR/*; do
    if [[ ! -d $cipher ]]; then continue; fi
lwc-tester committed
52 53

    CIPHER_SLUG=$(basename $cipher)
54
    CIPHER_TIMESTAMP=$(cat "$cipher/cipher_mtime.txt")
lwc-tester committed
55

56
    TEST_PATH="$DESTDIR/$CIPHER_SLUG"
lwc-tester committed
57
    mkdir -p "$TEST_PATH" || exit 1
58 59
    TEST_PATH=$(realpath $TEST_PATH)

lwc-tester committed
60 61
    mv $cipher/*.log "$TEST_PATH"
    mv "$cipher/LWC_AEAD_KAT.txt" "$TEST_PATH"
62

63
    case $TEMPLATE in
lwc-tester committed
64
      f7)         mv $cipher/build/f7.* "$TEST_PATH"
65 66
                  ;;

lwc-tester committed
67
      maixduino)  mv $cipher/.pio/build/sipeed-maixduino/firmware.* "$TEST_PATH"
68 69
                  ;;

lwc-tester committed
70
      bluepill)   mv $cipher/.pio/build/bluepill_f103c8/firmware.* "$TEST_PATH"
71 72
                  ;;
      
lwc-tester committed
73
      uno)        mv $cipher/.pio/build/uno/firmware.* "$TEST_PATH"
74 75
                  ;;
      
lwc-tester committed
76
      esp32)      mv $cipher/.pio/build/esp32dev/firmware.* $cipher/.pio/build/esp32dev/partitions.bin "$TEST_PATH"
77
                  ;;
78
    esac
79

80

81 82 83
    curl \
      --request 'POST' \
      --header "Content-Type: application/json" \
84 85 86 87 88 89 90 91 92
      --data "\
{\
\"build_dir\":\"$TEST_PATH\",\
\"cipher\":\"$CIPHER_SLUG\",\
\"cipher_timestamp\":\"$CIPHER_TIMESTAMP\",\
\"template\":\"$TEMPLATE\",\
\"template_commit\":\"$TEMPLATE_COMMIT\",\
\"template_timestamp\":\"$TEMPLATE_TIMESTAMP\"\
}" \
93
      "http://127.0.0.1:5002/schedule_test"
lwc-tester committed
94

95
  done
96

97
  rm -rf "$TMPDIR"
98 99 100
}

if [[ $1 == "run" ]]; then
101
  run $2 $3 $4
102
else
103 104 105 106 107
  ZIP_PATH="$1"
  if [[ ! -f $ZIP_PATH ]]; then
    echo "Error: '$ZIP_PATH' is not a zip file"
    exit 1
  fi
108 109 110

  MAINDIR=email-submissions/$(date +%Y-%m-%d-%H:%M)
  mkdir -p $MAINDIR
lwc-tester committed
111
  TMPDIR=$(mktemp -d -t submission-XXXXXXXXXX)
112
  echo "Extracting in $TMPDIR"
lwc-tester committed
113
  unzip $ZIP_PATH -d $TMPDIR
114 115 116 117
  for i in templates/*; do
    TEMPLATE="${i##*/}"
    echo "Template is $TEMPLATE"
    touch $MAINDIR/locky.lock
118
    flock $MAINDIR/locky.lock $0 run $TMPDIR $TEMPLATE $MAINDIR/$TEMPLATE
119
  done
lwc-tester committed
120
  rm -rf $TMPDIR
121 122 123 124

fi