~darragh-ssa/deditor/deditor

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
#!/usr/bin/env python
import wx, os, sys, subprocess, shutil, ConfigParser
from dialogs import CSDialog

class Update():
    def __init__(self, instance):
        self.instance = instance
        self.gui = instance.main
        
        self.check_update()

    def check_update(self):
        #fetch version
        try:
            os.popen("wget -q http://kruptein.alwaysdata.net/version.txt")
        except:
            pass
        else:
            f = open("version.txt", "r")
            self.version = self.string_to_tuple(f.read())
            f.close()
            os.remove("version.txt")
            self.local_version = self.string_to_tuple(self.instance.get_base_config("general","version"))
            if self.version > self.local_version:
                if CSDialog( "Update possible", "Update to version %s now?" % self.tuple_to_string(self.version)).GetReturnCode():
                    self.update()

    def pd(self):
        import threading
        app = wx.PySimpleApp(0)
        gauge = ProgressBar(None, -1, '')
        def f():
            gauge.Show()
            app.MainLoop()
        my_thread=threading.Thread(target=f)
        my_thread.start()
        return gauge

    def update(self):
        self.gui.quit()
        gauge = self.pd()
        print gauge.text.GetLabel()
        print
        print "Updating"
        print
        print "STEP 1: Save configs and plugins"
        gauge.text.SetLabel("Saving configuration and plugins")
        self.save_configs()
        gauge.Update()
        print "STEP 2: download new version"
        gauge.text.SetLabel("Downloading new version")
        self.download()
        gauge.Update()
        print "STEP 3: install new version"
        gauge.text.SetLabel("installing new version")
        self.install()
        gauge.Update()
        print "STEP 4: config and plugins reset"
        gauge.text.SetLabel("Reset configuration and plugins")
        self.reset()
        gauge.Update()
        print "FINISHED UPDATING"
        gauge.text.SetLabel("Finished, restarting deditor")
        print "Starting Deditor"
        python = sys.executable
        os.execl(python, python, * sys.argv)

    def save_configs(self):
        ded = os.path.join( os.path.expanduser("~"), ".deditor" )
        versionsave = os.path.join( ded, self.tuple_to_string(self.local_version) )
        if not os.path.isdir( ded ): os.mkdir( ded )
        if os.path.isdir( versionsave ):    shutil.rmtree( versionsave )
        shutil.copytree(self.instance.plugindir, os.path.join(versionsave, "Plugins"))
        shutil.copy(os.path.join(self.instance.directory, "configuration.ded"), versionsave)
        shutil.copy(os.path.join(self.instance.directory, "pluginconf.ded"), versionsave)

    def download(self):
        os.popen("wget -q http://kruptein.alwaysdata.net/downloads/deb/deditor-%s.deb" % self.tuple_to_string(self.version))
        
    def install(self):
        args = ["gksudo","dpkg -i", "deditor-%s.deb" % self.tuple_to_string(self.version)]
        subprocess.call(args)
    
    def reset(self):
        #Configuration Files
        ded = os.path.join( os.path.expanduser("~"), ".deditor" )
        versionsave = os.path.join( ded, self.tuple_to_string(self.local_version) )
        self.compare("main",os.path.join(versionsave, "configuration.ded"))
        self.compare("plugin",os.path.join(versionsave, "pluginconf.ded"))
        #Plugins
        self.compare_plugins(os.path.join(versionsave, "Plugins"))
    
    def compare(self, type, file):
        old_conf = ConfigParser.ConfigParser()
        old_conf.read(file)
        new_conf = ConfigParser.ConfigParser()
        if type == "main":  new_conf.read(os.path.join(self.instance.directory,"configuration.ded"))
        elif type == "plugin":  new_conf.read(os.path.join(self.instance.directory,"pluginconf.ded"))
        for section in new_conf.sections():
            if section in old_conf.sections():
                for option in old_conf.options(section):
                    if option in new_conf.options(section) and option != "version":
                        new_conf.set(section, option, old_conf.get(section, option))
        if type == "main":
            with open(os.path.join(self.instance.directory,"configuration.ded"),"wb") as configf:
                new_conf.write(configf)
        elif type == "plugin":
            #In the pluginconf all sections should be added because these are user-dependent
            #the global conf should always have the same sections/release
            for section in old_conf.sections():
                if section not in new_conf.sections():
                    new_conf.add_section(section)
                    for option in old_conf.options(section):
                        print new_conf.get(section, option)
                        print old_conf.get(section, option)
                        new_conf.set(section, option, old_conf.get(section, option))
            with open(os.path.join(self.instance.directory,"pluginconf.ded"),"wb") as configf:
                new_conf.write(configf)
    
    def compare_plugins(self, path):
        for plugin in os.listdir(path): self.comp_file(os.path.join(path, plugin))
    
    def comp_file(self, path):
        if os.path.isdir(path):
            dir = os.path.join(self.instance.plugindir, os.path.split(os.path.normpath(path))[1])
            if not os.path.isdir(dir):  os.mkdir(dir)
            for file in os.listdir(path):
                if os.path.isdir(file): self.comp_file(file)
                else:
                    if file in os.listdir(dir): pass
                    else:   shutil.copy(os.path.join(path, file), dir)
        else:   shutil.copy(path, os.path.join(self.instance.plugindir, os.path.split(path)[1]))

    def string_to_tuple(self,st):
        return tuple(int(bit) for bit in st.strip().split("."))

    def tuple_to_string(self, tup):
        return '.'.join(str(i) for i in tup)

class ProgressBar(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)

        self.count = 0
        panel = wx.Panel(self, -1)
        vbox = wx.BoxSizer(wx.VERTICAL)
        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        hbox3 = wx.BoxSizer(wx.HORIZONTAL)

        self.gauge = wx.Gauge(panel, -1, 4)
        self.text = wx.StaticText(panel, -1, 'Task to be done')

        hbox1.Add(self.gauge, 1, wx.EXPAND)
        hbox3.Add(self.text, 1)
        vbox.Add((0, 50), 0)
        vbox.Add(hbox1, 0, wx.ALIGN_CENTRE)
        vbox.Add((0, 30), 0)
        vbox.Add(hbox3, 1, wx.ALIGN_CENTRE)
        panel.SetSizer(vbox)
        self.Centre()

    def Update(self):
        self.count = self.count +1
        self.gauge.SetValue(self.count)