annotate rhodecode/model/gist.py @ 4016:cce2d984b001

User create/delete hooks for rcextensions. When a user is created or deleted, the CREATE_USER_HOOK or DELETE_USER_HOOK are called as part of the log_create_user and log_delete_user functions respectively. This is similar to the existing log_create_repository and log_delete_repository functions that already exist as part of the rcextensions module.
author Jonathan Sternberg <jonathansternberg@gmail.com>
date Mon, 17 Jun 2013 18:09:50 -0400
parents 2576a20d94ca
children ffd45b185016
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
2 """
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
3 rhodecode.model.gist
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
4 ~~~~~~~~~~~~~~~~~~~~
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
5
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
6 gist model for RhodeCode
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
7
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
8 :created_on: May 9, 2013
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
9 :author: marcink
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
10 :copyright: (C) 2011-2013 Marcin Kuzminski <marcin@python-works.com>
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
11 :license: GPLv3, see COPYING for more details.
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
12 """
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
13 # This program is free software: you can redistribute it and/or modify
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
14 # it under the terms of the GNU General Public License as published by
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
15 # the Free Software Foundation, either version 3 of the License, or
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
16 # (at your option) any later version.
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
17 #
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
18 # This program is distributed in the hope that it will be useful,
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
21 # GNU General Public License for more details.
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
22 #
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
23 # You should have received a copy of the GNU General Public License
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
3841
979edf6a2990 Prefix gist storage with rc, and store some metadata info into
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
25 from __future__ import with_statement
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
26 import os
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
27 import time
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
28 import logging
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
29 import traceback
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
30 import shutil
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
31
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
32 from pylons.i18n.translation import _
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
33 from rhodecode.lib.utils2 import safe_unicode, unique_id, safe_int, \
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
34 time_to_datetime, safe_str, AttributeDict
3841
979edf6a2990 Prefix gist storage with rc, and store some metadata info into
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
35 from rhodecode.lib.compat import json
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
36 from rhodecode.lib import helpers as h
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
37 from rhodecode.model import BaseModel
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
38 from rhodecode.model.db import Gist
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
39 from rhodecode.model.repo import RepoModel
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
40 from rhodecode.model.scm import ScmModel
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
41
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
42 log = logging.getLogger(__name__)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
43
3841
979edf6a2990 Prefix gist storage with rc, and store some metadata info into
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
44 GIST_STORE_LOC = '.rc_gist_store'
979edf6a2990 Prefix gist storage with rc, and store some metadata info into
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
45 GIST_METADATA_FILE = '.rc_gist_metadata'
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
46
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
47
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
48 class GistModel(BaseModel):
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
49
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
50 def _get_gist(self, gist):
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
51 """
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
52 Helper method to get gist by ID, or gist_access_id as a fallback
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
53
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
54 :param gist: GistID, gist_access_id, or Gist instance
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
55 """
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
56 return self._get_instance(Gist, gist,
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
57 callback=Gist.get_by_access_id)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
58
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
59 def __delete_gist(self, gist):
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
60 """
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
61 removes gist from filesystem
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
62
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
63 :param gist: gist object
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
64 """
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
65 root_path = RepoModel().repos_path
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
66 rm_path = os.path.join(root_path, GIST_STORE_LOC, gist.gist_access_id)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
67 log.info("Removing %s" % (rm_path))
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
68 shutil.rmtree(rm_path)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
69
3842
54bc7a89f090 gists: add some API related code improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 3841
diff changeset
70 def get_gist(self, gist):
54bc7a89f090 gists: add some API related code improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 3841
diff changeset
71 return self._get_gist(gist)
54bc7a89f090 gists: add some API related code improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 3841
diff changeset
72
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
73 def get_gist_files(self, gist_access_id):
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
74 """
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
75 Get files for given gist
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
76
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
77 :param gist_access_id:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
78 """
3842
54bc7a89f090 gists: add some API related code improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 3841
diff changeset
79 repo = Gist.get_by_access_id(gist_access_id)
54bc7a89f090 gists: add some API related code improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 3841
diff changeset
80 cs = repo.scm_instance.get_changeset()
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
81 return (
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
82 cs, [n for n in cs.get_node('/')]
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
83 )
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
84
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
85 def create(self, description, owner, gist_mapping,
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
86 gist_type=Gist.GIST_PUBLIC, lifetime=-1):
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
87 """
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
88
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
89 :param description: description of the gist
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
90 :param owner: user who created this gist
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
91 :param gist_mapping: mapping {filename:{'content':content},...}
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
92 :param gist_type: type of gist private/public
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
93 :param lifetime: in minutes, -1 == forever
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
94 """
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
95 gist_id = safe_unicode(unique_id(20))
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
96 lifetime = safe_int(lifetime, -1)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
97 gist_expires = time.time() + (lifetime * 60) if lifetime != -1 else -1
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
98 log.debug('set GIST expiration date to: %s'
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
99 % (time_to_datetime(gist_expires)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
100 if gist_expires != -1 else 'forever'))
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
101 #create the Database version
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
102 gist = Gist()
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
103 gist.gist_description = description
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
104 gist.gist_access_id = gist_id
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
105 gist.gist_owner = owner.user_id
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
106 gist.gist_expires = gist_expires
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
107 gist.gist_type = safe_unicode(gist_type)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
108 self.sa.add(gist)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
109 self.sa.flush()
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
110 if gist_type == Gist.GIST_PUBLIC:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
111 # use DB ID for easy to use GIST ID
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
112 gist_id = safe_unicode(gist.gist_id)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
113 gist.gist_access_id = gist_id
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
114 self.sa.add(gist)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
115
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
116 gist_repo_path = os.path.join(GIST_STORE_LOC, gist_id)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
117 log.debug('Creating new %s GIST repo in %s' % (gist_type, gist_repo_path))
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
118 repo = RepoModel()._create_repo(repo_name=gist_repo_path, alias='hg',
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
119 parent=None)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
120
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
121 processed_mapping = {}
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
122 for filename in gist_mapping:
3846
2576a20d94ca Gist: don't allow files inside directories when creating gists
Marcin Kuzminski <marcin@python-works.com>
parents: 3842
diff changeset
123 if filename != os.path.basename(filename):
2576a20d94ca Gist: don't allow files inside directories when creating gists
Marcin Kuzminski <marcin@python-works.com>
parents: 3842
diff changeset
124 raise Exception('Filename cannot be inside a directory')
2576a20d94ca Gist: don't allow files inside directories when creating gists
Marcin Kuzminski <marcin@python-works.com>
parents: 3842
diff changeset
125
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
126 content = gist_mapping[filename]['content']
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
127 #TODO: expand support for setting explicit lexers
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
128 # if lexer is None:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
129 # try:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
130 # lexer = pygments.lexers.guess_lexer_for_filename(filename,content)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
131 # except pygments.util.ClassNotFound:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
132 # lexer = 'text'
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
133 processed_mapping[filename] = {'content': content}
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
134
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
135 # now create single multifile commit
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
136 message = 'added file'
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
137 message += 's: ' if len(processed_mapping) > 1 else ': '
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
138 message += ', '.join([x for x in processed_mapping])
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
139
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
140 #fake RhodeCode Repository object
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
141 fake_repo = AttributeDict(dict(
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
142 repo_name=gist_repo_path,
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
143 scm_instance_no_cache=lambda: repo,
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
144 ))
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
145 ScmModel().create_nodes(
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
146 user=owner.user_id, repo=fake_repo,
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
147 message=message,
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
148 nodes=processed_mapping,
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
149 trigger_push_hook=False
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
150 )
3841
979edf6a2990 Prefix gist storage with rc, and store some metadata info into
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
151 # store metadata inside the gist, this can be later used for imports
979edf6a2990 Prefix gist storage with rc, and store some metadata info into
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
152 # or gist identification
979edf6a2990 Prefix gist storage with rc, and store some metadata info into
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
153 metadata = {
979edf6a2990 Prefix gist storage with rc, and store some metadata info into
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
154 'gist_db_id': gist.gist_id,
979edf6a2990 Prefix gist storage with rc, and store some metadata info into
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
155 'gist_access_id': gist.gist_access_id,
979edf6a2990 Prefix gist storage with rc, and store some metadata info into
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
156 'gist_owner_id': owner.user_id,
979edf6a2990 Prefix gist storage with rc, and store some metadata info into
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
157 'gist_type': gist.gist_type,
979edf6a2990 Prefix gist storage with rc, and store some metadata info into
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
158 'gist_exipres': gist.gist_expires
979edf6a2990 Prefix gist storage with rc, and store some metadata info into
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
159 }
979edf6a2990 Prefix gist storage with rc, and store some metadata info into
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
160 with open(os.path.join(repo.path, '.hg', GIST_METADATA_FILE), 'wb') as f:
979edf6a2990 Prefix gist storage with rc, and store some metadata info into
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
161 f.write(json.dumps(metadata))
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
162 return gist
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
163
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
164 def delete(self, gist, fs_remove=True):
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
165 gist = self._get_gist(gist)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
166
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
167 try:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
168 self.sa.delete(gist)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
169 if fs_remove:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
170 self.__delete_gist(gist)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
171 else:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
172 log.debug('skipping removal from filesystem')
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
173
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
174 except Exception:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
175 log.error(traceback.format_exc())
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
176 raise