def _push_contents(self, file): checksum_type = 'sha256' # FIXME: this should be configuration option file['file_size'] = 0 file['is_binary'] = 'N' file_path = file.get('path') file_contents = file.get('file_contents') or '' if 'enc64' in file and file_contents: file_contents = base64.decodestring(file_contents) if 'config_file_type_id' not in file: log_debug( 4, "Client does not support config directories, so set file_type_id to 1" ) file['config_file_type_id'] = '1' file['checksum_type'] = checksum_type file['checksum'] = getStringChecksum(checksum_type, file_contents or '') if file_contents: file['file_size'] = len(file_contents) if file['file_size'] > self._get_maximum_file_size(): raise ConfigFileTooLargeError(file_path, file['file_size']) # Is the content binary data? # XXX We may need a heuristic; this is what the web site does, and we # have to be consistent # XXX Yes this is iterating over a string try: file_contents.decode('UTF-8') except UnicodeDecodeError: file['is_binary'] = 'Y' h = rhnSQL.prepare(self._query_content_lookup) h.execute(**file) row = h.fetchone_dict() if row: db_contents = rhnSQL.read_lob(row['contents']) or '' if file_contents == db_contents: # Same content file['config_content_id'] = row['id'] log_debug(5, "same content") return # We have to insert a new file now content_seq = rhnSQL.Sequence('rhn_confcontent_id_seq') config_content_id = content_seq.next() file['config_content_id'] = config_content_id file['contents'] = file_contents h = rhnSQL.prepare(self._query_insert_content, blob_map={'contents': 'contents'}) h.execute(**file)
def _push_contents(self, file): checksum_type = 'sha256' # FIXME: this should be configuration option file['file_size'] = 0 file['is_binary'] = 'N' file_path = file.get('path') file_contents = file.get('file_contents') or '' if 'enc64' in file and file_contents: file_contents = base64.decodestring(file_contents) if 'config_file_type_id' not in file: log_debug(4, "Client does not support config directories, so set file_type_id to 1") file['config_file_type_id'] = '1' file['checksum_type'] = checksum_type file['checksum'] = getStringChecksum(checksum_type, file_contents or '') if file_contents: file['file_size'] = len(file_contents) if file['file_size'] > self._get_maximum_file_size(): raise ConfigFileTooLargeError(file_path, file['file_size']) # Is the content binary data? # XXX We may need a heuristic; this is what the web site does, and we # have to be consistent # XXX Yes this is iterating over a string try: file_contents.decode('UTF-8') except UnicodeDecodeError: file['is_binary'] = 'Y' h = rhnSQL.prepare(self._query_content_lookup) h.execute(**file) row = h.fetchone_dict() if row: db_contents = rhnSQL.read_lob(row['contents']) or '' if file_contents == db_contents: # Same content file['config_content_id'] = row['id'] log_debug(5, "same content") return # We have to insert a new file now content_seq = rhnSQL.Sequence('rhn_confcontent_id_seq') config_content_id = content_seq.next() file['config_content_id'] = config_content_id file['contents'] = file_contents h = rhnSQL.prepare(self._query_insert_content, blob_map={'contents': 'contents'}) h.execute(**file)
def _push_contents(self, file): checksum_type = 'md5' # FIXME: this should be configuration option file['file_size'] = 0 file['is_binary'] = 'N' file_path = file.get('path') file_contents = file.get('file_contents') or '' if file.has_key('enc64') and file_contents: file_contents = base64.decodestring(file_contents) if not file.has_key('config_file_type_id'): log_debug(4, "Client does not support config directories, so set file_type_id to 1") file['config_file_type_id'] = '1' file['checksum_type'] = checksum_type file['checksum'] = getStringChecksum(checksum_type, file_contents or '') if file_contents: file['file_size'] = len(file_contents) if file['file_size'] > self._get_maximum_file_size(): raise ConfigFileTooLargeError(file_path, file['file_size']) # Is the content binary data? # XXX We may need a heuristic; this is what the web site does, and we # have to be consistent # XXX Yes this is iterating over a string for c in file_contents: if ord(c) > 127: file['is_binary'] = 'Y' break h = rhnSQL.prepare(self._query_content_lookup) apply(h.execute, (), file) row = h.fetchone_dict() if row: db_contents = rhnSQL.read_lob(row['contents']) or '' if file_contents == db_contents: # Same content file['config_content_id'] = row['id'] log_debug(5, "same content") return # We have to insert a new file now content_seq = rhnSQL.Sequence('rhn_confcontent_id_seq') config_content_id = content_seq.next() file['config_content_id'] = config_content_id if file_contents: h = rhnSQL.prepare(self._query_insert_content) else: h = rhnSQL.prepare(self._query_insert_null_content) apply(h.execute, (), file) # Row should be there now h = rhnSQL.prepare(self._query_get_content_row) apply(h.execute, (), file) row = h.fetchone_dict() if not row: # Ouch raise rhnException("Row should have been inserted but it's not") if file_contents: log_debug(5, "writing file contents to blob") lob = row['contents'] lob.write(file_contents)