speccount.sh #3

  • //
  • guest/
  • amo/
  • scripts/
  • speccount.sh
  • View
  • Commits
  • Open Download .zip Download (4 KB)
#!/bin/bash

# ==========
# $File: //guest/amo/scripts/speccount.sh $
# $Revision: #3 $
# $Change: 24893 $
#
# $Author: amo $
# $DateTimeTZ: 2018/11/21 05:18:29 -0800 PST $
# ----------
# An attempt to count the number of specs in the system on each day during the range specified. 
# Uses the spec-depot files. 
# Requires four parameters
# 1. P4PORT for the server to be checked
# 2. a spec depot path (for example, '//spec/user/...') 
# 3. a date on which the checks should start, in the format 'yyyy/mm/dd'
# 4. a date on which the checks should end, also in the format 'yyyy/mm/dd'
#
# Drops out on failing one of various checks
# - wrong number of parameters
# - no 'p4' available
# - no valid login ticket
# - can't find path specified
# - log file already exists (might not want to overwrite the content)
# - startdate invalid
# - enddate invalid
#
# Once through the checks, the script runs 'p4 files' against the specs in the spec depot. 
# It reports the count on a given day (specs not deleted), then number added, edited and deleted.
# ----------
# Notes: 
# - 'p4 files @date,date' may not be the quickest command to run - performance improvements may be possible;
# - this does not distinguish between specs of different types, just counts the specs it finds;
#   - in particular, no distinction is made between standard, operator and service users;
# - some specs may not be present in the spec depot (spec depot created after them, perhaps);
# - the SpecMap field in the spec depot spec may exclude other specs. 
# 
# Additions:
# - build the spec depot path by searching for the spec depot name, asking for the spec type instead of a depot path
# - further sanity checks and error-handling
# - check protections to ensure the current user has permission to access the spec depot
# 
# ==========

# expecting four parameters - P4PORT, a depot path and two dates
if [ "$#" -ne 4 ]; then 
    printf "Usage: %s %s %s %s %s\n%s\n%s\n" $0 "P4PORT" "depotpath" "date" "date" "depot: //spec/user/..." "date format is 'yyyy/mm/dd'" 
    exit 1
fi

# check there is a 'p4' available
p4cli=`which p4` 
if [ "$?" -ne 0 ]; then 
    printf "%s: 'p4' client not found.\n" $0
    exit 2
fi
 
# check there's a valid connection (looks for ticket)
$p4cli login -s > /dev/null 2>&1
if [ $? -ne 0 ]; then 
    printf "%s: 'p4' not logged in or no connection available.\n" $0
    exit 3
fi

p4cmd="$p4cli -p $1 files"
depotpath=$2

# is there a spec depot?
## spec=`$p4cmd -F %name% depots -t spec`
## need to test 'spec' - it's either empty, or the spec depot name
## with the spec type (checked below) this could be used to build the correct depot path rather than relying on the path entered by the user.

# valid spec type?
##spectype=$2
##case $spectype in
##    branch|client|depot|group|job|label|ldap|protects|remote|server|stream|trigger|typemap|user) ;;
##    *) printf "%s: Not a valid spec type.\n" $0; exit 4 ;;
##esac

# check if the path exists/can be accessed
$p4cmd -m1 $depotpath > /dev/null 
if [ $? -ne 0 ]; then
    printf "%s: path %s does not exist or is not accessible.\n" $0 $depotpath
    exit 4
fi

# name the output file using the script's name
of="$(basename $0).log"

if [ -f $of ]; then
    printf "%s: log file '%s' already exists. Not continuing.\n" $0 $of
    exit 5
fi

sd=$3
ed=$4

startdate=$( date -d $sd +"%Y/%m/%d" ) || exit 6
enddate=$( date -d $ed +"%Y/%m/%d" ) || exit 7

d="$startdate"
D=$( date -d "$d + 1 day" +"%Y/%m/%d" )

#open file descriptor 3 for read-write on the log file
exec 3<> $of

printf "$0: counting spec changes in the spec depot\n" >&3
printf "Parameters: %s %s %s %s\n" $1 $2 $3 $4 >&3
printf "Total date range: %s - %s\n" $startdate $enddate >&3
printf "%s\t %s\t %s\t %s\t %s\n" "Range" "Count" "Added" "Edited" "Deleted" >&3

while [ "$( date -d "$d" +%Y%m%d)" -le "$( date -d "$enddate" +%Y%m%d)" ]; do
    uc=$( $p4cmd $depotpath@$d     2>/dev/null | grep -v "delete default change" | wc -l )
    ua=$( $p4cmd $depotpath@$d,@$D 2>/dev/null | grep    "add default change"    | wc -l )
    ue=$( $p4cmd $depotpath@$d,@$D 2>/dev/null | grep    "edit default change"   | wc -l )
    ud=$( $p4cmd $depotpath@$d,@$D 2>/dev/null | grep    "delete default change" | wc -l )

    printf "%s-%s\t %d\t %d\t %d\t %d\n" $d $D $uc $ua $ue $ud >&3
    d=$D
    D=$( date -d "$d + 1 day" +"%Y/%m/%d" )
done

printf "$0: end of count.\n" >&3

# close file descriptor 3
exec 3>&-
# Change User Description Committed
#3 24893 Adam Morriss Tidying up and adding to the notes.
Adding a header line to the output.
Removing the initial letter/colon combo in front of each count being output to file.
The beginnings of some code (commented-out) that could retrieve the spec depot name, if one exists.
#2 24799 Adam Morriss Further comment, and a potential (but commented-out) case statement should it be better to check spec types.
#1 24798 Adam Morriss renaming again - actually a spec-counting script, not just users
//guest/amo/scripts/usercount.sh
#2 24797 Adam Morriss adding some notes
#1 24796 Adam Morriss renaming the file
//guest/amo/scripts/uc.sh
#3 24795 Adam Morriss Minor adjustments.
* Improved the filename to which the output is written.
* Switched the write-to-file mechanism.
* Corrected the counting commands (an additional 'files' was mistakenly left behind in an earlier tidy-up).
#2 24794 Adam Morriss minor update - added newline
#1 24793 Adam Morriss A user-counting script