3
# packagesync.py - sync installed packages across several computers.
5
# Copyright 2010 Duane Hinnen
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.
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.
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/>.
28
from desktopcouch.records.server import CouchDatabase
29
from desktopcouch.records.record import Record as CouchRecord
32
database = CouchDatabase("packages", create=True)
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"
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()]
47
def run(self, widget, data=None):
50
print "making database1"
53
print "making database2"
59
def delete_event(self, widget, event, data=None):
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)
70
button = gtk.CheckButton("sync packages")
71
button.connect("toggled", self.callback1, "sync packages")
72
vbox.pack_start(button, True, True, 2)
75
button = gtk.CheckButton("sync config files")
76
button.connect("toggled", self.callback2, "sync config files")
77
vbox.pack_start(button, True, True, 2)
79
##Run Command Button ##
80
button = gtk.Button("Run")
81
button.connect("clicked", self.run)
82
vbox.pack_start(button, True, True, 2)
85
button = gtk.Button("Quit")
86
button.connect("clicked", lambda wid: gtk.main_quit())
87
vbox.pack_start(button, True, True, 2)
100
gets list of package selections. Cleans up the output.
101
saves the data to a CouchDB.
104
getsel_unfor = commands.getoutput("dpkg --get-selections '*'")
105
## need to rewrite search() search(getsel_unfor)
106
getsel = getsel_unfor.replace("\t"," ") #.replace("\n"," ")
108
record_one = CouchRecord({ "_id" : "deb" ,
109
"profile" : "user001" ,
110
"packages" : getsel ,
112
record_type = RECORD_TYPE)
114
databaserecord = database.put_record(record_one)
117
def search(search_string):
118
if search_string.find("emacs") != -1:
119
with open(EMACS_CONFIG, 'r') as emacs:
124
record_two = CouchRecord({ "_id" : "emacs" ,
125
"profile" : "user001" ,
129
record_type = RECORD_TYPE)
131
databaserecord = database.put_record(record_two)
135
if search_string.find("vim") != -1:
136
with open(VIM_CONFIG, 'r') as vim:
141
record_two = CouchRecord({ "_id" : "vim" ,
142
"profile" : "user001" ,
146
record_type = RECORD_TYPE)
148
databaserecord = database.put_record(record_two)
153
if search_string.find("zsh") != -1:
154
with open(ZSH_CONFIG, 'r') as zsh:
159
record_two = CouchRecord({ "_id" : "zsh" ,
160
"profile" : "user001" ,
164
record_type = RECORD_TYPE)
166
databaserecord = database.put_record(record_two)
171
if search_string.find("bash") != -1:
172
with open(BASH_CONFIG, 'r') as bash:
177
record_two = CouchRecord({ "_id" : "bash" ,
178
"profile" : "user001" ,
182
record_type = RECORD_TYPE)
184
databaserecord = database.put_record(record_two)
191
def UseSettings(self):
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
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")
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")
214
Create a View. Execute view. Iterate over it.
215
Write the data to a .config in users ~/
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")
224
config_iter = row.value
226
emacs_file = open(os.path.expanduser("~") + "/.emacs2", 'w')
227
emacs_file.write(config_iter)
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")
236
config_iter = row.value
238
vim_file = open(os.path.expanduser("~") + "/.vimrc2", 'w')
239
vim_file.write(config_iter)
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")
248
config_iter = row.value
250
bash_file = open(os.path.expanduser("~") + "/.bashrc2", 'w')
251
bash_file.write(config_iter)
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")
260
config_iter = row.value
262
zsh_file = open(os.path.expanduser("~") + "/.zshrc2", 'w')
263
zsh_file.write(config_iter)
268
if __name__ == "__main__":
271
'''sr = raw_input("Do you want to save or restore? (s/r): ")
278
print "please type 's' or 'r'"