Mercurial > kallithea
comparison pylons_app/model/repo_model.py @ 265:0e5455fda8fd
Implemented basic repository managment. Implemented repo2db mappings, model, helpers updates and code cleanups
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Mon, 07 Jun 2010 00:18:33 +0200 |
parents | |
children | 29370bb76fa6 |
comparison
equal
deleted
inserted
replaced
264:0d68a749db33 | 265:0e5455fda8fd |
---|---|
1 #!/usr/bin/env python | |
2 # encoding: utf-8 | |
3 # model for handling repositories actions | |
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> | |
5 # This program is free software; you can redistribute it and/or | |
6 # modify it under the terms of the GNU General Public License | |
7 # as published by the Free Software Foundation; version 2 | |
8 # of the License or (at your opinion) any later version of the license. | |
9 # | |
10 # This program is distributed in the hope that it will be useful, | |
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 # GNU General Public License for more details. | |
14 # | |
15 # You should have received a copy of the GNU General Public License | |
16 # along with this program; if not, write to the Free Software | |
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
18 # MA 02110-1301, USA. | |
19 | |
20 """ | |
21 Created on Jun 5, 2010 | |
22 model for handling repositories actions | |
23 @author: marcink | |
24 """ | |
25 from pylons_app.model.meta import Session | |
26 from pylons_app.model.db import Repository | |
27 import shutil | |
28 import os | |
29 from datetime import datetime | |
30 from pylons_app.lib.utils import check_repo | |
31 from pylons import app_globals as g | |
32 import logging | |
33 log = logging.getLogger(__name__) | |
34 | |
35 class RepoModel(object): | |
36 | |
37 def __init__(self): | |
38 self.sa = Session() | |
39 | |
40 def get(self, id): | |
41 return self.sa.query(Repository).get(id) | |
42 | |
43 | |
44 def update(self, id, form_data): | |
45 try: | |
46 if id != form_data['repo_name']: | |
47 self.__rename_repo(id, form_data['repo_name']) | |
48 cur_repo = self.sa.query(Repository).get(id) | |
49 for k, v in form_data.items(): | |
50 if k == 'user': | |
51 cur_repo.user_id = v | |
52 else: | |
53 setattr(cur_repo, k, v) | |
54 | |
55 self.sa.add(cur_repo) | |
56 self.sa.commit() | |
57 except Exception as e: | |
58 log.error(e) | |
59 self.sa.rollback() | |
60 raise | |
61 | |
62 def create(self, form_data, cur_user): | |
63 try: | |
64 new_repo = Repository() | |
65 for k, v in form_data.items(): | |
66 setattr(new_repo, k, v) | |
67 | |
68 new_repo.user_id = cur_user.user_id | |
69 self.sa.add(new_repo) | |
70 self.sa.commit() | |
71 self.__create_repo(form_data['repo_name']) | |
72 except Exception as e: | |
73 log.error(e) | |
74 self.sa.rollback() | |
75 raise | |
76 | |
77 def delete(self, repo): | |
78 try: | |
79 self.sa.delete(repo) | |
80 self.sa.commit() | |
81 self.__delete_repo(repo.repo_name) | |
82 except Exception as e: | |
83 log.error(e) | |
84 self.sa.rollback() | |
85 raise | |
86 | |
87 def __create_repo(self, repo_name): | |
88 repo_path = os.path.join(g.base_path, repo_name) | |
89 if check_repo(repo_name, g.base_path): | |
90 log.info('creating repo %s in %s', repo_name, repo_path) | |
91 from vcs.backends.hg import MercurialRepository | |
92 MercurialRepository(repo_path, create=True) | |
93 | |
94 def __rename_repo(self, old, new): | |
95 log.info('renaming repoo from %s to %s', old, new) | |
96 old_path = os.path.join(g.base_path, old) | |
97 new_path = os.path.join(g.base_path, new) | |
98 shutil.move(old_path, new_path) | |
99 | |
100 def __delete_repo(self, name): | |
101 rm_path = os.path.join(g.base_path, name) | |
102 log.info("Removing %s", rm_path) | |
103 #disable hg | |
104 shutil.move(os.path.join(rm_path, '.hg'), os.path.join(rm_path, 'rm__.hg')) | |
105 #disable repo | |
106 shutil.move(rm_path, os.path.join(g.base_path, 'rm__%s-%s' % (datetime.today(), id))) |