def _start_browser(lock=_browser_lock): global _browser _quit_browser(lock=lock) with lock: if cc.feedback_on_commands(): feedback("Opening a new browser, just a moment...") _browser = Firefox(executable_path=os.path.join(get_my_directory(), "drivers", "geckodriver.exe"))
async def __transfer_file(reader, writer, file_name): #Determine file location filename = file_name location = os.path.dirname(file_name) if mysysteminfo.get_my_directory() in location: location = cc.FileLocation.SOURCE filename = filename.replace(mysysteminfo.get_my_directory(), '', 1) elif mysysteminfo.get_hive_directory() in location: location = cc.FileLocation.HIVE filename = filename.replace(mysysteminfo.get_hive_directory(), '', 1) else: location = cc.FileLocation.ABSOLUTE #Send header header = Message("file header", data={cc.LOCATION: location, cc.NAME: filename}) await Secretary._write_message(writer=writer, msg=header) #Send file with open(file_name, 'r') as open_file: data = open_file.read(512) while data: response = await Secretary._read_message(reader=reader) if response == cc.CLOSE_CONNECTION: break elif response != cc.READY: #wait for ready continue await Secretary._write_message(writer=writer, msg=data) data = open_file.read(512) #One last wait for READY response response = "" while response != cc.READY: response = await Secretary._read_message(reader=reader) if response == cc.CLOSE_CONNECTION: return False #Send 'eof' await Secretary._write_message(writer=writer, msg=cc.FINISHED) return True
async def get_cerebrate_file_names(): '''Returns a list of the names of files involved in cerebrate's execution. ''' file_names = [] source_directory = mysysteminfo.get_my_directory() for dirname, subdirnames, filenames in os.walk(source_directory): subdirnames = [ subdir for subdir in subdirnames if 'backup' not in subdir ] for filename in filenames: if 'backup' in filename: continue extension = os.path.splitext(filename)[1] if extension in CEREBRATE_FILE_EXTENSIONS: file_names.append(os.path.join(dirname, filename)) return file_names
async def __receive_files(reader, writer): data = None while not Secretary.terminating: await Secretary._write_message(writer=writer, msg=cc.READY) msg = await Secretary._read_message(reader=reader) if cc.CLOSE_CONNECTION in msg.header: return BY_REQUEST #Get proper directory to save this file to location = msg.data.get(cc.LOCATION, cc.FileLocation.ABSOLUTE) if location == cc.FileLocation.SOURCE: location = mysysteminfo.get_my_directory() elif location == cc.FileLocation.HIVE: location = mysysteminfo.get_hive_directory() else: location = "" filename = msg.data.get(cc.NAME, None) if not filename: return "no cc.NAME included" file_path = os.path.join(location, filename) #backup file we're overwriting, just in case utilities.backup_file(file_path=file_path) try: with open(file_path, 'w+') as new_file: await Secretary._write_message(writer=writer, msg=cc.READY) data = await Secretary._read_message(reader=reader) while data != cc.FINISHED: new_file.write(data) await Secretary._write_message(writer=writer, msg=cc.READY) data = await Secretary._read_message(reader=reader) except Exception as ex: logging.error(ex) traceback.print_exc() utilities.restore_file(file_path=file_path) return "failed while receiving/writing file" return "secretary terminating"
import os import sys import enum import functools import traceback from importlib import import_module, util from utilities import aprint, dprint, intersect_strings from definitions import Command from communication import distill_msg from commands import emit, info, system, web, media_player import cerebrate_config as cc, mysysteminfo COMMANDS_DIRECTORY = os.path.join(mysysteminfo.get_my_directory(), "commands") command_modules = [] async def _shutdown_(msg): return "'shutdown' not implemented yet" async def _shut_down_(msg): return _shutdown_(msg) async def _start_(msg): return "'start' not implemented yet" async def _stop_(msg): return "'stop' not implemented yet"