def update_repomd_xml_file(repomd_path, comps_path): """ Update the repomd.xml with the checksum info for comps_path @param repomd_path: repomd.xml file path @param comps_path: comps.xml file path @return: True if repomd_path has been updated, False otherwise """ # Copy comps_f to a new file name prepending the sha256sum to the file name comps_orig = comps_path compsxml_checksum = util.get_file_checksum(hashtype="sha", filename=comps_orig) comps_path = os.path.join(os.path.split(comps_orig)[0], "%s-%s" % (compsxml_checksum, os.path.split(comps_orig)[1])) shutil.copyfile(comps_orig, comps_path) compsxml_timestamp = util.get_file_timestamp(comps_path) # Create gzipped version of comps.xml comps_gz_path_orig = "%s.gz" % (comps_orig) f_in = open(comps_path, "rb") f_out = gzip.open(comps_gz_path_orig, "wb") try: f_out.writelines(f_in) finally: f_in.close() f_out.close() compsxml_gz_checksum = util.get_file_checksum(hashtype="sha", filename=comps_gz_path_orig) comps_gz_path = os.path.join( os.path.split(comps_orig)[0], "%s-%s.gz" % (compsxml_gz_checksum, os.path.split(comps_orig)[1]) ) shutil.move(comps_gz_path_orig, comps_gz_path) compsxml_gz_timestamp = util.get_file_timestamp(comps_gz_path) # Save current group and group_gz file paths so we may cleanup after the update old_mddata = util.get_repomd_filetype_dump(repomd_path) try: # Update repomd.xml with the new comps information f = open(repomd_path, "r") try: repomd = f.read() finally: f.close() updated_xml = update_repomd_xml_string( repomd, comps_path, compsxml_checksum, compsxml_timestamp, comps_gz_path, compsxml_gz_checksum, compsxml_checksum, compsxml_gz_timestamp, ) f = open(repomd_path, "w") try: f.write(updated_xml.encode("UTF-8")) finally: f.close() log.info("update_repomd_xml_file completed") except xml.dom.DOMException, e: log.error(e) log.error("Unable to update group info for %s" % (repomd_path)) return False
f.close() updated_xml = update_repomd_xml_string(repomd, comps_path, compsxml_checksum, compsxml_timestamp, comps_gz_path, compsxml_gz_checksum, compsxml_checksum, compsxml_gz_timestamp) f = open(repomd_path, "w") try: f.write(updated_xml.encode("UTF-8")) finally: f.close() log.info("update_repomd_xml_file completed") except xml.dom.DOMException, e: log.error(e) log.error("Unable to update group info for %s" % (repomd_path)) return False current_mddata = util.get_repomd_filetype_dump(repomd_path) # Remove old groups and groups_gz if old_mddata.has_key("group") and old_mddata["group"].has_key("location"): group_path = os.path.join(os.path.dirname(repomd_path), "../", old_mddata["group"]["location"]) if old_mddata["group"]["location"] != current_mddata["group"]["location"]: # For the case when no change occured to metadata, don't delete the 'old', since 'old' == current try: if os.path.basename(group_path) != "comps.xml": log.info("Removing old group metadata: %s" % (group_path)) os.unlink(group_path) except: log.exception("Unable to delete old group metadata: %s" % (group_path)) if old_mddata.has_key("group_gz") and old_mddata["group_gz"].has_key("location"): group_gz_path = os.path.join(os.path.dirname(repomd_path), "../", old_mddata["group_gz"]["location"]) if old_mddata["group_gz"]["location"] != current_mddata["group_gz"]["location"]: # For the case when no change occured to metadata, don't delete the 'old', since 'old' == current