converge.py 2.46 KB
from datetime import date
import subprocess
import time
import os
import sys
#Needs future lib: pip install future
import concurrent.futures
#Needs git python: pip install gitpython
import git

#Get current dir
os.chdir(os.path.dirname(os.path.abspath(sys.argv[0])))

SRC_PATH = os.path.abspath(os.path.join(os.getcwd(), os.pardir))
SPB_PATH = os.path.abspath(os.path.join(SRC_PATH, os.pardir))
SPB_ENV = 'local'
today = str(date.today())
BUILDS = []
BUILD_COUNT = 0
buildDict = dict()


spb_repo = git.Git(SPB_PATH)
print('Using git stash to save your local changes before converging')
spb_repo.stash('save')
print('Trying to checkout from current branch to master')
spb_repo.checkout('master')
spb_repo.pull()

def checkBuildStatus(packageName,buildId):
	buildStatus = str(subprocess.check_output(['copr-cli', 'status', buildId])).rstrip()
	while buildStatus not in ('succeeded', 'failed'):
		buildStatus = str(subprocess.check_output(['copr-cli', 'status', buildId])).rstrip()
		print('Currently running build for %s with id %s, build status is: %s'% (packageName,buildId,buildStatus))
		time.sleep(30)
	print('%s build with id %s exited with status: %s'% (packageName,buildId,buildStatus))
	
	with open(today+'_log', 'a+') as f:
		f.write('%s build with id %s exited with status: %s\n'% (packageName,buildId,buildStatus))
	
	if(buildStatus == 'succeeded'):
		return 0
	else:
		return 1 
	
pluginsUpload = subprocess.Popen('make release_plugins', shell=True, cwd=SRC_PATH)

pluginsUpload.wait()

noosferoUpload = subprocess.Popen('make noosfero-upload', shell=True, cwd=SRC_PATH)

noosferoUpload.wait()

colabUpload = subprocess.Popen('make colab-upload', shell=True, cwd=SRC_PATH)

colabUpload.wait()

with open(today+'.yaml') as f:
	BUILD_COUNT = sum(1 for _ in f)

with open(today+'.yaml') as f:
	BUILDS = f.read().splitlines()[-5:]
	for build in BUILDS:
		package, buildId = build.strip().split(':')
		buildDict.update({package : buildId})

with concurrent.futures.ThreadPoolExecutor(max_workers=BUILD_COUNT) as executor:
	future_builds = {executor.submit(checkBuildStatus,packageName,buildId) for packageName, buildId in buildDict.iteritems()}

for build in concurrent.futures.as_completed(future_builds):
	status = build.result()
	if(status != 0):
		sys.exit('One of the packages failed to build, aborting converge, check the log file')
	else:
		print('All builds are ok, proceding to converge')


spbConverge = subprocess.Popen(['rake converge SPB_ENV='+SPB_ENV], shell=True, cwd=SPB_PATH)

spbConverge.wait()