aboutsummaryrefslogtreecommitdiff
blob: faa49aa9ecf8e81f8ea1eb742b2d9692f53c0106 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
#
#    Rotator.py: this file is part of the GRS suite
#    Copyright (C) 2015  Anthony G. Basile
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import glob
import re
import os
import shutil

class Rotator():
    """ Super class for rotating files or directories.  """

    def rotate(self, obj, upper_limit = 20):
        """ Does the work of rotating objects fitting the pattern obj.(d+).

            obj -> The absolute path to the objects to be rotated.  The
            obj's are assumed to have a pattern obj.(d+) and does NOT
            include a bare obj not followed by a decimal.  (For that,
            use full_rotate() below).  Note that gaps in numbers are
            are preserved.  Eg.

                Old Name        New Name
                log             (untouched)
                log.0           log.1
                log.1           log.2
                log.3           log.4 (Note the gap is preserved.)
                log.4           log.5

            obj's paste an upper limit are deleted.
        """
        objs = glob.glob('%s.*' % obj)
        indexed_obj = {}
        for o in objs:
            m = re.search('^.+\.(\d+)$', o)
            indexed_obj[int(m.group(1))] = o
        count = list(indexed_obj.keys())
        count.sort()
        count.reverse()
        for c in count:
            current_obj = indexed_obj[c]
            if c >= upper_limit:
                try:
                    shutil.rmtree(current_obj)
                except NotADirectoryError:
                    os.unlink(current_obj)
                continue
            m = re.search('^(.+)\.\d+$', current_obj)
            next_obj = '%s.%d' % (m.group(1), c+1)
            shutil.move(current_obj, next_obj)


    def full_rotate(self, obj, upper_limit = 20):
        """ Rotate both obj and obj.(d+). """
        self.rotate(obj, upper_limit = upper_limit)
        if os.path.exists(obj):
            shutil.move(obj, '%s.0' % obj)