##// END OF EJS Templates
merge with sorted tags
merge with sorted tags

File last commit:

r1119:143b37b7 beta
r1131:461f5acc merge beta
Show More
repo.py
354 lines | 12.9 KiB | text/x-python | PythonLexer
when new repo is created make user follow it automatically,...
r786 # -*- coding: utf-8 -*-
"""
docs updates
r811 rhodecode.model.repo
~~~~~~~~~~~~~~~~~~~~
when new repo is created make user follow it automatically,...
r786
Repository model for rhodecode
:created_on: Jun 5, 2010
:author: marcink
fixed copyright year to 2011
r902 :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com>
when new repo is created make user follow it automatically,...
r786 :license: GPLv3, see COPYING for more details.
"""
Code refactoring,models renames...
r629 # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2
# of the License or (at your opinion) any later version of the license.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
when new repo is created make user follow it automatically,...
r786 import os
import shutil
import logging
import traceback
Code refactoring,models renames...
r629 from datetime import datetime
when new repo is created make user follow it automatically,...
r786
another major codes rewrite:...
r1045 from sqlalchemy.orm import joinedload, make_transient
Major refactoring, removed when possible calls to app globals....
r1036
from vcs.utils.lazy import LazyProperty
from vcs.backends import get_backend
when new repo is created make user follow it automatically,...
r786 from rhodecode.model import BaseModel
from rhodecode.model.caching_query import FromCache
added cache reset, stats reset, and delete into repository settings in admin....
r708 from rhodecode.model.db import Repository, RepoToPerm, User, Permission, \
Major refactoring, removed when possible calls to app globals....
r1036 Statistics, UsersGroup, UsersGroupToPerm, RhodeCodeUi
Code refactoring,models renames...
r629 from rhodecode.model.user import UserModel
#56 added assignments of users groups into repository
r1014 from rhodecode.model.users_group import UsersGroupMember, UsersGroupModel
when new repo is created make user follow it automatically,...
r786
Code refactoring,models renames...
r629 log = logging.getLogger(__name__)
fixed Example celery config to ampq,...
r752 class RepoModel(BaseModel):
Code refactoring,models renames...
r629
Major refactoring, removed when possible calls to app globals....
r1036 @LazyProperty
def repos_path(self):
"""Get's the repositories root path from database
"""
Fixed problems with repository creation
r1017
Major refactoring, removed when possible calls to app globals....
r1036 q = self.sa.query(RhodeCodeUi).filter(RhodeCodeUi.ui_key == '/').one()
return q.ui_value
#56 hacking on forms, and model for users groups
r1013
Code refactoring,models renames...
r629 def get(self, repo_id, cache=False):
repo = self.sa.query(Repository)\
added action loggers to following repositories,...
r735 .filter(Repository.repo_id == repo_id)
Code refactoring,models renames...
r629
if cache:
repo = repo.options(FromCache("sql_cache_short",
added action loggers to following repositories,...
r735 "get_repo_%s" % repo_id))
Code refactoring,models renames...
r629 return repo.scalar()
added action loggers to following repositories,...
r735
def get_by_repo_name(self, repo_name, cache=False):
repo = self.sa.query(Repository)\
.filter(Repository.repo_name == repo_name)
if cache:
repo = repo.options(FromCache("sql_cache_short",
"get_repo_%s" % repo_name))
fixed Example celery config to ampq,...
r752 return repo.scalar()
added action loggers to following repositories,...
r735
Major refactoring, removed when possible calls to app globals....
r1036
def get_full(self, repo_name, cache=False, invalidate=False):
repo = self.sa.query(Repository)\
.options(joinedload(Repository.fork))\
.options(joinedload(Repository.user))\
.filter(Repository.repo_name == repo_name)\
if cache:
repo = repo.options(FromCache("sql_cache_long",
"get_repo_full_%s" % repo_name))
another major code rafactor, reimplemented (almost from scratch)...
r1038 if invalidate and cache:
Major refactoring, removed when possible calls to app globals....
r1036 repo.invalidate()
extended admin rescan to show what repositories was added and what removed...
r1039 ret = repo.scalar()
another major codes rewrite:...
r1045
#make transient for sake of errors
make_transient(ret)
for k in ['fork', 'user']:
attr = getattr(ret, k, False)
if attr:
make_transient(attr)
extended admin rescan to show what repositories was added and what removed...
r1039 return ret
Major refactoring, removed when possible calls to app globals....
r1036
Code refactoring,models renames...
r629 def get_users_js(self):
users = self.sa.query(User).filter(User.active == True).all()
u_tmpl = '''{id:%s, fname:"%s", lname:"%s", nname:"%s"},'''
#56 hacking on forms, and model for users groups
r1013 users_array = '[%s]' % '\n'.join([u_tmpl % (u.user_id, u.name,
Code refactoring,models renames...
r629 u.lastname, u.username)
for u in users])
return users_array
#56 hacking on forms, and model for users groups
r1013 def get_users_groups_js(self):
users_groups = self.sa.query(UsersGroup)\
.filter(UsersGroup.users_group_active == True).all()
g_tmpl = '''{id:%s, grname:"%s",grmembers:"%s"},'''
users_groups_array = '[%s]' % '\n'.join([g_tmpl % \
(gr.users_group_id, gr.users_group_name,
len(gr.members))
for gr in users_groups])
return users_groups_array
Code refactoring,models renames...
r629 def update(self, repo_name, form_data):
try:
small code fixes to repo model
r822 cur_repo = self.get_by_repo_name(repo_name, cache=False)
user_model = UserModel(self.sa)
#56 added assignments of users groups into repository
r1014 users_group_model = UsersGroupModel(self.sa)
Code refactoring,models renames...
r629
#update permissions
#56 added assignments of users groups into repository
r1014 for member, perm, member_type in form_data['perms_updates']:
if member_type == 'user':
r2p = self.sa.query(RepoToPerm)\
.filter(RepoToPerm.user == user_model.get_by_username(member))\
.filter(RepoToPerm.repository == cur_repo)\
.one()
Code refactoring,models renames...
r629
#56 added assignments of users groups into repository
r1014 r2p.permission = self.sa.query(Permission)\
.filter(Permission.permission_name == perm)\
.scalar()
self.sa.add(r2p)
else:
g2p = self.sa.query(UsersGroupToPerm)\
.filter(UsersGroupToPerm.users_group == users_group_model.get_by_groupname(member))\
.filter(UsersGroupToPerm.repository == cur_repo)\
.one()
g2p.permission = self.sa.query(Permission)\
.filter(Permission.permission_name == perm)\
.scalar()
self.sa.add(g2p)
Code refactoring,models renames...
r629
#set new permissions
#56 added assignments of users groups into repository
r1014 for member, perm, member_type in form_data['perms_new']:
if member_type == 'user':
r2p = RepoToPerm()
r2p.repository = cur_repo
r2p.user = user_model.get_by_username(member)
Code refactoring,models renames...
r629
#56 added assignments of users groups into repository
r1014 r2p.permission = self.sa.query(Permission)\
.filter(Permission.permission_name == perm)\
.scalar()
self.sa.add(r2p)
else:
g2p = UsersGroupToPerm()
g2p.repository = cur_repo
g2p.users_group = users_group_model.get_by_groupname(member)
g2p.permission = self.sa.query(Permission)\
.filter(Permission.permission_name == perm)\
.scalar()
self.sa.add(g2p)
Code refactoring,models renames...
r629
#update current repo
for k, v in form_data.items():
if k == 'user':
small code fixes to repo model
r822 cur_repo.user = user_model.get(v)
Code refactoring,models renames...
r629 else:
setattr(cur_repo, k, v)
self.sa.add(cur_repo)
if repo_name != form_data['repo_name']:
#rename our data
self.__rename_repo(repo_name, form_data['repo_name'])
self.sa.commit()
except:
log.error(traceback.format_exc())
self.sa.rollback()
raise
def create(self, form_data, cur_user, just_db=False, fork=False):
try:
if fork:
some css updates....
r651 #force str since hg doesn't go with unicode
Code refactoring,models renames...
r629 repo_name = str(form_data['fork_name'])
org_name = str(form_data['repo_name'])
else:
org_name = repo_name = str(form_data['repo_name'])
new_repo = Repository()
Enable statistics are disabled by default now
r1075 new_repo.enable_statistics = False
Code refactoring,models renames...
r629 for k, v in form_data.items():
if k == 'repo_name':
v = repo_name
setattr(new_repo, k, v)
if fork:
parent_repo = self.sa.query(Repository)\
.filter(Repository.repo_name == org_name).scalar()
new_repo.fork = parent_repo
new_repo.user_id = cur_user.user_id
self.sa.add(new_repo)
#create default permission
repo_to_perm = RepoToPerm()
default = 'repository.read'
fixes #126, all credits go to cejones <chad.jones@milwaukeetool.com>
r1119 for p in UserModel(self.sa).get_by_username('default',
cache=False).user_perms:
Code refactoring,models renames...
r629 if p.permission.permission_name.startswith('repository.'):
default = p.permission.permission_name
break
default_perm = 'repository.none' if form_data['private'] else default
repo_to_perm.permission_id = self.sa.query(Permission)\
.filter(Permission.permission_name == default_perm)\
.one().permission_id
fixed issue with db transaction when filesystem creation of repository failed...
r1028 repo_to_perm.repository = new_repo
fixed Example celery config to ampq,...
r752 repo_to_perm.user_id = UserModel(self.sa)\
.get_by_username('default', cache=False).user_id
Code refactoring,models renames...
r629
self.sa.add(repo_to_perm)
fixed issue with db transaction when filesystem creation of repository failed...
r1028
if not just_db:
#109, added optional clone uri when creating repo....
r1112 self.__create_repo(repo_name, form_data['repo_type'],
form_data['clone_uri'])
fixed issue with db transaction when filesystem creation of repository failed...
r1028
Code refactoring,models renames...
r629 self.sa.commit()
when new repo is created make user follow it automatically,...
r786
#now automatically start following this repository as owner
from rhodecode.model.scm import ScmModel
ScmModel(self.sa).toggle_following_repo(new_repo.repo_id,
cur_user.user_id)
Code refactoring,models renames...
r629 except:
log.error(traceback.format_exc())
self.sa.rollback()
raise
def create_fork(self, form_data, cur_user):
from rhodecode.lib.celerylib import tasks, run_task
run_task(tasks.create_repo_fork, form_data, cur_user)
def delete(self, repo):
try:
self.sa.delete(repo)
fixed deletion of repository on filesystem, works based on scm type for git and hg....
r668 self.__delete_repo(repo)
Code refactoring,models renames...
r629 self.sa.commit()
except:
log.error(traceback.format_exc())
self.sa.rollback()
raise
def delete_perm_user(self, form_data, repo_name):
try:
self.sa.query(RepoToPerm)\
added action loggers to following repositories,...
r735 .filter(RepoToPerm.repository \
== self.get_by_repo_name(repo_name))\
Code refactoring,models renames...
r629 .filter(RepoToPerm.user_id == form_data['user_id']).delete()
self.sa.commit()
except:
log.error(traceback.format_exc())
self.sa.rollback()
raise
#56 added ajax removal of users groups,...
r1015 def delete_perm_users_group(self, form_data, repo_name):
try:
self.sa.query(UsersGroupToPerm)\
.filter(UsersGroupToPerm.repository \
== self.get_by_repo_name(repo_name))\
.filter(UsersGroupToPerm.users_group_id \
== form_data['users_group_id']).delete()
self.sa.commit()
except:
log.error(traceback.format_exc())
self.sa.rollback()
raise
added cache reset, stats reset, and delete into repository settings in admin....
r708 def delete_stats(self, repo_name):
try:
self.sa.query(Statistics)\
added action loggers to following repositories,...
r735 .filter(Statistics.repository == \
self.get_by_repo_name(repo_name)).delete()
added cache reset, stats reset, and delete into repository settings in admin....
r708 self.sa.commit()
except:
log.error(traceback.format_exc())
self.sa.rollback()
raise
#109, added optional clone uri when creating repo....
r1112 def __create_repo(self, repo_name, alias, clone_uri=False):
docs update for repo model
r787 """
makes repository on filesystem
fixes #126, all credits go to cejones <chad.jones@milwaukeetool.com>
r1119
docs update for repo model
r787 :param repo_name:
:param alias:
"""
Code refactoring,models renames...
r629 from rhodecode.lib.utils import check_repo
Major refactoring, removed when possible calls to app globals....
r1036 repo_path = os.path.join(self.repos_path, repo_name)
if check_repo(repo_name, self.repos_path):
#109, added optional clone uri when creating repo....
r1112 log.info('creating repo %s in %s @ %s', repo_name, repo_path,
clone_uri)
extended repo creation by repo type. fixed fork creation to maintain repo type.
r659 backend = get_backend(alias)
#109, added optional clone uri when creating repo....
r1112 backend(repo_path, create=True, src_url=clone_uri)
Code refactoring,models renames...
r629
def __rename_repo(self, old, new):
docs update for repo model
r787 """
renames repository on filesystem
fixes #126, all credits go to cejones <chad.jones@milwaukeetool.com>
r1119
docs update for repo model
r787 :param old: old name
:param new: new name
"""
Code refactoring,models renames...
r629 log.info('renaming repo from %s to %s', old, new)
Major refactoring, removed when possible calls to app globals....
r1036 old_path = os.path.join(self.repos_path, old)
new_path = os.path.join(self.repos_path, new)
Code refactoring,models renames...
r629 if os.path.isdir(new_path):
raise Exception('Was trying to rename to already existing dir %s',
new_path)
shutil.move(old_path, new_path)
fixed deletion of repository on filesystem, works based on scm type for git and hg....
r668 def __delete_repo(self, repo):
docs update for repo model
r787 """
removes repo from filesystem, the removal is acctually made by
added rm__ prefix into dir, and rename internat .hg/.git dirs so this
repository is no longer valid for rhodecode, can be undeleted later on
by reverting the renames on this repository
fixes #126, all credits go to cejones <chad.jones@milwaukeetool.com>
r1119
docs update for repo model
r787 :param repo: repo object
"""
Major refactoring, removed when possible calls to app globals....
r1036 rm_path = os.path.join(self.repos_path, repo.repo_name)
Code refactoring,models renames...
r629 log.info("Removing %s", rm_path)
fixed deletion of repository on filesystem, works based on scm type for git and hg....
r668 #disable hg/git
alias = repo.repo_type
shutil.move(os.path.join(rm_path, '.%s' % alias),
os.path.join(rm_path, 'rm__.%s' % alias))
Code refactoring,models renames...
r629 #disable repo
Major refactoring, removed when possible calls to app globals....
r1036 shutil.move(rm_path, os.path.join(self.repos_path, 'rm__%s__%s' \
fixes #126, all credits go to cejones <chad.jones@milwaukeetool.com>
r1119 % (datetime.today()\
.strftime('%Y%m%d_%H%M%S_%f'),
repo.repo_name)))