Example #1
0
def add_hook(request):
    if settings.TEST:
        print 'We are in test mode'
    try:
        s = str(request.POST['payload'])
        j = json.loads(s,strict=False)
        s = j['repository']['url']+'updated files: '+str(j['head_commit']['modified'])
        cloning_repo = j['repository']['git_url']
        target_repo = j['repository']['full_name']
        user = j['repository']['owner']['email']
        changed_files = j['head_commit']['modified']
        #changed_files+= j['head_commit']['removed']
        changed_files+= j['head_commit']['added']
        if 'Merge pull request' in  j['head_commit']['message'] or 'OnToology Configuration' == j['head_commit']['message']:
            print 'This is a merge request or Configuration push'
            try:
                repo = Repo.objects.get(url=target_repo)
                print 'got the repo'
                repo.last_used = datetime.today()
                repo.save()
                print 'repo saved'
            except DoesNotExist:
                repo = Repo()
                repo.url=target_repo
                repo.save()
            except Exception as e:
                print 'database_exception: '+str(e)
            msg = 'This indicate that this merge request will be ignored'
            if settings.TEST:
                print msg
                return
            else:
                return render_to_response('msg.html',{'msg': msg},context_instance=RequestContext(request))
    except:
        msg = 'This request should be a webhook ping'
        if settings.TEST:
            print msg 
            return
        else:
            return render_to_response('msg.html',{'msg': msg},context_instance=RequestContext(request))
    print '##################################################'
    print 'changed_files: '+str(changed_files)
    # cloning_repo should look like '[email protected]:AutonUser/target.git'
    tar = cloning_repo.split('/')[-2]
    cloning_repo = cloning_repo.replace(tar,ToolUser)
    cloning_repo = cloning_repo.replace('git://github.com/','[email protected]:')
    comm = "python /home/ubuntu/OnToology/OnToology/autoncore.py "
    comm+=' "'+target_repo+'" "'+user+'" "'+cloning_repo+'" '
    for c in changed_files:
        comm+='"'+c+'" '
    if settings.TEST:
        print 'will call git_magic with target=%s, user=%s, cloning_repo=%s, changed_files=%s'%(target_repo, user, cloning_repo, str(changed_files))
        git_magic(target_repo, user, cloning_repo, changed_files)
        return
    else:
        print 'running autoncore code as: '+comm
        subprocess.Popen(comm,shell=True)
        return render_to_response('msg.html',{'msg': ''+s},context_instance=RequestContext(request))
Example #2
0
def generateforall(target_repo, user_email):
    cloning_repo = '[email protected]:' + target_repo
    tar = cloning_repo.split('/')[-2].split(':')[1]
    cloning_repo = cloning_repo.replace(tar, ToolUser)
    user = user_email
    ontologies = get_ontologies_in_online_repo(target_repo)
    changed_files = ontologies
    print 'current file dir: %s' % str(
        os.path.dirname(os.path.realpath(__file__)))
    # comm = "python /home/ubuntu/OnToology/OnToology/autoncore.py "
    #comm = "python %s " % \
    #       str((os.path.join(os.path.dirname(os.path.realpath(__file__)), 'autoncore.py')))
    if 'virtual_env_dir' in os.environ:
        print 'virtual_env_dir is in environ'
        comm = "%s %s " % \
               (os.path.join(os.environ['virtual_env_dir'], 'bin', 'python'),
                (os.path.join(os.path.dirname(os.path.realpath(__file__)), 'autoncore.py')))
    else:
        print 'virtual_env_dir is NOT in environ'
        comm = "python %s " % \
            (os.path.join(os.path.dirname(os.path.realpath(__file__)), 'autoncore.py'))
    comm += ' "' + target_repo + '" "' + user + '" '
    for c in changed_files:
        comm += '"' + c.strip() + '" '
    if settings.test_conf['local']:
        print "running autoncode in the same thread"
        git_magic(target_repo, user, changed_files)
    else:
        print 'running autoncore code as: ' + comm

        try:
            subprocess.Popen(comm, shell=True)
        except Exception as e:
            sys.stdout.flush()
            sys.stderr.flush()
            error_msg = str(e)
            print 'error running generall all subprocess: ' + error_msg
            if 'execv() arg 2 must contain only strings' in error_msg:
                return {
                    'status':
                    False,
                    'error':
                    'make sure that your repository filenames does not have accents or special characters'
                }
            else:
                return {
                    'status':
                    False,
                    'error':
                    'generic error, please report the problem to us at [email protected]'
                }
    sys.stdout.flush()
    sys.stderr.flush()
    return {'status': True}
Example #3
0
def handle_action(j, logger, raise_exp=False):
    """
    :param j:
    :return:
    """
    try:
        logger.debug("try action")
        try:
            import autoncore
            autoncore.django_setup_script()
        except:
            from OnToology import autoncore

        print("set logger")
        logger.debug("handle_action> ")
        repo = j['repo']
        if j['action'] == 'magic':
            logger.debug("going for magic: " + str(j))
            try:
                autoncore.git_magic(j['repo'],
                                    j['useremail'],
                                    j['changedfiles'],
                                    j['branch'],
                                    raise_exp=raise_exp)
                logger.debug("magic success")
            except Exception as e:
                logger.debug("dException in magic")
                logger.debug("dException in magic for repo: " + j['repo'])
                logger.debug(str(e))
                logger.error("Exception in magic for repo: " + j['repo'])
                logger.error(str(e))
                print("Exception in magic for repo: " + j['repo'])
                print(str(e))
                traceback.print_exc()
                if raise_exp:
                    raise Exception(str(e))
            logger.debug("magic is done")
        else:
            logger.debug("dInvalid magic redirect: ")
            logger.debug("dInvalid magic redirect with j: " + str(j))
            logger.error("Invalid magic redirect: ")
            logger.error("Invalid magic redirect with j: " + str(j))
    except Exception as e:
        logger.debug("dException 2 ")
        logger.debug("dException 2 for magic: " + str(e))
        logger.debug("dException for j: " + str(j))
        logger.error("Exception 2 ")
        logger.error("Exception 2 for magic: " + str(e))
        logger.error("Exception for j: " + str(j))
        traceback.print_exc()
        if raise_exp:
            raise Exception(str(e))
    logger.debug("finished handle_action: " + str(j))
Example #4
0
def handle_action(j):
    """
    :param j:
    :return:
    """
    global logger
    import autoncore
    if j['action'] == 'magic':
        logger.debug("going for magic")
        try:
            autoncore.git_magic(j['repo'], j['useremail'], j['changedfiles'])
            logger.debug("magic success")
        except Exception as e:
            logger.error("Exception in magic for repo: " + j['repo'])
            logger.error(str(e))
            print("Exception in magic for repo: " + j['repo'])
            print(str(e))
        logger.debug("magic is done")
Example #5
0
def generateforall(target_repo, user_email):
    cloning_repo = '[email protected]:' + target_repo
    tar = cloning_repo.split('/')[-2].split(':')[1]
    cloning_repo = cloning_repo.replace(tar, ToolUser)
    user = user_email
    ontologies = get_ontologies_in_online_repo(target_repo)
    changed_files = ontologies
    comm = "python /home/ubuntu/OnToology/OnToology/autoncore.py "
    comm += ' "' + target_repo + '" "' + user + '" "' + cloning_repo + '" '
    for c in changed_files:
        comm += '"' + c.strip() + '" '
    if settings.TEST:
        print 'will call git_magic with target=%s, user=%s, cloning_repo=%s, changed_files=%s' % \
              (target_repo, user, cloning_repo, str(changed_files))
        git_magic(target_repo, user, cloning_repo, changed_files)
    else:
        print 'running autoncore code as: ' + comm
        subprocess.Popen(comm, shell=True)
Example #6
0
def generateforall(target_repo, user_email):
    cloning_repo = '[email protected]:' + target_repo
    tar = cloning_repo.split('/')[-2].split(':')[1]
    cloning_repo = cloning_repo.replace(tar, ToolUser)
    user = user_email
    ontologies = get_ontologies_in_online_repo(target_repo)
    changed_files = ontologies
    comm = "python /home/ubuntu/OnToology/OnToology/autoncore.py "
    comm += ' "' + target_repo + '" "' + user + '" "' + cloning_repo + '" '
    for c in changed_files:
        comm += '"' + c.strip() + '" '
    if settings.TEST:
        print 'will call git_magic with target=%s, user=%s, cloning_repo=%s, changed_files=%s' % \
              (target_repo, user, cloning_repo, str(changed_files))
        git_magic(target_repo, user, cloning_repo, changed_files)
    else:
        print 'running autoncore code as: ' + comm
        subprocess.Popen(comm, shell=True)
Example #7
0
def add_hook(request):
    print "in add hook function"
    if settings.test_conf['local']:
        print 'We are in test mode'
    try:
        print "\n\nPOST DATA\n\n: " + str(request.POST)
        s = str(request.POST['payload'])
        print "payload: " + s
        j = json.loads(s, strict=False)
        print "json is loaded"
        if "ref" in j and j["ref"] == "refs/heads/gh-pages":
            print "it is just gh-pages"
            return render(request, 'msg.html', {'msg': 'it is gh-pages, so nothing'})
        s = j['repository']['url'] + 'updated files: ' + str(j['head_commit']['modified'])
        print "just s: " + str(s)
        cloning_repo = j['repository']['git_url']
        target_repo = j['repository']['full_name']
        user = j['repository']['owner']['email']
        print "cloning_repo: " + str(cloning_repo)
        print "target_repo: " + str(target_repo)
        print "user email: " + str(user)
        changed_files = get_changed_files_from_payload(j)
        print "early changed files: " + str(changed_files)
        if 'Merge pull request' in j['head_commit']['message'] or \
                'OnToology Configuration' == j['head_commit']['message'] or \
                'OnToology Publish' == j['head_commit']['message']:
            print 'This is a merge request or Configuration push'
            try:
                repo = Repo.objects.get(url=target_repo)
                print 'got the repo'
                repo.last_used = datetime.today()
                repo.progress = 100.0
                repo.save()
                print 'repo saved'
            except DoesNotExist:
                repo = Repo()
                repo.url = target_repo
                repo.save()
            except Exception as e:
                print 'database_exception: ' + str(e)
            msg = 'This indicate that this merge request will be ignored'
            print msg
            if settings.test_conf['local']:
                print msg
                return
            else:
                return render(request, 'msg.html', {'msg': msg})
    except Exception as e:
        print "add hook exception: " + str(e)
        msg = 'This request should be a webhook ping'
        if settings.test_conf['local']:
            print msg
            return
        else:
            return render(request, 'msg.html', {'msg': msg},)
    print '##################################################'
    print 'changed_files: ' + str(changed_files)
    # cloning_repo should look like '[email protected]:AutonUser/target.git'
    tar = cloning_repo.split('/')[-2]
    cloning_repo = cloning_repo.replace(tar, ToolUser)
    cloning_repo = cloning_repo.replace('git://github.com/', '[email protected]:')
    if 'virtual_env_dir' in os.environ:
        comm = "%s %s " % \
               (os.path.join(os.environ['virtual_env_dir'], 'bin', 'python'),
                (os.path.join(os.path.dirname(os.path.realpath(__file__)), 'autoncore.py')))
    else:
        comm = "python %s " % \
               (os.path.join(os.path.dirname(os.path.realpath(__file__)), 'autoncore.py'))
    print 'in addhook'
    print "target repo: %s" % target_repo
    print "user: %s" % user
    # comm += ' "' + target_repo + '" "' + user + '" '
    comm += '--magic --target_repo "' + target_repo + '" --useremail "' + user + '" --changedfiles '
    for c in changed_files:
        comm += '"' + c + '" '
    if settings.test_conf['local']:
        print 'will call git_magic with target=%s, user=%s, cloning_repo=%s, changed_files=%s' % (target_repo, user,
                                                                                                  cloning_repo,
                                                                                                  str(changed_files))
        git_magic(target_repo, user, changed_files)
        return
    else:
        print 'running autoncore code as: ' + comm
        try:
            subprocess.Popen(comm, shell=True)
        except Exception as e:
            error_msg = str(e)
            print 'error running generall all subprocess: ' + error_msg
            sys.stdout.flush()
            sys.stderr.flush()
            if 'execv() arg 2 must contain only strings' in error_msg:
                error_msg = 'make sure that your repository filenames does not have accents or special characters'
            else:
                error_msg = 'generic error, please report the problem to us [email protected]'
            s = error_msg
        # subprocess.Popen(comm, shell=True)
        return render('msg.html', {'msg': '' + s}, )
Example #8
0
def add_hook(request):
    if settings.TEST:
        print 'We are in test mode'
    try:
        s = str(request.POST['payload'])
        j = json.loads(s, strict=False)
        s = j['repository']['url'] + 'updated files: ' + str(
            j['head_commit']['modified'])
        cloning_repo = j['repository']['git_url']
        target_repo = j['repository']['full_name']
        user = j['repository']['owner']['email']
        changed_files = j['head_commit']['modified']
        #changed_files+= j['head_commit']['removed']
        changed_files += j['head_commit']['added']
        if 'Merge pull request' in j['head_commit'][
                'message'] or 'OnToology Configuration' == j['head_commit'][
                    'message']:
            print 'This is a merge request or Configuration push'
            try:
                repo = Repo.objects.get(url=target_repo)
                print 'got the repo'
                repo.last_used = datetime.today()
                repo.save()
                print 'repo saved'
            except DoesNotExist:
                repo = Repo()
                repo.url = target_repo
                repo.save()
            except Exception as e:
                print 'database_exception: ' + str(e)
            msg = 'This indicate that this merge request will be ignored'
            if settings.TEST:
                print msg
                return
            else:
                return render_to_response(
                    'msg.html', {'msg': msg},
                    context_instance=RequestContext(request))
    except:
        msg = 'This request should be a webhook ping'
        if settings.TEST:
            print msg
            return
        else:
            return render_to_response('msg.html', {'msg': msg},
                                      context_instance=RequestContext(request))
    print '##################################################'
    print 'changed_files: ' + str(changed_files)
    # cloning_repo should look like '[email protected]:AutonUser/target.git'
    tar = cloning_repo.split('/')[-2]
    cloning_repo = cloning_repo.replace(tar, ToolUser)
    cloning_repo = cloning_repo.replace('git://github.com/', '[email protected]:')
    comm = "python /home/ubuntu/OnToology/OnToology/autoncore.py "
    comm += ' "' + target_repo + '" "' + user + '" "' + cloning_repo + '" '
    for c in changed_files:
        comm += '"' + c + '" '
    if settings.TEST:
        print 'will call git_magic with target=%s, user=%s, cloning_repo=%s, changed_files=%s' % (
            target_repo, user, cloning_repo, str(changed_files))
        git_magic(target_repo, user, cloning_repo, changed_files)
        return
    else:
        print 'running autoncore code as: ' + comm
        subprocess.Popen(comm, shell=True)
        return render_to_response('msg.html', {'msg': '' + s},
                                  context_instance=RequestContext(request))