diff --git a/common.py b/common.py index 8dbb71f..7c854b6 100644 --- a/common.py +++ b/common.py @@ -6,11 +6,24 @@ import ConfigParser import logging import os +import re import socket import uuid +CAMERA_CONFIG = { + 'SAMSUNG WB150': {'desc_file': os.path.join('DLNA_WEB_ROOT', + 'SAMSUNGAUTOBACKUPDESC.INI')}, + 'SAMSUNG NX1000': {'desc_file': os.path.join('dlna_web_root', + 'SAMSUNGAutoBackupDESC.ini')}} CONFIG_FILE = os.path.expanduser("~/pc_autobackup.cfg") -DESC_FILE = os.path.join('DLNA_WEB_ROOT', 'SAMSUNGAUTOBACKUPDESC.INI') +CAMERA_INFO_FILE = [os.path.join('system', 'device.xml'), + os.path.join('SYSTEM', 'DEVICE.XML'), + os.path.join('SYSTEM', 'Device.xml')] +CAMERA_MODEL = re.compile(r'') + +DESC_SERVER_NAME = re.compile(r'friendlyName\s*=\s*(.*)') +DESC_UUID = re.compile(r'UDN\s*=\s*uuid:(.*)') + DESC_INI = '''MacAddr=%(mac_address)s UDN=uuid:%(uuid)s friendlyName=%(server_name)s diff --git a/mediaserver.py b/mediaserver.py index fec2480..68fb0b9 100644 --- a/mediaserver.py +++ b/mediaserver.py @@ -208,6 +208,8 @@ obj_id = backup.CreateObject(obj_name, obj_date, obj_type, obj_size) obj_details = backup.GetObjectDetails(obj_id) + self.logger.info('Ready to receive %s (%s size:%s)', obj_name, obj_type, + obj_size) response = CREATE_OBJ_RESPONSE % { 'interface': self.config.get('AUTOBACKUP', 'default_interface'), 'obj_id': obj_id, diff --git a/pc_autobackup.py b/pc_autobackup.py index f194dc0..eb3b4e9 100755 --- a/pc_autobackup.py +++ b/pc_autobackup.py @@ -22,6 +22,32 @@ import mediaserver +def GetCameraConfig(mountpoint): + logger = logging.getLogger('PCAutoBackup') + + device_file = None + for f in common.CAMERA_INFO_FILE: + if os.path.isfile(os.path.join(mountpoint, f)): + device_file = os.path.join(mountpoint, f) + break + + if device_file: + if os.path.isfile(device_file): + with open(device_file, 'r') as f: + device_xml = f.read() + m = common.CAMERA_MODEL.search(device_xml) + if m: + camera_config = common.CAMERA_CONFIG.get(m.group(1)) + if camera_config: + return camera_config + else: + logger.error('Unsupported camera: %s', m.group(1)) + sys.exit(1) + + logger.error('Unable to determine camera model') + sys.exit(1) + + def GetSystemInfo(): logger = logging.getLogger('PCAutoBackup') logger.debug('Command-line: %s', ' '.join(sys.argv)) @@ -38,13 +64,59 @@ config.get(section, option)) +def ImportCameraConfig(mountpoint): + logger = logging.getLogger('PCAutoBackup') + + camera_config = GetCameraConfig(mountpoint) + desc_file = os.path.join(mountpoint, camera_config['desc_file']) + + if os.path.isfile(desc_file): + with open(desc_file, 'r') as f: + desc_data = f.read() + logger.info('Loading configuration from camera') + config = common.LoadOrCreateConfig() + + m = common.DESC_SERVER_NAME.search(desc_data) + if m: + friendly_name = m.group(1) + else: + logging.error('Unable to determine server name from camera config') + sys.exit(1) + + m = common.DESC_UUID.search(desc_data) + if m: + uuid = m.group(1) + else: + logging.error('Unable to determine server name from camera config') + sys.exit(1) + + config.set('AUTOBACKUP', 'server_name', friendly_name) + config.set('AUTOBACKUP', 'uuid', uuid) + with open(common.CONFIG_FILE, 'wb') as config_file: + logger.info('Saving server configuration') + try: + config.write(config_file) + logger.info('Configuration saved successfully') + except IOError as e: + logger.error('Unable to save configuration: %s', str(e)) + sys.exit(1) + logger.info('Updating camera configuration') + UpdateCameraConfig(mountpoint) + logger.info('IMPORTANT: Disable PC AutoBackup on your Windows server!') + else: + logger.error('Camera configuration %s does not exist!', desc_file) + sys.exit(1) + + def UpdateCameraConfig(mountpoint, create_desc_file=False): logger = logging.getLogger('PCAutoBackup') + mac_address = hex(uuid.getnode()) mac_address = re.findall('..', mac_address) mac_address = ':'.join(mac_address[1:]).upper() - desc_file = os.path.join(mountpoint, common.DESC_FILE) + camera_config = GetCameraConfig(mountpoint) + desc_file = os.path.join(mountpoint, camera_config['desc_file']) if create_desc_file: with open(desc_file, 'w+') as f: @@ -75,6 +147,9 @@ metavar='MOUNTPOINT') parser.add_option('-d', '--debug', dest='debug', action='store_true', default=False, help='enable debug logging to file') + parser.add_option('--import_camera_config', dest='import_camera_config', + help='update server with cameras configuration', + metavar='MOUNTPOINT') parser.add_option('--log_file', dest='log_file', default='backup.log', help='change output log file (default: backup.log)', metavar='FILE') @@ -128,6 +203,10 @@ UpdateCameraConfig(options.create_camera_config, create_desc_file=True) sys.exit(0) + if options.import_camera_config: + ImportCameraConfig(options.import_camera_config) + sys.exit(0) + if options.update_camera: UpdateCameraConfig(options.update_camera) sys.exit(0)