-
Notifications
You must be signed in to change notification settings - Fork 2
/
bareshare.py~
399 lines (330 loc) · 12.3 KB
/
bareshare.py~
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/usr/bin/env python
#
# BareShare
# Authors: Daniel Holm, <[email protected]>, 120110 Updated 120203
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License version 3, as published by the
# Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the applicable version of the GNU Lesser General Public
# License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License version 3 along with this program. If not, see
# <http://www.gnu.org/licenses/>
#
# Imports needed modules
import pygtk
pygtk.require('2.0')
import gtk
import gobject
import appindicator
import pynotify
import os
import sys
import subprocess
import threading
import csv
from ConfigParser import SafeConfigParser
# Settingsdir and -file.
home = os.getenv('HOME')
configdir = home + "/.bareshare"
configfile = home + "/.bareshare/bareshare.conf"
lsyncdconfig = home + "/.bareshare/lsyncd.conf"
lsyncdlog = home + "/.bareshare/lsyncd.log"
baresharelog = home + "/.bareshare/bareshare.log"
# Some other variables
icon = "/home/daniel/Program/Projekt/BareShare/icons/bareshare-dark.png" # Fix
picon = "/home/daniel/Program/Projekt/BareShare/icons/bareshare-dark-passive.png" # Fix
uicon ="/home/daniel/Program/Projekt/BareShare/icons/bareshare-dark-upload.png" # Fix
sicon ="/home/daniel/Program/Projekt/BareShare/icons/bareshare-dark-sync.png" # Fix
# Messages
startingM = "Starting..."
syncingM = "Syncing..."
finishedM = "All files are up to date."
buildM = "Building file list..."
uploadM = "Uploading..."
downloadM = "Downloading..."
# Creates the class for the application
class BareShareAppIndicator:
# Check if config files and dirs exist. If not, create them.
if not os.path.exists(configdir):
print "Config dir and file did not exist. Creating..."
os.makedirs(configdir)
if not os.path.exists(configfile):
print "Configfile did not exist. Creating..."
open(configfile,'w').close()
if not os.path.exists(lsyncdconfig):
print "Configfile for lsyncd did not exist. Creating..."
open(configfile,'w').close()
# Start the "first run" dialog comes in 0.2
# self.first_run()
# Some log file stuff
os.system("cp "+lsyncdlog+" "+lsyncdlog+".1 && rm "+lsyncdlog) # Move old log file
# os.system("cp "+baresharelog+" "+baresharelog+".1 && rm "+baresharelog) # Move old log file
# Get actions from menu and print 'em (debug)
def menuitem_response(w, buf):
print buf
def __init__(self):
# Get settings from config (sections)
parser = SafeConfigParser()
parser.read(configfile)
# Bandwith speed and use rsync --bwlimit=value
value = 0 # Default bandwidth value
download = parser.get('profile', 'download')
upload = parser.get('profile', 'upload')
shares = parser.get('profile', 'shares')
print "DEBUG: Shares: "+shares
# For each section of shares in conf, get data from its own section
def worker():
for share in shares.split(', '):
username = parser.get(share, 'username')
sharename = parser.get(share, 'name')
way = parser.get(share, 'way')
local = parser.get(share, 'local')
remote = parser.get(share, 'remote')
domain = parser.get(share, 'domain')
remotedir = username+"@"+domain+":"+remote
rsynclog = home + "/.bareshare/"+share+"rsync.log"
# Backup old logs if they exist
if os.path.exists(rsynclog):
os.system("cp "+rsynclog+" "+rsynclog+".1 && rm "+rsynclog) # Move and remove old log
print 'DEBUG: Starting "'+sharename+'" '+way # Debugging
# Run rsync of each share - but check for which direction
if way == "download":
self.rsyncRun = subprocess.Popen(["rsync","--bwlimit="+download,"--stats","--progress","-azvv","-e","ssh",remotedir,local,"--log-file="+rsynclog], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# The status message
self.statusM = downloadM
if way == "upload":
self.rsyncRun = subprocess.Popen(["rsync","--bwlimit="+upload,"--stats","--progress","-azvv","-e","ssh",local,remotedir,"--log-file="+rsynclog], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# The status message
self.statusM = uploadM
# Print whats happening
self.line = self.rsyncRun.stdout.readline()
rsyncM = self.line.rstrip()
print "DEBUG: "+rsyncM
print "DEBUG: Done, next!"
self.t = threading.Thread(target = worker)
self.t.daemon = True
self.t.start()
# self.t.join()
# Start the sync daemon in the background
print "DEBUG: Starting lsyncd."
self.lsyncdRun = subprocess.Popen(["lsyncd",lsyncdconfig], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Print the debugging
# self.lineL = self.lsyncdRun.stdout.readline()
# lsyncdM = self.lineL.rstrip()
# print "DEBUG: "+lsyncdM
# Keep the labels updated
gobject.timeout_add(1000, self.lsyncdOutput, None)
# gobject.timeout_add(100, self.rsyncOutput, None)
# Create the appindicator
self.ind = appindicator.Indicator ("BareShare", icon, appindicator.CATEGORY_APPLICATION_STATUS)
self.ind.set_status (appindicator.STATUS_ACTIVE)
self.ind.set_icon(icon)
# Create a menu
self.menu = gtk.Menu()
# Dynamic label here
self.label = gtk.MenuItem()
self.label.set_label(startingM)
self.label.set_sensitive(False)
self.label.show()
self.menu.append(self.label)
# Dyamic label for fileinfo
self.labelR = gtk.MenuItem()
self.labelR.set_label("Fileinfo here")
self.labelR.set_sensitive(False)
self.labelR.hide()
self.menu.append(self.labelR)
# Separator
sep = gtk.SeparatorMenuItem()
sep.show()
self.menu.append(sep)
# Add share guide
add = "Add Share"
add_share = gtk.ImageMenuItem(gtk.STOCK_ADD)
add_share.connect("activate", self.first_run, None)
add_share.show()
self.menu.append(add_share)
# Pause/Resume sync
# Check weather lsyncd is running or not
self.ppus = gtk.MenuItem()
self.ppus.set_label("Pause Sync")
self.ppus.connect("activate", self.pauseUn)
self.ppus.show()
self.menu.append(self.ppus)
# Open preferences dialog
pref = "Preferences"
settings = gtk.ImageMenuItem(gtk.STOCK_PREFERENCES)
settings.connect("activate", self.prefD, None)
settings.show()
self.menu.append(settings)
# Separator
sep = gtk.SeparatorMenuItem()
sep.show()
self.menu.append(sep)
# Open About
about = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
about.connect("activate", self.show_about, None)
about.show()
self.menu.append(about)
# Quit-item
image = gtk.ImageMenuItem(gtk.STOCK_QUIT)
image.connect("activate", self.quit)
image.show()
self.menu.append(image)
# Show it all
self.menu.show()
self.ind.set_menu(self.menu)
# Close the indicator and kill the sync daemon
def quit(self, widget, data=None):
gtk.main_quit()
os.system("killall -9 lsyncd rsync")
# Pause or unpause function
def pauseUn(self, widget):
# Check if lsyncd is running
if pidL: #if it isnt
# Resume sync
self.ppus.set_label("Pause Sync")
self.label.set_label(startingM)
self.ind.set_icon(icon) # Set to active icon
else: # if it is
# Pause sync
self.ppus.set_label("Resume Sync")
self.label.set_label("Paused")
self.ind.set_icon(picon) # Passive icon
# Updates the label about rsync transer data
def rsyncOutput(self, widget):
self.labelR.set_label(rsyncM)
print "DEBUG: "+rsyncM
return True # make it go on
# Updates the label about lsyncd transer data
def lsyncdOutput(self, widget):
# Get output from lsyncd subprocess
# self.line = self.lsyncdRun.stdout.readline()
# lsyncdM = self.line.rstrip()
# self.labelR.set_label(lsyncdM)
# print "DEBUG: "+lsyncdM
return True # Keep it go on
# About
def show_about(self, widget, data):
# Create AboutDialog object
dialog = gtk.AboutDialog()
# Add the application name to the dialog
dialog.set_name('BareShare')
# Add a application icon to the dialog
dialog.set_logo(gtk.gdk.pixbuf_new_from_file("/home/daniel/Dokument/BareShare/icons/bareshare-light.png"))
# Set the application version
dialog.set_version('0.1.5')
# Set a list of authors.
dialog.set_authors(['Daniel Holm - Dev', 'Icons by Gentleface - http://www.gentleface.com/'])
# "coyright" - I'd like to see copyleft ;)
dialog.set_copyright("Daniel Holm")
# Add a short comment about the application.
dialog.set_comments('Backup and Share Service')
# Add license information.
license = "This program is free software: you can redistribute it and/or modify\nit under the terms of the GNU General Public License as published by\nthe Free Software Foundation, either version 3 of the License, or\n(at your option) any later version.\n\nThis program is distributed in the hope that it will be useful,\nbut WITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\nGNU General Public License for more details.\n\nYou should have received a copy of the GNU General Public License\nalong with this program. If not, see <http://www.gnu.org/licenses/>."
dialog.set_license(license)
# Add a homepage to the dialog.
dialog.set_website("https://github.com/danielholm/BareShare")
# Show the dialog
dialog.run()
# The destroy method must be called otherwise the 'Close' button will
# not work.
dialog.destroy()
def hide_dialog(self, widget, data):
widget.hide()
# First run
def first_run(self, widget, data):
dialog = gtk.MessageDialog(
None,
gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
gtk.MESSAGE_QUESTION,
gtk.BUTTONS_OK,
None)
# Set some window settings
dialog.set_title("BareShare - Add new share")
# dialog.set_size_request(400, 300)
dialog.set_position(gtk.WIN_POS_CENTER)
dialog.set_markup('Please enter the needed information to connect.')
hbox = gtk.VBox(False, 2)
vbox = gtk.HBox(True, 2)
# Share name
name = gtk.Entry()
name.set_text("Name of share")
hbox.add(name)
# Server adress
adress = gtk.Entry()
adress.set_text("Server adress (domain)")
hbox.add(adress)
# Server username
username = gtk.Entry()
username.set_text("Username on server")
hbox.add(username)
# local directory
local = gtk.Entry()
local.set_text("Directory on machine")
hbox.add(local)
# remote directory
remote = gtk.Entry()
remote.set_text("Directory on server")
hbox.add(remote)
dialog.vbox.pack_end(hbox, True, True, 0)
dialog.show_all()
dialog.run()
# Get the text from entry fields
get_name = name.get_text()
get_adress = adress.get_text()
get_username = username.get_text()
get_local = local.get_text()
get_remote = remote.get_text()
# When clicked ok, destroy the window and send the data
dialog.connect("destroy", self.addShare, get_name, get_adress, get_username, get_local, get_remote)
dialog.destroy()
# Collect the data from the add share dialog and modify the config
def addShare(self, widget, name, adress, username, local, remote):
# Debugging
print "Share name: "+ name
print "Domain: "+ adress
print "Username: "+ username
print "Local dir: "+ local
print "Remote dir: "+ remote
# Backup the old config file
os.system("cp "+configfile+" "+configfile+".bak")
# Set up the share settings template variable
tpl = "\n["+name+"]\nname = "+name+"\nusername = "+username+"\nway = download\nlocal = "+local+"/\nremote = "+remote+"/\ndomain = "+adress+"\n"
# Parse the config file to get the previous shares
parser = SafeConfigParser()
parser.read(configfile)
shares = parser.get('profile', 'shares')
# Add the new share to the variable and replace the row in the config file
sharesNew = "shares = "+shares+", "+name+"\n"
# Replace the row in config file
lines = open(configfile, 'r').readlines()
lines[3] = sharesNew
out = open(configfile, 'w')
out.writelines(lines)
out.close()
# Add the new share to the config file
with open(configfile, "a") as f:
f.write(tpl)
# Preferences window
def prefD(self, widget, data):
pref = gtk.MessageDialog(None,
gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_WARNING,
gtk.BUTTONS_CLOSE, "Not yet implented")
pref.set_title("BareShare - Preferences")
pref.set_size_request(500, 300)
pref.set_position(gtk.WIN_POS_CENTER)
pref.run()
pref.destroy()
def main():
gtk.main()
return 0
if __name__ == "__main__":
indicator = BareShareAppIndicator()
main()