aboutsummaryrefslogtreecommitdiff
blob: f566dfff156073b96d724b3848a68c6b4a4e58c3 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#! /usr/bin/python
#
# $Header$
#
# Distributed under the terms of the GNU General Public License v2
# Copyright (c) 2003 Karl Trygve Kalleberg
#
# Based on previous versions, by
#  - Brandon Low <lostlogic@gentoo.org>
#  - Jochem Kossen <j.kossen@home.nl>
#  - Leo Lipelis <aeoo@gentoo.org>

from dialog import Dialog
import portage
import time
import re
import os

__author__ = "Karl Trygve Kalleberg"
__email__ = "karltk@gentoo.org"
__version__ = "0.2.0"
__productname__ = "etc-update"
__description__ = "Interactive config file updater"

globals = portage.settings.configdict["globals"]

for i in globals["CONFIG_PROTECT"].split():
    print i

# list all files in all CONFIG_PROTECT dirs
# list them in the gui
# one-by-one:
#  - is update to header only? 
#  - is the original unmodified from the previous package? (not checkable - duh!)
#  - 

class Config:
    pass

def loadConfig():
    cfg = Config()
    globals = portage.settings.configdict["globals"]
    cfg.config_protect = globals["CONFIG_PROTECT"].split()
    return cfg

def _recurseFiles(path):
    files = []
    if os.path.exists(path):
        try:
            tmpfiles = os.listdir(path)
            for i in tmpfiles:
                fn = path + "/" + i
                if os.path.isdir(fn):
                    files += _recurseFiles(fn)
                elif os.path.isfile(fn):
                    m = re.search("\._cfg...._",fn)
                    if m:
                        files.append(fn)
                else:
                    print "What is this anyway?:", fn
        except OSError:
            pass
            # print "Access denied:", path
            
    return files
                
def findAllFiles(dlg, config):
    files = []
    gauge = dlg.gauge(0,
                      "Processing CONFIG_PROTECT directories...",
                      7,
                      60,
                      "Gauge",
                      sleep=3)
    num_dirs = len(config.config_protect)
    for i in xrange(num_dirs):
        rem = repr(num_dirs - i / num_dirs)
        gauge.update(rem, "Directories remaining: %s" % rem)
        files += _recurseFiles(config.config_protect[i])
    return files

def prettifyFiles(files):
    rx = re.compile("\._cfg...._")
    def strip_cfg(x):
        """Remove ._cfg????_ part """
        m = rx.search(x)
        if m:
            s,e = m.span(0)
            return x[:s] + x[e:]
        return x
    return map(strip_cfg, files)

def updateFile(dlg, original):

    # Find candidates

    dir = os.path.dirname(original)
    filename = os.path.basename(original)
    rx = re.compile("\._cfg...._" + filename)
    cand = filter(lambda x: rx.search(x), os.listdir(dir))

    if len(cand) > 1:
        
        # Add mtimes
        for i in xrange(len(cand)):
            stamp = time.localtime(os.path.getmtime(dir + "/" + cand[i]))
            tstr = time.strftime("%a, %d %b %Y %H:%M:%S", stamp)
            cand[i] = cand[i] + " - " + tstr

    
        # Show selection
        replacement = dlg.menu("Files that need updating",
                          Dialog.AUTO_SIZE,
                          list = cand,
                          showHelp = False,
                          title="Checklist")

    else:
        replacement = cand[0]

    
    # Display diff

    dlg.yesno("Would you like to update \n" + \
              "[" + original + "]with\n[" + dir + "/" + replacement + "] ?")
    
def displayFiles(dlg, config, files):

    pretty_files = prettifyFiles(files)

    while 1:
        result = dlg.menu("Files that need updating",
                          Dialog.AUTO_SIZE,
                          list = pretty_files,
                          showHelp = False,
                          title="Checklist")
        if result == None:
            if dlg.ERR_CANCEL:
                break

        updateFile(dlg, result)
        
    if len(pretty_files):
        print "!!! Warning: There are still files that require updating."
        
    
def main():
    dlg = Dialog("etc-update")
#    dlg = None
    
    config = loadConfig()
    files = findAllFiles(dlg, config)
    displayFiles(dlg, config,files)
    
    
if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        print "Operation aborted!"

# TODO:
# - option for automatically update untouched files
# - show coloured diff
# - proper progress bar