##// END OF EJS Templates
fixes issue #756 cleanup repos didn't properly compose paths of repos to be cleaned up....
marcink -
r3356:031a698e default
parent child Browse files
Show More
@@ -1,143 +1,144
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 package.rhodecode.lib.cleanup
4 4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 5
6 6 :created_on: Jul 14, 2012
7 7 :author: marcink
8 8 :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com>
9 9 :license: GPLv3, see COPYING for more details.
10 10 """
11 11 # This program is free software: you can redistribute it and/or modify
12 12 # it under the terms of the GNU General Public License as published by
13 13 # the Free Software Foundation, either version 3 of the License, or
14 14 # (at your option) any later version.
15 15 #
16 16 # This program is distributed in the hope that it will be useful,
17 17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 19 # GNU General Public License for more details.
20 20 #
21 21 # You should have received a copy of the GNU General Public License
22 22 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 23 from __future__ import with_statement
24 24
25 25 import os
26 26 import sys
27 27 import re
28 28 import shutil
29 29 import logging
30 30 import datetime
31 31
32 32 from os.path import dirname as dn, join as jn
33 33 from rhodecode.model import init_model
34 34 from rhodecode.lib.utils2 import engine_from_config, safe_str
35 35 from rhodecode.model.db import RhodeCodeUi
36 36
37 37
38 38 #to get the rhodecode import
39 39 sys.path.append(dn(dn(dn(os.path.realpath(__file__)))))
40 40
41 41 from rhodecode.lib.utils import BasePasterCommand, Command, ask_ok,\
42 42 REMOVED_REPO_PAT, add_cache
43 43
44 44 log = logging.getLogger(__name__)
45 45
46 46
47 47 class CleanupCommand(BasePasterCommand):
48 48
49 49 max_args = 1
50 50 min_args = 1
51 51
52 52 usage = "CONFIG_FILE"
53 53 summary = "Cleanup deleted repos"
54 54 group_name = "RhodeCode"
55 55 takes_config_file = -1
56 56 parser = Command.standard_parser(verbose=True)
57 57
58 58 def _parse_older_than(self, val):
59 59 regex = re.compile(r'((?P<days>\d+?)d)?((?P<hours>\d+?)h)?((?P<minutes>\d+?)m)?((?P<seconds>\d+?)s)?')
60 60 parts = regex.match(val)
61 61 if not parts:
62 62 return
63 63 parts = parts.groupdict()
64 64 time_params = {}
65 65 for (name, param) in parts.iteritems():
66 66 if param:
67 67 time_params[name] = int(param)
68 68 return datetime.timedelta(**time_params)
69 69
70 70 def _extract_date(self, name):
71 71 """
72 72 Extract the date part from rm__<date> pattern of removed repos,
73 73 and convert it to datetime object
74 74
75 75 :param name:
76 76 """
77 77 date_part = name[4:19] # 4:19 since we don't parse milisecods
78 78 return datetime.datetime.strptime(date_part, '%Y%m%d_%H%M%S')
79 79
80 80 def command(self):
81 81 logging.config.fileConfig(self.path_to_ini_file)
82 82 from pylons import config
83 83
84 84 #get to remove repos !!
85 85 add_cache(config)
86 86 engine = engine_from_config(config, 'sqlalchemy.db1.')
87 87 init_model(engine)
88 88
89 89 repos_location = RhodeCodeUi.get_repos_location()
90 90 to_remove = []
91 91 for dn, dirs, f in os.walk(safe_str(repos_location)):
92 92 for loc in dirs:
93 93 if REMOVED_REPO_PAT.match(loc):
94 to_remove.append([loc, self._extract_date(loc)])
94 to_remove.append([os.path.join(dn, loc),
95 self._extract_date(loc)])
95 96
96 97 #filter older than (if present)!
97 98 now = datetime.datetime.now()
98 99 older_than = self.options.older_than
99 100 if older_than:
100 101 to_remove_filtered = []
101 102 older_than_date = self._parse_older_than(older_than)
102 103 for name, date_ in to_remove:
103 104 repo_age = now - date_
104 105 if repo_age > older_than_date:
105 106 to_remove_filtered.append([name, date_])
106 107
107 108 to_remove = to_remove_filtered
108 109 print >> sys.stdout, 'removing [%s] deleted repos older than %s[%s]' \
109 110 % (len(to_remove), older_than, older_than_date)
110 111 else:
111 112 print >> sys.stdout, 'removing all [%s] deleted repos' \
112 113 % len(to_remove)
113 114 if self.options.dont_ask or not to_remove:
114 115 # don't ask just remove !
115 116 remove = True
116 117 else:
117 118 remove = ask_ok('are you sure to remove listed repos \n%s [y/n]?'
118 119 % ', \n'.join(['%s removed on %s'
119 120 % (safe_str(x[0]), safe_str(x[1])) for x in to_remove]))
120 121
121 122 if remove:
122 123 for name, date_ in to_remove:
123 124 print >> sys.stdout, 'removing repository %s' % name
124 125 shutil.rmtree(os.path.join(repos_location, name))
125 126 else:
126 127 print 'nothing done exiting...'
127 128 sys.exit(0)
128 129
129 130 def update_parser(self):
130 131 self.parser.add_option('--older-than',
131 132 action='store',
132 133 dest='older_than',
133 134 help=(
134 135 "only remove repos that have been removed "
135 136 "at least given time ago "
136 137 "ex. --older-than=30d deletes repositores "
137 138 "removed more than 30days ago. Possible options "
138 139 "d[ays]/h[ours]/m[inutes]/s[seconds]. OPTIONAL"),
139 140 )
140 141 self.parser.add_option('--dont-ask',
141 142 action='store_true',
142 143 dest='dont_ask',
143 144 help=("Don't ask to remove repos"))
General Comments 0
You need to be logged in to leave comments. Login now