Geekboy

Untitled

Sep 1st, 2014
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.17 KB | None | 0 0
  1. from flask import Flask,request,abort
  2. import logging,subprocess
  3. import sys,logging,threading,json,os
  4. app = Flask(__name__)
  5.  
  6. @app.route('/')
  7. def MainPage():
  8.     return 'Eventually will show the list of active projects and links to there info'
  9.  
  10. @app.route('/post', methods=['GET','POST'])
  11. def PostRequests():
  12.     if request.method == 'POST':
  13.         do_post_handler()
  14.     else:
  15.         show_error_page()
  16.  
  17. def show_error_page():
  18.     app.logger.debug('Idiot tried to load a useless page')
  19.     abort(403)
  20.  
  21. def do_post_handler():
  22.    
  23.     app.logger.debug('Post received')
  24.     #Here we need to actually parse teh post data and call the appropriate stuff
  25.     true = True
  26.     false = False
  27.     app.logger.debug('Mime Type: %s', request.mimetype)
  28.     app.logger.debug('Method Type: %s', request.method)
  29.     app.logger.debug('Request.Form: %s', request.form)
  30.    # app.logger.debug('Request.Form[0][1]: %s', request.form[0][1])
  31.     dat = json.loads(request.form['payload'])
  32.    
  33.     #start with checking for bitbucket If its not bitbucket ditch asap
  34.     try:
  35.         if dat['canon_url'] != "https://bitbucket.org":
  36.             app.logger.warn('Unknown Post client: %s', request.form['canon_url'])
  37.             abort(403)
  38.     except KeyError:
  39.          app.logger.warn('Unknown Post client: no Canon_url')
  40.          abort(403)
  41.    
  42.  
  43.     #ok here we handle checking the repo for validity
  44.     if dat['repository']['scm'] != 'hg':
  45.         app.logger.warn('Unknown scm: %s', request.form['repository']['scm'])
  46.         abort(403)
  47.  
  48.     #ok now we get to the scary stuff.
  49.    
  50.     #first check to see if owner has a path
  51.     ownerpath = os.path.join(datapath,dat['repository']['owner'])
  52.     ownerrepo = os.path.join(datapath,dat['repository']['owner'],'repo')
  53.     app.logger.debug('ownerpath: %s', ownerpath)
  54.     app.logger.debug('ownerpathreporoot: %s', ownerrepo)
  55.    
  56.     if not os.path.exists(ownerrepo):
  57.         os.makedirs(ownerrepo)
  58.         app.logger.info('Owner Path Created: %s', ownerrepo)
  59.  
  60.    
  61.     #ok now check to see if the currently wanted repository exists.
  62.     repourl = dat['canon_url']+dat['repository']['absolute_url']
  63.     repopath = os.path.join(ownerrepo,dat['repository']['slug'])
  64.    
  65.     if __debug__:
  66.         app.logger.debug("Dumping post data to file")
  67.         postdata = open(os.path.join(rootpath,"postdata"),'w+')  
  68.         json.dump(dat,postdata)
  69.         postdata.close()  
  70.    
  71.    
  72.     app.logger.debug('repopath: %s',repopath)
  73.  
  74.    # import pdb; pdb.set_trace()
  75.     if not os.path.exists(repopath):
  76.         try:
  77.             os.chdir(ownerrepo)
  78.             app.logger.info('Cloneing Repo: %s', dat['repository']['name'])
  79.             app.logger.debug('repourl: %s', repourl)
  80.             varout = subprocess.check_output(['hg','clone','--insecure',repourl])
  81.             hgclone = open(os.path.join(repopath,"hgclone.stdout"),'w+')
  82.             hgclone.write(varout)
  83.             hgclone.close()
  84.             app.logger.info('Done Cloneing repo')
  85.         except subprocess.CalledProcessError:
  86.             app.logger.error('Hg clone failed..idunno watdo')
  87.        
  88.         os.chdir(repopath)
  89.         repoconfig = openrepoconfig(repopath)
  90.         buildbranches = repoconfig['Branches']
  91.  
  92.        
  93.     else: #if path already exists...
  94.         os.chdir(repopath)
  95.        
  96.         repoconfig = openrepoconfig(repopath)
  97.         buildbranches = []
  98.  
  99.         for branch in repoconfig['Branches']:
  100.             app.logger.info('Checking for updates to branch: %s',branch)
  101.             try:
  102.                 varout = subprocess.check_output(['hg','incoming','--insecure','--bundle','-b',branch])
  103.                 buildbranches += [branch]
  104.                 app.logger.info('Branch %s queued for update', branch)
  105.             except subprocess.CalledProcessError:
  106.                 app.logger.info('Branch %s has no updates', branch)
  107.        
  108.         for branch in buildbranches:
  109.             app.logger.info('Updating repo: {}, branch: {}'.format(dat['repository']['name'], branch))
  110.             varout = subprocess.check_output(['hg','pull','--insecure','-b',branch,repourl])
  111.             hgupdate = open(os.path.join(repopath,branch+'.hgupdate.stdout'),'w+')
  112.             hgupdate.write(varout)
  113.             hgupdate.close()
  114.             app.logger.info('Updating repo: %s complete')
  115.  
  116.  
  117.    
  118.     #ok repo exists. and are updated
  119.     #buildbranches contains all the branches that had updates
  120.     os.chdir(repopath)
  121.  
  122.    
  123.  
  124.    
  125.     #ok now parse config file and drop data in appropriate places
  126.    
  127.     #hg update build repeat
  128.     for branch in buildbranches:
  129.         app.logger.info('Switching to updated branch: %s',branch)
  130.         varout = subprocess.check_output(['hg','update','-c',branch])
  131.        
  132.         hgupdate = open(os.path.join(repopath,branch+'.hgupdate.stdout'),'w+')
  133.         hgupdate.write(varout)
  134.         hgupdate.close()
  135.         do_build_repo(repopath,branch,repoconfig,dat)
  136.         do_make_webpage(repopath,branch)
  137.        
  138.        
  139.        
  140.        
  141.  
  142.     app.logger.warning('EOL for do_post_handler we should never be here ABORT SIR ABORT')  
  143.     abort(403) #if we ever get back here we have issues...
  144.    
  145. def do_build_repo(repopath,branch,repoconfig,dat):
  146.     """ repopath = basepath to repo
  147.        branch = branch to compile. """
  148.         #already updated to that branch just do compile magic
  149.         #then the webgen magic guess i should figure out what i want the webpage to look like
  150.     if sys.platform == 'linux2':
  151.         brass = ['mono',os.path.join(rootpath,'tools/brass')]
  152.         spasm = [os.path.join(rootpath,'tools/spasmnix')]
  153.         app.logger.debug('Linux binary paths: {}, {}'.format(brass,spasm) )
  154.     else:
  155.         brass = [os.path.join(rootpath,'tools/brass')]
  156.         spasm = [os.path.join(rootpath,'tools/spasm')]
  157.         app.logger.debug('Windows binary paths: {}, {}'.format(brass,spasm) )
  158.    
  159.     import pdb;pdb.set_trace()      
  160.     for buildid in repoconfig['Build']:
  161.         try:
  162.             assembler = get_assembler(repoconfig['Build'][buildid]['Assembler'],brass,spasm)
  163.             app.logger.info('Build {}: Assembler {}'.format(buildid,assembler))
  164.         except:
  165.             app.logger.error('Assembler {}: Is not currently supported'.format(repoconfig['Build'][buildid]['Assembler']))
  166.             abort(403)
  167.            
  168.            
  169. def get_assembler(assembler,brass,spasm):  #hate that i have to have each of those but i sadly know its right :(
  170.     return {
  171.         'Brass': brass,
  172.         'Spasm': spasm}.get(assembler)
  173.        
  174.        
  175. def do_make_webpage(repopath,branch):
  176.     """ generates files for easy page generation from jinja
  177.        should make a json item for it and put in appropriate directory"""
  178.         #directory is as such
  179.         #rootpath/<owner>/html/repository/<shortid>
  180.        
  181. def openrepoconfig(repopath):
  182.     try:
  183.         repoconfigfile = open(os.path.join(repopath,'z80ci.build'),'r+')
  184.     except IOError:
  185.         app.logger.error('No Config File for this repo Aborting')
  186.         abort(403) #eventually clean up repo if this happens. OR make a dummy config
  187.     try:
  188.         repoconfig = json.load(repoconfigfile)    
  189.     except:
  190.         app.logger.error('Config File Failed to load is it corrupt?')
  191.         abort(403)
  192.        
  193.     app.logger.info('Config File loaded')
  194.     return repoconfig
  195.    
  196. if __name__ == '__main__':
  197.    
  198.     rootpath = os.path.dirname(os.path.realpath(__file__))
  199.     datapath = os.path.join(rootpath,'data')    
  200.        
  201.     std = logging.StreamHandler(sys.stdout)
  202.     logfile = logging.FileHandler(os.path.join(rootpath,'z80ci.log'))
  203.     logfile.setFormatter(logging.Formatter('''%(asctime)s: %(levelname)s:  || %(message)s'''))
  204.     std.setFormatter(logging.Formatter('''
  205.    %(asctime)s: %(levelname)s:  || %(message)s
  206.    '''))
  207.    
  208.     std.setLevel(logging.DEBUG)
  209.     app.logger.addHandler(logfile)
  210.     logfile.setLevel(logging.DEBUG)
  211.      
  212.     try:
  213.         options = open(os.path.join(rootpath,'z80ci.json'))
  214.     except IOError:
  215.         app.logger.error('No Options File Go read the Docs!!!!')
  216.         exit()  
  217.    
  218.     optionsjson = json.load(options)
  219.   #ok we got an options file yaaayyy
  220.  
  221.  
  222.     app.run(debug=True,host='0.0.0.0')
Advertisement
Add Comment
Please, Sign In to add comment