-
Notifications
You must be signed in to change notification settings - Fork 0
/
p4clean.py
executable file
·86 lines (74 loc) · 3.2 KB
/
p4clean.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
"""Clean up shelved or pending changes.
Python script to clean up old shelved and/or pending 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, status='shelved'):
"""Delete the shelved or pending changes owned by the current user.
:param p4: Perforce API
:param args: command line arguments
:param user: Perforce user object
:param status: the change status to query for (e.g. 'shelved', 'pending')
"""
week_ago = datetime.now() - timedelta(7)
for change in p4.iterate_changes('-u', user['User'], '-s', status):
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']))
if date <= week_ago or args.all:
if args.delete:
try:
if status == 'shelved':
p4.delete_shelve(change['Change'])
elif status == 'pending':
p4.delete_change(change['Change'])
print("Deleted change {}".format(change['Change']))
except P4.P4Exception as e:
print("Error removing {}: {}".format(change['Change'], e))
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 or pending 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("-p", "--pending", action="store_true",
help="select for pending changes (vs. shelved)")
parser.add_argument("-y", "--delete", action="store_true",
help="perform the deletion")
parser.add_argument("-c", "--client", metavar="CLIENT",
help="perform the deletion")
args = parser.parse_args()
try:
p4 = P4.P4()
if args.client:
p4.client = args.client
p4.connect()
user = p4.fetch_user()
if user:
if args.pending:
delete_changes(p4, args, user, 'pending')
else:
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()