Skip to content

Commit

Permalink
scripts: librefactor: implement download functions
Browse files Browse the repository at this point in the history
test version

Signed-off-by: YuLong Yao <[email protected]>

add

Signed-off-by: YuLong Yao <[email protected]>
  • Loading branch information
feilongfl committed Aug 9, 2022
1 parent 425716b commit a1c65ef
Show file tree
Hide file tree
Showing 19 changed files with 200 additions and 44 deletions.
2 changes: 2 additions & 0 deletions scripts/lib_refactor/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tmp
dest
4 changes: 4 additions & 0 deletions scripts/lib_refactor/clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

rm -rf tmp
rm -rf dest
44 changes: 0 additions & 44 deletions scripts/lib_refactor/make_new_hal.sh

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions scripts/lib_refactor/resources/gd32e10x.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
NAME=gd32e10x
VER=1.3.0

URL=https://gd32mcu.com/download/down/document_id/230/path_type/1
FILENAME=${NAME}.rar
HASH=a395cdc5b4689259176e0bbbcd5350f10a026ceb

function after_compress() {
find $2/*/ -maxdepth 1 -exec mv {} $2 \;
find $2/ -maxdepth 1 -name "*Firmware_Library*" -exec rm -rv {} \;
}
11 changes: 11 additions & 0 deletions scripts/lib_refactor/resources/gd32f3x0.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
NAME=gd32f3x0
VER=2.2.1

URL=https://www.gd32mcu.com/download/down/document_id/143/path_type/1
FILENAME=${NAME}.rar
HASH=9619dc83b0252926ab709f1a602d49e978f54fcd

function after_compress() {
find $2/*/ -maxdepth 1 -exec mv {} $2 \;
find $2/ -maxdepth 1 -name "*Firmware_Library*" -exec rm -rv {} \;
}
11 changes: 11 additions & 0 deletions scripts/lib_refactor/resources/gd32f403.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
NAME=gd32f403
VER=2.1.4

URL=https://www.gd32mcu.com/download/down/document_id/132/path_type/1
FILENAME=${NAME}.rar
HASH=e1cc01fafc03afd47bd0f4f3a088cf53de414051

function after_compress() {
find $2/*/ -maxdepth 1 -exec mv {} $2 \;
find $2/ -maxdepth 1 -name "*Firmware_Library*" -exec rm -rv {} \;
}
13 changes: 13 additions & 0 deletions scripts/lib_refactor/resources/gd32f4xx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
NAME=gd32f4xx
VER=3.0.1

URL=https://www.gd32mcu.com/download/down/document_id/247/path_type/1
FILENAME=${NAME}.rar
HASH=46e35c69ea2483b377af03d50da0ca6a4efaf69f

function after_compress() {
echo $1 $2
find $2/*/ -maxdepth 1 -exec mv {} $2 \;
find $2/*/ -maxdepth 1 -exec mv {} $2 \;
find $2/ -maxdepth 1 -name "*Firmware_Library*" -exec rm -rv {} \;
}
11 changes: 11 additions & 0 deletions scripts/lib_refactor/resources/gd32vf103.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
NAME=gd32vf103
VER=1.1.4

URL=https://www.gd32mcu.com/download/down/document_id/386/path_type/1
FILENAME=${NAME}.rar
HASH=81a2e0fb119604e595066064d9ff423803933c00

function after_compress() {
find $2/*/ -maxdepth 1 -exec mv {} $2 \;
find $2/ -maxdepth 1 -name "*Firmware_Library*" -exec rm -rv {} \;
}
3 changes: 3 additions & 0 deletions scripts/lib_refactor/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

find ./resources -name "*.conf" | xargs -n1 ./utils/build.sh
3 changes: 3 additions & 0 deletions scripts/lib_refactor/update_hash.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

find ./resources -name "*.conf" | xargs -n1 ./utils/gen_hash.sh
29 changes: 29 additions & 0 deletions scripts/lib_refactor/utils/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

# prepare env var
source utils/utils.sh
source $1

TMPDIR=tmp
DESTDIR=dest/${NAME}

echo Processing ${NAME}

# make workspace folder
mkdir -p ${TMPDIR}
mkdir -p ${DESTDIR}

# download files
getfile ${TMPDIR}/${FILENAME} ${URL} ${HASH}
checkfilehash ${HASH} ${TMPDIR}/${FILENAME}
if [ $? -ne 0 ]; then
# abort if hash not match
echo "file ${NAME}:${FILENAME} download failed!"
exit 1
fi

uncompress ${TMPDIR}/${FILENAME} ${DESTDIR}

drop_unnessary_files ${DESTDIR}
change_file_format ${DESTDIR}
patch_cocci ${DESTDIR}
File renamed without changes.
21 changes: 21 additions & 0 deletions scripts/lib_refactor/utils/gen_hash.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

source utils/utils.sh
source $1

TMPDIR=tmp

# make workspace folder
mkdir -p ${TMPDIR}

# force re-download
download ${TMPDIR}/${FILENAME} ${URL}
if [ $? -ne 0 ]; then
# abort if hash not match
echo "file ${NAME}:${FILENAME} hash update failed!"
exit 1
fi

HASH=$(sha1sum ${TMPDIR}/${FILENAME} | cut -d ' ' -f 1)
echo "file ${NAME}:${FILENAME} hash update to ${HASH}"
sed -i -r "s/^HASH=.*$/HASH=${HASH}/" $1
81 changes: 81 additions & 0 deletions scripts/lib_refactor/utils/utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env bash

function after_compress() {
echo "no actions in after_compress hook"
}

function download() {
wget -O $1 $2
return $?
}

function checkfilehash() {
echo "$1 $2"
echo "$1 $2" | sha1sum -c -
return $?
}

function getfile() {
checkfilehash $3 $1
if [ $? -ne 0 ]; then
# download if not exist or hash not match
download $1 $2
fi
}

function uncompress_rar() {
unrar x $1 $2
}

function uncompress() {
rm -rvf $2
mkdir -p $2

ftpye=$(file "$1")
case "$ftpye" in
"$1: RAR archive"*)
uncompress_rar $1 $2
;;
*)
echo unsupport file format: $ftpye
;;
esac

after_compress $1 $2 # call hook
}

function rename_to_lower() {
mv -v $(dirname $1 | tr '[:upper:]' '[:lower:]')/$(basename $1) $(echo $1 | tr '[:upper:]' '[:lower:]')
}
export -f rename_to_lower

function drop_unnessary_files() {
libbak=$1.bak
rm -rf $libbak
mv $1 $libbak
mkdir -p $1

cp -rv $libbak/Firmware/CMSIS $1/cmsis
find $1/cmsis/GD/ -maxdepth 2 -type d | xargs -I {} bash -c 'rename_to_lower "{}"' \;
rm $1/cmsis/*.h
rm -r $1/cmsis/gd/*/source/ARM
rm -r $1/cmsis/gd/*/source/IAR

cp -rv $libbak/Firmware/*_standard_peripheral $1/standard_peripheral
find $1/standard_peripheral -maxdepth 1 -type d | xargs -I {} bash -c 'rename_to_lower "{}"' \;
cp -v $libbak/Template/*_libopt.h $1/standard_peripheral/include/
rm -rv $libbak
}

function change_file_format() {
find $1 type f -print0 | xargs -0 dos2unix -k
}

function patch_cocci() {
script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
# pushd $script_dir
for cocci in $script_dir/patches/*.cocci; do
find $1 -name "*.h" -or -name "*.c" | xargs -P0 -n1 echo $script_dir/eval_patch.sh $cocci
done
# popd
}

0 comments on commit a1c65ef

Please sign in to comment.