forked from openSUSE/openSUSE-release-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToolBase.py
209 lines (168 loc) · 6.26 KB
/
ToolBase.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/python
from __future__ import print_function
from xml.etree import cElementTree as ET
import cmdln
import datetime
import itertools
import logging
import signal
import sys
import time
try:
from urllib.error import HTTPError
from urllib.parse import quote_plus
except ImportError:
# python 2.x
from urllib2 import HTTPError
from urllib import quote_plus
import osc.conf
import osc.core
from osclib.memoize import memoize
logger = logging.getLogger()
http_GET = osc.core.http_GET
http_DELETE = osc.core.http_DELETE
http_POST = osc.core.http_POST
try:
import __builtin__
input = getattr(__builtin__, 'raw_input')
except (ImportError, AttributeError):
pass
# http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python
def chunks(l, n):
""" Yield successive n-sized chunks from l.
"""
for i in range(0, len(l), n):
yield l[i:i+n]
class ToolBase(object):
def __init__(self):
self.apiurl = osc.conf.config['apiurl']
self.debug = osc.conf.config['debug']
self.caching = False
self.dryrun = False
@memoize(add_invalidate=True)
def _cached_GET(self, url):
return self.retried_GET(url).read()
def cached_GET(self, url):
if self.caching:
return self._cached_GET(url)
return self.retried_GET(url).read()
def retried_GET(self, url):
try:
return http_GET(url)
except HTTPError as e:
if 500 <= e.code <= 599:
print('Retrying {}'.format(url))
time.sleep(1)
return self.retried_GET(url)
logging.error('%s: %s', e, url)
raise e
def http_PUT(self, *args, **kwargs):
if self.dryrun:
logging.debug("dryrun PUT %s %s", args, str(kwargs)[:200])
else:
osc.core.http_PUT(*args, **kwargs)
def http_POST(self, *args, **kwargs):
if self.dryrun:
logging.debug("dryrun POST %s %s", args, str(kwargs)[:200])
else:
osc.core.http_POST(*args, **kwargs)
def get_project_meta(self, prj):
url = self.makeurl(['source', prj, '_meta'])
return self.cached_GET(url)
def _meta_get_packagelist(self, prj, deleted=None, expand=False):
query = {}
if deleted:
query['deleted'] = 1
if expand:
query['expand'] = 1
u = self.makeurl(['source', prj], query)
return self.cached_GET(u)
def meta_get_packagelist(self, prj, deleted=None, expand=False):
root = ET.fromstring(self._meta_get_packagelist(prj, deleted, expand))
return [ node.get('name') for node in root.findall('entry') if not node.get('name') == '000product' and not node.get('name').startswith('patchinfo.') ]
# FIXME: duplicated from manager_42
def latest_packages(self, project):
data = self.cached_GET(self.makeurl(['project', 'latest_commits', project]))
lc = ET.fromstring(data)
packages = set()
for entry in lc.findall('{http://www.w3.org/2005/Atom}entry'):
title = entry.find('{http://www.w3.org/2005/Atom}title').text
if title.startswith('In '):
packages.add(title[3:].split(' ')[0])
return sorted(packages)
def makeurl(self, l, query=None):
"""
Wrapper around osc's makeurl passing our apiurl
:return url made for l and query
"""
query = [] if not query else query
return osc.core.makeurl(self.apiurl, l, query)
def process(self, packages):
""" reimplement this """
True
class CommandLineInterface(cmdln.Cmdln):
def __init__(self, *args, **kwargs):
cmdln.Cmdln.__init__(self, *args, **kwargs)
def get_optparser(self):
parser = cmdln.Cmdln.get_optparser(self)
parser.add_option("--apiurl", '-A', metavar="URL", help="api url")
parser.add_option("--dry", action="store_true", help="dry run")
parser.add_option("-d", "--debug", action="store_true", help="debug output")
parser.add_option("--osc-debug", action="store_true", help="osc debug output")
parser.add_option("--verbose", action="store_true", help="verbose")
parser.add_option('--cache-requests', action='store_true', default=False,
help='cache GET requests. Not recommended for daily use.')
return parser
def postoptparse(self):
level = None
if (self.options.debug):
level = logging.DEBUG
elif (self.options.verbose):
level = logging.INFO
logging.basicConfig(level=level)
osc.conf.get_config(override_apiurl = self.options.apiurl)
if self.options.osc_debug:
osc.conf.config['debug'] = 1
self.tool = self.setup_tool()
self.tool.dryrun = self.options.dry
self.tool.caching = self.options.cache_requests
def setup_tool(self, toolclass = ToolBase):
""" reimplement this """
tool = toolclass()
return tool
# example
# @cmdln.option('-n', '--interval', metavar="minutes", type="int", help="periodic interval in minutes")
# def do_process(self, subcmd, opts, project):
# def work():
# self.tool.process()
#
# self.runner(work, opts.interval)
def runner(self, workfunc, interval):
""" runs the specified callback every <interval> minutes or
once if interval is None or 0
"""
class ExTimeout(Exception):
"""raised on timeout"""
if interval:
def alarm_called(nr, frame):
raise ExTimeout()
signal.signal(signal.SIGALRM, alarm_called)
while True:
try:
workfunc()
except Exception as e:
logger.exception(e)
if interval:
logger.info("sleeping %d minutes. Press enter to check now ..."%interval)
signal.alarm(interval*60)
try:
input()
except ExTimeout:
pass
signal.alarm(0)
logger.info("recheck at %s"%datetime.datetime.now().isoformat())
continue
break
if __name__ == "__main__":
app = CommandLineInterface()
sys.exit( app.main() )