Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask,request,abort
- import logging,subprocess
- import sys,logging,threading,json,os
- app = Flask(__name__)
- @app.route('/')
- def MainPage():
- return 'Eventually will show the list of active projects and links to there info'
- @app.route('/post', methods=['GET','POST'])
- def PostRequests():
- if request.method == 'POST':
- do_post_handler()
- else:
- show_error_page()
- def show_error_page():
- app.logger.debug('Idiot tried to load a useless page')
- abort(403)
- def do_post_handler():
- app.logger.debug('Post received')
- #Here we need to actually parse teh post data and call the appropriate stuff
- true = True
- false = False
- app.logger.debug('Mime Type: %s', request.mimetype)
- app.logger.debug('Method Type: %s', request.method)
- app.logger.debug('Request.Form: %s', request.form)
- # app.logger.debug('Request.Form[0][1]: %s', request.form[0][1])
- dat = json.loads(request.form['payload'])
- #start with checking for bitbucket If its not bitbucket ditch asap
- try:
- if dat['canon_url'] != "https://bitbucket.org":
- app.logger.warn('Unknown Post client: %s', request.form['canon_url'])
- abort(403)
- except KeyError:
- app.logger.warn('Unknown Post client: no Canon_url')
- abort(403)
- #ok here we handle checking the repo for validity
- if dat['repository']['scm'] != 'hg':
- app.logger.warn('Unknown scm: %s', request.form['repository']['scm'])
- abort(403)
- #ok now we get to the scary stuff.
- #first check to see if owner has a path
- ownerpath = os.path.join(datapath,dat['repository']['owner'])
- ownerrepo = os.path.join(datapath,dat['repository']['owner'],'repo')
- app.logger.debug('ownerpath: %s', ownerpath)
- app.logger.debug('ownerpathreporoot: %s', ownerrepo)
- if not os.path.exists(ownerrepo):
- os.makedirs(ownerrepo)
- app.logger.info('Owner Path Created: %s', ownerrepo)
- #ok now check to see if the currently wanted repository exists.
- repourl = dat['canon_url']+dat['repository']['absolute_url']
- repopath = os.path.join(ownerrepo,dat['repository']['slug'])
- if __debug__:
- app.logger.debug("Dumping post data to file")
- postdata = open(os.path.join(rootpath,"postdata"),'w+')
- json.dump(dat,postdata)
- postdata.close()
- app.logger.debug('repopath: %s',repopath)
- # import pdb; pdb.set_trace()
- if not os.path.exists(repopath):
- try:
- os.chdir(ownerrepo)
- app.logger.info('Cloneing Repo: %s', dat['repository']['name'])
- app.logger.debug('repourl: %s', repourl)
- varout = subprocess.check_output(['hg','clone','--insecure',repourl])
- hgclone = open(os.path.join(repopath,"hgclone.stdout"),'w+')
- hgclone.write(varout)
- hgclone.close()
- app.logger.info('Done Cloneing repo')
- except subprocess.CalledProcessError:
- app.logger.error('Hg clone failed..idunno watdo')
- os.chdir(repopath)
- repoconfig = openrepoconfig(repopath)
- buildbranches = repoconfig['Branches']
- else: #if path already exists...
- os.chdir(repopath)
- repoconfig = openrepoconfig(repopath)
- buildbranches = []
- for branch in repoconfig['Branches']:
- app.logger.info('Checking for updates to branch: %s',branch)
- try:
- varout = subprocess.check_output(['hg','incoming','--insecure','--bundle','-b',branch])
- buildbranches += [branch]
- app.logger.info('Branch %s queued for update', branch)
- except subprocess.CalledProcessError:
- app.logger.info('Branch %s has no updates', branch)
- for branch in buildbranches:
- app.logger.info('Updating repo: {}, branch: {}'.format(dat['repository']['name'], branch))
- varout = subprocess.check_output(['hg','pull','--insecure','-b',branch,repourl])
- hgupdate = open(os.path.join(repopath,branch+'.hgupdate.stdout'),'w+')
- hgupdate.write(varout)
- hgupdate.close()
- app.logger.info('Updating repo: %s complete')
- #ok repo exists. and are updated
- #buildbranches contains all the branches that had updates
- os.chdir(repopath)
- #ok now parse config file and drop data in appropriate places
- #hg update build repeat
- for branch in buildbranches:
- app.logger.info('Switching to updated branch: %s',branch)
- varout = subprocess.check_output(['hg','update','-c',branch])
- hgupdate = open(os.path.join(repopath,branch+'.hgupdate.stdout'),'w+')
- hgupdate.write(varout)
- hgupdate.close()
- do_build_repo(repopath,branch,repoconfig,dat)
- do_make_webpage(repopath,branch)
- app.logger.warning('EOL for do_post_handler we should never be here ABORT SIR ABORT')
- abort(403) #if we ever get back here we have issues...
- def do_build_repo(repopath,branch,repoconfig,dat):
- """ repopath = basepath to repo
- branch = branch to compile. """
- #already updated to that branch just do compile magic
- #then the webgen magic guess i should figure out what i want the webpage to look like
- if sys.platform == 'linux2':
- brass = ['mono',os.path.join(rootpath,'tools/brass')]
- spasm = [os.path.join(rootpath,'tools/spasmnix')]
- app.logger.debug('Linux binary paths: {}, {}'.format(brass,spasm) )
- else:
- brass = [os.path.join(rootpath,'tools/brass')]
- spasm = [os.path.join(rootpath,'tools/spasm')]
- app.logger.debug('Windows binary paths: {}, {}'.format(brass,spasm) )
- import pdb;pdb.set_trace()
- for buildid in repoconfig['Build']:
- try:
- assembler = get_assembler(repoconfig['Build'][buildid]['Assembler'],brass,spasm)
- app.logger.info('Build {}: Assembler {}'.format(buildid,assembler))
- except:
- app.logger.error('Assembler {}: Is not currently supported'.format(repoconfig['Build'][buildid]['Assembler']))
- abort(403)
- def get_assembler(assembler,brass,spasm): #hate that i have to have each of those but i sadly know its right :(
- return {
- 'Brass': brass,
- 'Spasm': spasm}.get(assembler)
- def do_make_webpage(repopath,branch):
- """ generates files for easy page generation from jinja
- should make a json item for it and put in appropriate directory"""
- #directory is as such
- #rootpath/<owner>/html/repository/<shortid>
- def openrepoconfig(repopath):
- try:
- repoconfigfile = open(os.path.join(repopath,'z80ci.build'),'r+')
- except IOError:
- app.logger.error('No Config File for this repo Aborting')
- abort(403) #eventually clean up repo if this happens. OR make a dummy config
- try:
- repoconfig = json.load(repoconfigfile)
- except:
- app.logger.error('Config File Failed to load is it corrupt?')
- abort(403)
- app.logger.info('Config File loaded')
- return repoconfig
- if __name__ == '__main__':
- rootpath = os.path.dirname(os.path.realpath(__file__))
- datapath = os.path.join(rootpath,'data')
- std = logging.StreamHandler(sys.stdout)
- logfile = logging.FileHandler(os.path.join(rootpath,'z80ci.log'))
- logfile.setFormatter(logging.Formatter('''%(asctime)s: %(levelname)s: || %(message)s'''))
- std.setFormatter(logging.Formatter('''
- %(asctime)s: %(levelname)s: || %(message)s
- '''))
- std.setLevel(logging.DEBUG)
- app.logger.addHandler(logfile)
- logfile.setLevel(logging.DEBUG)
- try:
- options = open(os.path.join(rootpath,'z80ci.json'))
- except IOError:
- app.logger.error('No Options File Go read the Docs!!!!')
- exit()
- optionsjson = json.load(options)
- #ok we got an options file yaaayyy
- app.run(debug=True,host='0.0.0.0')
Advertisement
Add Comment
Please, Sign In to add comment