Repository: fedora-infra/bodhi

Issue:
The web server recently gained a dependency on createrepo_c
@nirik noticed that the Bodhi server did not start because it didn't have ```createrepo_c``` available to it. One of the patches that went into Bodhi 3.10.0 added a function to ```bodhi.server.util``` that uses ```createrepo_c```. That function is actually only used by the composer via ```bodhi.server.metadata```, and thus isn't really needed by the web server.

Rather than adding a new dependency to the web server, I think we should just move this code from ```bodhi.server.util``` to ```bodhi.server.metadata```.


Discussion:

Resolving patch:
diff --git a/bodhi/server/metadata.py b/bodhi/server/metadata.py
--- a/bodhi/server/metadata.py
+++ b/bodhi/server/metadata.py
@@ -21,6 +21,7 @@
 import logging
 import os
 import shelve
+import shutil
 import tempfile
 
 from kitchen.text.converters import to_bytes
@@ -37,6 +38,40 @@
 log = logging.getLogger(__name__)
 
 
+def insert_in_repo(comp_type, repodata, filetype, extension, source):
+    """
+    Inject a file into the repodata with the help of createrepo_c.
+
+    Args:
+        comp_type (int): createrepo_c compression type indication.
+        repodata (basestring): The path to the repo where the metadata will be inserted.
+        filetype (basestring): What type of metadata will be inserted by createrepo_c.
+            This does allow any string to be inserted (custom types). There are some
+            types which are used with dnf repos as primary, updateinfo, comps, filelist etc.
+        extension (basestring): The file extension (xml, sqlite).
+        source (basestring): A file path. File holds the dump of metadata until
+            copied to the repodata folder.
+    """
+    log.info('Inserting %s.%s into %s', filetype, extension, repodata)
+    target_fname = os.path.join(repodata, '%s.%s' % (filetype, extension))
+    shutil.copyfile(source, target_fname)
+    repomd_xml = os.path.join(repodata, 'repomd.xml')
+    repomd = cr.Repomd(repomd_xml)
+    # create a new record for our repomd.xml
+    rec = cr.RepomdRecord(filetype, target_fname)
+    # compress our metadata file with the comp_type
+    rec_comp = rec.compress_and_fill(cr.SHA256, comp_type)
+    # add hash to the compresed metadata file
+    rec_comp.rename_file()
+    # set type of metadata
+    rec_comp.type = filetype
+    # insert metadata about our metadata in repomd.xml
+    repomd.set_record(rec_comp)
+    with open(repomd_xml, 'w') as repomd_file:
+        repomd_file.write(repomd.xml_dump())
+    os.unlink(target_fname)
+
+
 def modifyrepo(comp_type, compose_path, filetype, extension, source):
     """
     Inject a file into the repodata for each architecture with the help of createrepo_c.
@@ -56,7 +91,7 @@ def modifyrepo(comp_type, compose_path, filetype, extension, source):
             repodata = os.path.join(repo_path, arch, 'tree', 'repodata')
         else:
             repodata = os.path.join(repo_path, arch, 'os', 'repodata')
-        util.insert_in_repo(comp_type, repodata, filetype, extension, source)
+        insert_in_repo(comp_type, repodata, filetype, extension, source)
 
 
 class UpdateInfoMetadata(object):
diff --git a/bodhi/server/util.py b/bodhi/server/util.py
--- a/bodhi/server/util.py
+++ b/bodhi/server/util.py
@@ -25,7 +25,6 @@
 import json
 import os
 import pkg_resources
-import shutil
 import socket
 import subprocess
 import tempfile
@@ -36,7 +35,6 @@
 import arrow
 import bleach
 import colander
-import createrepo_c as cr
 import hawkey
 import libcomps
 import libravatar
@@ -99,40 +97,6 @@ def get_rpm_header(nvr, tries=0):
     raise ValueError("No rpm headers found in koji for %r" % nvr)
 
 
-def insert_in_repo(comp_type, repodata, filetype, extension, source):
-    """
-    Inject a file into the repodata with the help of createrepo_c.
-
-    Args:
-        comp_type (int): createrepo_c compression type indication.
-        repodata (basestring): The path to the repo where the metadata will be inserted.
-        filetype (basestring): What type of metadata will be inserted by createrepo_c.
-            This does allow any string to be inserted (custom types). There are some
-            types which are used with dnf repos as primary, updateinfo, comps, filelist etc.
-        extension (basestring): The file extension (xml, sqlite).
-        source (basestring): A file path. File holds the dump of metadata until
-            copied to the repodata folder.
-    """
-    log.info('Inserting %s.%s into %s', filetype, extension, repodata)
-    target_fname = os.path.join(repodata, '%s.%s' % (filetype, extension))
-    shutil.copyfile(source, target_fname)
-    repomd_xml = os.path.join(repodata, 'repomd.xml')
-    repomd = cr.Repomd(repomd_xml)
-    # create a new record for our repomd.xml
-    rec = cr.RepomdRecord(filetype, target_fname)
-    # compress our metadata file with the comp_type
-    rec_comp = rec.compress_and_fill(cr.SHA256, comp_type)
-    # add hash to the compresed metadata file
-    rec_comp.rename_file()
-    # set type of metadata
-    rec_comp.type = filetype
-    # insert metadata about our metadata in repomd.xml
-    repomd.set_record(rec_comp)
-    with open(repomd_xml, 'w') as repomd_file:
-        repomd_file.write(repomd.xml_dump())
-    os.unlink(target_fname)
-
-
 def flash_log(msg):
     """
     Log the given message at debug level.


Test patch:
diff --git a/bodhi/tests/server/base.py b/bodhi/tests/server/base.py
--- a/bodhi/tests/server/base.py
+++ b/bodhi/tests/server/base.py
@@ -27,7 +27,8 @@
 import createrepo_c
 import mock
 
-from bodhi.server import bugs, buildsys, models, initialize_db, Session, config, main, webapp, util
+from bodhi.server import (bugs, buildsys, models, initialize_db, Session, config, main, metadata,
+                          webapp)
 from bodhi.tests.server import create_update, populate
 
 
@@ -406,5 +407,5 @@ def mkmetadatadir(path, updateinfo=None, comps=None):
                            '--quiet',
                            path])
     if updateinfo is not False:
-        util.insert_in_repo(createrepo_c.XZ, os.path.join(path, 'repodata'), 'updateinfo', 'xml',
-                            os.path.join(path, 'updateinfo.xml'))
+        metadata.insert_in_repo(createrepo_c.XZ, os.path.join(path, 'repodata'), 'updateinfo',
+                                'xml', os.path.join(path, 'updateinfo.xml'))