Skip to content

Commit

Permalink
Fix a bug in pre-commit-copyright and other shell script fixes. (#1116)
Browse files Browse the repository at this point in the history
* Fix a bug in pre-commit-copyright and other shell script fixes.

+ Pre-commit hooks and `check_style.sh` expect the path `/tmp/$USER` to exist.  Create this path if
  it doesn't already exist.
+ For the copyright block check, also check shell scripts (extension `.sh`), but exclude
  `check_style.sh` as the script logic gets confused for this file (too many `Copyright` strings).
+ Add bash function `rdp` (runs `xfreerdp`).  This is useful for running desktop-on-demand Win10
  sessions from the Cortez network.  Also, cleanup comment formatting in `bash_functions.sh`.

* Fix shell check lint issues.
  • Loading branch information
KineticTheory authored Aug 19, 2021
1 parent 605e1e1 commit fc46b6d
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 110 deletions.
195 changes: 106 additions & 89 deletions environment/bashrc/bash_functions.sh
Original file line number Diff line number Diff line change
@@ -1,42 +1,46 @@
#!/bin/bash
## -*- Mode: bash -*-
##-----------------------------------------------------------------------------------------------##
## File : environment/bashrc/bash_functions.sh
## Date : Tuesday, May 31, 2016, 14:48 pm
## Author: Kelly Thompson
## Note : Copyright (C) 2016-2020, Triad National Security, LLC., All rights are reserved.
##-----------------------------------------------------------------------------------------------##
##
## Summary: Misc bash functions useful during development of code.
##
## Functions
## ---------
##
## whichall <string> - show path of all commands in path that match <string>
##
## cleanemacs - recursively remove ~ files, .flc files and .rel files.
##
## findsymbol <sym> - search all libraries (.so and .a files) in the current directory for symbol
## <sym>.
##
## npwd - function used to set the prompt under bash.
##
## xfstatus - report status of transfer.lanl.gov
##
## rm_from_path - remove a directory from $PATH
##
## add_to_path - add a directory to $PATH
##
## proxy - (un)set http_proxy variables
##
## fn_exists - test if a bash function is defined
##
## run - echo then evaluate a bash command
##
## rdde - reload the default draco environment
##
## qrm - quick remove (for lustre filesystems).
##-----------------------------------------------------------------------------------------------##
# -*- Mode: bash -*-
# ------------------------------------------------------------------------------------------------ #
# File : environment/bashrc/bash_functions.sh
# Date : Tuesday, May 31, 2016, 14:48 pm
# Author: Kelly Thompson
# Note : Copyright (C) 2019-2021 Triad National Security, LLC., All rights reserved.
# ------------------------------------------------------------------------------------------------ #

#
# Summary: Misc bash functions useful during development of code.
#
# Functions
# ---------
#
# add_to_path - add a directory to $PATH
#
# cleanemacs - recursively remove ~ files, .flc files and .rel files.
#
# findsymbol <sym> - search all libraries (.so and .a files) in the current directory for symbol
# <sym>.
#
# fn_exists - test if a bash function is defined
#
# npwd - function used to set the prompt under bash.
#
# proxy - (un)set http_proxy variables
#
# qrm - quick remove (for lustre filesystems).
#
# rdde - reload the default draco environment
#
# rdp - start an xfree rdp session (Cortez)
#
# rm_from_path - remove a directory from $PATH
#
# run - echo then evaluate a bash command
#
# whichall <string> - show path of all commands in path that match <string>
#
# xfstatus - report status of transfer.lanl.gov
# ------------------------------------------------------------------------------------------------ #


# pragma once
if [[ $(type dbf_pragma_once 2>&1 | grep -c "bash: type: ") != 0 ]]; then
Expand All @@ -48,9 +52,9 @@ if [[ "${verbose:=false}" == "true" ]]; then
echo "in draco/environment/bashrc/bash_functions.sh";
fi

##-----------------------------------------------------------------------------------------------##
## Find all matches in PATH (not just the first one)
##-----------------------------------------------------------------------------------------------##
#-----------------------------------------------------------------------------------------------#
# Find all matches in PATH (not just the first one)
#-----------------------------------------------------------------------------------------------#

function whichall ()
{
Expand All @@ -60,9 +64,9 @@ function whichall ()
}
export -f whichall

##-----------------------------------------------------------------------------------------------##
## Recursively delete all ~ files.
##-----------------------------------------------------------------------------------------------##
#-----------------------------------------------------------------------------------------------#
# Recursively delete all ~ files.
#-----------------------------------------------------------------------------------------------#

function cleanemacs
{
Expand All @@ -79,12 +83,12 @@ function cleanemacs
}
export -f cleanemacs

##-----------------------------------------------------------------------------------------------##
## Used for formatting PROMPT.
## $HOME -> ~
## ...scratch... -> #
## .../projects/... -> @
##-----------------------------------------------------------------------------------------------##
#-----------------------------------------------------------------------------------------------#
# Used for formatting PROMPT.
# $HOME -> ~
# ...scratch... -> #
# .../projects/... -> @
#-----------------------------------------------------------------------------------------------#
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
Expand Down Expand Up @@ -151,14 +155,14 @@ export -f npwd_alt
# PROMPT_COMMAND='DIR=`pwd|sed -e "s!$HOME!~!"`; if [ ${#DIR} -gt 30 ]; then CurDir=${DIR:0:12}...${DIR:${#DIR}-15}; else CurDir=$DIR; fi'
# PS1="[\$CurDir] \$ "

##-----------------------------------------------------------------------------------------------##
## Usage:
## findsymbol <symbol>
##
## Searches all .a and .so files in local directory for symbol
## <symbol>. If found, the script provides the name of the library
## that contains the symbol.
##-----------------------------------------------------------------------------------------------##
#-----------------------------------------------------------------------------------------------#
# Usage:
# findsymbol <symbol>
#
# Searches all .a and .so files in local directory for symbol
# <symbol>. If found, the script provides the name of the library
# that contains the symbol.
#-----------------------------------------------------------------------------------------------#

function findsymbol()
{
Expand All @@ -181,37 +185,37 @@ function findsymbol()
}
export -f findsymbol

##-----------------------------------------------------------------------------------------------##
## Transfer 2.0 (Mercury replacement)
## Ref: http://transfer.lanl.gov
##
## Examples:
## xfpush foo.txt
## xfstatus
## xfpull foo.txt
##-----------------------------------------------------------------------------------------------##
#-----------------------------------------------------------------------------------------------#
# Transfer 2.0 (Mercury replacement)
# Ref: http://transfer.lanl.gov
#
# Examples:
# xfpush foo.txt
# xfstatus
# xfpull foo.txt
#-----------------------------------------------------------------------------------------------#

function xfstatus()
{
ssh [email protected] myfiles
}
export -f xfstatus

##-----------------------------------------------------------------------------------------------##
## If string is found in PATH, remove it.
##-----------------------------------------------------------------------------------------------##
#-----------------------------------------------------------------------------------------------#
# If string is found in PATH, remove it.
#-----------------------------------------------------------------------------------------------#
function rm_from_path ()
{
export PATH="${PATH//${1}/}"
}
export -f rm_from_path

##-----------------------------------------------------------------------------------------------##
## If path is a directory add it to PATH (if not already in PATH)
##
## Use:
## add_to_path <path> TEXINPUTS|BSTINPUTS|BIBINPUTS|PATH
##-----------------------------------------------------------------------------------------------##
#-----------------------------------------------------------------------------------------------#
# If path is a directory add it to PATH (if not already in PATH)
#
# Use:
# add_to_path <path> TEXINPUTS|BSTINPUTS|BIBINPUTS|PATH
#-----------------------------------------------------------------------------------------------#
function add_to_path ()
{
case $2 in
Expand All @@ -231,10 +235,10 @@ function add_to_path ()
}
export -f add_to_path

##-----------------------------------------------------------------------------------------------##
## Toggle LANL proxies on/off
## https://wiki.archlinux.org/index.php/proxy_settings
##-----------------------------------------------------------------------------------------------##
#-----------------------------------------------------------------------------------------------#
# Toggle LANL proxies on/off
# https://wiki.archlinux.org/index.php/proxy_settings
#-----------------------------------------------------------------------------------------------#
function proxy()
{
if [[ ! ${http_proxy} ]]; then
Expand All @@ -259,9 +263,9 @@ function proxy()
}
export -f proxy

##-----------------------------------------------------------------------------------------------##
## Test to determine if named bash function exists in the current environment.
##-----------------------------------------------------------------------------------------------##
#-----------------------------------------------------------------------------------------------#
# Test to determine if named bash function exists in the current environment.
#-----------------------------------------------------------------------------------------------#
function fn_exists()
{
type "$1" 2>/dev/null | grep -q 'is a function'
Expand All @@ -271,18 +275,18 @@ function fn_exists()
}
export -f fn_exists

##-----------------------------------------------------------------------------------------------##
## Echo commands before execution (used in scripts)
##-----------------------------------------------------------------------------------------------##
#-----------------------------------------------------------------------------------------------#
# Echo commands before execution (used in scripts)
#-----------------------------------------------------------------------------------------------#
function run () {
echo "$1"
if [[ "${dry_run:-notset}" != "notset" ]]; then eval "$1"; fi
}
export -f run

##-----------------------------------------------------------------------------------------------##
## Reset the draco developer environment
##-----------------------------------------------------------------------------------------------##
#-----------------------------------------------------------------------------------------------#
# Reset the draco developer environment
#-----------------------------------------------------------------------------------------------#
function rdde ()
{
unset DRACO_BASHRC_DONE
Expand All @@ -291,6 +295,19 @@ function rdde ()
}
export -f rdde

# ------------------------------------------------------------------------------------------------ #
# Start a Remote Desktop Session: `rdp mymachine`
# ------------------------------------------------------------------------------------------------ #
function rdp()
{
if [[ $1 == "" ]]; then
echo "FATAL ERROR: expecting a machine name as an argument. Example: 'rdp centipede'."
exit 1
fi
xfreerdp /sec:rdp +clipboard /dynamic-resolution /size:90% "/v:$1"
}
export -f rdp

#--------------------------------------------------------------------------------------------------#
# Quick remove: instead of 'rm -rf', mv the directory to .../trash/tmpname
#
Expand Down
17 changes: 9 additions & 8 deletions environment/git/pre-commit-copyright
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ debugprint "running pre-commit-copyright"
# DELETE_OLD_PATCHES=true

# file types to parse.
FILE_EXTS=".c .cc .cmake .h .hh .in .f90 .F90 .f .F .py .txt"
FILE_EXTS=".c .cc .cmake .h .hh .in .f90 .F90 .f .F .py .sh .txt"
#FILE_ENDINGS_INCLUDE="_f.h _f77.h _f90.h"
FILE_ENDINGS_EXCLUDE="ChangeLog Release.cc"
FILE_ENDINGS_EXCLUDE="ChangeLog Release.cc check_style.sh"
export FILE_EXTS FILE_ENDINGS_EXCLUDE

##################################################################
Expand Down Expand Up @@ -58,7 +58,7 @@ function rewrite_copyright_block()

# This data was found in the header comments. It might be a single year or a range.
local crl
crl=$(grep Copyright "${filename}")
crl=$(grep Copyright "${filename}" | head -n 1)
local create_date
# shellcheck disable=SC2001
create_date=$(echo "${crl}" | sed -e 's/.* \([0-9][0-9]*\).*/\1/')
Expand All @@ -75,10 +75,10 @@ function rewrite_copyright_block()
debugprint "$create_date $git_last_mod_date $git_create_date"

# Sanity Checks
[[ "${create_date}" =~ "Copyright" ]] && die "Failed to parse copyright line"
[[ "${create_date}" =~ "Copyright" ]] && die "Failed to parse copyright line (err 1)"
# [[ "${mod_date}" =~ "Copyright" ]] && die "Failed to parse copyright line"
[[ "${git_last_mod_date}" =~ "Copyright" ]] && die "Failed to parse copyright line"
[[ "${git_create_date}" =~ "Copyright" ]] && die "Failed to parse copyright line"
[[ "${git_last_mod_date}" =~ "Copyright" ]] && die "Failed to parse copyright line (err 2)"
[[ "${git_create_date}" =~ "Copyright" ]] && die "Failed to parse copyright line (err 3)"
if [[ "${create_date}" -gt "${today}" ]] || [[ "${create_date}" -lt "1990" ]]; then
die "Existing copyright date range is corrupt. Please fix $filename manually."
fi
Expand Down Expand Up @@ -119,7 +119,8 @@ prefix="pre-commit-copyright"
suffix="$(date +%s)"

# clean up any older fprettify patches
# $DELETE_OLD_PATCHES && rm -f /tmp/$prefix-*. &> /dev/null
# $DELETE_OLD_PATCHES && rm -f /tmp/$USER/$prefix-*. &> /dev/null
mkdir -p "/tmp/$USER" || die "Could not create /tmp/$USER"
patchfile=$(mktemp "/tmp/$USER/${prefix}-${suffix}.patch.XXXXXXXX")

# create one patch containing all changes to the files
Expand All @@ -140,7 +141,7 @@ do

debugprint "Looking at $file"
file_nameonly=$(basename "${file}")
tmpfile1="/tmp/${prefix}-$file_nameonly"
tmpfile1="/tmp/$USER/${prefix}-$file_nameonly"

# Copy the file and attempt update it.
cp "${file}" "${tmpfile1}"
Expand Down
Loading

0 comments on commit fc46b6d

Please sign in to comment.