def resend_if_local(msg, publisher): """Resend the message provided all uris point to local files.""" for uri in gen_dict_extract(msg.data, 'uri'): urlobj = urlparse(uri) if not publisher or not socket.gethostbyname(urlobj.netloc) in get_local_ips(): return else: LOGGER.debug('Sending: %s', str(msg)) publisher.send(str(msg))
def run(self): with Subscribe('', topics=self.attrs['listen'], addr_listener=True) as sub: for msg in sub.recv(1): if msg is None: if not self.loop: break else: continue # check that files are local for uri in gen_dict_extract(msg.data, 'uri'): urlobj = urlparse(uri) if (urlobj.scheme not in ['', 'file'] and not socket.gethostbyname( urlobj.netloc) in get_local_ips()): break else: LOGGER.debug('We have a match: %s', str(msg)) #pathname = unpack(orig_pathname, **attrs) info = self.attrs.get("info", {}) if info: info = dict((elt.strip().split('=') for elt in info.split(";"))) for infokey, infoval in info.items(): if "," in infoval: info[infokey] = infoval.split(",") # info.update(parse(attrs["origin"], orig_pathname)) # info['uri'] = pathname # info['uid'] = os.path.basename(pathname) info.update(msg.data) info['request_address'] = self.attrs.get( "request_address", get_own_ip()) + ":" + self.attrs["request_port"] old_data = msg.data msg = Message(self.attrs["topic"], msg.type, info) self.publisher.send(str(msg)) with file_cache_lock: for filename in gen_dict_extract(old_data, 'uid'): file_cache.appendleft(self.attrs["topic"] + '/' + filename) LOGGER.debug("Message sent: " + str(msg)) if not self.loop: break
def make_uris(msg, destination, login=None): duri = urlparse(destination) scheme = duri.scheme or 'ssh' dest_hostname = duri.hostname or socket.gethostname() if socket.gethostbyname(dest_hostname) in get_local_ips(): scheme_, host_ = "ssh", dest_hostname # local file else: scheme_, host_ = scheme, dest_hostname # remote file if login: # Add (only) user to uri. host_ = login.split(":")[0] + "@" + host_ def uri_callback(var): uid = var['uid'] path = os.path.join(duri.path, uid) var['uri'] = urlunparse((scheme_, host_, path, "", "", "")) return var msg.data = translate_dict(msg.data, ('uri', 'uid'), uri_callback) return msg