annotate pylons_app/lib/utils.py @ 140:b5e59e2b5cfe

moved cache invalidating to utils, as seperate function. Implemented invalidating in
author Marcin Kuzminski <marcin@python-works.com>
date Sun, 09 May 2010 15:06:00 +0200
parents 36102488d634
children 52bbeb1e813f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
114
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
1 import os
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
2 import logging
125
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 114
diff changeset
3 from mercurial import ui, config, hg
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 114
diff changeset
4 from mercurial.error import RepoError
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 114
diff changeset
5 log = logging.getLogger(__name__)
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 114
diff changeset
6
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 114
diff changeset
7
82
670713507d03 Moved summary to seperate controller,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
8 def get_repo_slug(request):
670713507d03 Moved summary to seperate controller,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
9 path_info = request.environ.get('PATH_INFO')
96
f24b9a2934cf added is mercurial method in utils,
Marcin Kuzminski <marcin@python-works.com>
parents: 93
diff changeset
10 uri_lst = path_info.split('/')
93
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
11 repo_name = uri_lst[1]
82
670713507d03 Moved summary to seperate controller,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
12 return repo_name
96
f24b9a2934cf added is mercurial method in utils,
Marcin Kuzminski <marcin@python-works.com>
parents: 93
diff changeset
13
f24b9a2934cf added is mercurial method in utils,
Marcin Kuzminski <marcin@python-works.com>
parents: 93
diff changeset
14 def is_mercurial(environ):
f24b9a2934cf added is mercurial method in utils,
Marcin Kuzminski <marcin@python-works.com>
parents: 93
diff changeset
15 """
f24b9a2934cf added is mercurial method in utils,
Marcin Kuzminski <marcin@python-works.com>
parents: 93
diff changeset
16 Returns True if request's target is mercurial server - header
f24b9a2934cf added is mercurial method in utils,
Marcin Kuzminski <marcin@python-works.com>
parents: 93
diff changeset
17 ``HTTP_ACCEPT`` of such request would start with ``application/mercurial``.
f24b9a2934cf added is mercurial method in utils,
Marcin Kuzminski <marcin@python-works.com>
parents: 93
diff changeset
18 """
f24b9a2934cf added is mercurial method in utils,
Marcin Kuzminski <marcin@python-works.com>
parents: 93
diff changeset
19 http_accept = environ.get('HTTP_ACCEPT')
f24b9a2934cf added is mercurial method in utils,
Marcin Kuzminski <marcin@python-works.com>
parents: 93
diff changeset
20 if http_accept and http_accept.startswith('application/mercurial'):
f24b9a2934cf added is mercurial method in utils,
Marcin Kuzminski <marcin@python-works.com>
parents: 93
diff changeset
21 return True
f24b9a2934cf added is mercurial method in utils,
Marcin Kuzminski <marcin@python-works.com>
parents: 93
diff changeset
22 return False
114
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
23
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
24 def check_repo_dir(paths):
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
25 repos_path = paths[0][1].split('/')
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
26 if repos_path[-1] in ['*', '**']:
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
27 repos_path = repos_path[:-1]
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
28 if repos_path[0] != '/':
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
29 repos_path[0] = '/'
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
30 if not os.path.isdir(os.path.join(*repos_path)):
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
31 raise Exception('Not a valid repository in %s' % paths[0][1])
125
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 114
diff changeset
32
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 114
diff changeset
33 def check_repo(repo_name, base_path):
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 114
diff changeset
34
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 114
diff changeset
35 repo_path = os.path.join(base_path, repo_name)
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 114
diff changeset
36
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 114
diff changeset
37 try:
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 114
diff changeset
38 r = hg.repository(ui.ui(), repo_path)
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 114
diff changeset
39 hg.verify(r)
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 114
diff changeset
40 #here we hnow that repo exists it was verified
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 114
diff changeset
41 log.info('%s repo is already created', repo_name)
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 114
diff changeset
42 return False
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 114
diff changeset
43 #raise Exception('Repo exists')
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 114
diff changeset
44 except RepoError:
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 114
diff changeset
45 log.info('%s repo is free for creation', repo_name)
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 114
diff changeset
46 #it means that there is no valid repo there...
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 114
diff changeset
47 return True
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 114
diff changeset
48
114
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
49 def make_ui(path='hgwebdir.config', checkpaths=True):
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
50 """
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
51 A funcion that will read python rc files and make an ui from read options
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
52
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
53 @param path: path to mercurial config file
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
54 """
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
55 if not os.path.isfile(path):
125
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 114
diff changeset
56 log.error('Unable to read config file %s' % path)
114
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
57 return False
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
58 #propagated from mercurial documentation
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
59 sections = [
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
60 'alias',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
61 'auth',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
62 'decode/encode',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
63 'defaults',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
64 'diff',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
65 'email',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
66 'extensions',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
67 'format',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
68 'merge-patterns',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
69 'merge-tools',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
70 'hooks',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
71 'http_proxy',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
72 'smtp',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
73 'patch',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
74 'paths',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
75 'profiling',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
76 'server',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
77 'trusted',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
78 'ui',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
79 'web',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
80 ]
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
81
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
82 baseui = ui.ui()
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
83 cfg = config.config()
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
84 cfg.read(path)
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
85 if checkpaths:check_repo_dir(cfg.items('paths'))
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
86
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
87 for section in sections:
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
88 for k, v in cfg.items(section):
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
89 baseui.setconfig(section, k, v)
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
90
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
91 return baseui
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
92
140
b5e59e2b5cfe moved cache invalidating to utils, as seperate function. Implemented invalidating in
Marcin Kuzminski <marcin@python-works.com>
parents: 136
diff changeset
93 def invalidate_cache(name):
b5e59e2b5cfe moved cache invalidating to utils, as seperate function. Implemented invalidating in
Marcin Kuzminski <marcin@python-works.com>
parents: 136
diff changeset
94 from beaker.cache import region_invalidate
b5e59e2b5cfe moved cache invalidating to utils, as seperate function. Implemented invalidating in
Marcin Kuzminski <marcin@python-works.com>
parents: 136
diff changeset
95 if name == 'repo_list_2':
b5e59e2b5cfe moved cache invalidating to utils, as seperate function. Implemented invalidating in
Marcin Kuzminski <marcin@python-works.com>
parents: 136
diff changeset
96 log.info('INVALIDATING CACHE FOR %s', name)
b5e59e2b5cfe moved cache invalidating to utils, as seperate function. Implemented invalidating in
Marcin Kuzminski <marcin@python-works.com>
parents: 136
diff changeset
97 from pylons_app.lib.base import _get_repos
b5e59e2b5cfe moved cache invalidating to utils, as seperate function. Implemented invalidating in
Marcin Kuzminski <marcin@python-works.com>
parents: 136
diff changeset
98 #clear our cached list for refresh with new repo
b5e59e2b5cfe moved cache invalidating to utils, as seperate function. Implemented invalidating in
Marcin Kuzminski <marcin@python-works.com>
parents: 136
diff changeset
99 region_invalidate(_get_repos, None, 'repo_list_2')
b5e59e2b5cfe moved cache invalidating to utils, as seperate function. Implemented invalidating in
Marcin Kuzminski <marcin@python-works.com>
parents: 136
diff changeset
100
136
36102488d634 Added empty changeset to use in newly created repository, and used this inside a hg model in repos list
Marcin Kuzminski <marcin@python-works.com>
parents: 125
diff changeset
101 from vcs.backends.base import BaseChangeset
36102488d634 Added empty changeset to use in newly created repository, and used this inside a hg model in repos list
Marcin Kuzminski <marcin@python-works.com>
parents: 125
diff changeset
102 from vcs.utils.lazy import LazyProperty
36102488d634 Added empty changeset to use in newly created repository, and used this inside a hg model in repos list
Marcin Kuzminski <marcin@python-works.com>
parents: 125
diff changeset
103 class EmptyChangeset(BaseChangeset):
36102488d634 Added empty changeset to use in newly created repository, and used this inside a hg model in repos list
Marcin Kuzminski <marcin@python-works.com>
parents: 125
diff changeset
104
36102488d634 Added empty changeset to use in newly created repository, and used this inside a hg model in repos list
Marcin Kuzminski <marcin@python-works.com>
parents: 125
diff changeset
105 revision = -1
114
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
106
136
36102488d634 Added empty changeset to use in newly created repository, and used this inside a hg model in repos list
Marcin Kuzminski <marcin@python-works.com>
parents: 125
diff changeset
107 @LazyProperty
36102488d634 Added empty changeset to use in newly created repository, and used this inside a hg model in repos list
Marcin Kuzminski <marcin@python-works.com>
parents: 125
diff changeset
108 def raw_id(self):
36102488d634 Added empty changeset to use in newly created repository, and used this inside a hg model in repos list
Marcin Kuzminski <marcin@python-works.com>
parents: 125
diff changeset
109 """
36102488d634 Added empty changeset to use in newly created repository, and used this inside a hg model in repos list
Marcin Kuzminski <marcin@python-works.com>
parents: 125
diff changeset
110 Returns raw string identifing this changeset, useful for web
36102488d634 Added empty changeset to use in newly created repository, and used this inside a hg model in repos list
Marcin Kuzminski <marcin@python-works.com>
parents: 125
diff changeset
111 representation.
36102488d634 Added empty changeset to use in newly created repository, and used this inside a hg model in repos list
Marcin Kuzminski <marcin@python-works.com>
parents: 125
diff changeset
112 """
36102488d634 Added empty changeset to use in newly created repository, and used this inside a hg model in repos list
Marcin Kuzminski <marcin@python-works.com>
parents: 125
diff changeset
113 return '0' * 12
114
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
114