def _list(params): result = [] path = params['path'] if bpio.Linux() or bpio.Mac(): path = '/' + path.lstrip('/') lst = backup_fs.ListByPathAdvanced(path) if not isinstance(lst, list): lg.warn('backup_fs.ListByPathAdvanced returned: %s' % lst) return { "result": [], } for item in lst: if item[2] == 'index': continue result.append({ "type": item[0], "name": item[1], "id": item[2], "rights": "", "size": item[3], "source_size": item[7].size, "date": item[4], "dirpath": item[5], "has_childs": item[6], "content": '1' if item[7].exist() else '', "versions": item[8], }) return { 'result': result, }
def execute_in_shell(cmdargs, base_dir=None): global _CurrentProcess from system import nonblocking import subprocess if _Debug: lg.out(_DebugLevel, 'run_upnpc.execute_in_shell: "%s"' % (' '.join(cmdargs))) in_shell = True if bpio.Mac(): in_shell = False _CurrentProcess = nonblocking.Popen( cmdargs, shell=in_shell, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, ) out_data = _CurrentProcess.communicate()[0] returncode = _CurrentProcess.returncode if _Debug: lg.out( _DebugLevel, 'run_upnpc.execute_in_shell returned: %s and %d bytes output' % (returncode, len(out_data))) if returncode > 0: if _Debug: lg.out(_DebugLevel, '\n' + out_data) return (out_data, returncode) # _CurrentProcess
def _upload(params): path = params['path'] if bpio.Linux() or bpio.Mac(): path = '/' + (path.lstrip('/')) localPath = unicode(path) if not bpio.pathExist(localPath): return { 'result': { "success": False, "error": 'local path %s was not found' % path } } result = [] pathID = backup_fs.ToID(localPath) if pathID is None: if bpio.pathIsDir(localPath): pathID, iter, iterID = backup_fs.AddDir(localPath, read_stats=True) result.append('new folder was added: %s' % localPath) else: pathID, iter, iterID = backup_fs.AddFile(localPath, read_stats=True) result.append('new file was added: %s' % localPath) pathID = global_id.CanonicalID(pathID) backup_control.StartSingle(pathID=pathID, localPath=localPath) backup_fs.Calculate() backup_control.Save() control.request_update([ ('pathID', pathID), ]) result.append('backup started: %s' % pathID) return { 'result': result, }
def _list_local(params): result = [] path = params['path'] if bpio.Linux() or bpio.Mac(): path = '/' + path.lstrip('/') path = bpio.portablePath(path) only_folders = params['onlyFolders'] if (path == '' or path == '/') and bpio.Windows(): for itemname in bpio.listLocalDrivesWindows(): result.append({ "name": itemname.rstrip('\\').rstrip('/').lower(), "rights": "drwxr-xr-x", "size": "", "date": "", "type": "dir", "dirpath": path, }) else: if bpio.Windows() and len(path) == 2 and path[1] == ':': path += '/' apath = path for itemname in bpio.list_dir_safe(apath): itempath = os.path.join(apath, itemname) if only_folders and not os.path.isdir(itempath): continue result.append({ "name": itemname, "rights": "drwxr-xr-x", "size": str(os.path.getsize(itempath)), "date": str(os.path.getmtime(itempath)), "type": "dir" if os.path.isdir(itempath) else "file", "dirpath": apath, }) return {'result': result, }
def _download(params): # localName = params['name'] backupID = global_id.CanonicalID(params['backupid']) destpath = params['dest_path'] if bpio.Linux() or bpio.Mac(): destpath = '/' + destpath.lstrip('/') restorePath = bpio.portablePath(destpath) # overwrite = params['overwrite'] customerGlobalID, remotePath, version = packetid.SplitBackupID(backupID) pathID = packetid.MakeBackupID(customerGlobalID, remotePath) if not customerGlobalID: customerGlobalID = my_id.getGlobalID() if not packetid.IsCanonicalVersion(version): return {'result': {"success": False, "error": "path %s is not valid" % backupID}} if not remotePath: return {'result': {"success": False, "error": "path %s is not valid" % backupID}} if not packetid.Valid(remotePath): return {'result': {"success": False, "error": "path %s is not valid" % backupID}} if backup_control.IsBackupInProcess(backupID): return {'result': {"success": True, "error": None}} if backup_control.HasTask(pathID): return {'result': {"success": True, "error": None}} localPath = backup_fs.ToPath(remotePath) if localPath == restorePath: restorePath = os.path.dirname(restorePath) def _itemRestored(backupID, result): customerGlobalID, remotePath, _ = packetid.SplitBackupID(backupID) backup_fs.ScanID(remotePath, customer_idurl=global_id.GlobalUserToIDURL(customerGlobalID)) backup_fs.Calculate() restore_monitor.Start(backupID, restorePath, callback=_itemRestored) return {'result': {"success": True, "error": None}}
def ExplorePathInOS(filepath): """ Very nice and portable way to show location or file on local disk. """ try: if bpio.Windows(): # os.startfile(filepath) if os.path.isfile(filepath): subprocess.Popen([ 'explorer', '/select,', '%s' % (filepath.replace('/', '\\')) ]) else: subprocess.Popen( ['explorer', '%s' % (filepath.replace('/', '\\'))]) elif bpio.Linux(): subprocess.Popen(['`which xdg-open`', filepath]) elif bpio.Mac(): subprocess.Popen(["open", "-R", filepath]) except: try: import webbrowser webbrowser.open(filepath) except: lg.exc() return
def ReadRepoLocation(): """ This method reutrn a tuple of two strings: "name of the current repo" and "repository location". """ if bpio.Linux() or bpio.Mac(): repo_file = os.path.join(bpio.getExecutableDir(), 'repo') if os.path.isfile(repo_file): src = bpio.ReadTextFile(repo_file) if src: try: return src.split('\n')[0].strip(), src.split( '\n')[1].strip() except: lg.exc() return 'sources', 'https://bitdust.io/download/' src = strng.to_bin(bpio.ReadTextFile(settings.RepoFile())).strip() if not src: return settings.DefaultRepo(), settings.DefaultRepoURL( settings.DefaultRepo()) l = src.split('\n') if len(l) < 2: return settings.DefaultRepo(), settings.DefaultRepoURL( settings.DefaultRepo()) return l[0], l[1]
def run(args_list, base_dir=None, callback=None): # TODO: Currently disabled return None global _CurrentProcess if _CurrentProcess is not None: lg.warn('only one process at once') return None if bpio.Windows(): cmdargs = [ 'upnpc-static.exe', ] elif bpio.Linux(): cmdargs = [ 'upnpc', ] elif bpio.Mac(): cmdargs = [ 'upnpc', ] else: return None if bpio.Windows(): # if we run windows binaries - upnpc-static.exe can be in the system sub folder if not os.path.isfile(cmdargs[0]): if os.path.isfile(os.path.join('system', cmdargs[0])): cmdargs[0] = os.path.join('system', cmdargs[0]) else: lg.warn('can not find executable file ' + cmdargs[0]) return None cmdargs += args_list if _Debug: lg.out(_DebugLevel, 'run_upnpc.run is going to execute: %s' % cmdargs) try: out_data, returncode = execute_in_shell(cmdargs) except: lg.exc() return None if _Debug: lg.out( _DebugLevel, ' %s finished with return code: %s' % (str(_CurrentProcess), str(returncode))) _CurrentProcess = None return out_data
def setClipboardText(txt): """ A portable way to set a clipboard data - just like when you select something and press Ctrl-C. """ if bpio.Windows(): try: import win32clipboard # @UnresolvedImport import win32con # @UnresolvedImport win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard() win32clipboard.SetClipboardData(win32con.CF_TEXT, txt) win32clipboard.CloseClipboard() except: lg.exc() elif bpio.Linux(): try: import wx clipdata = wx.TextDataObject() # @UndefinedVariable clipdata.SetText(txt) if wx.TheClipboard: # @UndefinedVariable wx.TheClipboard.Open() # @UndefinedVariable wx.TheClipboard.SetData(clipdata) # @UndefinedVariable wx.TheClipboard.Close() # @UndefinedVariable except: lg.exc() elif bpio.Mac(): try: fd, fname = tempfile.mkstemp() os.write(fd, txt) os.close(fd) os.system('cat %s | pbcopy' % fname) os.remove(fname) except: lg.exc()
def init(UI='', options=None, args=None, overDict=None, executablePath=None): """ In the method ``main()`` program firstly checks the command line arguments and then calls this method to start the whole process. This initialize some low level modules and finally create an instance of ``initializer()`` state machine and send it an event "run". """ global AppDataDir from logs import lg lg.out(4, 'bpmain.run UI="%s"' % UI) from system import bpio #---settings--- from main import settings if overDict: settings.override_dict(overDict) settings.init(AppDataDir) if not options or options.debug is None: lg.set_debug_level(settings.getDebugLevel()) from main import config config.conf().addCallback('logs/debug-level', lambda p, value, o, r: lg.set_debug_level(value)) #---USE_TRAY_ICON--- if os.path.isfile(settings.LocalIdentityFilename()) and os.path.isfile(settings.KeyFileName()): try: from system.tray_icon import USE_TRAY_ICON if bpio.Mac() or not bpio.isGUIpossible(): lg.out(4, ' GUI is not possible') USE_TRAY_ICON = False if USE_TRAY_ICON: from twisted.internet import wxreactor wxreactor.install() lg.out(4, ' wxreactor installed') except: USE_TRAY_ICON = False lg.exc() else: lg.out(4, ' local identity or key file is not ready') USE_TRAY_ICON = False lg.out(4, ' USE_TRAY_ICON=' + str(USE_TRAY_ICON)) if USE_TRAY_ICON: from system import tray_icon icons_path = bpio.portablePath(os.path.join(bpio.getExecutableDir(), 'icons')) lg.out(4, 'bpmain.run call tray_icon.init(%s)' % icons_path) tray_icon.init(icons_path) def _tray_control_func(cmd): if cmd == 'exit': from . import shutdowner shutdowner.A('stop', 'exit') tray_icon.SetControlFunc(_tray_control_func) #---OS Windows init--- if bpio.Windows(): try: from win32event import CreateMutex # @UnresolvedImport mutex = CreateMutex(None, False, "BitDust") lg.out(4, 'bpmain.run created a Mutex: %s' % str(mutex)) except: lg.exc() #---twisted reactor--- lg.out(4, 'bpmain.run want to import twisted.internet.reactor') try: from twisted.internet import reactor # @UnresolvedImport except: lg.exc() sys.exit('Error initializing reactor in bpmain.py\n') #---logfile---- if lg.logs_enabled() and lg.log_file(): lg.out(2, 'bpmain.run want to switch log files') if bpio.Windows() and bpio.isFrozen(): lg.stdout_stop_redirecting() lg.close_log_file() lg.open_log_file(settings.MainLogFilename()) # lg.open_log_file(settings.MainLogFilename() + '-' + time.strftime('%y%m%d%H%M%S') + '.log') if bpio.Windows() and bpio.isFrozen(): lg.stdout_start_redirecting() #---memdebug--- # if settings.uconfig('logs.memdebug-enable') == 'True': # try: # from logs import memdebug # memdebug_port = int(settings.uconfig('logs.memdebug-port')) # memdebug.start(memdebug_port) # reactor.addSystemEventTrigger('before', 'shutdown', memdebug.stop) # lg.out(2, 'bpmain.run memdebug web server started on port %d' % memdebug_port) # except: # lg.exc() #---process ID--- try: pid = os.getpid() pid_file_path = os.path.join(settings.MetaDataDir(), 'processid') bpio.WriteTextFile(pid_file_path, str(pid)) lg.out(2, 'bpmain.run wrote process id [%s] in the file %s' % (str(pid), pid_file_path)) except: lg.exc() # #---reactor.callLater patch--- # if lg.is_debug(12): # patchReactorCallLater(reactor) # monitorDelayedCalls(reactor) # #---plugins--- # from plugins import plug # plug.init() # reactor.addSystemEventTrigger('before', 'shutdown', plug.shutdown) lg.out(2, " python executable is: %s" % sys.executable) lg.out(2, " python version is:\n%s" % sys.version) lg.out(2, " python sys.path is:\n %s" % ('\n '.join(sys.path))) lg.out(2, "bpmain.run UI=[%s]" % UI) if lg.is_debug(20): lg.out(0, '\n' + bpio.osinfofull()) lg.out(4, 'import automats') #---START!--- from automats import automat automat.LifeBegins(lg.when_life_begins()) automat.OpenLogFile(settings.AutomatsLog()) from main import events events.init() from main import initializer IA = initializer.A() lg.out(4, 'sending event "run" to initializer()') reactor.callWhenRunning(IA.automat, 'run', UI) # @UndefinedVariable return IA
def renderWizardStoragePage(self, request): template = 'pages/wizard_storage.html' req = {} if request is not None: req = request.REQUEST self.data['customersdir'] = unicode(req.get('customersdir', settings.getCustomersFilesDir())) self.data['localbackupsdir'] = unicode(req.get('localbackupsdir', settings.getLocalBackupsDir())) self.data['restoredir'] = unicode(req.get('restoredir', settings.getRestoreDir())) self.data['needed'] = req.get('needed', self.data['needed']) neededV = diskspace.GetBytesFromString(self.data['needed'] + ' Mb', settings.DefaultNeededBytes()) self.data['donated'] = req.get('donated', self.data['donated']) donatedV = diskspace.GetBytesFromString(self.data['donated'] + ' Mb', settings.DefaultDonatedBytes()) self.data['suppliers'] = req.get('suppliers', self.data['suppliers']) mounts = [] freeSpaceIsOk = True if bpio.Windows(): for d in bpio.listLocalDrivesWindows(): free, total = diskusage.GetWinDriveSpace(d[0]) if free is None or total is None: continue color = '#ffffff' if self.data['customersdir'][0].upper() == d[0].upper(): color = '#60e060' if donatedV >= free: color = '#e06060' freeSpaceIsOk = False if self.data['localbackupsdir'][0].upper() == d[0].upper(): color = '#60e060' if neededV >= free: color = '#e06060' freeSpaceIsOk = False mounts.append((d[0:2], diskspace.MakeStringFromBytes(free), diskspace.MakeStringFromBytes(total), color,)) elif bpio.Linux() or bpio.Mac(): for mnt in bpio.listMountPointsLinux(): free, total = diskusage.GetLinuxDriveSpace(mnt) if free is None or total is None: continue color = '#ffffff' if bpio.getMountPointLinux(self.data['customersdir']) == mnt: color = '#60e060' if donatedV >= free: color = '#e06060' freeSpaceIsOk = False if bpio.getMountPointLinux(self.data['localbackupsdir']) == mnt: color = '#60e060' if neededV >= free: color = '#e06060' freeSpaceIsOk = False mounts.append((mnt, diskspace.MakeStringFromBytes(free), diskspace.MakeStringFromBytes(total), color,)) ok = True out = '' if not freeSpaceIsOk: out += '<font color=red>you do not have enough free space on the disk</font><br/>\n' ok = False if donatedV < settings.MinimumDonatedBytes(): out += '<font color=red>you must donate at least %f MB</font><br/>\n' % ( round(settings.MinimumDonatedBytes() / (1024.0 * 1024.0), 2)) ok = False if not os.path.isdir(self.data['customersdir']): out += '<font color=red>directory %s not exist</font><br/>\n' % self.data['customersdir'] ok = False if not os.access(self.data['customersdir'], os.W_OK): out += '<font color=red>folder %s does not have write permissions</font><br/>\n' % self.data['customersdir'] ok = False if not os.path.isdir(self.data['localbackupsdir']): out += '<font color=red>directory %s not exist</font><br/>\n' % self.data['localbackupsdir'] ok = False if not os.access(self.data['localbackupsdir'], os.W_OK): out += '<font color=red>folder %s does not have write permissions</font><br/>\n' % self.data['localbackupsdir'] ok = False if int(self.data['suppliers']) not in settings.getECCSuppliersNumbers(): out += '<font color=red>incorrect number of suppliers, correct values are: %s</font><br/>\n' % ( str(settings.getECCSuppliersNumbers()).strip('[]')) ok = False context = {'output': out, 'mounts': mounts, 'needed': self.data['needed'], 'donated': self.data['donated'], 'localbackupsdir': self.data['localbackupsdir'], 'customersdir': self.data['customersdir'], 'restoredir': self.data['restoredir'], 'suppliers': self.data['suppliers'], } if request is None: return template, context, request action = request.REQUEST.get('action', None) if action == 'next': if ok: install_wizard.A(action, self.data) return None if action == 'back': install_wizard.A(action) return None return template, context, request