def setUp(self): try: bpio.rmdir_recursive('/tmp/.bitdust_tmp') except Exception: pass lg.set_debug_level(30) settings.init(base_dir='/tmp/.bitdust_tmp') try: os.makedirs('/tmp/.bitdust_tmp/metadata') except: pass automat.OpenLogFile('/tmp/.bitdust_tmp/logs/automats.log') self.my_current_key = None fout = open('/tmp/_some_priv_key', 'w') fout.write(_some_priv_key) fout.close() fout = open(settings.LocalIdentityFilename(), 'w') fout.write(_some_identity_xml) fout.close() self.assertTrue(key.LoadMyKey(keyfilename='/tmp/_some_priv_key')) self.assertTrue(my_id.loadLocalIdentity()) my_id.init() try: os.makedirs('/tmp/.bitdust_tmp/logs') except: pass local_fs.WriteTextFile('/tmp/.bitdust_tmp/logs/parallelp.log', '') tmpfile.init(temp_dir_path='/tmp/.bitdust_tmp/temp/') os.makedirs( '/tmp/.bitdust_tmp/backups/[email protected]_8084/1/F1234') try: bpio.rmdir_recursive('/tmp/_some_folder', ignore_errors=True) except: pass os.makedirs('/tmp/_some_folder')
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 main(): from system import bpio from storage import backup_tar bpio.init() settings.init() lg.set_debug_level(24) lg.life_begins() automat.LifeBegins(lg.when_life_begins()) automat.OpenLogFile(settings.AutomatsLog()) key.InitMyKey() my_id.init() sourcePath = sys.argv[1] compress_mode = 'none' # 'gz' backupID = sys.argv[2] raid_worker.A('init') if bpio.pathIsDir(sourcePath): backupPipe = backup_tar.backuptardir_thread(sourcePath, compress=compress_mode) else: backupPipe = backup_tar.backuptarfile_thread(sourcePath, compress=compress_mode) def _bk_done(bid, result): from crypt import signed customer, remotePath = packetid.SplitPacketID(bid) try: os.mkdir( os.path.join(settings.getLocalBackupsDir(), customer, remotePath + '.out')) except: pass for filename in os.listdir( os.path.join(settings.getLocalBackupsDir(), customer, remotePath)): filepath = os.path.join(settings.getLocalBackupsDir(), customer, remotePath, filename) payld = bpio.ReadBinaryFile(filepath) newpacket = signed.Packet('Data', my_id.getLocalID(), my_id.getLocalID(), filename, payld, 'http://megafaq.ru/cvps1010.xml') newfilepath = os.path.join(settings.getLocalBackupsDir(), customer, remotePath + '.out', filename) bpio.WriteBinaryFile(newfilepath, newpacket.Serialize()) def _bk_closed(*args, **kwargs): # job.automat('fail') # del job reactor.stop() # @UndefinedVariable def _bk_start(): job = backup(backupID, backupPipe, blockSize=16 * 1024 * 1024) job.finishCallback = _bk_done # lambda bid, result: _bk_done(bid, result, job) job.addStateChangedCallback(_bk_closed, oldstate=None, newstate='DONE') reactor.callLater(1, job.automat, 'start') # @UndefinedVariable reactor.callLater(0, _bk_start) # @UndefinedVariable reactor.run() # @UndefinedVariable settings.shutdown()