-
Notifications
You must be signed in to change notification settings - Fork 196
/
promotions.py
74 lines (58 loc) · 2.2 KB
/
promotions.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
#!/usr/bin/env python
# -*- coding: utf-8 -*
# Copyright: [CUP] - See LICENSE for details.
# Authors: liushuxian(liushuxian)
"""
This module provides Promotions object.
Promotions is a container of Promotion.
"""
from cup.jenkinslib.internal import base
from cup.jenkinslib.internal import exception
class Promotions(base.JenkinsBase):
"""A container of Promotions objects."""
def __init__(self, job, depth=None):
"""initialize Promotions object.
Args:
job: Job object.
depth: set param 'depth' for jenkins api,
the more deeper it is, the more data you can get back.
"""
self.job = job
self.depth = depth
url = "%s/promotion" % job.url
super(Promotions, self).__init__(url, static=job.is_static)
def get_jenkins_obj(self):
"""get object of current jenkins."""
return self.job.jenkins
def _poll(self, tree=None):
"""poll out api info.
If there is no promotion, api will return 404, catch it and build empty data.
"""
url = self.python_api_url(self.url)
params = {} if self.depth is None else {'depth': self.depth}
try:
return self.get_data(url, params=params, tree=tree)
except exception.UnsupportedAPI:
return {}
def __getitem__(self, promotion_name):
"""get promotion by name."""
for row in self._data.get('processes', []):
if row["name"] == promotion_name:
return self.get_jenkins_obj().Promotion(row['url'], promotion_name, self.job)
raise exception.UnknownPromotion(promotion_name)
def __contains__(self, promotion_name):
"""promotion exists or not."""
return promotion_name in self.keys()
def __len__(self):
"""promotion num."""
return len(self.keys())
def __iter__(self):
"""iterator for job names."""
return (row['name'] for row in self._data.get('processes', []))
def iterkeys(self):
"""get all promotion name."""
for row in self._data.get('processes', []):
yield row['name']
def keys(self):
"""get all promotion name."""
return list(self.iterkeys())