annotate rhodecode/model/gist.py @ 4147:1c8f818787b3 rhodecode-2.2.5-gpl

old style: show the full link box on summary page - no overlap or truncation
author Mads Kiilerich <madski@unity3d.com>
date Wed, 02 Jul 2014 19:03:23 -0400
parents ffd45b185016
children 7e5f8c12a3fc
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 # 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
3 # 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
4 # 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
5 # (at your option) any later version.
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
6 #
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
7 # 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
8 # 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
9 # 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
10 # GNU General Public License for more details.
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
11 #
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
12 # 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
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
14 """
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
15 rhodecode.model.gist
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
16 ~~~~~~~~~~~~~~~~~~~~
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
17
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
18 gist model for RhodeCode
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
19
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
20 :created_on: May 9, 2013
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
21 :author: marcink
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
22 :copyright: (c) 2013 RhodeCode GmbH.
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
23 :license: GPLv3, see LICENSE for more details.
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
24 """
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
25
3841
979edf6a2990 Prefix gist storage with rc, and store some metadata info into
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
26 from __future__ import with_statement
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
27 import os
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
28 import time
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
29 import logging
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
30 import traceback
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
31 import shutil
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
32
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
33 from pylons.i18n.translation import _
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
34 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
35 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
36 from rhodecode.lib.compat import json
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
37 from rhodecode.lib import helpers as h
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
38 from rhodecode.model import BaseModel
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
39 from rhodecode.model.db import Gist
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
40 from rhodecode.model.repo import RepoModel
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
41 from rhodecode.model.scm import ScmModel
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
42
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
43 log = logging.getLogger(__name__)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
44
3841
979edf6a2990 Prefix gist storage with rc, and store some metadata info into
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
45 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
46 GIST_METADATA_FILE = '.rc_gist_metadata'
3840
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
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
49 class GistModel(BaseModel):
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
50 cls = Gist
3840
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 def _get_gist(self, gist):
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 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
55
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
56 :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
57 """
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
58 return self._get_instance(Gist, gist, callback=Gist.get_by_access_id)
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
59
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
60 def __delete_gist(self, gist):
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
61 """
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
62 removes gist from filesystem
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
63
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
64 :param gist: gist object
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
65 """
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
66 root_path = RepoModel().repos_path
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
67 rm_path = os.path.join(root_path, GIST_STORE_LOC, gist.gist_access_id)
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
68 log.info("Removing %s" % (rm_path,))
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
69 shutil.rmtree(rm_path)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
70
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
71 def _store_metadata(self, repo, gist_id, gist_access_id, user_id, gist_type,
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
72 gist_expires):
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
73 """
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
74 store metadata inside the gist, this can be later used for imports
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
75 or gist identification
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
76 """
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
77 metadata = {
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
78 'metadata_version': '1',
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
79 'gist_db_id': gist_id,
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
80 'gist_access_id': gist_access_id,
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
81 'gist_owner_id': user_id,
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
82 'gist_type': gist_type,
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
83 'gist_expires': gist_expires,
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
84 'gist_updated': time.time(),
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
85 }
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
86 with open(os.path.join(repo.path, '.hg', GIST_METADATA_FILE), 'wb') as f:
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
87 f.write(json.dumps(metadata))
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
88
3842
54bc7a89f090 gists: add some API related code improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 3841
diff changeset
89 def get_gist(self, gist):
54bc7a89f090 gists: add some API related code improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 3841
diff changeset
90 return self._get_gist(gist)
54bc7a89f090 gists: add some API related code improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 3841
diff changeset
91
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
92 def get_gist_files(self, gist_access_id, revision=None):
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
93 """
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
94 Get files for given gist
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
95
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
96 :param gist_access_id:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
97 """
3842
54bc7a89f090 gists: add some API related code improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 3841
diff changeset
98 repo = Gist.get_by_access_id(gist_access_id)
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
99 cs = repo.scm_instance.get_changeset(revision)
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
100 return cs, [n for n in cs.get_node('/')]
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
101
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
102 def create(self, description, owner, gist_mapping,
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
103 gist_type=Gist.GIST_PUBLIC, lifetime=-1):
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
104 """
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
105
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
106 :param description: description of the gist
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
107 :param owner: user who created this gist
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
108 :param gist_mapping: mapping {filename:{'content':content},...}
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
109 :param gist_type: type of gist private/public
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
110 :param lifetime: in minutes, -1 == forever
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
111 """
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
112 owner = self._get_user(owner)
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
113 gist_id = safe_unicode(unique_id(20))
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
114 lifetime = safe_int(lifetime, -1)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
115 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
116 log.debug('set GIST expiration date to: %s'
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
117 % (time_to_datetime(gist_expires)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
118 if gist_expires != -1 else 'forever'))
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
119 #create the Database version
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
120 gist = Gist()
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
121 gist.gist_description = description
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
122 gist.gist_access_id = gist_id
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
123 gist.gist_owner = owner.user_id
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
124 gist.gist_expires = gist_expires
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
125 gist.gist_type = safe_unicode(gist_type)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
126 self.sa.add(gist)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
127 self.sa.flush()
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
128 if gist_type == Gist.GIST_PUBLIC:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
129 # 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
130 gist_id = safe_unicode(gist.gist_id)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
131 gist.gist_access_id = gist_id
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
132 self.sa.add(gist)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
133
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
134 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
135 log.debug('Creating new %s GIST repo in %s' % (gist_type, gist_repo_path))
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
136 repo = RepoModel()._create_filesystem_repo(
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
137 repo_name=gist_id, repo_type='hg', repo_group=GIST_STORE_LOC)
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
138
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
139 processed_mapping = {}
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
140 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
141 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
142 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
143
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
144 content = gist_mapping[filename]['content']
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
145 #TODO: expand support for setting explicit lexers
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
146 # if lexer is None:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
147 # try:
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
148 # guess_lexer = pygments.lexers.guess_lexer_for_filename
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
149 # lexer = guess_lexer(filename,content)
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
150 # except pygments.util.ClassNotFound:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
151 # lexer = 'text'
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
152 processed_mapping[filename] = {'content': content}
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
153
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
154 # now create single multifile commit
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
155 message = 'added file'
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
156 message += 's: ' if len(processed_mapping) > 1 else ': '
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
157 message += ', '.join([x for x in processed_mapping])
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
158
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
159 #fake RhodeCode Repository object
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
160 fake_repo = AttributeDict(dict(
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
161 repo_name=gist_repo_path,
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
162 scm_instance_no_cache=lambda: repo,
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 ScmModel().create_nodes(
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
165 user=owner.user_id, repo=fake_repo,
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
166 message=message,
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
167 nodes=processed_mapping,
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
168 trigger_push_hook=False
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
169 )
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
170
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
171 self._store_metadata(repo, gist.gist_id, gist.gist_access_id,
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
172 owner.user_id, gist.gist_type, gist.gist_expires)
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
173 return gist
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
174
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
175 def delete(self, gist, fs_remove=True):
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
176 gist = self._get_gist(gist)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
177 try:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
178 self.sa.delete(gist)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
179 if fs_remove:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
180 self.__delete_gist(gist)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
181 else:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
182 log.debug('skipping removal from filesystem')
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
183 except Exception:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
184 log.error(traceback.format_exc())
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
185 raise
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
186
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
187 def update(self, gist, description, owner, gist_mapping, gist_type,
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
188 lifetime):
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
189 gist = self._get_gist(gist)
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
190 gist_repo = gist.scm_instance
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
191
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
192 lifetime = safe_int(lifetime, -1)
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
193 if lifetime == 0: # preserve old value
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
194 gist_expires = gist.gist_expires
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
195 else:
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
196 gist_expires = time.time() + (lifetime * 60) if lifetime != -1 else -1
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
197
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
198 #calculate operation type based on given data
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
199 gist_mapping_op = {}
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
200 for k, v in gist_mapping.items():
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
201 # add, mod, del
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
202 if not v['org_filename'] and v['filename']:
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
203 op = 'add'
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
204 elif v['org_filename'] and not v['filename']:
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
205 op = 'del'
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
206 else:
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
207 op = 'mod'
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
208
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
209 v['op'] = op
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
210 gist_mapping_op[k] = v
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
211
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
212 gist.gist_description = description
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
213 gist.gist_expires = gist_expires
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
214 gist.owner = owner
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
215 gist.gist_type = gist_type
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
216 self.sa.add(gist)
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
217 self.sa.flush()
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
218
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
219 message = 'updated file'
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
220 message += 's: ' if len(gist_mapping) > 1 else ': '
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
221 message += ', '.join([x for x in gist_mapping])
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
222
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
223 #fake RhodeCode Repository object
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
224 fake_repo = AttributeDict(dict(
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
225 repo_name=gist_repo.path,
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
226 scm_instance_no_cache=lambda: gist_repo,
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
227 ))
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
228
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
229 self._store_metadata(gist_repo, gist.gist_id, gist.gist_access_id,
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
230 owner.user_id, gist.gist_type, gist.gist_expires)
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
231
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
232 ScmModel().update_nodes(
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
233 user=owner.user_id,
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
234 repo=fake_repo,
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
235 message=message,
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
236 nodes=gist_mapping_op,
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
237 trigger_push_hook=False
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
238 )
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
239
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3846
diff changeset
240 return gist