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
87
88
89
90
91
92
93
94
95
96
97
98
99
|
#!/usr/bin/python
# This script must be run with bin/py.
from lpscripts import LaunchpadAPIScript
from optparse import OptionValueError
from datetime import (
datetime,
timedelta,
)
import pytz
MAXIMUM_DAYS = 90
class ExtendCommercialSubscription(LaunchpadAPIScript):
"""Extend the commercial subscription for a project.
The subscription expiration date is extended by the specified number of
days. The intent is to add a short amount of time to a commercial
subscription as a courtesy in situations where we delayed the
provisioning of a project, for example. The extension is limited to 90
days.
"""
USAGE = """\
%prog [options] [projects]
'projects' is a list of project names or URLs to projects in
Launchpad which will have their subscription extended.
NOTE: Under normal conditions an expiring subscription needs to be renewed by
applying a new voucher. This script is to be used to give a complimentary
small amount of time for instances where we caused the project some sort of
delay.
"""
def addParserOptions(self):
# Only the lpsystem option is needed but the others are harmless in
# this context.
super(ExtendCommercialSubscription, self).addParserOptions()
def check_days(option, opt, value, parser):
if value > 90:
raise OptionValueError(
"This script is only to be used to extend a subscription "
"by a small amount of time as a courtesy. Longer "
"extensions should be done by applying a new voucher.")
setattr(parser.values, option.dest, value)
self.parser.add_option(
'-d', '--days',
type='int', default=0, dest='days',
help="""\
Number of days to extend the subscription.""")
self.parser.add_option(
'-f', '--force',
action='store_true', default=False, dest='force',
help='Force override of maximum number of days')
def parseArgs(self, args=None):
super(ExtendCommercialSubscription, self).parseArgs(args)
if self.opts.days < 1 and not self.opts.force:
self.parser.error("You must supply a positive number of days.")
elif self.opts.days > MAXIMUM_DAYS and not self.opts.force:
self.parser.error(
"The number of days cannot be greater than %d days." % (
MAXIMUM_DAYS))
def process_project(self, proj):
subscription = proj.commercial_subscription
if subscription is None:
print "%s does not have a commercial subscription." % (
proj.display_name)
return
print "Project %s" % proj.display_name
print "Original expiration: ", subscription.date_expires
if isinstance(subscription.date_expires, basestring):
# unicode for a date?
date_str, tzinfo = subscription.date_expires.split('+')
date_expires = datetime.strptime(date_str, '%Y-%m-%dT%H:%M:%S.%f')
date_expires.replace(tzinfo=pytz.UTC)
else:
date_expires = subscription.date_expires
date_expires += timedelta(days=self.opts.days)
subscription.date_expires = date_expires
print "Extended expiration: ", subscription.date_expires
if not self.opts.dry_run:
subscription.lp_save()
else:
print "Action not performed."
if __name__ == "__main__":
script = ExtendCommercialSubscription()
script.run()
|