#!/usr/bin/env python3
#
# Python script to clean up old shelved Perforce changes owned by the current
# user. Use -h to display usage information. This script requires the P4Python
# library to be installed. See http://www.perforce.com/product/components/apis
#

import argparse
from datetime import datetime, timedelta
import sys
import P4


def delete_changes(p4, args, user):
    """Delete the shelved changes owned by the current user.

    Keyword arguments:
    p4    -- Perforce API
    args  -- command line arguments
    user  -- Perforce user object
    """
    for change in p4.iterate_changes('-u', user['User'], '-s', 'shelved'):
        try:
            date = datetime.strptime(change['Date'], '%Y/%m/%d %H:%M:%S')
        except ValueError:
            sys.stderr.write('failed to parse date {} in change {}'.
                format(change['Date'], change['Change']))
        week_ago = datetime.now() - timedelta(7)
        if date <= week_ago or args.all:
            if args.delete:
                p4.delete_shelve(change['Change'])
                print("Deleted change {}".format(change['Change']))
            else:
                print("p4 shelve -dc {}".format(change['Change']))
        else:
            print("Ignoring recent change {}".format(change['Change']))


def main():
    """Parse command line arguments and do the work.
    """
    desc = '''Removes old shelved changes owned by the current user.
    By default, nothing is removed unless -y is passed (a la obliterate).
    Any changes made in the last week will be retained, unless -a is given.
    '''
    parser = argparse.ArgumentParser(description=desc)
    parser.add_argument("-a", "--all", action="store_true",
                        help="remove all shelved changes, even recent ones")
    parser.add_argument("-y", "--delete", action="store_true",
                        help="perform the deletion")
    args = parser.parse_args()

    try:
        p4 = P4.P4()
        p4.connect()
        user = p4.fetch_user()
        if user:
            delete_changes(p4, args, user)
            if not args.delete:
                print("This was report mode. Use -y to remove changes.")
        else:
            sys.stderr.write("Cannot retrieve current Perforce user\n")
    except P4.P4Exception as e:
        sys.stderr.write("error: p4 action failed: {}\n".format(e))


if __name__ == '__main__':
    main()

@@ -58,16 +58,31 @@
 shelved 
+and/or pending 
 Perforce
@@ -87,32 +87,34 @@
 ce changes owned
+%0A#
  by the current%0A
@@ -112,18 +112,16 @@
  current
-%0A#
  user. U
@@ -167,16 +167,18 @@
 s script
+%0A#
  require
@@ -191,18 +191,16 @@
 P4Python
-%0A#
  library
@@ -220,16 +220,18 @@
 led. See
+%0A#
  http://
@@ -387,16 +387,34 @@
 gs, user
+, status='shelved'
 ):%0A    %22
@@ -426,32 +426,43 @@
 ete the shelved 
+or pending 
 changes owned by
@@ -511,19 +511,16 @@
 %0A    p4 
-   
 -- Perfo
@@ -536,17 +536,16 @@
    args 
- 
 -- comma
@@ -571,17 +571,16 @@
    user 
- 
 -- Perfo
@@ -595,27 +595,145 @@
  object%0A    
-%22%22%22
+status -- the change status to query for (e.g. 'shelved', 'pending')%0A    %22%22%22%0A    week_ago = datetime.now() - timedelta(7)
 %0A    for cha
@@ -784,25 +784,22 @@
 , '-s', 
-'shelved'
+status
 ):%0A     
@@ -1038,57 +1038,8 @@
 %5D))%0A
-        week_ago = datetime.now() - timedelta(7)%0A
     
@@ -1123,23 +1123,164 @@
     
-p4.delete_shelv
+if status == 'shelved':%0A                    p4.delete_shelve(change%5B'Change'%5D)%0A                elif status == 'pending':%0A                    p4.delete_chang
 e(ch
@@ -1639,24 +1639,35 @@
 old shelved 
+or pending 
 changes owne
@@ -2069,42 +2069,292 @@
 t(%22-
-y%22, %22--delete%22, action=%22store_true
+p%22, %22--pending%22, action=%22store_true%22,%0A                        help=%22select for pending changes (vs. shelved)%22)%0A    parser.add_argument(%22-y%22, %22--delete%22, action=%22store_true%22,%0A                        help=%22perform the deletion%22)%0A    parser.add_argument(%22-c%22, %22--client%22, metavar=%22CLIENT
 %22,%0A 
@@ -2467,16 +2467,76 @@
 P4.P4()%0A
+        if args.client:%0A            p4.client = args.client%0A
         
@@ -2596,16 +2596,125 @@
 f user:%0A
+            if args.pending:%0A                delete_changes(p4, args, user, 'pending')%0A            else:%0A    
         

Allow removing pending changes