#!/usr/bin/env python # Copyright(C) 2013, 2014, 2015 Open Information Security Foundation # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, version 2 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Note to Docker users: # If you are running SELinux in enforced mode, you may want to run # chcon -Rt svirt_sandbox_file_t SURICATA_ROOTSRC_DIR # or the buildbot will not be able to access to the data in /data/oisf # and the git step will fail. import urllib, urllib2, cookielib try: import simplejson as json except: import json import time import argparse import sys import os GOT_DOCKER = True try: from docker import Client except: GOT_DOCKER = False # variables # - github user # - buildbot user and password BASE_URI="https://buildbot.openinfosecfoundation.org/" GITHUB_BASE_URI = "https://api.github.com/repos/" GITHUB_MASTER_URI = "https://api.github.com/repos/inliniac/suricata/commits?sha=master" parser = argparse.ArgumentParser(prog='prscript', description='Script checking validity of branch before PR') parser.add_argument('-u', '--username', dest='username', help='github and buildbot user') parser.add_argument('-p', '--password', dest='password', help='buildbot password') parser.add_argument('-c', '--check', action='store_const', const=True, help='only check last build', default=False) parser.add_argument('-v', '--verbose', action='store_const', const=True, help='verbose output', default=False) parser.add_argument('--norebase', action='store_const', const=True, help='do not test if branch is in sync with master', default=False) parser.add_argument('-r', '--repository', dest='repository', default='suricata', help='name of suricata repository on github') parser.add_argument('-d', '--docker', action='store_const', const=True, help='use docker based testing', default=False) parser.add_argument('-l', '--local', action='store_const', const=True, help='local testing before github push', default=False) if GOT_DOCKER: parser.add_argument('-C', '--create', action='store_const', const=True, help='create docker container', default=False) parser.add_argument('-s', '--start', action='store_const', const=True, help='start docker container', default=False) parser.add_argument('-S', '--stop', action='store_const', const=True, help='stop docker container', default=False) parser.add_argument('branch', metavar='branch', help='github branch to build') args = parser.parse_args() username = args.username password = args.password cookie = None if args.docker: BASE_URI="http://localhost:8010/" BUILDERS_LIST = ["gcc", "clang", "debug", "features", "profiling", "pcaps"] else: BUILDERS_LIST = [username, username + "-pcap"] BUILDERS_URI=BASE_URI+"builders/" JSON_BUILDERS_URI=BASE_URI+"json/builders/" def TestRepoSync(branch): request = urllib2.Request(GITHUB_MASTER_URI) page = urllib2.urlopen(request) json_result = json.loads(page.read()) sha_orig = json_result[0]["sha"] request = urllib2.Request(GITHUB_BASE_URI + username + "/" + args.repository + "/commits?sha=" + branch + "&per_page=100") page = urllib2.urlopen(request) json_result = json.loads(page.read()) found = -1 for commit in json_result: if commit["sha"] == sha_orig: found = 1 break return found def OpenBuildbotSession(): auth_params = { 'username':username,'passwd':password, 'name':'login'} cookie = cookielib.LWPCookieJar() params = urllib.urlencode(auth_params) opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie)) urllib2.install_opener(opener) request = urllib2.Request(BASE_URI + 'login', params) page = urllib2.urlopen(request) return cookie def SubmitBuild(branch, extension = "", builder_name = None): raw_params = {'branch':branch,'reason':'Testing ' + branch, 'name':'force_build', 'forcescheduler':'force'} params = urllib.urlencode(raw_params) if not args.docker: opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie)) urllib2.install_opener(opener) if builder_name == None: builder_name = username + extension request = urllib2.Request(BUILDERS_URI + builder_name + '/force', params) page = urllib2.urlopen(request) result = page.read() if args.verbose: print "=== response ===" print result print "=== end of response ===" if args.docker: if "