From ac5a6ebe6418bf7fcc40fe69f8b997046c6526e8 Mon Sep 17 00:00:00 2001 From: Gulshan Singh Date: Tue, 25 Oct 2022 17:46:53 -0700 Subject: [PATCH] Minor lint and pre-hook changes (#1349) --- docs/source/api/generate.sh | 2 +- lint.sh | 11 ++++++----- profiling/benchmark.sh | 2 +- profiling/profile.sh | 6 +++--- setup-dev.sh | 33 +++++++++++++++++++++++++++------ setup.sh | 16 ++++++++-------- 6 files changed, 46 insertions(+), 24 deletions(-) diff --git a/docs/source/api/generate.sh b/docs/source/api/generate.sh index 1b42fc8ec..e868b9cd1 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 << EOF :mod:\`pwndbg.$1\` --- pwndbg.$1 ============================================= diff --git a/lint.sh b/lint.sh index 3025e6879..e991670bc 100755 --- a/lint.sh +++ b/lint.sh @@ -31,7 +31,7 @@ set -o xtrace LINT_FILES="pwndbg tests *.py" LINT_TOOLS="isort black flake8 vermin" -if ! type ${LINT_TOOLS} &>/dev/null; then +if ! type ${LINT_TOOLS} &> /dev/null; then PIP_CMD="pip install -Ur dev-requirements.txt" echo "Missing one of the following tools: ${LINT_TOOLS}" echo "Running '${PIP_CMD}'" @@ -47,14 +47,15 @@ else black --check --diff ${LINT_FILES} fi -flake8 --show-source ${LINT_FILES} - if [ -x "$(command -v shfmt)" ]; then - # Indents are four spaces, binary ops can start a line, and indent switch cases - shfmt -i 4 -bn -ci -d . + # Indents are four spaces, binary ops can start a line, indent switch cases, + # and allow spaces following a redirect + shfmt -i 4 -bn -ci -sr -d . else echo "shfmt not installed, skipping" fi # Checking minimum python version vermin -q -t=3.6 --violations ./pwndbg/ + +flake8 --show-source ${LINT_FILES} diff --git a/profiling/benchmark.sh b/profiling/benchmark.sh index 4ac9316b0..8e4fd40d3 100755 --- a/profiling/benchmark.sh +++ b/profiling/benchmark.sh @@ -1,6 +1,6 @@ #!/bin/bash # Benchmark context command -make test >/dev/null +make test > /dev/null git log --abbrev-commit --pretty=oneline HEAD^..HEAD gdb ./test \ -ex "source ../gdbinit.py" \ diff --git a/profiling/profile.sh b/profiling/profile.sh index 2b1f416c1..1c6dc2377 100755 --- a/profiling/profile.sh +++ b/profiling/profile.sh @@ -1,7 +1,7 @@ #!/bin/bash if ! (($#)); then - cat <<-_EOF_ + cat <<- _EOF_ $0: [profile-script] Example: $0 context.py @@ -13,7 +13,7 @@ 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" \ @@ -32,7 +32,7 @@ p = pstats.Stats('${basedir}/stats') 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 +if command -v pyprof2calltree &> /dev/null && command -v kcachegrind &> /dev/null; then pyprof2calltree -k -i "${basedir}/stats" fi diff --git a/setup-dev.sh b/setup-dev.sh index 19190a868..5f272e278 100755 --- a/setup-dev.sh +++ b/setup-dev.sh @@ -5,14 +5,35 @@ echo "# Install testing tools." echo "# Only works with Ubuntu / APT." echo "# --------------------------------------" -hook_script_name=".git/hooks/pre-push" +hook_script_path=".git/hooks/pre-push" +hook_script=$( + cat << 'EOF' +#!/bin/bash + +diff_command="git diff --no-ext-diff --ignore-submodules" + +old_diff=$($diff_command) + +./lint.sh -f +exit_code=$? + +new_diff=$($diff_command) + +if [[ "$new_diff" != "$old_diff" ]]; then + echo "Files were modified by the linter, amend your commit and try again" + exit 1 +fi + +exit $exit_code +EOF +) if [ -t 1 ]; then echo "Install a git hook to automatically lint files before pushing? (y/N)" read yn if [[ "$yn" == [Yy]* ]]; then - echo "./lint.sh -f" >>$hook_script_name - echo "pre-push hook installed to $hook_script_name" + echo "$hook_script" > "$hook_script_path" + echo "pre-push hook installed to $hook_script_path" fi fi @@ -26,14 +47,14 @@ echo "ZIGPATH set to $ZIGPATH" # If we are a root in a container and `sudo` doesn't exist # 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 +if ! hash sudo 2> /dev/null && whoami | grep root; then sudo() { ${*} } fi linux() { - uname | grep -i Linux &>/dev/null + uname | grep -i Linux &> /dev/null } install_apt() { @@ -69,7 +90,7 @@ install_apt() { tar -C /tmp -xJf /tmp/zig.tar.xz - mv /tmp/zig-linux-x86_64-* ${ZIGPATH} 2>/dev/null >/dev/null || true + mv /tmp/zig-linux-x86_64-* ${ZIGPATH} &> /dev/null || true echo "Zig installed to ${ZIGPATH}" } diff --git a/setup.sh b/setup.sh index d02f2d02a..b6b8adc6e 100755 --- a/setup.sh +++ b/setup.sh @@ -4,7 +4,7 @@ set -ex # If we are a root in a container and `sudo` doesn't exist # 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 +if ! hash sudo 2> /dev/null && whoami | grep root; then sudo() { ${*} } @@ -12,17 +12,17 @@ fi # Helper functions linux() { - uname | grep -i Linux &>/dev/null + uname | grep -i Linux &> /dev/null } osx() { - uname | grep -i Darwin &>/dev/null + uname | grep -i Darwin &> /dev/null } 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 } @@ -109,7 +109,7 @@ if linux; then ;; "gentoo") install_emerge - if ! hash sudo 2>/dev/null && whoami | grep root; then + if ! hash sudo 2> /dev/null && whoami | grep root; then sudo() { ${*} } @@ -163,6 +163,6 @@ ${PYTHON} -m pip install ${INSTALLFLAGS} --upgrade pip ${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 +if ! grep pwndbg ~/.gdbinit &> /dev/null; then + echo "source $PWD/gdbinit.py" >> ~/.gdbinit fi