class BEConnection( object ): """ This is the basic backend connection object. You probably don't want to use this directly. """ logmodule = 'Python Backend Connection' def __init__(self, backend, port, localname=None, blockshutdown=False, timeout=10.0): self.connected = False self.log = MythLog(self.logmodule) self._socklock = allocate_lock() self.host = backend self.port = port self.hostname = None self.deadline = timeout self.blockshutdown = blockshutdown self.localname = localname if self.localname is None: self.localname = socket.gethostname() try: self.connect() except socket.error, e: self.log.logTB(MythLog.SOCKET) self.connected = False self.log(MythLog.GENERAL, MythLog.CRIT, "Couldn't connect to backend [%s]:%d" \ % (self.host, self.port)) raise MythBEError(MythError.PROTO_CONNECTION, self.host, self.port) except:
def __init__(self, backend, port, localname=None, blockshutdown=False, timeout=10.0): self.connected = False self.log = MythLog(self.logmodule) self._socklock = allocate_lock() self.host = backend self.port = port self.hostname = None self.deadline = timeout self.blockshutdown = blockshutdown self.localname = localname if self.localname is None: self.localname = socket.gethostname() try: self.connect() except socket.error as e: self.log.logTB(MythLog.SOCKET) self.connected = False self.log(MythLog.GENERAL, MythLog.CRIT, "Couldn't connect to backend [%s]:%d" \ % (self.host, self.port)) raise MythBEError(MythError.PROTO_CONNECTION, self.host, self.port) except: raise
def __init__(self, backend, port, localname=None, blockshutdown=False, timeout=10.0): self.connected = False self.log = MythLog(self.logmodule) self._socklock = allocate_lock() self.host = backend self.port = port self.hostname = None self.deadline = timeout self.blockshutdown = blockshutdown self.localname = localname if self.localname is None: self.localname = socket.gethostname() try: self.connect() except socket.error, e: self.log.logTB(MythLog.SOCKET) self.connected = False self.log(MythLog.GENERAL, MythLog.CRIT, "Couldn't connect to backend [%s]:%d" \ % (self.host, self.port)) raise MythBEError(MythError.PROTO_CONNECTION, self.host, self.port)
def __init__(self, backend=None, port=None, db=None): if backend and port: XMLConnection.__init__(self, backend, port) self.db = db return self.db = DBCache(db) self.log = MythLog('Python XML Connection') if backend is None: # use master backend backend = self.db.getMasterBackend() # assume hostname from settings host = self.db._getpreferredaddr(backend) if host: port = int(self.db.settings[backend].BackendStatusPort) else: # assume ip address hostname = self.db._gethostfromaddr(backend) host = backend port = int(self.db.settings[hostname].BackendStatusPort) # resolve ip address from name reshost, resport = resolve_ip(host, port) if not reshost: raise MythDBError(MythError.DB_SETTING, backend + ': BackendServerAddr') if not resport: raise MythDBError(MythError.DB_SETTING, backend + ': BackendStatusPort') self.host = host self.port = port
def formatJob(self, cmd): """ Program.formatPath(cmd) -> formatted command string 'cmd' string is formatted as per MythJobQueue syntax """ for tag in ('chanid','title','subtitle','description','hostname', 'category','recgroup','playgroup','parentid','findid', 'recstatus','rectype'): cmd = cmd.replace('%%%s%%' % tag.upper(), str(self[tag])) cmd = cmd.replace('%ORIGINALAIRDATE%', self.airdate.isoformat()) for (tag, data) in (('STARTTIME','recstartts'),('ENDTIME','recendts'), ('PROGSTART','starttime'),('PROGEND','endtime')): t = self[data] cmd = cmd.replace('%%%s%%' % tag, t.mythformat()) cmd = cmd.replace('%%%sISO%%' % tag, t.isoformat()) cmd = cmd.replace('%%%sISOUTC%%' % tag, \ (t+timedelta(0,altzone)).isoformat()) cmd = cmd.replace('%VERBOSELEVEL%', MythLog._parsemask()) cmd = cmd.replace('%RECID%', str(self.recordid)) path = FileOps(self.hostname, db=self._db).fileExists(\ self.filename.rsplit('/',1)[1], self.storagegroup) cmd = cmd.replace('%DIR%', path.rsplit('/',1)[0]) cmd = cmd.replace('%FILE%',path.rsplit('/',1)[1]) cmd = cmd.replace('%REACTIVATE%', str(OldRecorded(\ (self.chanid, self.recstartts),db=self._db).reactivate)) return cmd
def __init__(self, backend=None, port=None, db=None): if backend and port: XMLConnection.__init__(self, backend, port) self.db = db return self.db = DBCache(db) self.log = MythLog('Python XML Connection') if backend is None: # use master backend backend = self.db.settings.NULL.MasterServerIP if re.match('(?:\d{1,3}\.){3}\d{1,3}',backend) or \ check_ipv6(backend): # process ip address host = self.db._gethostfromaddr(backend) self.host = backend self.port = int(self.db.settings[host].BackendStatusPort) else: # assume given a hostname self.host = backend self.port = int(self.db.settings[self.host].BackendStatusPort) if not self.port: # try a truncated hostname self.host = backend.split('.')[0] self.port = int(self.db.setting[self.host].BackendStatusPort) if not self.port: raise MythDBError(MythError.DB_SETTING, backend + ': BackendStatusPort')
def __init__(self, host, port, deadline=10.0, test=True): self.isConnected = False self.host = host self.port = int(port) self.log = MythLog('Python Frontend Connection') self._deadline = deadline self.connect(test)
def formatJob(self, cmd): """ Program.formatPath(cmd) -> formatted command string 'cmd' string is formatted as per MythJobQueue syntax """ for tag in ('chanid', 'title', 'subtitle', 'description', 'hostname', 'category', 'recgroup', 'playgroup', 'parentid', 'findid', 'recstatus', 'rectype'): cmd = cmd.replace('%%%s%%' % tag.upper(), str(self[tag])) cmd = cmd.replace('%ORIGINALAIRDATE%', self.airdate.isoformat()) for (tag, data) in (('STARTTIME', 'recstartts'), ('ENDTIME', 'recendts'), ('PROGSTART', 'starttime'), ('PROGEND', 'endtime')): t = self[data] cmd = cmd.replace('%%%s%%' % tag, t.mythformat()) cmd = cmd.replace('%%%sISO%%' % tag, t.isoformat()) cmd = cmd.replace('%%%sISOUTC%%' % tag, \ (t+timedelta(0,altzone)).isoformat()) cmd = cmd.replace('%VERBOSELEVEL%', MythLog._parsemask()) cmd = cmd.replace('%RECID%', str(self.recordid)) path = FileOps(self.hostname, db=self._db).fileExists(\ self.filename.rsplit('/',1)[1], self.storagegroup) cmd = cmd.replace('%DIR%', path.rsplit('/', 1)[0]) cmd = cmd.replace('%FILE%', path.rsplit('/', 1)[1]) cmd = cmd.replace('%REACTIVATE%', str(OldRecorded(\ (self.chanid, self.recstartts),db=self._db).reactivate)) return cmd
def __init__(self, dbconn): self.log = MythLog('Python Database Connection') self.tablefields = None self.settings = None self.dbconn = dbconn self._refs = {} self.log(MythLog.DATABASE, MythLog.INFO, "Attempting connection: {0}".format(dbconn.ident)) try: _Connection_Pool.__init__(self) except: raise MythDBError(MythError.DB_CONNECTION, dbconn)
def __init__(self, path=None, setting=None, db=None, useshell=True, prefix=''): DBCache.__init__(self, db=db) self.log = MythLog(self.logmodule, db=self) self.path = None if setting is not None: # pull local setting from database host = self.gethostname() self.path = self.settings[host][setting] if self.path is None: # see if that setting is applied globally self.path = self.settings['NULL'][setting] if self.path is None: # not set globally either, use supplied default self.path = path if self.path is None: # no default supplied, just error out raise MythDBError(MythError.DB_SETTING, setting, host) if self.path is None: # setting not given, use path from argument if path is None: raise MythError('Invalid input to System()') self.path = path if prefix: self.path = os.path.join(prefix, self.path) cmd = self.path.split()[0] if self.path.startswith('/'): # test full given path if not os.access(cmd, os.F_OK): raise MythFileError('Defined executable path does not exist.') else: # search command from PATH for folder in os.environ['PATH'].split(':'): if os.access(os.path.join(folder, cmd), os.F_OK): self.path = os.path.join(folder, self.path) break else: raise MythFileError('Defined executable path does not exist.') self.returncode = 0 self.stderr = '' self.useshell = useshell
def __init__(self): self.log = MythLog('Python M-Search') port = 1900 addr = '239.255.255.250' self.dest = (addr, port) self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) listening = False while listening == False: try: self.sock.bind(('', port)) self.addr = (addr, port) listening = True except socket.error, e: if port < 1910: port += 1 else: raise MythError(MythError.SOCKET, e)
def __init__(self, *args, **kwargs): socket.socket.__init__(self, *args, **kwargs) self.log = MythLog('Python Socket') self.setdeadline(10.0)
def __init__(self, db): self.db = db self.log = MythLog('Schema Update (%s)' % self._schema_name)
def __init__(self, backend=None, blockshutdown=False, events=False, db=None): self.db = DBCache(db) self.log = MythLog(self.logmodule, db=self.db) self.hostname = None self.sendcommands = True self.blockshutdown = blockshutdown self.receiveevents = events if backend is None: # no backend given, use master self.host = self.db.settings.NULL.MasterServerIP self.hostname = self.db._gethostfromaddr(self.host) else: backend = backend.strip('[]') query = "SELECT hostname FROM settings WHERE value=? AND data=?" if self._reip.match(backend): # given backend is IP address self.host = backend self.hostname = self.db._gethostfromaddr( backend, 'BackendServerIP') elif check_ipv6(backend): # given backend is IPv6 address self.host = backend self.hostname = self.db._gethostfromaddr( backend, 'BackendServerIP6') else: # given backend is hostname, pull address from database self.hostname = backend self.host = self.db._getpreferredaddr(backend) # lookup port from database self.port = int(self.db.settings[self.hostname].BackendServerPort) if not self.port: raise MythDBError(MythError.DB_SETTING, 'BackendServerPort', self.port) self._ident = '%s:%d' % (self.host, self.port) if self._ident in self._shared: # existing connection found self._conn = self._shared[self._ident] if self.sendcommands: if self._conn.command is None: self._conn.command = self._newcmdconn() elif self.blockshutdown: # upref block of shutdown # issue command to backend if needed self._conn.blockshutdown += 1 if self._conn.blockshutdown == 1: self._conn.command.blockShutdown() if self.receiveevents: if self._conn.event is None: self._conn.event = self._neweventconn() else: # no existing connection, create new self._conn = self._ConnHolder() if self.sendcommands: self._conn.command = self._newcmdconn() if self.blockshutdown: self._conn.blockshutdown = 1 if self.receiveevents: self._conn.event = self._neweventconn() self._shared[self._ident] = self._conn self._events = self._listhandlers() for func in self._events: self.registerevent(func)
def ftopen(file, mode, forceremote=False, nooverwrite=False, db=None, \ chanid=None, starttime=None, download=False): """ ftopen(file, mode, forceremote=False, nooverwrite=False, db=None) -> FileTransfer object -> file object Method will attempt to open file locally, falling back to remote access over mythprotocol if necessary. 'forceremote' will force a FileTransfer object if possible. 'file' takes a standard MythURI: myth://<group>@<host>:<port>/<path> 'mode' takes a 'r' or 'w' 'nooverwrite' will refuse to open a file writable, if a local file is found. """ db = DBCache(db) log = MythLog('Python File Transfer', db=db) reuri = re.compile(\ 'myth://((?P<group>.*)@)?(?P<host>[\[\]a-zA-Z0-9_\-\.]*)(:[0-9]*)?/(?P<file>.*)') reip = re.compile('(?:\d{1,3}\.){3}\d{1,3}') if mode not in ('r', 'w'): raise TypeError("File I/O must be of type 'r' or 'w'") if chanid and starttime: protoopen = lambda host, file, storagegroup: \ RecordFileTransfer(host, file, storagegroup,\ mode, chanid, starttime, db) elif download: protoopen = lambda host, lfile, storagegroup: \ DownloadFileTransfer(host, lfile, storagegroup, \ mode, file, db) else: protoopen = lambda host, file, storagegroup: \ FileTransfer(host, file, storagegroup, mode, db) # process URI (myth://<group>@<host>[:<port>]/<path/to/file>) match = reuri.match(file) if match is None: raise MythError('Invalid FileTransfer input string: ' + file) host = match.group('host') filename = match.group('file') sgroup = match.group('group') if sgroup is None: sgroup = 'Default' # get full system name host = host.strip('[]') if reip.match(host) or check_ipv6(host): host = db._gethostfromaddr(host) # user forced to remote access if forceremote: if (mode == 'w') and (filename.find('/') != -1): raise MythFileError(MythError.FILE_FAILED_WRITE, file, 'attempting remote write outside base path') if nooverwrite and FileOps(host, db=db).fileExists(filename, sgroup): raise MythFileError(MythError.FILE_FAILED_WRITE, file, 'refusing to overwrite existing file') return protoopen(host, filename, sgroup) if mode == 'w': # check for pre-existing file path = FileOps(host, db=db).fileExists(filename, sgroup) sgs = list(db.getStorageGroup(groupname=sgroup)) if path is not None: if nooverwrite: raise MythFileError(MythError.FILE_FAILED_WRITE, file, 'refusing to overwrite existing file') for sg in sgs: if sg.dirname in path: if sg.local: return open(sg.dirname + filename, mode) else: return protoopen(host, filename, sgroup) # prefer local storage for new files for i, v in reversed(list(enumerate(sgs))): if not v.local: sgs.pop(i) else: st = os.statvfs(v.dirname) v.free = st[0] * st[3] if len(sgs) > 0: # choose path with most free space sg = sorted(sgs, key=lambda sg: sg.free, reverse=True)[0] # create folder if it does not exist if filename.find('/') != -1: path = sg.dirname + filename.rsplit('/', 1)[0] if not os.access(path, os.F_OK): os.makedirs(path) log(log.FILE, log.INFO, 'Opening local file (w)', sg.dirname + filename) return open(sg.dirname + filename, mode) # fallback to remote write else: if filename.find('/') != -1: raise MythFileError( MythError.FILE_FAILED_WRITE, file, 'attempting remote write outside base path') return protoopen(host, filename, sgroup) else: # search for file in local directories sg = findfile(filename, sgroup, db) if sg is not None: # file found, open local log(log.FILE, log.INFO, 'Opening local file (r)', sg.dirname + filename) return open(sg.dirname + filename, mode) else: # file not found, open remote return protoopen(host, filename, sgroup)
def __init__(self, host, port): self.host, self.port = host, int(port) self.log = MythLog('Python XML Connection')
def __init__(self, backend=None, blockshutdown=False, events=False, db=None): self.db = DBCache(db) self.log = MythLog(self.logmodule, db=self.db) self.hostname = None self.sendcommands = True self.blockshutdown = blockshutdown self.receiveevents = events if backend is None: # use master backend backend = self.db.getMasterBackend() else: backend = backend.strip('[]') # assume backend is hostname from settings host = self.db._getpreferredaddr(backend) if host: port = int(self.db.settings[backend].BackendServerPort) self.hostname = backend else: # assume ip address self.hostname = self.db._gethostfromaddr(backend) host = backend port = int(self.db.settings[self.hostname].BackendServerPort) # resolve ip address from name reshost, resport = resolve_ip(host,port) if not reshost: raise MythDBError(MythError.DB_SETTING, backend+': BackendServerAddr') if not resport: raise MythDBError(MythError.DB_SETTING, backend+': BackendServerPort') self.host = host self.port = port self._ident = '%s:%d' % (self.host, self.port) if self._ident in self._shared: # existing connection found self._conn = self._shared[self._ident] if self.sendcommands: if self._conn.command is None: self._conn.command = self._newcmdconn() elif self.blockshutdown: # upref block of shutdown # issue command to backend if needed self._conn.blockshutdown += 1 if self._conn.blockshutdown == 1: self._conn.command.blockShutdown() if self.receiveevents: if self._conn.event is None: self._conn.event = self._neweventconn() else: # no existing connection, create new self._conn = self._ConnHolder() if self.sendcommands: self._conn.command = self._newcmdconn() if self.blockshutdown: self._conn.blockshutdown = 1 if self.receiveevents: self._conn.event = self._neweventconn() self._shared[self._ident] = self._conn self._events = self._listhandlers() for func in self._events: self.registerevent(func)
class BEConnection(object): """ This is the basic backend connection object. You probably don't want to use this directly. """ logmodule = 'Python Backend Connection' def __init__(self, backend, port, localname=None, blockshutdown=False, timeout=10.0): self.connected = False self.log = MythLog(self.logmodule) self._socklock = allocate_lock() self.host = backend self.port = port self.hostname = None self.deadline = timeout self.blockshutdown = blockshutdown self.localname = localname if self.localname is None: self.localname = socket.gethostname() try: self.connect() except socket.error as e: self.log.logTB(MythLog.SOCKET) self.connected = False self.log(MythLog.GENERAL, MythLog.CRIT, "Couldn't connect to backend [%s]:%d" \ % (self.host, self.port)) raise MythBEError(MythError.PROTO_CONNECTION, self.host, self.port) except: raise def __del__(self): self.disconnect() def connect(self): if self.connected: return self.log(MythLog.SOCKET | MythLog.NETWORK, MythLog.INFO, "Connecting to backend [%s]:%d" % (self.host, self.port)) if ':' in self.host: self.socket = deadlinesocket(socket.AF_INET6, socket.SOCK_STREAM) else: self.socket = deadlinesocket(socket.AF_INET, socket.SOCK_STREAM) self.socket.log = self.log self.socket.connect((self.host, self.port)) self.socket.setdeadline(self.deadline) self.connected = True self.check_version() self.announce() def disconnect(self, hard=False): if not self.connected: return self.log(MythLog.SOCKET | MythLog.NETWORK, MythLog.INFO, "Terminating connection to [%s]:%d" % (self.host, self.port)) if not hard: self.backendCommand('DONE', 0) self.connected = False self.socket.shutdown(1) self.socket.close() def reconnect(self, hard=False): self.disconnect(hard) self.connect() def announce(self): # set type, 'Playback' blocks backend shutdown type = ('Monitor', 'Playback')[self.blockshutdown] res = self.backendCommand('ANN %s %s 0' % (type, self.localname)) if res != 'OK': self.log(MythLog.GENERAL, MythLog.ERR, "Unexpected answer to ANN", res) raise MythBEError(MythError.PROTO_ANNOUNCE, self.host, self.port, res) else: self.log(MythLog.SOCKET, MythLog.INFO, "Successfully connected to backend", "[%s]:%d" % (self.host, self.port)) self.hostname = self.backendCommand('QUERY_HOSTNAME') def check_version(self): res = self.backendCommand('MYTH_PROTO_VERSION %s %s' \ % (PROTO_VERSION, PROTO_TOKEN)).split(BACKEND_SEP) if res[0] == 'REJECT': self.log(MythLog.GENERAL, MythLog.ERR, "Backend has version %s, and we speak %s" %\ (res[1], PROTO_VERSION)) raise MythBEError(MythError.PROTO_MISMATCH, int(res[1]), PROTO_VERSION) def backendCommand(self, data, deadline=None): """ obj.backendCommand(data=None, timeout=None) -> response string Sends a formatted command via a socket to the mythbackend. 'timeout' will override the default timeout given when the object was created. If 'data' is None, the method will return any events in the receive buffer. """ # return if not connected if not self.connected: return u'' # pull default timeout if deadline is None: deadline = self.socket.getdeadline() if deadline < 1000: deadline += time() try: # lock socket access with self._socklock: # send command string self.socket.sendheader(data.encode("utf-8")) # wait timeout for data to be received on the socket t = time() timeout = (deadline - t) if (deadline - t > 0) else 0.0 if len(select([self.socket], [], [], timeout)[0]) == 0: # no data, return return u'' res = self.socket.recvheader(deadline=deadline) # convert to unicode try: res = str(''.join([res]), 'utf8') except: res = u''.join([res]) return res except MythError as e: if e.sockcode == 54: # remote has closed connection, attempt reconnect self.reconnect(True) return self.backendCommand(data, deadline) else: raise def blockShutdown(self): if not self.blockshutdown: self.backendCommand('BLOCK_SHUTDOWN') self.blockshutdown = True def unblockShutdown(self): if self.blockshutdown: self.backendCommand('ALLOW_SHUTDOWN') self.blockshutdown = False