Example #1
0
 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
Example #2
0
 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"
Example #3
0
import os
import shelve
import datetime
import cerebratesinfo
import communication
import asyncio
import traceback
from utilities import run_coroutine
from definitions import Resource
from mysysteminfo import get_hive_directory, get_mac_address
from abc import ABC, abstractmethod

RESOURCES_BASE_LOCATION = os.path.join(get_hive_directory(), "resources")
RESOURCE_FILE_EXTENSION = "res"

FOLDER_SEPARATOR = ':|:'

MODIFIED_TIME = "res_modified_time"
RESOURCE_VALUE = "res_value"

initialized = False


class Resource_BC(ABC):
    '''Base class for resource classes.
	'''
    @abstractmethod
    def update(self, resource):
        '''Updates this resource instance with the given resource instance.
		If given resource instance class does not match this one an EnvironmentError will be raised.
		'''
Example #4
0
import os
import enum
import asyncio
import mysysteminfo
import shelve

CEREBRATE_CONFIG_PATH = os.path.join(mysysteminfo.get_hive_directory(),
                                     'cerebrate_config.db')


class Config_Keys(enum.Enum):
    debug_in_effect = "debug"


my_version = 0.46

COMMAND = "cmd"
REMOTE_COMMAND = "remotecmd"
FILE_TRANSFER = "filetransfer"
CLOSE_CONNECTION = "close connection"

RECIPROCATE = "reciprocate"
OVERRULE = "overrule"

SUCCESS = "success"
READY = "ready"
FINISHED = "finished"

LOCATION = "location"
NAME = "name"
Example #5
0
import collections
import os
import shelve
import json
import enum
import datetime
import logging
import mysysteminfo
from utilities import dprint

CEREBRATE_RECORDS = os.path.join(mysysteminfo.get_hive_directory(),
                                 'cerebrates.db')

MY_RECORD_DEFAULT_NAME = 'myinfo'


class Record(enum.Enum):
    NAME = enum.auto()
    MAC = enum.auto()
    IP = enum.auto()
    LOCATION = enum.auto()
    ROLE = enum.auto()
    STATUS = enum.auto()
    LASTCONTACT = enum.auto()


class Status(enum.Enum):
    AWAKE = enum.auto()
    UNKNOWN = enum.auto()
    ASLEEP = enum.auto()