From cc50024417ee0fb5d617f7b49dc37165ffc8325c Mon Sep 17 00:00:00 2001 From: Gulshan Singh Date: Mon, 5 Sep 2022 04:24:52 -0700 Subject: [PATCH] Format shell scripts with shfmt (#1123) * Add lint.sh script * Format shell scripts with shfmt and add to lint.sh Co-authored-by: Disconnect3d --- .github/workflows/lint.yml | 1 + docs/source/api/generate.sh | 2 +- lint.sh | 3 ++ profiling/benchmark.sh | 18 ++++---- profiling/profile.sh | 20 ++++----- setup-test-tools.sh | 88 ++++++++++++++++++------------------- setup.sh | 8 ++-- tests.sh | 8 ++-- 8 files changed, 75 insertions(+), 73 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 44dbfdf79..92a8072d5 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -23,6 +23,7 @@ jobs: pip install isort pip install black pip install flake8 + sudo snap install shfmt - name: Run linters run: | diff --git a/docs/source/api/generate.sh b/docs/source/api/generate.sh index ec9c13bc3..1b42fc8ec 100755 --- a/docs/source/api/generate.sh +++ b/docs/source/api/generate.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash set -ex -cat > $1.rst <$1.rst < /dev/null +make test >/dev/null git log --abbrev-commit --pretty=oneline HEAD^..HEAD gdb ./test \ - -ex "source ../gdbinit.py" \ - -ex "b main" -ex "r" \ - -ex "python import timeit; print(' 1ST RUN:', timeit.repeat('pwndbg.commands.context.context()', repeat=1, number=1, globals=globals())[0])" \ - -ex "si" \ - -ex "python import timeit; print(' 2ND RUN:', timeit.repeat('pwndbg.commands.context.context()', repeat=1, number=1, globals=globals())[0])" \ - -ex "si" \ - -ex "python import timeit; print('MULTIPLE RUNS:', timeit.repeat('pwndbg.commands.context.context()', repeat=1, number=10, globals=globals())[0] / 10)" \ - -ex "quit" | grep 'RUNS*:' + -ex "source ../gdbinit.py" \ + -ex "b main" -ex "r" \ + -ex "python import timeit; print(' 1ST RUN:', timeit.repeat('pwndbg.commands.context.context()', repeat=1, number=1, globals=globals())[0])" \ + -ex "si" \ + -ex "python import timeit; print(' 2ND RUN:', timeit.repeat('pwndbg.commands.context.context()', repeat=1, number=1, globals=globals())[0])" \ + -ex "si" \ + -ex "python import timeit; print('MULTIPLE RUNS:', timeit.repeat('pwndbg.commands.context.context()', repeat=1, number=10, globals=globals())[0] / 10)" \ + -ex "quit" | grep 'RUNS*:' diff --git a/profiling/profile.sh b/profiling/profile.sh index 9224765cf..2b1f416c1 100755 --- a/profiling/profile.sh +++ b/profiling/profile.sh @@ -1,30 +1,30 @@ #!/bin/bash -if ! (( $# )); then - cat <<- _EOF_ +if ! (($#)); then + cat <<-_EOF_ $0: [profile-script] Example: $0 context.py _EOF_ - exit 1 + exit 1 fi module=$(basename "${1/.py/}") basedir=$(dirname "$0") # Quick and dirty script to profile pwndbg using cProfile. -make -C "${basedir}" test > /dev/null +make -C "${basedir}" test >/dev/null gdb "${basedir}/test" \ - -ex "source ${basedir}/../gdbinit.py" \ - -ex "b main" \ - -ex "r" \ - -ex "python + -ex "source ${basedir}/../gdbinit.py" \ + -ex "b main" \ + -ex "r" \ + -ex "python import cProfile; import profiling.${module} as profile; profile.warmup(); cProfile.run('profile.run()', '${basedir}/stats')" \ - -ex "quit" + -ex "quit" python3 -c " import pstats @@ -33,7 +33,7 @@ p.strip_dirs().sort_stats('tottime').print_stats(20) " if command -v pyprof2calltree >/dev/null 2>&1 && command -v kcachegrind >/dev/null 2>&1; then - pyprof2calltree -k -i "${basedir}/stats" + pyprof2calltree -k -i "${basedir}/stats" fi # vim: ts=4 sw=4 noet diff --git a/setup-test-tools.sh b/setup-test-tools.sh index 51326fc99..4641a0936 100755 --- a/setup-test-tools.sh +++ b/setup-test-tools.sh @@ -16,63 +16,63 @@ echo "ZIGPATH set to $ZIGPATH" # lets overwrite it with a function that just executes things passed to sudo # (yeah it won't work for sudo executed with flags) if ! hash sudo 2>/dev/null && whoami | grep root; then - sudo() { - ${*} - } + sudo() { + ${*} + } fi linux() { - uname | grep -i Linux &>/dev/null + uname | grep -i Linux &>/dev/null } install_apt() { - sudo apt-get update || true - sudo apt-get install -y \ - nasm \ - gcc \ - libc6-dev \ - curl \ - build-essential \ - gdb - test -f /usr/bin/go || sudo apt-ge\t install -y golang + sudo apt-get update || true + sudo apt-get install -y \ + nasm \ + gcc \ + libc6-dev \ + curl \ + build-essential \ + gdb + test -f /usr/bin/go || sudo apt-ge\t install -y golang - # Install zig to current directory - # We use zig to compile some test binaries as it is much easier than with gcc + # Install zig to current directory + # We use zig to compile some test binaries as it is much easier than with gcc - ZIG_TAR_URL="https://ziglang.org/builds/zig-linux-x86_64-0.10.0-dev.3685+dae7aeb33.tar.xz" - ZIG_TAR_SHA256="dfc8f5ecb651342f1fc2b2828362b62f74fadac9931bda785b80bf7ecfcfabb2" - curl --output /tmp/zig.tar.xz "${ZIG_TAR_URL}" - ACTUAL_SHA256=$(sha256sum /tmp/zig.tar.xz | cut -d' ' -f1) - if [ "${ACTUAL_SHA256}" != "${ZIG_TAR_SHA256}" ]; then - echo "Zig binary checksum mismatch" - echo "Expected: ${ZIG_TAR_SHA256}" - echo "Actual: ${ACTUAL_SHA256}" - exit 1 - fi + ZIG_TAR_URL="https://ziglang.org/builds/zig-linux-x86_64-0.10.0-dev.3685+dae7aeb33.tar.xz" + ZIG_TAR_SHA256="dfc8f5ecb651342f1fc2b2828362b62f74fadac9931bda785b80bf7ecfcfabb2" + curl --output /tmp/zig.tar.xz "${ZIG_TAR_URL}" + ACTUAL_SHA256=$(sha256sum /tmp/zig.tar.xz | cut -d' ' -f1) + if [ "${ACTUAL_SHA256}" != "${ZIG_TAR_SHA256}" ]; then + echo "Zig binary checksum mismatch" + echo "Expected: ${ZIG_TAR_SHA256}" + echo "Actual: ${ACTUAL_SHA256}" + exit 1 + fi - tar -C /tmp -xJf /tmp/zig.tar.xz + tar -C /tmp -xJf /tmp/zig.tar.xz - mv /tmp/zig-linux-x86_64-* ${ZIGPATH} 2>/dev/null >/dev/null || true - echo "Zig installed to ${ZIGPATH}" + mv /tmp/zig-linux-x86_64-* ${ZIGPATH} 2>/dev/null >/dev/null || true + echo "Zig installed to ${ZIGPATH}" } if linux; then - distro=$(grep "^ID=" /etc/os-release | cut -d'=' -f2 | sed -e 's/"//g') + distro=$(grep "^ID=" /etc/os-release | cut -d'=' -f2 | sed -e 's/"//g') - case $distro in - "ubuntu") - install_apt - ;; - *) # we can add more install command for each distros. - echo "\"$distro\" is not supported distro. Will search for 'apt' or 'dnf' package managers." - if hash apt; then - install_apt - else - echo "\"$distro\" is not supported and your distro don't have apt or dnf that we support currently." - exit - fi - ;; - esac + case $distro in + "ubuntu") + install_apt + ;; + *) # we can add more install command for each distros. + echo "\"$distro\" is not supported distro. Will search for 'apt' or 'dnf' package managers." + if hash apt; then + install_apt + else + echo "\"$distro\" is not supported and your distro don't have apt or dnf that we support currently." + exit + fi + ;; + esac - python3 -m pip install -r dev-requirements.txt + python3 -m pip install -r dev-requirements.txt fi diff --git a/setup.sh b/setup.sh index 649c781d3..d02f2d02a 100755 --- a/setup.sh +++ b/setup.sh @@ -22,7 +22,7 @@ install_apt() { sudo apt-get update || true sudo apt-get install -y git gdb python3-dev python3-pip python3-setuptools libglib2.0-dev libc6-dbg - if uname -m | grep x86_64 > /dev/null; then + if uname -m | grep x86_64 >/dev/null; then sudo dpkg --add-architecture i386 || true sudo apt-get update || true sudo apt-get install -y libc6-dbg:i386 || true @@ -51,7 +51,7 @@ install_zypper() { sudo zypper refresh || true sudo zypper install -y gdb gdbserver python-devel python3-devel python2-pip python3-pip glib2-devel make glibc-debuginfo - if uname -m | grep x86_64 > /dev/null; then + if uname -m | grep x86_64 >/dev/null; then sudo zypper install -y glibc-32bit-debuginfo || true fi } @@ -141,7 +141,7 @@ git submodule update --init --recursive PYVER=$(gdb -batch -q --nx -ex 'pi import platform; print(".".join(platform.python_version_tuple()[:2]))') PYTHON+=$(gdb -batch -q --nx -ex 'pi import sys; print(sys.executable)') if ! osx; then -PYTHON+="${PYVER}" + PYTHON+="${PYVER}" fi # Find the Python site-packages that we need to use so that @@ -164,5 +164,5 @@ ${PYTHON} -m pip install ${INSTALLFLAGS} -Ur requirements.txt # Load Pwndbg into GDB on every launch. if ! grep pwndbg ~/.gdbinit &>/dev/null; then - echo "source $PWD/gdbinit.py" >> ~/.gdbinit + echo "source $PWD/gdbinit.py" >>~/.gdbinit fi diff --git a/tests.sh b/tests.sh index f1b1557e7..91b759801 100755 --- a/tests.sh +++ b/tests.sh @@ -19,7 +19,7 @@ TEST_NAME_FILTER="" while [[ $# -gt 0 ]]; do case $1 in - -p|--pdb) + -p | --pdb) USE_PDB=1 echo "Will run tests with Python debugger" shift @@ -34,7 +34,6 @@ while [[ $# -gt 0 ]]; do esac done - if [[ -z "$ZIGPATH" ]]; then # If ZIGPATH is not set, set it to $pwd/.zig # In Docker environment this should by default be set to /opt/zig @@ -42,7 +41,6 @@ if [[ -z "$ZIGPATH" ]]; then fi echo "ZIGPATH set to $ZIGPATH" - cd ./tests/binaries || exit 1 make clean && make all || exit 2 cd ../../ @@ -76,9 +74,9 @@ for test_case in ${TESTS_LIST}; do exit_status=$? if [ ${exit_status} -eq 0 ]; then - (( ++tests_passed_or_skipped )) + ((++tests_passed_or_skipped)) else - (( ++tests_failed )) + ((++tests_failed)) fi done