~kermiac/+junk/stipple_empathy-plugins

« back to all changes in this revision

Viewing changes to packagesync-0.0.3.py

  • Committer: duanedesign
  • Date: 2010-04-27 17:24:54 UTC
  • Revision ID: duanedesign@gmail.com-20100427172454-d27pht0gpkhmb2no
started adding a GUI

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
3
# packagesync.py - sync installed packages across several computers.
 
4
#
 
5
# Copyright 2010 Duane Hinnen
 
6
#
 
7
# This program is free software: you can redistribute it and/or modify it
 
8
# under the terms of the GNU General Public License version 3, as published
 
9
# by the Free Software Foundation.
 
10
#
 
11
# This program is distributed in the hope that it will be useful, but
 
12
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
13
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
14
# PURPOSE.  See the GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License along
 
17
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
#
 
19
 
 
20
import tempfile
 
21
import sys
 
22
import os
 
23
 
 
24
import pygtk
 
25
pygtk.require('2.0')
 
26
import gtk
 
27
 
 
28
from desktopcouch.records.server import CouchDatabase
 
29
from desktopcouch.records.record import Record as CouchRecord
 
30
import commands
 
31
 
 
32
database = CouchDatabase("packages", create=True)
 
33
databaserecord = ""
 
34
RECORD_TYPE = 'http://www.okiebuntu.homelinux.com/packagesync'
 
35
EMACS_CONFIG = os.path.expanduser("~") + "/.emacs"
 
36
VIM_CONFIG = os.path.expanduser("~") + "/.vimrc"
 
37
ZSH_CONFIG = os.path.expanduser("~") + "/.zshrc"
 
38
BASH_CONFIG = os.path.expanduser("~") + "/.bashrc"
 
39
 
 
40
class Window:
 
41
    def callback1(self, widget, data=None):
 
42
       # print "%s was toggled %s" % (data, ("OFF", "ON")[widget.get_active()])
 
43
        self.a = (1, 2)[widget.get_active()]
 
44
    def callback2(self, widget, data=None):
 
45
        self.b = (1, 2)[widget.get_active()]
 
46
 
 
47
    def run(self, widget, data=None):
 
48
        save = Save()
 
49
        if self.a == 2:
 
50
            print "making database1"
 
51
            save.makeDB()
 
52
        if self.b == 2:
 
53
            print "making database2"
 
54
            save.search()
 
55
        else:
 
56
            pass
 
57
 
 
58
 
 
59
    def delete_event(self, widget, event, data=None):
 
60
        gtk.main_quit()
 
61
        return False
 
62
        
 
63
    def __init__(self):
 
64
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
 
65
        self.window.connect("delete_event", self.delete_event)
 
66
        self.window.set_border_width(20)
 
67
        vbox = gtk.VBox(True, 2)
 
68
        self.window.add(vbox)
 
69
        ## 1st Button ##
 
70
        button = gtk.CheckButton("sync packages")
 
71
        button.connect("toggled", self.callback1, "sync packages")
 
72
        vbox.pack_start(button, True, True, 2)
 
73
        button.show()
 
74
        ## 2nd Button ##
 
75
        button = gtk.CheckButton("sync config files")
 
76
        button.connect("toggled", self.callback2, "sync config files")
 
77
        vbox.pack_start(button, True, True, 2)
 
78
        button.show()
 
79
        ##Run Command Button ##
 
80
        button = gtk.Button("Run")
 
81
        button.connect("clicked", self.run)
 
82
        vbox.pack_start(button, True, True, 2)
 
83
        button.show()
 
84
        ## Quit Button ##
 
85
        button = gtk.Button("Quit")
 
86
        button.connect("clicked", lambda wid: gtk.main_quit())
 
87
        vbox.pack_start(button, True, True, 2)
 
88
        button.show()
 
89
 
 
90
 
 
91
        vbox.show()
 
92
        self.window.show()
 
93
 
 
94
    def main(self):
 
95
        gtk.main()
 
96
 
 
97
class Save:
 
98
    def makeDB(self):
 
99
        '''
 
100
        gets list of package selections. Cleans up the output.
 
101
        saves the data to a CouchDB.
 
102
        '''
 
103
 
 
104
        getsel_unfor = commands.getoutput("dpkg --get-selections '*'")
 
105
## need to rewrite search()    search(getsel_unfor)
 
106
        getsel = getsel_unfor.replace("\t"," ") #.replace("\n"," ")
 
107
        
 
108
        record_one = CouchRecord({  "_id"       :  "deb"     ,
 
109
                                    "profile"   :  "user001" ,
 
110
                                    "packages"  :  getsel    ,
 
111
                                  },
 
112
                                  record_type = RECORD_TYPE)
 
113
 
 
114
        databaserecord = database.put_record(record_one)
 
115
 
 
116
 
 
117
    def search(search_string):
 
118
        if search_string.find("emacs") != -1:
 
119
                with open(EMACS_CONFIG, 'r') as emacs:
 
120
                    er = emacs.read()
 
121
                print er
 
122
 
 
123
 
 
124
                record_two = CouchRecord({  "_id"       :  "emacs"   ,  
 
125
                                            "profile"   :  "user001" ,
 
126
                                            "type"      :  "dotfile" ,
 
127
                                            "file"      :  er        ,
 
128
                                         },
 
129
                                         record_type = RECORD_TYPE)
 
130
 
 
131
                databaserecord = database.put_record(record_two)
 
132
                emacs.close()
 
133
        else:
 
134
                pass
 
135
        if search_string.find("vim") != -1:
 
136
                with open(VIM_CONFIG, 'r') as vim:
 
137
                    vr = vim.read()
 
138
                print vr
 
139
 
 
140
 
 
141
                record_two = CouchRecord({  "_id"       :  "vim"   ,  
 
142
                                            "profile"   :  "user001" ,
 
143
                                            "type"      :  "dotfile" ,
 
144
                                            "file"      :  vr        ,
 
145
                                         },
 
146
                                         record_type = RECORD_TYPE)
 
147
 
 
148
                databaserecord = database.put_record(record_two)
 
149
                vim.close()
 
150
        else:
 
151
                pass
 
152
 
 
153
        if search_string.find("zsh") != -1:
 
154
                with open(ZSH_CONFIG, 'r') as zsh:
 
155
                    zr = zsh.read()
 
156
                print zr
 
157
 
 
158
 
 
159
                record_two = CouchRecord({  "_id"       :  "zsh"   ,  
 
160
                                            "profile"   :  "user001" ,
 
161
                                            "type"      :  "dotfile" ,
 
162
                                            "file"      :  zr        ,
 
163
                                         },
 
164
                                         record_type = RECORD_TYPE)
 
165
 
 
166
                databaserecord = database.put_record(record_two)
 
167
                zsh.close()
 
168
        else:
 
169
                pass
 
170
 
 
171
        if search_string.find("bash") != -1:
 
172
                with open(BASH_CONFIG, 'r') as bash:
 
173
                    br = bash.read()
 
174
                print br
 
175
 
 
176
 
 
177
                record_two = CouchRecord({  "_id"       :  "bash"   ,  
 
178
                                            "profile"   :  "user001" ,
 
179
                                            "type"      :  "dotfile" ,
 
180
                                            "file"      :  br        ,
 
181
                                         },
 
182
                                         record_type = RECORD_TYPE)
 
183
 
 
184
                databaserecord = database.put_record(record_two)
 
185
                bash.close()
 
186
        else:
 
187
                pass
 
188
 
 
189
        
 
190
class Restore:
 
191
    def UseSettings(self):
 
192
        '''
 
193
        Create a View. Execute view. Iterate over it.
 
194
        Write the package selections to a tmp file.
 
195
        Use file to set selections and then intall the packages
 
196
        '''
 
197
        map_js = 'function(doc){if (doc._id == "deb"){emit(null,doc.packages);}}'
 
198
        database.add_view("installed", map_js, None, "getnames")
 
199
        result = database.execute_view("installed", "getnames")
 
200
 
 
201
        for row in result:
 
202
            dpkglist = row.value
 
203
        
 
204
        tmp_file = open('/tmp/workfile', 'w')
 
205
        tmp_file.write(dpkglist)
 
206
        os.system ("sudo dpkg --set-selections < /tmp/workfile")
 
207
        os.system ("sudo apt-get -y update")
 
208
        os.system ("sudo apt-get dselect-upgrade")
 
209
        tmp_file.close()
 
210
 
 
211
 
 
212
    def UseConfig(self):
 
213
        '''
 
214
        Create a View. Execute view. Iterate over it.
 
215
        Write the data to a .config in users ~/
 
216
        '''
 
217
    #    config_iter = ""
 
218
    ### emacs ###
 
219
        map_js2 = 'function(doc){if (doc._id == "emacs"){emit(null,doc.file);}}'
 
220
        database.add_view("emacsconfig", map_js2, None, "getfile")
 
221
        result = database.execute_view("emacsconfig", "getfile")
 
222
 
 
223
        for row in result:
 
224
            config_iter = row.value
 
225
            print config_iter
 
226
        emacs_file = open(os.path.expanduser("~") + "/.emacs2", 'w')
 
227
        emacs_file.write(config_iter)
 
228
        emacs_file.close()
 
229
 
 
230
    ### vim ####
 
231
        map_js2 = 'function(doc){if (doc._id == "vim"){emit(null,doc.file);}}'
 
232
        database.add_view("vimconfig", map_js2, None, "getfile")
 
233
        result = database.execute_view("vimconfig", "getfile")
 
234
 
 
235
        for row in result:
 
236
            config_iter = row.value
 
237
 
 
238
        vim_file = open(os.path.expanduser("~") + "/.vimrc2", 'w')
 
239
        vim_file.write(config_iter)
 
240
        vim_file.close()
 
241
 
 
242
    ### bash ####
 
243
        map_js2 = 'function(doc){if (doc._id == "bash"){emit(null,doc.file);}}'
 
244
        database.add_view("bashconfig", map_js2, None, "getfile")
 
245
        result = database.execute_view("bashconfig", "getfile")
 
246
 
 
247
        for row in result:
 
248
            config_iter = row.value
 
249
 
 
250
        bash_file = open(os.path.expanduser("~") + "/.bashrc2", 'w')
 
251
        bash_file.write(config_iter)
 
252
        bash_file.close()
 
253
 
 
254
    ### zsh ####
 
255
        map_js2 = 'function(doc){if (doc._id == "zsh"){emit(null,doc.file);}}'
 
256
        database.add_view("zshconfig", map_js2, None, "getfile")
 
257
        result = database.execute_view("zshconfig", "getfile")
 
258
 
 
259
        for row in result:
 
260
            config_iter = row.value
 
261
 
 
262
        zsh_file = open(os.path.expanduser("~") + "/.zshrc2", 'w')
 
263
        zsh_file.write(config_iter)
 
264
        zsh_file.close()   
 
265
 
 
266
 
 
267
 
 
268
if __name__ == "__main__":
 
269
    window = Window()
 
270
    window.main()
 
271
    '''sr = raw_input("Do you want to save or restore? (s/r): ")
 
272
    if sr == "s":
 
273
        makeDB()
 
274
    elif sr == "r":
 
275
        #UseSettings()
 
276
        UseConfig()
 
277
    else:
 
278
        print "please type 's' or 'r'"
 
279
    print "finished"   '''