Ejemplo n.º 1
0
def repo_rename():
    if request.method == 'POST':
        try:
            old_reponame = request.form.keys()[0]
        except IndexError:
            old_reponame = ''
        new_reponame = request.form.get(old_reponame, '')
        old_name_file = BASE_DIR + '/conf/repos/' \
            + old_reponame + '.conf'
        new_name_file = BASE_DIR + '/conf/repos/' \
            + new_reponame + '.conf'
        if new_reponame.strip() != '' and new_reponame != old_reponame:
            renamerepo = db_session.query(GitRepo).filter_by(
                name=old_reponame).first()
            renamerepo.name = new_reponame
            db_session.commit()
            db_session.close()
            #~ repodict.pop(old_reponame)
            new_lines = []
            with open(old_name_file, 'rb') as f:
                while True:
                    lines = f.readlines(8192)
                    if not lines:
                        break
                    for line in lines:
                        line = line.rstrip('\n')
                        line = re.sub('@' + old_reponame, '@' + new_reponame,
                                      line)
                        new_lines.append(line)
            with open(old_name_file, 'wb') as f:
                f.truncate()
                for _ in new_lines:
                    print >> f, _
            os.rename(old_name_file, new_name_file)
        return redirect(url_for('repo'))
Ejemplo n.º 2
0
def add_asso_group():
    if request.method == 'POST':
        try:
            request_key = request.form.keys()[0]
            get_user_id = int(request_key.split('-')[1])
            get_group_id = int(request.form.get(request_key, ''))
            assouser = db_session.query(GitUser).filter_by(
                id=get_user_id).first()
            assogroup = db_session.query(GitGroup).filter_by(
                id=get_group_id).first()
            if assogroup not in assouser.git_group:
                assouser.git_group.append(assogroup)

            assos = db_session.query(asso_group_user).all()
            if (get_group_id, get_user_id) not in assos:
                try:
                    db_session.add(assouser)
                    db_session.commit()
                    print '====>  Store (%s) to DB successfully.' % assouser.name

                    assogroup_file = BASE_DIR + '/conf/groups/' \
                        + assogroup.name + '.conf'
                    gconf_dict = {}
                    with open(assogroup_file, 'rb') as f:
                        lines = f.readlines()
                        for line in lines:
                            gconf_group = line.split('=')[0].lstrip(
                                '@').strip()
                            gconf_user = line.split('=')[1].split()
                            gconf_dict[gconf_group] = gconf_user
                    allowed_ext_user = []
                    for _ in allowed_ext:
                        allowed_ext_user.append('.'.join(
                            assouser.name.split('.')[:-1]))
                    for _ in set(allowed_ext_user):
                        if gconf_dict.has_key(
                                assogroup.name) and _ not in gconf_dict[
                                    assogroup.name]:
                            gconf_dict[assogroup.name].append(str(_))
                            f = open(assogroup_file, 'wb')
                            f.truncate()
                            for _ in gconf_dict:
                                f.write('@' + _ + ' = ' +
                                        ' '.join(gconf_dict[_]))
                            f.close()
                except TypeError:
                    db_session.rollback()
                    print '====>  TypeError Need string or buffer, list found.'
                finally:
                    db_session.close()
            else:
                print '====>  The record(%s) already exists.' % assouser.name
        except IndexError:
            print '====>  IndexError because of out of range.'
        return redirect(url_for('user'))
Ejemplo n.º 3
0
def user_remove():
    if request.method == 'POST':
        try:
            remove_user_name = request.form.keys()[0]
        except IndexError:
            remove_user_name = ''
        assouser = db_session.query(GitUser).filter_by(
            name=remove_user_name).first()
        assogroup_relation = db_session.query(asso_group_user).filter_by(
            git_user_id=assouser.id).all()

        assogroup_list = []
        for _ in assogroup_relation:
            assogroup = db_session.query(GitGroup).filter_by(
                id=_.git_group_id).first()
            assogroup_list.append(assogroup)
        if not assogroup_list:
            for assogroup in assogroup_list:
                assouser.git_group.remove(assogroup)
                db_session.delete(assouser)
        db_session.delete(assouser)
        db_session.commit()

        deleteuser_assogroup = []
        for _ in assogroup_list:
            deleteuser_assogroup_file = BASE_DIR \
                + '/conf/groups/' + _.name + '.conf'
            deleteuser_assogroup.append(deleteuser_assogroup_file)

        gconf_dict = {}
        for i in deleteuser_assogroup:
            with open(i, 'rb') as f:
                lines = f.readlines()
                for line in lines:
                    gconf_group = line.split('=')[0].lstrip('@').strip()
                    gconf_user = line.split('=')[1].split()
                    gconf_dict[gconf_group] = gconf_user

        assouser.name = '.'.join(assouser.name.split('.')[:-1])
        assouser_name = str(assouser.name)
        for each_group in gconf_dict:
            if assouser_name in gconf_dict[each_group]:
                gconf_dict[each_group].remove(assouser_name)
                assogroup_file = BASE_DIR + '/conf/groups/' + each_group \
                    + '.conf'
                f = open(assogroup_file, 'wb')
                f.truncate()
                f.write('@' + each_group + ' = ' +
                        ' '.join(gconf_dict[each_group]))
                f.close()
        db_session.close()
        user_name_file = os.path.join(KEY_DIR, remove_user_name)
        os.remove(user_name_file)
        return redirect(url_for('user'))
Ejemplo n.º 4
0
def group_remove():
    if request.method == 'POST':
        try:
            remove_group_name = request.form.keys()[0]
        except IndexError:
            remove_group_name = ''
        group_name_file = BASE_DIR + '/conf/groups/' \
            + remove_group_name + '.conf'
        deletegroup = db_session.query(GitGroup).filter_by(
            name=remove_group_name).first()
        db_session.delete(deletegroup)
        db_session.commit()
        db_session.close()
        os.remove(group_name_file)
        #~ groupdict.pop(remove_group_name)
        return redirect(url_for('group'))
Ejemplo n.º 5
0
def remove_asso_group():
    if request.method == 'POST':
        try:
            request_key = request.form.keys()[0]
            get_user_id = int(request_key.split('-')[1])
            get_group_id = request.form.get(request_key, '')
            assouser = db_session.query(GitUser).filter_by(
                id=get_user_id).first()
            assogroup = db_session.query(GitGroup).filter_by(
                id=get_group_id).first()
            assouser.git_group.remove(assogroup)
            db_session.add(assouser)
            db_session.commit()
            print '====>  Sponge (%s) from DB successfully.' % assouser.name

            assogroup_file = BASE_DIR + '/conf/groups/' \
                + assogroup.name + '.conf'
            gconf_dict = {}
            with open(assogroup_file, 'rb') as f:
                lines = f.readlines()
                for line in lines:
                    gconf_group = line.split('=')[0].lstrip('@').strip()
                    gconf_user = line.split('=')[1].split()
                    gconf_dict[gconf_group] = gconf_user
            allowed_ext_user = []
            for _ in allowed_ext:
                allowed_ext_user.append('.'.join(
                    assouser.name.split('.')[:-1]))
            for _ in set(allowed_ext_user):
                if gconf_dict.has_key(
                        assogroup.name) and _ in gconf_dict[assogroup.name]:
                    gconf_dict[assogroup.name].remove(str(_))
                    f = open(assogroup_file, 'wb')
                    f.truncate()
                    for _ in gconf_dict:
                        f.write('@' + _ + ' = ' + ' '.join(gconf_dict[_]))
                    f.close()
        except IndexError:
            db_session.rollback()
            print '====>  IndexError Out of range.'
        except ValueError:
            db_session.rollback()
            print '====>  ValueError Not in list.'
        finally:
            db_session.close()
        return redirect(url_for('user'))
Ejemplo n.º 6
0
def add_asso_subrepo():
    if request.method == 'POST':
        try:
            request_key = request.form.keys()[0]
            get_repo_name = request_key.split('-')[1]
            get_subrepo_name = request.form.get(request_key, '')
            assosubrepo = db_session.query(GitSubRepo).filter_by(
                name=get_subrepo_name).first()
            assorepo = db_session.query(GitRepo).filter_by(
                name=get_repo_name).first()
            if assosubrepo.main_repo_id != assorepo.id:
                try:
                    assosubrepo.main_repo_id = assorepo.id
                    db_session.commit()

                    assorepo_file = BASE_DIR + '/conf/repos/' \
                        + assorepo.name + '.conf'
                    rconf_dict = {}
                    with open(assorepo_file, 'rb') as f:
                        lines = f.readlines()
                        for line in lines:
                            rconf_repo = line.split('=')[0].lstrip('@').strip()
                            rconf_subrepo = line.split('=')[1].split()
                            rconf_dict[rconf_repo] = rconf_subrepo

                    if rconf_dict.has_key(
                            assorepo.name
                    ) and get_subrepo_name not in rconf_dict[assorepo.name]:
                        rconf_dict[assorepo.name].append(
                            str(re.sub('~', '/', get_subrepo_name)))
                        f = open(assorepo_file, 'wb')
                        f.truncate()
                        for _ in rconf_dict:
                            f.write('@' + _ + ' = ' + ' '.join(rconf_dict[_]))
                        f.close()
                except TypeError:
                    db_session.rollback()
                    print '====>  TypeError Need string or buffer, list found.'
                finally:
                    db_session.close()
        except IndexError:
            db_session.rollback()
            print '====>  IndexError Out of range.'
    return redirect(url_for('repo'))
Ejemplo n.º 7
0
def remove_asso_subrepo():
    if request.method == 'POST':
        try:
            request_key = request.form.keys()[0]
            get_repo_name = request_key.split('-')[1]
            get_subrepo_name = request.form.get(request_key, '')
            assorepo = db_session.query(GitRepo).filter_by(
                name=get_repo_name).first()
            assosubrepo = db_session.query(GitSubRepo).filter_by(
                name=get_subrepo_name).first()
            assosubrepo.main_repo_id = None
            db_session.add(assosubrepo)
            db_session.commit()
            print '====>  Sponge (%s) from DB successfully.' % assosubrepo.name

            assorepo_file = BASE_DIR + '/conf/repos/' \
                + assorepo.name + '.conf'
            rconf_dict = {}
            with open(assorepo_file, 'rb') as f:
                lines = f.readlines()
                for line in lines:
                    rconf_repo = line.split('=')[0].lstrip('@').strip()
                    rconf_subrepo = line.split('=')[1].split()
                    rconf_dict[rconf_repo] = rconf_subrepo

            if rconf_dict.has_key(assorepo.name) and '/'.join(
                    get_subrepo_name.split('~')) in rconf_dict[assorepo.name]:
                rconf_dict[assorepo.name].remove(
                    str(re.sub('~', '/', get_subrepo_name)))
                f = open(assorepo_file, 'wb')
                f.truncate()
                for _ in rconf_dict:
                    f.write('@' + _ + ' = ' + ' '.join(rconf_dict[_]))
                f.close()
        except IndexError:
            db_session.rollback()
            print '====>  IndexError Out of range.'
        except ValueError:
            db_session.rollback()
            print '====>  ValueError Not in list.'
        finally:
            db_session.close()
        return redirect(url_for('repo'))
Ejemplo n.º 8
0
def sub_repo_remove():
    if request.method == 'POST':
        try:
            remove_sub_repo_name = request.form.get('subRepoName', '')
        except IndexError:
            remove_sub_repo_name = ''
        if remove_sub_repo_name and '/' in remove_sub_repo_name:
            remove_sub_repo_name = '~'.join(remove_sub_repo_name.split('/'))
        sub_repo_name_file = BASE_DIR + '/conf/subrepos/' \
            + remove_sub_repo_name + '.conf'
        deletesubrepo = db_session.query(GitSubRepo).filter_by(
            name=remove_sub_repo_name).first()
        if deletesubrepo:
            db_session.delete(deletesubrepo)
            db_session.commit()
            db_session.close()
            os.remove(sub_repo_name_file)
        tab_page = 1
        return redirect(url_for('repo', tab_page=tab_page))
Ejemplo n.º 9
0
def group():
    groupdict = {}
    groupfile = item_traversal('conf/groups')
    try:
        groupfile = groupfile['conf/groups']
        if groupfile:
            for _ in groupfile:
                if allowed_file(_, allowed_ext):
                    filepath = BASE_DIR + '/conf/groups/' + _
                    with open(filepath, 'rb') as f:
                        content = f.readlines()
                        for _ in content:
                            group_name = (((_.strip('\n')).split('='))[0]
                                          ).strip().lstrip('@')
                            group_member = ((_.strip('\n')).split('='))[1]
                            groupdict[group_name] = group_member
    except:
        groupfile = []
    if request.method == 'POST':
        new_group_name = request.form.get('addGroupName', '')
        query_group = db_session.query(GitGroup).filter_by(name=new_group_name)
        if not db_session.query(
            query_group.exists()
        ).scalar() and new_group_name.strip() != '' and \
        new_group_name.strip() != 'all':
            try:
                new_group = GitGroup(name=new_group_name)
                db_session.add(new_group)
                db_session.commit()
                group_init(new_group_name)
            except:
                db_session.rollback()
            finally:
                db_session.close()
            return redirect(url_for('group'))
    return render_template(
        'group.html',
        groupdict=groupdict,
    )
Ejemplo n.º 10
0
def repo():
    repodict = {}
    repofile = item_traversal('conf/repos')
    try:
        repofile = repofile['conf/repos']
    except:
        repofile = []
    for _ in repofile:
        if allowed_file(_, allowed_ext):
            filepath = BASE_DIR + '/conf/repos/' + _
            with open(filepath, 'rb') as f:
                content = f.readlines()
                for _ in content:
                    if '=' in _:
                        repo_name = (((
                            _.strip('\n')).split('='))[0]).strip().lstrip('@')
                        repo_member = ((_.strip('\n')).split('='))[1]
                        repodict[repo_name] = repo_member

    subrepodict = {}
    subrepoitem = {}
    subrepofile = item_traversal('conf/subrepos')
    try:
        subrepofile = subrepofile['conf/subrepos']
    except:
        subrepofile = []
    for _ in subrepofile:
        if allowed_file(_, allowed_ext):
            filepath = BASE_DIR + '/conf/subrepos/' + _
            with open(filepath, 'rb') as f:
                first_line = f.readline().strip('\n')
                subrepo_name = first_line[4:].strip()
                content = f.readlines()
                for _ in content:
                    if '=' in _:
                        subrepo_item = (((
                            _.strip('\n')).split('='))[0]).strip().lstrip('@')
                        subrepo_member = ((_.strip('\n')).split('='))[1]
                        subrepoitem[subrepo_item] = subrepo_member
            subrepodict[subrepo_name] = subrepoitem.copy()
    subrepotorepodict = {}
    assignedlist = []
    assignedgroup = {}

    for gitsubrepo in GitSubRepo.query.filter_by(main_repo_id=None).all():
        subrepotorepodict[gitsubrepo.id] = gitsubrepo.name
    for i in repodict.keys():
        queries = GitSubRepo.query.join(GitRepo).filter(
            GitRepo.name == i).all()
        for query in queries:
            assignedlist.append(query.name)
        assignedgroup[i] = assignedlist
        assignedlist = []

    if request.args:
        tab_page = request.args.get('tab_page', '')
        subRepoName = request.args.get('subRepoName', '')
    else:
        tab_page = 0
        subRepoName = ''
    subitemdict = {}
    select_subrepo = ''
    grouptosubrepodict = {}
    for gitgroup in db_session.query(GitGroup).all():
        grouptosubrepodict[gitgroup.id] = gitgroup.name
    grouptosubrepodict[0] = u'all'

    if request.method == 'GET':
        if 'subRepoName' in request.args.keys() and \
            'tab_page' in request.args.keys():
            select_subrepo = str(request.args.get('subRepoName', ''))
            subitemdict = subrepodict[select_subrepo]

    if request.method == 'POST':
        if 'addMainRepoName' in request.form.keys():
            get_repo_name = request.form.get('addMainRepoName', '')
            new_repo_name = ''

            if get_repo_name.strip() != '':
                if get_repo_name[0] == '%' and get_repo_name[-1] == '%':
                    new_repo_name = get_repo_name.strip('%')
                elif not (get_repo_name[0] == '%'
                          and get_repo_name[-1] == '%'):
                    if not get_repo_name.endswith('_repo'):
                        get_repo_name = get_repo_name + '_repo'
                    new_repo_name = get_repo_name
                query_repo = db_session.query(GitRepo).filter_by(
                    name=new_repo_name)
                if not db_session.query(query_repo.exists()).scalar(
                ) and new_repo_name.strip() != '':
                    try:
                        new_repo = GitRepo(name=new_repo_name)
                        db_session.add(new_repo)
                        db_session.commit()
                        repo_init(new_repo_name)
                    except:
                        db_session.rollback()
                    finally:
                        db_session.close()
            return redirect(url_for('repo'))
        elif 'addSubRepoName' in request.form.keys():
            new_sub_repo_name = request.form.get('addSubRepoName', '')
            if '/' in new_sub_repo_name:
                new_sub_repo_name = '~'.join(new_sub_repo_name.split('/'))
            query_repo = db_session.query(GitSubRepo).filter_by(
                name=new_sub_repo_name)
            if not db_session.query(query_repo.exists()).scalar(
            ) and new_sub_repo_name.strip() != '':
                try:
                    new_sub_repo = GitSubRepo(name=new_sub_repo_name)
                    db_session.add(new_sub_repo)
                    db_session.commit()
                    sub_repo_init(new_sub_repo_name)
                except:
                    db_session.rollback()
                finally:
                    db_session.close()
            return redirect(url_for('repo'))
        elif 'subRepoName' in request.form.keys():
            tab_page = 1
            select_subrepo = str(request.form.get('subRepoName', ''))
            subitemdict = subrepodict[select_subrepo]
    return render_template(
        'repo.html',
        #~ repofile=repofile,
        repodict=repodict,
        subrepodict=subrepodict,
        subrepotorepodict=subrepotorepodict,
        assignedgroup=assignedgroup,
        tab_page=tab_page,
        select_subrepo=select_subrepo,
        subitemdict=subitemdict,
        grouptosubrepodict=grouptosubrepodict,
        subRepoName=subRepoName,
    )
Ejemplo n.º 11
0
def user():
    status_dict = {
        0: 'Upload successfully.',
        1: 'Not a pub key file.',
        2: 'The pub key already exists.',
        3: 'Please choose a pub key file.',
        4: 'Error while writing into db.',
        5: 'Please check if `keydir` exists.',
    }
    #~ keydict = item_traversal('keydir')
    userdict = {}
    grouptouserdict = {}
    assignedlist = []
    assignedgroup = {}
    upload_status = {'status': '-1'}

    for gituser in db_session.query(GitUser).all():
        userdict[gituser.id] = gituser.name
    for gitgroup in db_session.query(GitGroup).all():
        grouptouserdict[gitgroup.id] = gitgroup.name
    for i in userdict.keys():
        queries = GitGroup.query.filter(GitGroup.git_user.any(id=i)).all()
        for query in queries:
            assignedlist.append(query.name)
        assignedgroup[i] = assignedlist
        assignedlist = []

    f = open('log.log', 'ab+')
    if request.method == 'POST' and request.files:
        file = request.files.get('keyFile', '')
        if file:
            filename = secure_filename(file.filename)
            filepath = os.path.join(KEY_DIR, filename)
            if not allowed_file(filename, allowed_ext):
                upload_status['status'] = 1
                print >> f, filename + ' is not a pub key.'
                return render_template(
                    'upload.html',
                    upload_status=upload_status,
                )
            if os.path.exists(filepath):
                upload_status['status'] = 2
                return render_template(
                    'upload.html',
                    upload_status=upload_status,
                )
            try:
                new_user = GitUser(name=filename)
                db_session.add(new_user)
                file.save(filepath)
                db_session.commit()
                upload_status['status'] = 0
                print >> f, filename + ' has been saved locally.'
                return render_template(
                    'upload.html',
                    upload_status=upload_status,
                )
            except IOError:
                db_session.rollback()
                upload_status['status'] = 5
                return render_template(
                    'upload.html',
                    upload_status=upload_status,
                )
            except:
                db_session.rollback()
                upload_status['status'] = 4
                return render_template(
                    'upload.html',
                    upload_status=upload_status,
                )
            finally:
                db_session.close()
        else:
            upload_status['status'] = 3
            return render_template(
                'upload.html',
                upload_status=upload_status,
            )
    f.close()
    return render_template(
        'user.html',
        status_dict=status_dict,
        userdict=userdict,
        grouptouserdict=grouptouserdict,
        assignedgroup=assignedgroup,
    )