summaryrefslogtreecommitdiff
path: root/fsck-larch
blob: aef6647f61194ad8f363cf79a7cb7c6b35513579 (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
71
72
73
74
75
76
77
78
#!/usr/bin/python
# Copyright 2010, 2011  Lars Wirzenius
# 
# 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 cliapp
import logging
import sys
import tracing
import ttystatus

import larch.fsck


class Fsck(cliapp.Application):

    def add_settings(self):
        self.settings.string_list(['trace'], 'add PATTERN to trace patterns',
                                  metavar='PATTERN')
        self.settings.boolean(['fix'], 'fix problems found?')

    def process_args(self, args):
        for pattern in self.settings['trace']:
            tracing.trace_add_pattern(pattern)
    
        self.ts = ttystatus.TerminalStatus(period=0.1)
        self.ts['check'] = 0
        self.ts['checks'] = 0
        self.ts['checkname'] = ''
        self.ts.add(ttystatus.PercentDone('check', 'checks', decimals=2))
        self.ts.add(ttystatus.Literal(' '))
        self.ts.add(ttystatus.RemainingTime('check', 'checks'))
        self.ts.add(ttystatus.Literal(' remaining; now: '))
        self.ts.add(ttystatus.String('checkname'))

        self.errors = False
        for dirname in args:
            self.ts.notify('fsck-larch for %s' % dirname)
            forest = larch.open_forest(dirname=dirname)
            fsck = larch.fsck.Fsck(forest, self.warning, self.error, 
                                   self.settings['fix'])
            all_work = list(fsck.find_work())
            
            self.ts['checks'] = len(all_work)
            self.ts['check'] = 0
            for work in all_work:
                self.ts['check'] += 1
                self.ts['checkname'] = str(work)
                work.do()

        self.ts.finish()
        
        if self.errors:
            sys.exit(1)

    def error(self, msg):
        self.errors = True
        self.ts.notify(msg)
        logging.error(msg)

    def warning(self, msg):
        self.ts.notify(msg)
        logging.warning(msg)

if __name__ == '__main__':
    Fsck().run()