diff --git a/common.py b/common.py index 55c71c7..8dbb71f 100644 --- a/common.py +++ b/common.py @@ -10,9 +10,16 @@ import uuid CONFIG_FILE = os.path.expanduser("~/pc_autobackup.cfg") +DESC_FILE = os.path.join('DLNA_WEB_ROOT', 'SAMSUNGAUTOBACKUPDESC.INI') +DESC_INI = '''MacAddr=%(mac_address)s +UDN=uuid:%(uuid)s +friendlyName=%(server_name)s +WOLSupport=1 +ServerFlag=1 +''' + LOG_DATE_FMT = '[%m/%d/%Y %I:%M %p]' LOG_FMT = '%(asctime)s[%(name)s] %(levelname)s:%(message)s' - LOG_DEFAULTS = {'level': logging.INFO, 'format': LOG_FMT, 'datefmt': LOG_DATE_FMT} diff --git a/pc_autobackup.py b/pc_autobackup.py index 550f6e3..1289c75 100755 --- a/pc_autobackup.py +++ b/pc_autobackup.py @@ -7,9 +7,12 @@ import logging import optparse +import os import platform +import re import socket import sys +import uuid from twisted.internet import reactor from twisted.web.server import Site @@ -35,6 +38,28 @@ config.get(section, option)) +def UpdateCameraConfig(mountpoint): + 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) + if os.path.isfile(desc_file): + with open(desc_file, 'wb') as f: + config = common.LoadOrCreateConfig() + ini_params = {'mac_address': mac_address, + 'server_name': config.get('AUTOBACKUP', 'server_name'), + 'uuid': config.get('AUTOBACKUP', 'uuid')} + try: + f.write(common.DESC_INI % ini_params) + logger.info('Configuration saved successfully') + except IOError as e: + logger.error('Unable to save configuration: %s', str(e)) + else: + logger.error('Camera configuration %s does not exist!') + + def main(): parser = optparse.OptionParser() parser.add_option('-b', '--bind', dest='bind', @@ -51,6 +76,9 @@ help='output directory for files', metavar='DIR') parser.add_option('-q', '--quiet', dest='quiet', action='store_true', default=False, help='only log errors to console') + parser.add_option('--update_camera', dest='update_camera', + help='update camera with this servers configuration', + metavar='MOUNTPOINT') (options, args) = parser.parse_args() console_logging_options = common.LOG_DEFAULTS.copy() @@ -88,6 +116,10 @@ with open(common.CONFIG_FILE, 'wb') as config_file: config.write(config_file) + if options.update_camera: + UpdateCameraConfig(options.update_camera) + sys.exit(0) + logger = logging.getLogger('PCAutoBackup') logger.info('PCAutoBackup started on %s', config.get('AUTOBACKUP', 'default_interface'))