repo.py
262 lines
| 9.2 KiB
| text/x-python
|
PythonLexer
r786 | # -*- coding: utf-8 -*- | |||
""" | ||||
r811 | rhodecode.model.repo | |||
~~~~~~~~~~~~~~~~~~~~ | ||||
r786 | ||||
Repository model for rhodecode | ||||
:created_on: Jun 5, 2010 | ||||
:author: marcink | ||||
:copyright: (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> | ||||
:license: GPLv3, see COPYING for more details. | ||||
""" | ||||
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. | ||||
r786 | import os | |||
import shutil | ||||
import logging | ||||
import traceback | ||||
r629 | from datetime import datetime | |||
r786 | ||||
r629 | from pylons import app_globals as g | |||
r786 | ||||
from rhodecode.model import BaseModel | ||||
from rhodecode.model.caching_query import FromCache | ||||
r708 | from rhodecode.model.db import Repository, RepoToPerm, User, Permission, \ | |||
Statistics | ||||
r629 | from rhodecode.model.user import UserModel | |||
r786 | ||||
from vcs.backends import get_backend | ||||
r629 | log = logging.getLogger(__name__) | |||
r752 | class RepoModel(BaseModel): | |||
r629 | ||||
def get(self, repo_id, cache=False): | ||||
repo = self.sa.query(Repository)\ | ||||
r735 | .filter(Repository.repo_id == repo_id) | |||
r629 | ||||
if cache: | ||||
repo = repo.options(FromCache("sql_cache_short", | ||||
r735 | "get_repo_%s" % repo_id)) | |||
r629 | return repo.scalar() | |||
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)) | ||||
r752 | return repo.scalar() | |||
r735 | ||||
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"},''' | ||||
users_array = '[%s];' % '\n'.join([u_tmpl % (u.user_id, u.name, | ||||
u.lastname, u.username) | ||||
for u in users]) | ||||
return users_array | ||||
def update(self, repo_name, form_data): | ||||
try: | ||||
r822 | cur_repo = self.get_by_repo_name(repo_name, cache=False) | |||
user_model = UserModel(self.sa) | ||||
r629 | ||||
#update permissions | ||||
for username, perm in form_data['perms_updates']: | ||||
r2p = self.sa.query(RepoToPerm)\ | ||||
r822 | .filter(RepoToPerm.user == user_model.get_by_username(username))\ | |||
.filter(RepoToPerm.repository == cur_repo)\ | ||||
r629 | .one() | |||
r822 | r2p.permission = self.sa.query(Permission)\ | |||
.filter(Permission.permission_name == perm)\ | ||||
.scalar() | ||||
r629 | self.sa.add(r2p) | |||
#set new permissions | ||||
for username, perm in form_data['perms_new']: | ||||
r2p = RepoToPerm() | ||||
r822 | r2p.repository = cur_repo | |||
r2p.user = user_model.get_by_username(username, cache=False) | ||||
r629 | ||||
r822 | r2p.permission = self.sa.query(Permission)\ | |||
.filter(Permission.permission_name == perm)\ | ||||
.scalar() | ||||
r629 | self.sa.add(r2p) | |||
#update current repo | ||||
for k, v in form_data.items(): | ||||
if k == 'user': | ||||
r822 | cur_repo.user = user_model.get(v) | |||
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: | ||||
r651 | #force str since hg doesn't go with unicode | |||
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() | ||||
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' | ||||
r752 | for p in UserModel(self.sa).get_by_username('default', cache=False).user_perms: | |||
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 | ||||
repo_to_perm.repository_id = new_repo.repo_id | ||||
r752 | repo_to_perm.user_id = UserModel(self.sa)\ | |||
.get_by_username('default', cache=False).user_id | ||||
r629 | ||||
self.sa.add(repo_to_perm) | ||||
self.sa.commit() | ||||
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) | ||||
r629 | if not just_db: | |||
r659 | self.__create_repo(repo_name, form_data['repo_type']) | |||
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) | ||||
r668 | self.__delete_repo(repo) | |||
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)\ | ||||
r735 | .filter(RepoToPerm.repository \ | |||
== self.get_by_repo_name(repo_name))\ | ||||
r629 | .filter(RepoToPerm.user_id == form_data['user_id']).delete() | |||
self.sa.commit() | ||||
except: | ||||
log.error(traceback.format_exc()) | ||||
self.sa.rollback() | ||||
raise | ||||
r708 | def delete_stats(self, repo_name): | |||
try: | ||||
self.sa.query(Statistics)\ | ||||
r735 | .filter(Statistics.repository == \ | |||
self.get_by_repo_name(repo_name)).delete() | ||||
r708 | self.sa.commit() | |||
except: | ||||
log.error(traceback.format_exc()) | ||||
self.sa.rollback() | ||||
raise | ||||
r659 | def __create_repo(self, repo_name, alias): | |||
r787 | """ | |||
makes repository on filesystem | ||||
:param repo_name: | ||||
:param alias: | ||||
""" | ||||
r629 | from rhodecode.lib.utils import check_repo | |||
repo_path = os.path.join(g.base_path, repo_name) | ||||
if check_repo(repo_name, g.base_path): | ||||
log.info('creating repo %s in %s', repo_name, repo_path) | ||||
r659 | backend = get_backend(alias) | |||
backend(repo_path, create=True) | ||||
r629 | ||||
def __rename_repo(self, old, new): | ||||
r787 | """ | |||
renames repository on filesystem | ||||
:param old: old name | ||||
:param new: new name | ||||
""" | ||||
r629 | log.info('renaming repo from %s to %s', old, new) | |||
old_path = os.path.join(g.base_path, old) | ||||
new_path = os.path.join(g.base_path, new) | ||||
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) | ||||
r668 | def __delete_repo(self, repo): | |||
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 | ||||
:param repo: repo object | ||||
""" | ||||
r668 | rm_path = os.path.join(g.base_path, repo.repo_name) | |||
r629 | log.info("Removing %s", rm_path) | |||
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)) | ||||
r629 | #disable repo | |||
shutil.move(rm_path, os.path.join(g.base_path, 'rm__%s__%s' \ | ||||
r668 | % (datetime.today(), repo.repo_name))) | |||