#!/bin/bash #============================================================================== # Copyright and license info is available in the LICENSE file included with # the Server Deployment Package (SDP), and also available online: # https://swarm.workshop.perforce.com/projects/perforce-software-sdp/view/main/LICENSE #------------------------------------------------------------------------------ #============================================================================== # Declarations and Environment # Allow override of P4U_HOME, which is set only when testing P4U scripts. export P4U_HOME="${P4U_HOME:-/p4/common/bin}" export P4U_LIB="${P4U_LIB:-/p4/common/lib}" export P4U_ENV="$P4U_LIB/p4u_env.sh" export TOOLS_DIR="${TOOLS_DIR:-.}" export P4U_LOG="Unset" export VERBOSITY="${VERBOSITY:-3}" # Environment isolation. For stability and security reasons, prepend # PATH to include dirs where known-good scripts exist. # known/tested PATH and, by implication, executables on the PATH. export PATH=$P4U_HOME:$PATH:~/bin:. export P4CONFIG=${P4CONFIG:-.p4config} [[ -r "$P4U_ENV" ]] || { echo -e "\\nError: Cannot load environment from: $P4U_ENV\\n\\n" exit 1 } declare BASH_LIBS=$P4U_ENV BASH_LIBS+=" $P4U_LIB/libcore.sh" BASH_LIBS+=" $P4U_LIB/libp4u.sh" BASH_LIBS+=" $TOOLS_DIR/env.sh" for bash_lib in $BASH_LIBS; do source $bash_lib ||\ { echo -e "\\nFATAL: Failed to load bash lib [$bash_lib]. Aborting.\\n"; exit 1; } done declare Version="1.0.1" declare ReportStyle="OpenJobs" declare ReportStyleTitle= declare JobSearchQuery= declare WorkshopUser="Unset" declare -i ShowUserJobsOnly=0 declare -i SilentMode=0 declare -i JobCount=0 export VERBOSITY=3 #============================================================================== # Local Functions #------------------------------------------------------------------------------ # Function: terminate function terminate { # Disable signal trapping. trap - EXIT SIGINT SIGTERM vvmsg "$THISSCRIPT: EXITCODE: $OverallReturnStatus" # Stop logging. [[ "${P4U_LOG}" == off ]] || stoplog # Don't litter. cleanTrash # With the trap removed, exit. exit $OverallReturnStatus } #------------------------------------------------------------------------------ # Function: usage (required function) # # Input: # $1 - style, either -h (for short form) or -man (for man-page like format). # The default is -h. # # $2 - error message (optional). Specify this if usage() is called due to # user error, in which case the given message displayed first, followed by the # standard usage message (short or long depending on $1). If displaying an # errror, usually $1 should be -h so that the longer usage message doesn't # obsure the error message. # # Sample Usage: # usage # usage -h # usage -man # usage -h "Incorrect command line usage." #------------------------------------------------------------------------------ function usage { declare style=${1:--h} declare errorMessage=${2:-Unset} if [[ $errorMessage != Unset ]]; then echo -e "\\n\\nUsage Error:\\n\\n$errorMessage\\n\\n" fi echo "USAGE for $THISSCRIPT v$Version: $THISSCRIPT [-a|-o] [-me | -u <workshop_user>] [-L <log>] [-si] [-v<n>] [-n] [-D] or $THISSCRIPT [-h|-man|-V] " if [[ $style == -man ]]; then echo -e " DESCRIPTION: This script a report of SDP jobs report. OPTIONS: -a Show all SDP jobs regardless of the state. -o Show only "Open" SDP jobs, which includes jobs in these states: * open * inprogress * blocked This skips jobs in these states: * closed * duplicate * fixed (optional status to use before closed). * punted * obsolete * suspended The '-o' behavior is the default. -me Show only jobs for which the OwnedBy setting is the current user. Guessing logic is applied to map the current OS user to a Workshop account, e.g. ttyler -> tom_tyler, etc. This works for some SDP project members. See the guess_workshop_user() function in env.sh for details. Works with '-a' and '-o'. -u <workshop_user> Show only jobs for which the OwnedBy setting is the specified Workshop user account. Specify the special value 'none' to list unassigned jobs, i.e. those for which the OwnedBy field is not set. Works with '-a' and '-o'. -v<n> Set verbosity 1-5 (-v1 = quiet, -v5 = highest). -L <log> Specify the path to report log file, or the special value 'off' to disable logging. By default, all output (stdout and stderr) goes to a file, then name of which is displayed when the script starts. NOTE: This script is self-logging. That is, output displayed on the screen is simultaneously captured in the log file. Do not run this script with redirection operators like '> log' or '2>&1', and do not use 'tee.' -si Operate silently. All output (stdout and stderr) is redirected to the log only; no output appears on the terminal. This cannot be used with '-L off'. -n No-Op. Prints commands instead of running them. -D Set extreme debugging verbosity. HELP OPTIONS: -h Display short help message -man Display man-style help message -V Dispay version info for this script and its libraries. EXAMPLES: Typical usage is with no arguments, to display open jobs: $THISSCRIPT Show opened jobs assigned to me (based on the \$WORKSHOP_USER shell environment setting). $THISSCRIPT -me SEE ALSO: " fi exit 1 } #============================================================================== # Command Line Processing declare -i shiftArgs=0 set +u while [[ $# -gt 0 ]]; do case $1 in (-a) ReportStyle="AllJobs";; (-o) ReportStyle="OpenJobs";; (-me) ShowUserJobsOnly=1; WorkshopUser="me";; (-u) ShowUserJobsOnly=1; WorkshopUser="$2"; shiftArgs=1;; (-h) usage -h;; (-man) usage -man;; (-V) show_versions; exit 1;; (-v1) export VERBOSITY=1;; (-v2) export VERBOSITY=2;; (-v3) export VERBOSITY=3;; (-v4) export VERBOSITY=4;; (-v5) export VERBOSITY=5;; (-L) export P4U_LOG="$2"; shiftArgs=1;; (-si) SilentMode=1;; (-n) export NO_OP=1;; (-D) set -x;; # Debug; use 'set -x' mode. (*) usage -h "Unknown arg ($1).";; esac # Shift (modify $#) the appropriate number of times. shift; while [[ $shiftArgs -gt 0 ]]; do [[ $# -eq 0 ]] && usage -h "Incorrect number of arguments." shiftArgs=$shiftArgs-1 shift done done set -u #============================================================================== # Command Line Verification [[ $SilentMode -eq 1 && "$P4U_LOG" == off ]] && \ usage -h "Cannot use '-si' with '-L off'." [[ "$P4U_LOG" == "Unset" ]] && export P4U_LOG="${LOGS:-.}/${THISSCRIPT%.sh}.$(date +'%Y%m%d-%H%M').log" [[ "$WorkshopUser" == "me" ]] && WorkshopUser="$(guess_workshop_user)" #============================================================================== # Main Program trap terminate EXIT SIGINT SIGTERM declare -i OverallReturnStatus=0 if [[ "${P4U_LOG}" != off ]]; then touch ${P4U_LOG} || bail "Couldn't touch log file [${P4U_LOG}]." # Redirect stdout and stderr to a log file. if [[ $SilentMode -eq 0 ]]; then exec > >(tee ${P4U_LOG}) exec 2>&1 else exec >${P4U_LOG} exec 2>&1 fi initlog msg "Log file is: $P4U_LOG" fi if [[ $ReportStyle == "OpenJobs" ]]; then # Query to show only "Open" SDP jobs. See usage info for this script and # 'p4 jobspec -o' for details on all states. JobSearchQuery="Project=$WORKSHOP_PROJECT ^Status=closed ^Status=duplicate ^Status=fixed ^Status=obsolete ^Status=punted ^Status=suspended" ReportStyleTitle="Open Jobs" else # Query to show all SDP jobs. JobSearchQuery="Project=$WORKSHOP_PROJECT" ReportStyleTitle="Open Jobs" fi [[ "$ShowUserJobsOnly" -eq 1 ]] && ReportStyleTitle+=" owned by $WorkshopUser" printf "SDP Jobs Report showing $ReportStyleTitle\\n\\n %-8s %-16s %-s\\n" "Job" "Owner" "Title" printf " %-8s %-16s %-s\\n" "--------" "----------------" "----------------------------------------" for job in $(p4 -ztag -F %Job% jobs -e "$JobSearchQuery"); do ownedBy=$(p4 -ztag -F %OwnedBy% job -o $job) # Skip jobs by users other than the current user of '-me' was specified. if [[ $ShowUserJobsOnly -eq 1 ]]; then if [[ "$WorkshopUser" == "none" ]]; then [[ -n "$ownedBy" ]] && continue else [[ "$ownedBy" != "$WorkshopUser" ]] && continue fi fi jobTitle=$(p4 -ztag -F %Description% job -o $job | head -1) printf " %-8s %-16s %-s\\n" "$job" "$ownedBy" "$jobTitle" JobCount+=1 done msg "${H}\\n$JobCount jobs reported in $(($SECONDS/3600)) hours $(($SECONDS%3600/60)) minutes $(($SECONDS%60)) seconds.\n" # See the terminate() function, which is really where this script exits. exit $OverallReturnStatus
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#17 | 29510 | C. Thomas Tyler |
First pass at add '-csv' option to generate a CSV file for JIRA Import. @veronica_kanczes |
||
#16 | 29508 | C. Thomas Tyler | Completed remove of '-V' | ||
#15 | 29507 | C. Thomas Tyler | Fixed minor doc typos. | ||
#14 | 29473 | C. Thomas Tyler | Experimental logging enhancement. | ||
#13 | 28055 | C. Thomas Tyler |
Fixed bug where '-st status1,status2,statue3,...' would show only the first specified status if more than two status values were supplied. |
||
#12 | 28033 | C. Thomas Tyler | Added 'Reporter' column to the jobs report. | ||
#11 | 27728 | C. Thomas Tyler | Cosmetic fix for SDP Jobs Report script. | ||
#10 | 26880 | C. Thomas Tyler | Improved SDP jobs report utilty, generally simplified. | ||
#9 | 26618 | C. Thomas Tyler |
Corrected grammatical error in script generating a report of SDP jobs. |
||
#8 | 26417 | C. Thomas Tyler |
Added WORKSHOP_PROJECT_TAG setting to env.sh to slow the jobs report script to be completely generic. |
||
#7 | 26362 | C. Thomas Tyler |
Adjusted formatting to limit max lenght of displayed lines. Changed default report file name. |
||
#6 | 26361 | C. Thomas Tyler |
Added 'Pr' priority/severity to displayed fields on the report. Removed excess 'Log file is' message. |
||
#5 | 26272 | C. Thomas Tyler | Corrected doc typo. | ||
#4 | 24787 | C. Thomas Tyler | Added Component to SDP jobs report. | ||
#3 | 24751 | C. Thomas Tyler |
Added '-st <status1>[,<status2>,...' flag to specify a list of values. Added Status column to report. Added job status query at verbosity level 4 (-v4) with comment indicating same. |
||
#2 | 24750 | C. Thomas Tyler | Added job count and cleaned up output. | ||
#1 | 24749 | C. Thomas Tyler |
Added basic SDP jobs report script, and refactored aliases to call it. |