How to get the list of 60Days old users in Perforce

Viewed 38

I need to return records where there is an access date older than 60 days along with Perforce UserName, Email & Last Accessed Date in Perforce. I am aware of the perforce command to get the all user list with "%User% %Email% %Access%" in combination with p4 users. However, I'm looking for some script that can be set in a cron job to daily monitor and send the alert through email.

The command that i am using to get all user list is

p4 -ztag -F "%User% %Email% %Access%" users | awk '{$3=strftime("%Y/%m/%d %H:%M:%S",$3)} {print}' | sort -k 2,3 | sed 's/[ \t]/,/g'

Any help would be much appreciated.

2 Answers

In Python, using the p4python module:

from datetime import datetime
from P4 import P4

with P4().connect() as p4:
    for user in p4.run_users():
        access_time = datetime.fromtimestamp(int(user['Access']))
        if (datetime.now() - access_time).days > 60:
            print(user['User'], user['Email'])
            # or do whatever else you want

On Linux, this'll generate a CSV-style list sorted by username:

p4 -ztag -F "%User% %Email% %Access%" users | awk '$3 < '"$(date -d "60 days ago" +%s)"' {print $1","$2","strftime("%Y/%m/%d %H:%M:%S",$3)}'

If you want to also sort by Access date, you could sort before awk.

p4 -ztag -F "%User% %Email% %Access%" users | sort -k3rn | awk '$3 < '"$(date -d "60 days ago" +%s)"' {print $1","$2","strftime("%Y/%m/%d %H:%M:%S",$3)}'

For OSX, this would look like:

p4 -ztag -F "%User% %Email% %Access%" users | sort -k3rn | awk '$3 < '"$(date -v-60d +%s)"' {print $1","$2","strftime("%Y/%m/%d %H:%M:%S",$3)}'

If you're redirecting any of the above commands to a text file, you could then read said text file back in as a list of users to then delete.

awk -F, '{print $1}' old.users.list | xargs -I{} p4 user -df {}

Note this is a fairly shallow way of doing it. It won't remove their workspaces or pending changelists/shelves. It's possible to incorporate those steps as part of a more thorough cleanup script but various companies have their own policies about what to do with shelved changes, etc.

You will at least be able to quickly free up a license this way.

Related