openSUSE-release-tools/osc-check_source.py

312 lines
11 KiB
Python
Raw Normal View History

2011-03-29 13:48:57 +02:00
#
# (C) 2011 coolo@suse.de, Novell Inc, openSUSE.org
# Distribute under GPLv2 or GPLv3
#
# Copy this script to ~/.osc-plugins/ or /var/lib/osc-plugins .
# Then try to run 'osc checker --help' to see the usage.
2011-03-30 14:29:01 +02:00
2011-03-29 13:48:57 +02:00
import os
2014-06-05 16:50:31 +02:00
import re
import shutil
2011-03-29 13:48:57 +02:00
import subprocess
import sys
2013-12-18 17:11:00 +01:00
import urllib2
2014-06-05 16:50:31 +02:00
from xml.etree import cElementTree as ET
2011-03-29 13:48:57 +02:00
2014-06-05 16:50:31 +02:00
from osc.core import checkout_package
from osc.core import http_GET
from osc.core import http_POST
from osc.core import makeurl
2014-06-05 16:50:31 +02:00
# For a description of this decorator, visit
# http://www.imdb.com/title/tt0067756/
def _silent_running(fn):
def _fn(*args, **kwargs):
_stdout = sys.stdout
sys.stdout = open(os.devnull, 'wb')
2014-06-26 11:46:37 +02:00
try:
result = fn(*args, **kwargs)
finally:
sys.stdout = _stdout
return result
return _fn
checkout_pkg = _silent_running(checkout_package)
2014-06-05 16:50:31 +02:00
def _checker_parse_name(self, apiurl, project, package,
revision=None, brief=False, verbose=False):
query = {'view': 'info', 'parse': 1}
if revision:
2014-06-05 16:50:31 +02:00
query['rev'] = revision
url = makeurl(apiurl, ['source', project, package], query)
try:
2014-06-05 16:50:31 +02:00
xml = ET.parse(http_GET(url))
name = xml.find('name')
except urllib2.HTTPError, e:
print('ERROR in URL %s [%s]' % (url, e))
2013-03-21 11:34:18 +01:00
return None
if name is None or not name.text:
2014-06-05 16:50:31 +02:00
return None
2013-03-21 11:34:18 +01:00
return name.text
2014-06-05 16:50:31 +02:00
2011-03-30 13:49:57 +02:00
def _checker_change_review_state(self, opts, id, newstate, by_group='', by_user='', message='', supersed=None):
2011-03-30 13:50:46 +02:00
""" taken from osc/osc/core.py, improved:
- verbose option added,
- empty by_user=& removed.
- numeric id can be int().
"""
2014-06-05 16:50:31 +02:00
query = {'cmd': 'changereviewstate', 'newstate': newstate}
if by_group:
query['by_group'] = by_group
if by_user:
query['by_user'] = by_user
if supersed:
query['superseded_by'] = supersed
url = makeurl(opts.apiurl, ['request', str(id)], query=query)
root = ET.parse(http_POST(url, data=message)).getroot()
2011-03-30 13:50:46 +02:00
return root.attrib['code']
2011-03-29 13:48:57 +02:00
2014-06-05 16:50:31 +02:00
2011-03-30 14:29:01 +02:00
def _checker_prepare_dir(self, dir):
2014-06-05 16:50:31 +02:00
olddir = os.getcwd()
2011-03-30 14:29:01 +02:00
os.chdir(dir)
shutil.rmtree(".osc")
os.chdir(olddir)
2014-06-05 16:50:31 +02:00
def _checker_add_review(self, opts, id, by_group=None, by_user=None, msg=None):
2014-06-05 16:50:31 +02:00
query = {'cmd': 'addreview'}
if by_group:
query['by_group'] = by_group
elif by_user:
query['by_user'] = by_user
else:
raise Exception('we need either by_group or by_user')
2011-11-22 13:00:03 +01:00
url = makeurl(opts.apiurl, ['request', str(id)], query)
try:
r = http_POST(url, data=msg)
2014-06-05 16:50:31 +02:00
except urllib2.HTTPError:
2011-11-22 13:00:03 +01:00
return 1
2011-11-22 13:00:03 +01:00
code = ET.parse(r).getroot().attrib['code']
if code != 'ok':
raise Exception(r)
return 0
2014-06-05 16:50:31 +02:00
def _checker_forward_to_staging(self, opts, id):
return self._checker_add_review(opts, id, by_group='factory-staging', msg="Pick Staging Project")
2014-06-05 16:50:31 +02:00
def _checker_add_review_team(self, opts, id):
return self._checker_add_review(opts, id, by_group='opensuse-review-team', msg="Please review sources")
2014-06-05 16:50:31 +02:00
def _checker_accept_request(self, opts, id, msg, diff=10000):
2014-06-05 05:40:07 +02:00
if diff > 12:
self._checker_add_review_team(opts, id)
else:
2014-06-05 10:52:33 +02:00
self._checker_add_review(opts, id, by_user='ancorgs', msg='Does it look harmless?')
2014-06-05 16:50:31 +02:00
self._checker_add_review(opts, id, by_user='factory-repo-checker', msg='Please review build success')
self._checker_forward_to_staging(opts, id)
2011-11-22 13:00:03 +01:00
self._checker_change_review_state(opts, id, 'accepted', by_group='factory-auto', message=msg)
print("accepted " + msg)
2014-06-05 16:50:31 +02:00
def _checker_one_request(self, rq, opts):
2011-03-30 13:50:46 +02:00
if (opts.verbose):
2011-03-29 13:48:57 +02:00
ET.dump(rq)
print(opts)
id = int(rq.get('id'))
act_id = 0
actions = rq.findall('action')
if len(actions) > 1:
2014-06-05 16:50:31 +02:00
msg = "2 actions in one SR is not supported - https://github.com/openSUSE/osc-plugin-factory/fork_select"
self._checker_change_review_state(opts, id, 'declined', by_group='factory-auto', message=msg)
print("declined " + msg)
return
2011-03-29 13:48:57 +02:00
for act in actions:
act_id += 1
2014-06-05 16:50:31 +02:00
_type = act.get('type')
if _type == "submit":
2011-03-29 13:48:57 +02:00
pkg = act.find('source').get('package')
prj = act.find('source').get('project')
rev = act.find('source').get('rev')
tprj = act.find('target').get('project')
tpkg = act.find('target').get('package')
2014-06-05 16:50:31 +02:00
src = {'package': pkg, 'project': prj, 'rev': rev, 'error': None}
2011-03-29 13:48:57 +02:00
e = []
2011-03-30 13:50:46 +02:00
if not pkg:
2011-03-29 13:48:57 +02:00
e.append('no source/package in request %d, action %d' % (id, act_id))
2011-03-30 13:50:46 +02:00
if not prj:
2011-03-29 13:48:57 +02:00
e.append('no source/project in request %d, action %d' % (id, act_id))
2014-06-05 16:50:31 +02:00
if e:
src.error = '; '.join(e)
2011-03-29 13:48:57 +02:00
e = []
2011-03-30 13:50:46 +02:00
if not tpkg:
2011-03-29 13:48:57 +02:00
e.append('no target/package in request %d, action %d; ' % (id, act_id))
2011-03-30 13:50:46 +02:00
if not prj:
2011-03-29 13:48:57 +02:00
e.append('no target/project in request %d, action %d; ' % (id, act_id))
# it is no error, if the target package dies not exist
subm_id = "SUBMIT(%d):" % id
2013-04-18 13:33:25 +02:00
print ("\n%s %s/%s -> %s/%s" % (subm_id,
2014-06-05 16:50:31 +02:00
prj, pkg,
tprj, tpkg))
2011-04-01 13:57:56 +02:00
dpkg = self._checker_check_devel_package(opts, tprj, tpkg)
2014-02-18 13:41:02 +01:00
# white list
2014-06-25 11:27:27 +02:00
self._devel_projects['X11:Bumblebee/'] = 'x2go'
2011-04-01 13:57:56 +02:00
if dpkg:
[dprj, dpkg] = dpkg.split('/')
else:
dprj = None
2014-06-05 16:50:31 +02:00
if dprj and (dprj != prj or dpkg != pkg) and ("IGNORE_DEVEL_PROJECTS" not in os.environ):
2011-04-01 13:57:56 +02:00
msg = "'%s/%s' is the devel package, submission is from '%s'" % (dprj, dpkg, prj)
self._checker_change_review_state(opts, id, 'declined', by_group='factory-auto', message=msg)
2013-04-18 13:33:25 +02:00
print("declined " + msg)
2011-04-01 13:57:56 +02:00
continue
2014-06-05 16:50:31 +02:00
if not dprj and (prj + "/") not in self._devel_projects:
2011-04-01 13:57:56 +02:00
msg = "'%s' is not a valid devel project of %s - please pick one of the existent" % (prj, tprj)
self._checker_change_review_state(opts, id, 'declined', by_group='factory-auto', message=msg)
2013-04-18 13:33:25 +02:00
print("declined " + msg)
2011-04-01 13:57:56 +02:00
continue
2012-01-30 13:46:30 +01:00
dir = os.path.expanduser("~/co/%s" % str(id))
2011-03-29 13:48:57 +02:00
if os.path.exists(dir):
2013-04-18 13:33:25 +02:00
print("%s already exists" % dir)
2011-03-30 13:50:46 +02:00
continue
2011-03-29 13:48:57 +02:00
os.mkdir(dir)
os.chdir(dir)
2011-03-31 12:35:05 +02:00
try:
checkout_pkg(opts.apiurl, tprj, tpkg, pathname=dir,
server_service_files=True, expand_link=True)
2011-03-31 12:35:05 +02:00
self._checker_prepare_dir(tpkg)
os.rename(tpkg, "_old")
except urllib2.HTTPError:
2014-06-05 16:50:31 +02:00
print("failed to checkout %s/%s" % (tprj, tpkg))
checkout_pkg(opts.apiurl, prj, pkg, revision=rev,
pathname=dir, server_service_files=True, expand_link=True)
2011-11-22 13:00:03 +01:00
os.rename(pkg, tpkg)
self._checker_prepare_dir(tpkg)
2011-03-30 14:29:01 +02:00
2014-06-05 16:50:31 +02:00
r = self._checker_parse_name(opts.apiurl, prj, pkg, revision=rev)
if r != tpkg:
msg = "A pkg submitted as %s has to build as 'Name: %s' - found Name '%s'" % (tpkg, tpkg, r)
2012-01-26 15:11:10 +01:00
self._checker_change_review_state(opts, id, 'declined', by_group='factory-auto', message=msg)
2014-06-05 16:50:31 +02:00
continue
sourcechecker = os.path.dirname(os.path.realpath(os.path.expanduser('~/.osc-plugins/osc-check_source.py')))
sourcechecker = os.path.join(sourcechecker, 'source-checker.pl')
civs = "LC_ALL=C perl %s _old %s 2>&1" % (sourcechecker, tpkg)
2011-03-30 14:29:01 +02:00
p = subprocess.Popen(civs, shell=True, stdout=subprocess.PIPE, close_fds=True)
2011-03-29 13:48:57 +02:00
ret = os.waitpid(p.pid, 0)[1]
checked = p.stdout.readlines()
2011-11-22 13:00:03 +01:00
output = ' '.join(checked).translate(None, '\033')
os.chdir("/tmp")
2014-06-05 16:50:31 +02:00
2011-03-29 13:48:57 +02:00
if ret != 0:
2011-11-22 13:00:03 +01:00
msg = "Output of check script:\n" + output
self._checker_change_review_state(opts, id, 'declined', by_group='factory-auto', message=msg)
2013-04-18 13:33:25 +02:00
print("declined " + msg)
2014-06-05 16:50:31 +02:00
shutil.rmtree(dir)
2011-03-30 13:50:46 +02:00
continue
2011-03-29 13:48:57 +02:00
2014-06-05 16:50:31 +02:00
shutil.rmtree(dir)
msg = 'Check script succeeded'
2014-06-05 05:40:07 +02:00
if len(checked) and checked[-1].startswith('DIFFCOUNT'):
# this is a major break through in perl<->python communication!
diff = int(checked.pop().split(' ')[1])
2014-06-05 16:50:31 +02:00
else: # e.g. new package
2014-06-05 05:40:07 +02:00
diff = 10000
2014-06-05 16:50:31 +02:00
2011-03-29 13:48:57 +02:00
if len(checked):
2011-11-22 13:00:03 +01:00
msg = msg + "\n\nOutput of check script (non-fatal):\n" + output
2014-06-05 16:50:31 +02:00
if self._checker_accept_request(opts, id, msg, diff=diff):
2014-06-05 16:50:31 +02:00
continue
2011-03-29 13:48:57 +02:00
else:
self._checker_forward_to_staging(opts, id)
2011-03-30 14:33:11 +02:00
self._checker_change_review_state(opts, id, 'accepted',
by_group='factory-auto',
message="Unchecked request type %s" % _type)
2011-03-29 13:48:57 +02:00
2014-06-05 16:50:31 +02:00
2011-04-01 13:57:56 +02:00
def _checker_check_devel_package(self, opts, project, package):
2014-06-05 16:50:31 +02:00
if project not in self._devel_projects:
url = makeurl(opts.apiurl, ['search', 'package'], "match=[@project='%s']" % project)
root = ET.parse(http_GET(url)).getroot()
2011-04-01 13:57:56 +02:00
for p in root.findall('package'):
name = p.attrib['name']
d = p.find('devel')
2014-06-05 16:50:31 +02:00
if d is not None:
2011-04-01 13:57:56 +02:00
dprj = d.attrib['project']
self._devel_projects["%s/%s" % (project, name)] = "%s/%s" % (dprj, d.attrib['package'])
# for new packages to check
self._devel_projects[dprj + "/"] = 1
elif not name.startswith("_product") and not name.startswith('preinstallimage') and not name == 'Test-DVD-x86_64':
2013-04-18 13:33:25 +02:00
print("NO DEVEL IN", name)
2011-04-01 13:57:56 +02:00
# mark we tried
self._devel_projects[project] = 1
try:
return self._devel_projects["%s/%s" % (project, package)]
except KeyError:
return None
2011-03-29 13:48:57 +02:00
2014-06-05 16:50:31 +02:00
2013-03-21 11:34:18 +01:00
def do_check_source(self, subcmd, opts, *args):
2011-03-29 13:48:57 +02:00
"""${cmd_name}: checker review of submit requests.
Usage:
2013-03-21 11:34:18 +01:00
osc check_source [OPT] [list] [FILTER|PACKAGE_SRC]
2011-03-29 13:48:57 +02:00
Shows pending review requests and their current state.
${cmd_option_list}
"""
2011-04-01 13:57:56 +02:00
self._devel_projects = {}
2011-03-29 13:48:57 +02:00
opts.verbose = False
opts.apiurl = self.get_api_url()
if len(args) and args[0] == 'skip':
2011-11-22 13:00:03 +01:00
for id in args[1:]:
2014-06-05 16:50:31 +02:00
self._checker_accept_request(opts, id, 'skip review')
2011-11-22 13:00:03 +01:00
return
2011-03-29 13:48:57 +02:00
ids = {}
for a in args:
2014-06-05 16:50:31 +02:00
if re.match('\d+', a):
2011-03-29 13:48:57 +02:00
ids[a] = 1
if (not len(ids)):
where = "@by_group='factory-auto'+and+@state='new'"
2014-06-05 16:50:31 +02:00
url = makeurl(opts.apiurl, ['search', 'request'], "match=state/@name='review'+and+review[" + where + "]")
root = ET.parse(http_GET(url)).getroot()
2011-03-29 13:48:57 +02:00
for rq in root.findall('request'):
self._checker_one_request(rq, opts)
2011-03-29 13:48:57 +02:00
else:
# we have a list, use them.
for id in ids.keys():
url = makeurl(opts.apiurl, ['request', id])
f = http_GET(url)
xml = ET.parse(f)
root = xml.getroot()
self._checker_one_request(root, opts)