def resourcelist(self): """Return resourcelist on disk based on current mappings Return resourcelist. Uses existing self.mapper settings. """ ### 0. Sanity checks if (len(self.mappings)<1): raise ClientFatalError("No source to destination mapping specified") ### 1. Build from disk ib = ResourceListBuilder(do_md5=self.checksum,mapper=self.mapper) ib.add_exclude_files(self.exclude_patterns) return( ib.from_disk() )
def baseline_or_audit(self, allow_deletion=False, audit_only=False): """Baseline synchonization or audit Both functions implemented in this routine because audit is a prerequisite for a baseline sync. """ action = ( 'audit' if (audit_only) else 'baseline sync' ) self.logger.debug("Starting "+action) ### 0. Sanity checks if (len(self.mappings)<1): raise ClientFatalError("No source to destination mapping specified") ### 1. Get inventories from both src and dst # 1.a source resourcelist ib = ResourceListBuilder(mapper=self.mapper) try: self.logger.info("Reading sitemap %s" % (self.sitemap)) src_sitemap = Sitemap(allow_multifile=self.allow_multifile, mapper=self.mapper) src_resourcelist = src_sitemap.read(uri=self.sitemap) self.logger.debug("Finished reading sitemap") except Exception as e: raise ClientFatalError("Can't read source resourcelist from %s (%s)" % (self.sitemap,str(e))) self.logger.info("Read source resourcelist, %d resources listed" % (len(src_resourcelist))) if (len(src_resourcelist)==0): raise ClientFatalError("Aborting as there are no resources to sync") if (self.checksum and not src_resourcelist.has_md5()): self.checksum=False self.logger.info("Not calculating checksums on destination as not present in source resourcelist") # 1.b destination resourcelist mapped back to source URIs ib.do_md5=self.checksum dst_resourcelist = ib.from_disk() ### 2. Compare these resourcelists respecting any comparison options (same,updated,deleted,created)=dst_resourcelist.compare(src_resourcelist) ### 3. Report status and planned actions status = " IN SYNC " if (len(updated)>0 or len(deleted)>0 or len(created)>0): status = "NOT IN SYNC" self.logger.warning("Status: %s (same=%d, updated=%d, deleted=%d, created=%d)" %\ (status,len(same),len(updated),len(deleted),len(created))) if (audit_only): self.logger.debug("Completed "+action) return ### 4. Check that sitemap has authority over URIs listed uauth = UrlAuthority(self.sitemap) for resource in src_resourcelist: if (not uauth.has_authority_over(resource.uri)): if (self.noauth): #self.logger.info("Sitemap (%s) mentions resource at a location it does not have authority over (%s)" % (self.sitemap,resource.uri)) pass else: raise ClientFatalError("Aborting as sitemap (%s) mentions resource at a location it does not have authority over (%s), override with --noauth" % (self.sitemap,resource.uri)) ### 5. Grab files to do sync for resource in updated: uri = resource.uri file = self.mapper.src_to_dst(uri) self.logger.info("updated: %s -> %s" % (uri,file)) self.update_resource(resource,file,'updated') for resource in created: uri = resource.uri file = self.mapper.src_to_dst(uri) self.logger.info("created: %s -> %s" % (uri,file)) self.update_resource(resource,file,'created') for resource in deleted: uri = resource.uri file = self.mapper.src_to_dst(uri) self.delete_resource(resource,file,allow_deletion) ### 6. For sync reset any incremental status for site if (not audit_only): links = self.extract_links(src_resourcelist) if ('next' in links): self.write_incremental_status(self.sitemap,links['next']) self.logger.info("Written config with next incremental at %s" % (links['next'])) else: self.write_incremental_status(self.sitemap) self.logger.debug("Completed "+action)