X-Git-Url: https://v.licheni.net/stack/code/dboxswitch.git/blobdiff_plain/d21d77af7b9aec8d003222854815b99d866c49d7..dba698695cae0309ca3f28201f4381ea240644bf:/profhandler.py diff --git a/profhandler.py b/profhandler.py old mode 100755 new mode 100644 index 5ef71e5..4db5ab0 --- a/profhandler.py +++ b/profhandler.py @@ -36,6 +36,7 @@ import re import shutil import os import errno +import signal from apperror import AppError from settings import appconf @@ -91,7 +92,13 @@ class ProfHandler(): print("Creating a new profile") if self.isValidProfileName(profileName): - os.makedirs(os.path.join(self.getProfileFolder(), profileName)) + try: + os.makedirs(os.path.join(self.getProfileFolder(), profileName)) + except OSError,e: + if e.errno == errno.EEXIST: + raise AppError("Profile exists.") + else: + raise AppError(str(e)) else: raise AppError('Profile Name not valid.\nAllowed only ascii characters.') print("Profile "+profileName+" created.") @@ -108,27 +115,47 @@ class ProfHandler(): raise AppError('Profile Name does not exists') else: raise AppError('Profile Name not valid') - print("Profile "+profileName+" created.") + print("Profile "+profileName+" deleted.") + + def isCurrentProfile(self, ppath): + """ Returns true if the current profile path is currently activated """ + + pl = platform.system() + if pl in ('Linux','Darwin'): + if os.path.exists(self.getDropboxDirectory()): + return True if os.readlink(self.getDropboxDirectory()) == ppath else False + else: + return False def isValidProfileName(self, pname): + if self.reg.match(pname) is not None: return True else: return False def activateProfile(self, ppath): + pl = platform.system() if ppath in self.getProfilesList(): self.stopDropbox() try: - with open(ppath) as pdir: - os.unlink(self.getDropboxDirectory()) + if pl in ('Linux','Darwin'): + if os.path.exists(self.getDropboxDirectory()): + os.unlink(self.getDropboxDirectory()) os.symlink(ppath, self.getDropboxDirectory()) + else: + raise NotImplementedError, "Not implemented yet." except IOError as e: - raise AppError('Error on activating Profile: '+ppath) + raise AppError('Error on activating Profile: '+ self.getBaseProfileName(ppath)) self.startDropbox() else: raise AppError("Trying to acrivate non existant profile") + def getBaseProfileName(self, ppath): + """ Returns the base name given a profile returned by getProfilesList """ + + return os.path.basename(ppath) + def getDropboxDirectory(self): pl = platform.system() if pl in ('Linux', 'Darwin'): @@ -139,6 +166,32 @@ class ProfHandler(): else: raise NotImplementedError, "Not implemented yet." + def stopDropbox(self): + """ Stop dropbox Daemon """ + pl = platform.system() + if pl == 'Linux': + os.system("dropbox stop") + if pl in ('Linux','Darwin'): + pidfile = os.path.expanduser("~/.dropbox/dropbox.pid") + try: + with open(pidfile, "r") as f: + pid = int(f.read()) + os.kill(pid, signal.SIGTERM) + except: + pass + + def startDropbox(self): + """ Sart dropbox Daemon """ + + pl = platform.system() + if pl == 'Linux': + try: + os.system("dropbox start -i") + except: + raise AppError(u"Could not start dropbox.") + elif pl == 'Darwin': + os.system("/Applications/Dropbox.app/Contents/MacOS/Dropbox &") + __CSL = None def winsymlink(source, link_name):