On 12/02/2011 05:36 PM, Jan Pazdziora wrote:
On Fri, Dec 02, 2011 at 05:23:25PM +0100, Ionuț Arțăriși wrote:
Hello,

I've written a patch to make reposync more modular. The essential
thing is moving the argument parsing to the spacewalk-repo-sync
script and out of python/site-packages, but there are also a lot of
other style improvements.

We've already applied those changes and others in our branch, I just
merged some of them into the spacewalk branch now.
Can you post this as multiple patches, one for each type of change?
This will make it easier to review than this one huge patch.

Ok, I've chopped that patch up. Here are the 18 resulting patches.


Thanks,
-Ionuț
>From 510fd29c84d1684d9ecbf4e25a5e35a6a23c86f9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ionu=C8=9B=20Ar=C8=9B=C4=83ri=C8=99i?= <iartar...@suse.cz>
Date: Mon, 5 Dec 2011 13:57:05 +0100
Subject: [PATCH 15/18] word-wrap to <80 chars and fix string concatenation

---
 backend/satellite_tools/repo_plugins/yum_src.py |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/backend/satellite_tools/repo_plugins/yum_src.py b/backend/satellite_tools/repo_plugins/yum_src.py
index dff98c7..34212c4 100644
--- a/backend/satellite_tools/repo_plugins/yum_src.py
+++ b/backend/satellite_tools/repo_plugins/yum_src.py
@@ -89,12 +89,16 @@ class ContentSource:
         self.proxy_user = CFG.http_proxy_username
         self.proxy_pass = CFG.http_proxy_password
 
-        if (self.proxy_user is not None and self.proxy_pass is not None and self.proxy_addr is not None):
-            self.proxy_url = "http://%s:%s@%s"; %(self.proxy_user, self.proxy_pass, self.proxy_addr)
-        elif (self.proxy_addr is not None):
-            self.proxy_url = "http://%s"; %(self.proxy_addr)
+        if (self.proxy_user is not None and
+            self.proxy_pass is not None and
+            self.proxy_addr is not None):
+            self.proxy_url = "http://%s:%s@%s"; % (
+                self.proxy_user, self.proxy_pass, self.proxy_addr)
+        elif self.proxy_addr is not None:
+            self.proxy_url = "http://"; + self.proxy_addr
         else:
             self.proxy_url = None
+
         repo = yum.yumRepo.YumRepository(name)
         self.repo = repo
         repo.cache = 0
-- 
1.7.3.4

>From 52397d165c922ebc0551b3744770039a8f5739c7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ionu=C8=9B=20Ar=C8=9B=C4=83ri=C8=99i?= <iartar...@suse.cz>
Date: Mon, 5 Dec 2011 13:54:43 +0100
Subject: [PATCH 14/18] CACHE_DIR is a constant so we declare it at the top of the file

---
 backend/satellite_tools/repo_plugins/yum_src.py |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/backend/satellite_tools/repo_plugins/yum_src.py b/backend/satellite_tools/repo_plugins/yum_src.py
index 1b85b79..dff98c7 100644
--- a/backend/satellite_tools/repo_plugins/yum_src.py
+++ b/backend/satellite_tools/repo_plugins/yum_src.py
@@ -32,6 +32,8 @@ except ImportError:
 from spacewalk.satellite_tools.reposync import ContentPackage
 from spacewalk.common.rhnConfig import CFG, initCFG
 
+CACHE_DIR = '/var/cache/rhn/reposync/'
+
 class YumWarnings:
     def write(self, s):
         pass
@@ -78,9 +80,8 @@ class YumUpdateMetadata(UpdateMetadata):
 
 class ContentSource:
     repo = None
-    cache_dir = '/var/cache/rhn/reposync/'
     def __init__(self, url, name):
-        self._clean_cache(self.cache_dir + name)
+        self._clean_cache(CACHE_DIR + name)
 
         # read the proxy configuration in /etc/rhn/rhn.conf
         initCFG('server.satellite')
@@ -100,9 +101,10 @@ class ContentSource:
         repo.metadata_expire = 0
         repo.mirrorlist = url
         repo.baseurl = [url]
-        repo.basecachedir = self.cache_dir
+        repo.basecachedir = CACHE_DIR
         if hasattr(repo, 'base_persistdir'):
-            repo.base_persistdir = self.cache_dir
+            repo.base_persistdir = CACHE_DIR
+
         if self.proxy_url is not None:
             repo.proxy = self.proxy_url
 
-- 
1.7.3.4

>From 0cb2f4cf338cc139b94ec60b44c4da8a46caced6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ionu=C8=9B=20Ar=C8=9B=C4=83ri=C8=99i?= <iartar...@suse.cz>
Date: Mon, 5 Dec 2011 13:52:19 +0100
Subject: [PATCH 12/18] move third-party module import yum lower

---
 backend/satellite_tools/repo_plugins/yum_src.py |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/backend/satellite_tools/repo_plugins/yum_src.py b/backend/satellite_tools/repo_plugins/yum_src.py
index f0949da..47da5fc 100644
--- a/backend/satellite_tools/repo_plugins/yum_src.py
+++ b/backend/satellite_tools/repo_plugins/yum_src.py
@@ -1,6 +1,6 @@
 #
 # Copyright (c) 2008--2011 Red Hat, Inc.
-# Copyright (c) 2010--2011 SUSE Linux Products GmbH
+# Copyright (c) 2010--2011 SUSE LINUX Products GmbH, Nuernberg, Germany.
 #
 # This software is licensed to you under the GNU General Public License,
 # version 2 (GPLv2). There is NO WARRANTY for this software, express or
@@ -8,15 +8,17 @@
 # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
 # along with this software; if not, see
 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
-# 
+#
 # Red Hat trademarks are not licensed under GPLv2. No permission is
 # granted to use or replicate Red Hat trademarks that are incorporated
-# in this software or its documentation. 
+# in this software or its documentation.
 #
-import yum
+
 import shutil
 import sys
 import gzip
+
+import yum
 from yum.update_md import UpdateMetadata, UpdateNoticeException, UpdateNotice
 from yum.yumRepo import YumRepository
 try:
-- 
1.7.3.4

>From aaaa0b36bb5befa8570d16e3336760f0fd5f1081 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ionu=C8=9B=20Ar=C8=9B=C4=83ri=C8=99i?= <iartar...@suse.cz>
Date: Mon, 5 Dec 2011 13:58:19 +0100
Subject: [PATCH 16/18] move repository setup to a separate method

---
 backend/satellite_tools/repo_plugins/yum_src.py |   29 +++++++++++++---------
 1 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/backend/satellite_tools/repo_plugins/yum_src.py b/backend/satellite_tools/repo_plugins/yum_src.py
index 34212c4..7fdc007 100644
--- a/backend/satellite_tools/repo_plugins/yum_src.py
+++ b/backend/satellite_tools/repo_plugins/yum_src.py
@@ -78,9 +78,10 @@ class YumUpdateMetadata(UpdateMetadata):
                             no = self._no_cache.setdefault(file['name'], set())
                             no.add(un)
 
-class ContentSource:
-    repo = None
+class ContentSource(object):
     def __init__(self, url, name):
+        self.url = url
+        self.name = name
         self._clean_cache(CACHE_DIR + name)
 
         # read the proxy configuration in /etc/rhn/rhn.conf
@@ -101,10 +102,18 @@ class ContentSource:
 
         repo = yum.yumRepo.YumRepository(name)
         self.repo = repo
+        self.sack = None
+
+        self.setup_repo(repo)
+        self.num_packages = 0
+        self.num_excluded = 0
+
+    def setup_repo(self, repo):
+        """Fetch repository metadata"""
         repo.cache = 0
         repo.metadata_expire = 0
-        repo.mirrorlist = url
-        repo.baseurl = [url]
+        repo.mirrorlist = self.url
+        repo.baseurl = [self.url]
         repo.basecachedir = CACHE_DIR
         if hasattr(repo, 'base_persistdir'):
             repo.base_persistdir = CACHE_DIR
@@ -116,21 +125,17 @@ class ContentSource:
         warnings.disable()
         repo.baseurlSetup()
         warnings.restore()
-
         repo.setup(False)
-        self.num_packages = 0
-        self.num_excluded = 0
-
+        self.sack = self.repo.getPackageSack()
 
     def list_packages(self, filters):
         """ list packages"""
-        sack = self.repo.getPackageSack()
-        sack.populate(self.repo, 'metadata', None, 0)
-        list = sack.returnPackages()
+        self.sack.populate(self.repo, 'metadata', None, 0)
+        list = self.sack.returnPackages()
         self.num_packages = len(list)
         if filters:
             list = self._filter_packages(list, filters)
-            list = self._get_package_dependencies(sack, list)
+            list = self._get_package_dependencies(self.sack, list)
             self.num_excluded = self.num_packages - len(list)
         to_return = []
         for pack in list:
-- 
1.7.3.4

>From 73b1ffc19a4b019d595215b4c926579a5bf70188 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ionu=C8=9B=20Ar=C8=9B=C4=83ri=C8=99i?= <iartar...@suse.cz>
Date: Mon, 5 Dec 2011 15:41:42 +0100
Subject: [PATCH 17/18] use systemExit instead of calling sys.stderr.write directly

---
 backend/satellite_tools/spacewalk-repo-sync |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/backend/satellite_tools/spacewalk-repo-sync b/backend/satellite_tools/spacewalk-repo-sync
index 98a1f95..671f4f1 100755
--- a/backend/satellite_tools/spacewalk-repo-sync
+++ b/backend/satellite_tools/spacewalk-repo-sync
@@ -48,14 +48,14 @@ def main():
 
     # quick check to see if you are a super-user.
     if os.getuid() != 0:
-        sys.stderr.write('ERROR: must be root to execute\n')
-        sys.exit(8)
+        systemExit(8, 'ERROR: must be root to execute.')
 
     LOCK = None
     try:
         LOCK = rhnLockfile.Lockfile('/var/run/spacewalk-repo-sync.pid')
     except rhnLockfile.LockfileLockedException:
-        systemExit(1, "ERROR: attempting to run more than one instance of spacewalk-repo-sync Exiting.")
+        systemExit(1, "ERROR: attempting to run more than one instance of "
+                   "spacewalk-repo-sync Exiting.")
 
     parser = OptionParser()
     parser.add_option('-u', '--url', action='store', dest='url',
-- 
1.7.3.4

>From 60d9693d5c1eb0b684ebfaf53a8824a85c4aa9c2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ionu=C8=9B=20Ar=C8=9B=C4=83ri=C8=99i?= <iartar...@suse.cz>
Date: Mon, 5 Dec 2011 15:42:07 +0100
Subject: [PATCH 18/18] use global LOCK

---
 backend/satellite_tools/spacewalk-repo-sync |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/backend/satellite_tools/spacewalk-repo-sync b/backend/satellite_tools/spacewalk-repo-sync
index 671f4f1..15ab22d 100755
--- a/backend/satellite_tools/spacewalk-repo-sync
+++ b/backend/satellite_tools/spacewalk-repo-sync
@@ -50,7 +50,7 @@ def main():
     if os.getuid() != 0:
         systemExit(8, 'ERROR: must be root to execute.')
 
-    LOCK = None
+    global LOCK
     try:
         LOCK = rhnLockfile.Lockfile('/var/run/spacewalk-repo-sync.pid')
     except rhnLockfile.LockfileLockedException:
-- 
1.7.3.4

>From 8a28fb40093ac1a72df3c4bbbd957f991acac09a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ionu=C8=9B=20Ar=C8=9B=C4=83ri=C8=99i?= <iartar...@suse.cz>
Date: Mon, 5 Dec 2011 13:53:16 +0100
Subject: [PATCH 13/18] fixed indentation and whitespace

---
 backend/satellite_tools/repo_plugins/yum_src.py |   12 ++++++------
 1 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/backend/satellite_tools/repo_plugins/yum_src.py b/backend/satellite_tools/repo_plugins/yum_src.py
index 47da5fc..1b85b79 100644
--- a/backend/satellite_tools/repo_plugins/yum_src.py
+++ b/backend/satellite_tools/repo_plugins/yum_src.py
@@ -131,7 +131,7 @@ class ContentSource:
             if pack.arch == 'src':
                 continue
             new_pack = ContentPackage()
-            new_pack.setNVREA(pack.name, pack.version, pack.release, 
+            new_pack.setNVREA(pack.name, pack.version, pack.release,
                               pack.epoch, pack.arch)
             new_pack.unique_id = pack
             new_pack.checksum_type = pack.checksums[0][0]
@@ -203,8 +203,8 @@ class ContentSource:
         shutil.rmtree(directory, True)
 
     def get_updates(self):
-      if not self.repo.repoXML.repoData.has_key('updateinfo'):
-        return []
-      um = YumUpdateMetadata()
-      um.add(self.repo, all=True)
-      return um.notices
+        if not self.repo.repoXML.repoData.has_key('updateinfo'):
+            return []
+        um = YumUpdateMetadata()
+        um.add(self.repo, all=True)
+        return um.notices
-- 
1.7.3.4

>From 1d9ceed702db3f018ff47180205a727f9cfacaf9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ionu=C8=9B=20Ar=C8=9B=C4=83ri=C8=99i?= <iartar...@suse.cz>
Date: Mon, 5 Dec 2011 13:51:27 +0100
Subject: [PATCH 11/18] remove use of deprecated `apply` function

---
 backend/satellite_tools/reposync.py |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/backend/satellite_tools/reposync.py b/backend/satellite_tools/reposync.py
index b41fabe..1893545 100644
--- a/backend/satellite_tools/reposync.py
+++ b/backend/satellite_tools/reposync.py
@@ -211,7 +211,7 @@ class RepoSync(object):
                        and at.label = 'rpm'
                        and ch.label = :channel_label
                 """ % (orgStatement, epochStatement))
-                apply(h.execute, (), param_dict)
+                h.execute(**param_dict)
                 cs = h.fetchone_dict() or None
 
                 if not cs:
-- 
1.7.3.4

>From 49932866a3a6929a7dc297d8fa84c272f82dfa2f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ionu=C8=9B=20Ar=C8=9B=C4=83ri=C8=99i?= <iartar...@suse.cz>
Date: Mon, 5 Dec 2011 13:19:16 +0100
Subject: [PATCH 01/18] simplified systemExit function with what we use

---
 backend/satellite_tools/spacewalk-repo-sync |   17 +++++++----------
 1 files changed, 7 insertions(+), 10 deletions(-)

diff --git a/backend/satellite_tools/spacewalk-repo-sync b/backend/satellite_tools/spacewalk-repo-sync
index ae06a96..39927b5 100755
--- a/backend/satellite_tools/spacewalk-repo-sync
+++ b/backend/satellite_tools/spacewalk-repo-sync
@@ -1,6 +1,7 @@
 #!/usr/bin/python -u
 #
 # Copyright (c) 2008--2011 Red Hat, Inc.
+# Copyright (c) 2011 SUSE LINUX Products GmbH, Nuernberg, Germany.
 #
 # This software is licensed to you under the GNU General Public License,
 # version 2 (GPLv2). There is NO WARRANTY for this software, express or
@@ -8,10 +9,10 @@
 # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
 # along with this software; if not, see
 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
-# 
+#
 # Red Hat trademarks are not licensed under GPLv2. No permission is
 # granted to use or replicate Red Hat trademarks that are incorporated
-# in this software or its documentation. 
+# in this software or its documentation.
 #
 
 LOCK = None
@@ -20,13 +21,9 @@ if __name__ != '__main__':
     raise ImportError, "module cannot be imported"
 
 import sys
-def systemExit(code, msgs=None):
+def systemExit(code, msg=None):
     "Exit with a code and optional message(s). Saved a few lines of code."
-    if msgs:
-        if type(msgs) not in [type([]), type(())]:
-            msgs = (msgs, )
-        for msg in msgs:
-            sys.stderr.write(str(msg)+'\n')
+    sys.stderr.write(str(msg)+'\n')
     sys.exit(code)
 
 try:
@@ -39,8 +36,8 @@ try:
 except KeyboardInterrupt:
     systemExit(0, "\nUser interrupted process.")
 except ImportError:
-    sys.stderr.write("Unable to find RHN code tree.\n"
-                     "Path not correct? '%s'\n" % _LIBPATH)
+    systemExit(1, "Unable to find code tree.\n"
+               "Path not correct? " + sys.path)
 
 def releaseLOCK():
     global LOCK
-- 
1.7.3.4

>From 2c807f78322deb3bb079a7ea9c18a87442a76ebc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ionu=C8=9B=20Ar=C8=9B=C4=83ri=C8=99i?= <iartar...@suse.cz>
Date: Mon, 5 Dec 2011 13:23:15 +0100
Subject: [PATCH 02/18] remove useless check for importing module

the module cannot be imported by python because of the hyphens in its name
---
 backend/satellite_tools/spacewalk-repo-sync |    2 --
 1 files changed, 0 insertions(+), 2 deletions(-)

diff --git a/backend/satellite_tools/spacewalk-repo-sync b/backend/satellite_tools/spacewalk-repo-sync
index 39927b5..defde5c 100755
--- a/backend/satellite_tools/spacewalk-repo-sync
+++ b/backend/satellite_tools/spacewalk-repo-sync
@@ -17,8 +17,6 @@
 
 LOCK = None
 
-if __name__ != '__main__':
-    raise ImportError, "module cannot be imported"
 
 import sys
 def systemExit(code, msg=None):
-- 
1.7.3.4

>From a616a50ff03a480e67bfc4641e3c3231134d9f11 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ionu=C8=9B=20Ar=C8=9B=C4=83ri=C8=99i?= <iartar...@suse.cz>
Date: Mon, 5 Dec 2011 13:23:47 +0100
Subject: [PATCH 03/18] moved standard library imports to the top of the file

---
 backend/satellite_tools/spacewalk-repo-sync |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/backend/satellite_tools/spacewalk-repo-sync b/backend/satellite_tools/spacewalk-repo-sync
index defde5c..774c1ec 100755
--- a/backend/satellite_tools/spacewalk-repo-sync
+++ b/backend/satellite_tools/spacewalk-repo-sync
@@ -16,17 +16,17 @@
 #
 
 LOCK = None
+import sys
+import os
+import socket
 
 
-import sys
 def systemExit(code, msg=None):
     "Exit with a code and optional message(s). Saved a few lines of code."
     sys.stderr.write(str(msg)+'\n')
     sys.exit(code)
 
 try:
-    import os
-    import socket
     from rhn import rhnLockfile
     from spacewalk.common.rhnConfig import CFG
     from spacewalk.common.rhnTB import fetchTraceback
-- 
1.7.3.4

>From 0d6e05f4b9ba5490a4d97de126784a8e996190f8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ionu=C8=9B=20Ar=C8=9B=C4=83ri=C8=99i?= <iartar...@suse.cz>
Date: Mon, 5 Dec 2011 13:51:10 +0100
Subject: [PATCH 10/18] move comment to its proper place

---
 backend/satellite_tools/reposync.py |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/backend/satellite_tools/reposync.py b/backend/satellite_tools/reposync.py
index 7c9f38a..b41fabe 100644
--- a/backend/satellite_tools/reposync.py
+++ b/backend/satellite_tools/reposync.py
@@ -347,11 +347,12 @@ class RepoSync(object):
 
         self.regen=True
         is_non_local_repo = (url.find("file://") < 0)
-        # try/except/finally doesn't work in python 2.4 (RHEL5), so here's a hack
+
         def finally_remove(path):
             if is_non_local_repo and path and os.path.exists(path):
                 os.remove(path)
 
+        # try/except/finally doesn't work in python 2.4 (RHEL5), so here's a hack
         for (index, what) in enumerate(to_process):
             pack, to_download, to_link = what
             localpath = None
-- 
1.7.3.4

>From 3749c30798422a3c7b348c48785c55f468b8b93f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ionu=C8=9B=20Ar=C8=9B=C4=83ri=C8=99i?= <iartar...@suse.cz>
Date: Mon, 5 Dec 2011 13:51:01 +0100
Subject: [PATCH 09/18] fix indentation, whitespace and string concatenation

---
 backend/satellite_tools/reposync.py |   31 ++++++++++++++-----------------
 1 files changed, 14 insertions(+), 17 deletions(-)

diff --git a/backend/satellite_tools/reposync.py b/backend/satellite_tools/reposync.py
index e858dba..7c9f38a 100644
--- a/backend/satellite_tools/reposync.py
+++ b/backend/satellite_tools/reposync.py
@@ -126,10 +126,10 @@ class RepoSync(object):
         return getattr(submod, "ContentSource")
 
     def import_updates(self, plug, url):
-      notices = plug.get_updates()
-      self.print_msg("Repo " + url + " has " + str(len(notices)) + " errata.")
-      if len(notices) > 0:
-        self.upload_updates(notices)
+        notices = plug.get_updates()
+        self.print_msg("Repo %s has %s errata." % (url, len(notices)))
+        if notices:
+            self.upload_updates(notices)
 
     def upload_updates(self, notices):
         batch = []
@@ -329,9 +329,9 @@ class RepoSync(object):
                     if db_pack['channel_label'] == self.channel_label:
                         # package is already in the channel
                         to_link = False
-		elif db_pack['channel_label'] == self.channel_label:
+                elif db_pack['channel_label'] == self.channel_label:
 		    # different package with SAME NVREA
-		    self.disassociate_package(db_pack)
+                    self.disassociate_package(db_pack)
 
             if to_download or to_link:
                 to_process.append((pack, to_download, to_link))
@@ -440,22 +440,19 @@ class RepoSync(object):
 		  checksum_type=pack['checksum_type'], checksum=pack['checksum'])
 
     def _importer_run(self, package, caller, backend):
-            importer = ChannelPackageSubscription(
-                       [IncompletePackage().populate(package)],
-                       backend, caller=caller, repogen=False)
-            importer.run()
-
+        importer = ChannelPackageSubscription(
+            [IncompletePackage().populate(package)],
+            backend, caller=caller, repogen=False)
+        importer.run()
 
     def load_channel(self):
         return rhnChannel.channel_info(self.channel_label)
 
-
     def print_msg(self, message):
         rhnLog.log_clean(0, message)
         if not self.quiet:
             print message
 
-
     def error_msg(self, message):
         rhnLog.log_clean(0, message)
         if not self.quiet:
@@ -470,10 +467,10 @@ class RepoSync(object):
     def _to_db_date(self, date):
         ret = ""
         if date.isdigit():
-          ret = datetime.fromtimestamp(float(date)).isoformat(' ')
+            ret = datetime.fromtimestamp(float(date)).isoformat(' ')
         else:
-          # we expect to get ISO formated date
-          ret = date
+            # we expect to get ISO formated date
+            ret = date
         return ret
 
     def fix_notice(self, notice):
@@ -585,7 +582,7 @@ class ContentPackage:
 
     def load_checksum_from_header(self):
         if self.path is None:
-           raise rhnFault(50, "Unable to load package", explain=0)
+            raise rhnFault(50, "Unable to load package", explain=0)
         self.file = open(self.path, 'rb')
         self.header, self.payload_stream, self.header_start, self.header_end = \
                 rhnPackageUpload.load_package(self.file)
-- 
1.7.3.4

>From f5f969d7b7cd5db58658c88525d48572ee15326f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ionu=C8=9B=20Ar=C8=9B=C4=83ri=C8=99i?= <iartar...@suse.cz>
Date: Mon, 5 Dec 2011 13:49:52 +0100
Subject: [PATCH 08/18] get the repo_type as an argument instead of reading it from the instance attr

---
 backend/satellite_tools/reposync.py |   15 ++++++++-------
 1 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/backend/satellite_tools/reposync.py b/backend/satellite_tools/reposync.py
index 788032b..e858dba 100644
--- a/backend/satellite_tools/reposync.py
+++ b/backend/satellite_tools/reposync.py
@@ -49,7 +49,6 @@ class RepoSync(object):
         self.fail = fail
         self.quiet = quiet
         self.filters = filters
-        self.type = repo_type
 
         initCFG('server')
         db_string = CFG.DEFAULT_DB #"rhnsat/rhnsat@rhnsat"
@@ -88,6 +87,7 @@ class RepoSync(object):
         else:
             self.urls = [(None, url)]
 
+        self.repo_plugin = self.load_plugin(repo_type)
         self.channel_label = channel_label
 
         self.channel = self.load_channel()
@@ -95,10 +95,12 @@ class RepoSync(object):
             self.print_msg("Channel does not exist or is not custom.")
             sys.exit(1)
 
+    def sync(self):
+        """Trigger a reposync"""
         start_time = datetime.now()
-        for (id, url) in self.urls:
-            plugin = self.load_plugin()(url, self.channel_label)
-            self.import_packages(plugin, id, url)
+        for (repo_id, url) in self.urls:
+            plugin = self.repo_plugin(url, self.channel_label)
+            self.import_packages(plugin, repo_id, url)
             self.import_updates(plugin, url)
         if self.regen:
             taskomatic.add_to_repodata_queue_for_channel_package_subscription(
@@ -117,9 +119,8 @@ class RepoSync(object):
                              where label = :channel""")
         h.execute(channel=self.channel['label'])
 
-
-    def load_plugin(self):
-        name = self.type + "_src"
+    def load_plugin(self, repo_type):
+        name = repo_type + "_src"
         mod = __import__('spacewalk.satellite_tools.repo_plugins', globals(), locals(), [name])
         submod = getattr(mod, name)
         return getattr(submod, "ContentSource")
-- 
1.7.3.4

>From 8fed51c253635028fb1469560703b626d9276943 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ionu=C8=9B=20Ar=C8=9B=C4=83ri=C8=99i?= <iartar...@suse.cz>
Date: Mon, 5 Dec 2011 13:33:39 +0100
Subject: [PATCH 07/18] move option parsing from reposync.py to the spacewalk-repo-sync script

---
 backend/satellite_tools/reposync.py         |   97 +++++++++------------------
 backend/satellite_tools/spacewalk-repo-sync |   38 ++++++++++-
 2 files changed, 69 insertions(+), 66 deletions(-)

diff --git a/backend/satellite_tools/reposync.py b/backend/satellite_tools/reposync.py
index 1fbd874..788032b 100644
--- a/backend/satellite_tools/reposync.py
+++ b/backend/satellite_tools/reposync.py
@@ -19,7 +19,7 @@ import re
 import sys
 import time
 from datetime import datetime
-from optparse import OptionParser
+
 from spacewalk.server import rhnPackage, rhnSQL, rhnChannel, rhnPackageUpload
 from spacewalk.common import rhnLog
 from spacewalk.common.rhnLog import log_debug
@@ -42,75 +42,55 @@ def set_filter_opt(option, opt_str, value, parser):
     parser.values.filters.append((f_type, re.split('[,\s]+', value)))
 
 
-    parser = None
-    type = None
-    urls = None
-    channel_label = None
-    channel = None
-    fail = False
-    quiet = False
-    regen = False
-    filters = []
 class RepoSync(object):
+    def __init__(self, channel_label, repo_type, url=None, fail=False,
+                 quiet=False, filters=[]):
+        self.regen = False
+        self.fail = fail
+        self.quiet = quiet
+        self.filters = filters
+        self.type = repo_type
 
-    def main(self):
         initCFG('server')
         db_string = CFG.DEFAULT_DB #"rhnsat/rhnsat@rhnsat"
         rhnSQL.initDB(db_string)
-        (options, args) = self.process_args()
 
+        # setup logging
         log_filename = 'reposync.log'
-        if options.channel_label:
-            date = time.localtime()
-            datestr = '%d.%02d.%02d-%02d:%02d:%02d' % (date.tm_year, date.tm_mon, date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec)
-            log_filename = options.channel_label + '-' +  datestr + '.log'
-
+        date = time.localtime()
+        datestr = '%d.%02d.%02d-%02d:%02d:%02d' % (
+            date.tm_year, date.tm_mon, date.tm_mday, date.tm_hour,
+            date.tm_min, date.tm_sec)
+        log_filename = channel_label + '-' +  datestr + '.log'
         rhnLog.initLOG(default_log_location + log_filename)
         #os.fchown isn't in 2.4 :/
         os.system("chgrp apache " + default_log_location + log_filename)
 
-        if options.type not in ["yum"]:
-            print "Error: Unknown type %s" % options.type
-            sys.exit(2)
-
-        quit = False
-        if not options.url:
-            if options.channel_label:
-                # TODO:need to look at user security across orgs
-                h = rhnSQL.prepare("""select s.id, s.source_url
-                                      from rhnContentSource s,
-                                           rhnChannelContentSource cs,
-                                           rhnChannel c
-                                     where s.id = cs.source_id
-                                       and cs.channel_id = c.id
-                                       and c.label = :label""")
-                h.execute(label=options.channel_label)
-                source_data = h.fetchall_dict() or []
-                if source_data:
-                    self.urls = [(row['id'], row['source_url']) for row in source_data]
-                else:
-                    quit = True
-                    self.error_msg("Channel has no URL associated")
-        else:
-            self.urls = [(None, options.url)]
-        if not options.channel_label:
-            quit = True
-            self.error_msg("--channel must be specified")
-
-        self.filters = options.filters
         self.log_msg("\nSync started: %s" % (time.asctime(time.localtime())))
         self.log_msg(str(sys.argv))
 
+        if not url:
+            # TODO:need to look at user security across orgs
+            h = rhnSQL.prepare("""select s.id, s.source_url
+                                  from rhnContentSource s,
+                                       rhnChannelContentSource cs,
+                                       rhnChannel c
+                                 where s.id = cs.source_id
+                                   and cs.channel_id = c.id
+                                   and c.label = :label""")
+            h.execute(label=channel_label)
+            source_data = h.fetchall_dict()
+            if source_data:
+                self.urls = [(row['id'], row['source_url']) for row in source_data]
+            else:
+                self.error_msg("Channel has no URL associated")
+                sys.exit(1)
+        else:
+            self.urls = [(None, url)]
 
-        if quit:
-            sys.exit(1)
+        self.channel_label = channel_label
 
-        self.type = options.type
-        self.channel_label = options.channel_label
-        self.fail = options.fail
-        self.quiet = options.quiet
         self.channel = self.load_channel()
-
         if not self.channel or not rhnChannel.isCustomChannel(self.channel['id']):
             self.print_msg("Channel does not exist or is not custom.")
             sys.exit(1)
@@ -137,17 +117,6 @@ class RepoSync(object):
                              where label = :channel""")
         h.execute(channel=self.channel['label'])
 
-    def process_args(self):
-        self.parser = OptionParser()
-        self.parser.add_option('-u', '--url', action='store', dest='url', help='The url to sync')
-        self.parser.add_option('-c', '--channel', action='store', dest='channel_label', help='The label of the channel to sync packages to')
-        self.parser.add_option('-t', '--type', action='store', dest='type', help='The type of repo, currently only "yum" is supported', default='yum')
-        self.parser.add_option('-f', '--fail', action='store_true', dest='fail', default=False , help="If a package import fails, fail the entire operation")
-        self.parser.add_option('-q', '--quiet', action='store_true', dest='quiet', default=False, help="Print no output, still logs output")
-        self.parser.add_option('-i', '--include', action='callback', callback=set_filter_opt, type='str', nargs=1, dest='filters', default=[], help="List of included packages")
-        self.parser.add_option('-e', '--exclude', action='callback', callback=set_filter_opt, type='str', nargs=1, dest='filters', default=[], help="List of excluded packages")
-
-        return self.parser.parse_args()
 
     def load_plugin(self):
         name = self.type + "_src"
diff --git a/backend/satellite_tools/spacewalk-repo-sync b/backend/satellite_tools/spacewalk-repo-sync
index 774c1ec..98a1f95 100755
--- a/backend/satellite_tools/spacewalk-repo-sync
+++ b/backend/satellite_tools/spacewalk-repo-sync
@@ -19,6 +19,7 @@ LOCK = None
 import sys
 import os
 import socket
+from optparse import OptionParser
 
 
 def systemExit(code, msg=None):
@@ -55,11 +56,44 @@ def main():
         LOCK = rhnLockfile.Lockfile('/var/run/spacewalk-repo-sync.pid')
     except rhnLockfile.LockfileLockedException:
         systemExit(1, "ERROR: attempting to run more than one instance of spacewalk-repo-sync Exiting.")
-    sync = reposync.RepoSync()
-    sync.main()
+
+    parser = OptionParser()
+    parser.add_option('-u', '--url', action='store', dest='url',
+                      help='The url to sync')
+    parser.add_option('-c', '--channel', action='store',
+                      dest='channel_label',
+                      help='The label of the channel to sync packages to')
+    parser.add_option('-t', '--type', action='store', dest='repo_type',
+                      help='The type of repo, currently only "yum" is supported',
+                      default='yum')
+    parser.add_option('-f', '--fail', action='store_true', dest='fail',
+                      default=False,
+                      help="If a package import fails, fail the entire operation")
+    parser.add_option('-q', '--quiet', action='store_true', dest='quiet',
+                      default=False, help="Print no output, still logs output")
+    parser.add_option('-i', '--include', action='callback',
+                      callback=reposync.set_filter_opt, type='str', nargs=1,
+                      dest='filters', default=[],
+                      help="List of included packages")
+    parser.add_option('-e', '--exclude', action='callback',
+                      callback=reposync.set_filter_opt,
+                      type='str', nargs=1, dest='filters', default=[],
+                      help="List of excluded packages")
+    (options, args) = parser.parse_args()
+
+    if not options.channel_label:
+        systemExit(1, "--channel must be specified")
+
+    reposync.RepoSync(channel_label=options.channel_label,
+                      repo_type=options.repo_type,
+                      url=options.url,
+                      fail=options.fail,
+                      quiet=options.quiet,
+                      filters=options.filters)
     releaseLOCK()
     return 0
 
+
 if __name__ == '__main__':
     try:
         sys.exit(abs(main() or 0))
-- 
1.7.3.4

>From e7f1e7cf4b7ace40884137b4cb3a57f8e44fbd64 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ionu=C8=9B=20Ar=C8=9B=C4=83ri=C8=99i?= <iartar...@suse.cz>
Date: Mon, 5 Dec 2011 13:27:06 +0100
Subject: [PATCH 06/18] use the print_msg function instead of print to also log the message

---
 backend/satellite_tools/reposync.py |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/backend/satellite_tools/reposync.py b/backend/satellite_tools/reposync.py
index d8e13b1..1fbd874 100644
--- a/backend/satellite_tools/reposync.py
+++ b/backend/satellite_tools/reposync.py
@@ -112,7 +112,7 @@ class RepoSync(object):
         self.channel = self.load_channel()
 
         if not self.channel or not rhnChannel.isCustomChannel(self.channel['id']):
-            print "Channel does not exist or is not custom"
+            self.print_msg("Channel does not exist or is not custom.")
             sys.exit(1)
 
         start_time = datetime.now()
-- 
1.7.3.4

>From 74afae5d0be54af51d42ba875a684519cf65dcfa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ionu=C8=9B=20Ar=C8=9B=C4=83ri=C8=99i?= <iartar...@suse.cz>
Date: Mon, 5 Dec 2011 13:25:47 +0100
Subject: [PATCH 05/18] use new-style classes

---
 backend/satellite_tools/reposync.py |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/backend/satellite_tools/reposync.py b/backend/satellite_tools/reposync.py
index 1e19f3e..d8e13b1 100644
--- a/backend/satellite_tools/reposync.py
+++ b/backend/satellite_tools/reposync.py
@@ -41,7 +41,6 @@ def set_filter_opt(option, opt_str, value, parser):
     else:                               f_type = '-'
     parser.values.filters.append((f_type, re.split('[,\s]+', value)))
 
-class RepoSync:
 
     parser = None
     type = None
@@ -52,6 +51,7 @@ class RepoSync:
     quiet = False
     regen = False
     filters = []
+class RepoSync(object):
 
     def main(self):
         initCFG('server')
-- 
1.7.3.4

>From 709d0cdfbf3a1b9cd43857810d4b14188ef223b4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ionu=C8=9B=20Ar=C8=9B=C4=83ri=C8=99i?= <iartar...@suse.cz>
Date: Mon, 5 Dec 2011 13:25:06 +0100
Subject: [PATCH 04/18] remove unused `traceback` import and arrange std lib modules one per line

---
 backend/satellite_tools/reposync.py |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/backend/satellite_tools/reposync.py b/backend/satellite_tools/reposync.py
index 2ba1d7d..1e19f3e 100644
--- a/backend/satellite_tools/reposync.py
+++ b/backend/satellite_tools/reposync.py
@@ -13,11 +13,12 @@
 # granted to use or replicate Red Hat trademarks that are incorporated
 # in this software or its documentation.
 #
-import sys, os, time
 import hashlib
+import os
 import re
+import sys
+import time
 from datetime import datetime
-import traceback
 from optparse import OptionParser
 from spacewalk.server import rhnPackage, rhnSQL, rhnChannel, rhnPackageUpload
 from spacewalk.common import rhnLog
-- 
1.7.3.4

_______________________________________________
Spacewalk-devel mailing list
Spacewalk-devel@redhat.com
https://www.redhat.com/mailman/listinfo/spacewalk-devel

Reply via email to