commit:     d82b7a95d697435dffd80028df21517e8c5fda7d
Author:     Brian Dolbec <dolsen <AT> gentoo <DOT> org>
AuthorDate: Tue May 27 06:03:33 2014 +0000
Commit:     Brian Dolbec <brian.dolbec <AT> gmail <DOT> com>
CommitDate: Tue May 27 06:04:40 2014 +0000
URL:        
http://git.overlays.gentoo.org/gitweb/?p=proj/portage.git;a=commit;h=d82b7a95

repoman/main.py: Create a new Changes class

This new class is to scan for and hold the changes in the repo.
Later it will act as a manager class to run the individual VCS plugin modules 
scan function.

---
 pym/repoman/main.py | 103 +++++-------------------------------------
 pym/repoman/scan.py | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 139 insertions(+), 91 deletions(-)

diff --git a/pym/repoman/main.py b/pym/repoman/main.py
index e03788f..49ad79d 100755
--- a/pym/repoman/main.py
+++ b/pym/repoman/main.py
@@ -75,7 +75,7 @@ from repoman.qa_data import (format_qa_output, 
format_qa_output_column, qahelp,
        qawarnings, qacats, no_exec, allvars, max_desc_len, missingvars,
        ruby_deprecated, suspect_virtual, suspect_rdepend, valid_restrict)
 from repoman.repos import has_global_mask, RepoSettings, repo_metadata
-from repoman.scan import scan
+from repoman.scan import Changes, scan
 from repoman._subprocess import repoman_popen, repoman_getstatusoutput
 from repoman import utilities
 from repoman.vcs.vcs import (git_supports_gpg_sign, vcs_files_to_cps,
@@ -275,91 +275,12 @@ elif options.pretend:
 else:
        print(green("\nRepoMan scours the neighborhood..."))
 
-new_ebuilds = set()
-modified_ebuilds = set()
-modified_changelogs = set()
-mychanged = []
-mynew = []
-myremoved = []
-
-if vcs_settings.vcs == "cvs":
-       mycvstree = cvstree.getentries("./", recursive=1)
-       mychanged = cvstree.findchanged(mycvstree, recursive=1, basedir="./")
-       mynew = cvstree.findnew(mycvstree, recursive=1, basedir="./")
-       if options.if_modified == "y":
-               myremoved = cvstree.findremoved(mycvstree, recursive=1, 
basedir="./")
-
-elif vcs_settings.vcs == "svn":
-       with repoman_popen("svn status") as f:
-               svnstatus = f.readlines()
-       mychanged = [
-               "./" + elem.split()[-1:][0]
-               for elem in svnstatus
-               if elem and elem[:1] in "MR"]
-       mynew = [
-               "./" + elem.split()[-1:][0]
-               for elem in svnstatus
-               if elem.startswith("A")]
-       if options.if_modified == "y":
-               myremoved = [
-                       "./" + elem.split()[-1:][0]
-                       for elem in svnstatus
-                       if elem.startswith("D")]
-
-elif vcs_settings.vcs == "git":
-       with repoman_popen(
-               "git diff-index --name-only "
-               "--relative --diff-filter=M HEAD") as f:
-               mychanged = f.readlines()
-       mychanged = ["./" + elem[:-1] for elem in mychanged]
-
-       with repoman_popen(
-               "git diff-index --name-only "
-               "--relative --diff-filter=A HEAD") as f:
-               mynew = f.readlines()
-       mynew = ["./" + elem[:-1] for elem in mynew]
-       if options.if_modified == "y":
-               with repoman_popen(
-                       "git diff-index --name-only "
-                       "--relative --diff-filter=D HEAD") as f:
-                       myremoved = f.readlines()
-               myremoved = ["./" + elem[:-1] for elem in myremoved]
-
-elif vcs_settings.vcs == "bzr":
-       with repoman_popen("bzr status -S .") as f:
-               bzrstatus = f.readlines()
-       mychanged = [
-               "./" + elem.split()[-1:][0].split('/')[-1:][0]
-               for elem in bzrstatus
-               if elem and elem[1:2] == "M"]
-       mynew = [
-               "./" + elem.split()[-1:][0].split('/')[-1:][0]
-               for elem in bzrstatus
-               if elem and (elem[1:2] == "NK" or elem[0:1] == "R")]
-       if options.if_modified == "y":
-               myremoved = [
-                       "./" + elem.split()[-3:-2][0].split('/')[-1:][0]
-                       for elem in bzrstatus
-                       if elem and (elem[1:2] == "K" or elem[0:1] == "R")]
+#####################
 
-elif vcs_settings.vcs == "hg":
-       with repoman_popen("hg status --no-status --modified .") as f:
-               mychanged = f.readlines()
-       mychanged = ["./" + elem.rstrip() for elem in mychanged]
-       with repoman_popen("hg status --no-status --added .") as f:
-               mynew = f.readlines()
-       mynew = ["./" + elem.rstrip() for elem in mynew]
-       if options.if_modified == "y":
-               with repoman_popen("hg status --no-status --removed .") as f:
-                       myremoved = f.readlines()
-               myremoved = ["./" + elem.rstrip() for elem in myremoved]
+changed = Changes(options)
+changed.scan(vcs_settings)
 
-if vcs_settings.vcs:
-       new_ebuilds.update(x for x in mynew if x.endswith(".ebuild"))
-       modified_ebuilds.update(x for x in mychanged if x.endswith(".ebuild"))
-       modified_changelogs.update(
-               x for x in chain(mychanged, mynew)
-               if os.path.basename(x) == "ChangeLog")
+######################
 
 have_pmasked = False
 have_dev_keywords = False
@@ -403,7 +324,7 @@ except FileNotFound:
 effective_scanlist = scanlist
 if options.if_modified == "y":
        effective_scanlist = sorted(vcs_files_to_cps(
-               chain(mychanged, mynew, myremoved)))
+               chain(changed.changed, changed.new, changed.removed)))
 
 for x in effective_scanlist:
        # ebuilds and digests added to cvs respectively.
@@ -886,7 +807,7 @@ for x in effective_scanlist:
        muselist = frozenset(musedict)
 
        changelog_path = os.path.join(checkdir_relative, "ChangeLog")
-       changelog_modified = changelog_path in modified_changelogs
+       changelog_modified = changelog_path in changed.changelogs
 
        # detect unused local USE-descriptions
        used_useflags = set()
@@ -901,7 +822,7 @@ for x in effective_scanlist:
                        ebuild_path = os.path.join(catdir, ebuild_path)
                ebuild_path = os.path.join(".", ebuild_path)
                if check_changelog and not changelog_modified \
-                       and ebuild_path in new_ebuilds:
+                       and ebuild_path in changed.new_ebuilds:
                        stats['changelog.ebuildadded'] += 1
                        fails['changelog.ebuildadded'].append(relative_path)
 
@@ -1025,7 +946,7 @@ for x in effective_scanlist:
                                not keyword.startswith("-"):
                                stable_keywords.append(keyword)
                if stable_keywords:
-                       if ebuild_path in new_ebuilds and catdir != "virtual":
+                       if ebuild_path in changed.new_ebuilds and catdir != 
"virtual":
                                stable_keywords.sort()
                                stats["KEYWORDS.stable"] += 1
                                fails["KEYWORDS.stable"].append(
@@ -1335,8 +1256,8 @@ for x in effective_scanlist:
                relative_path = os.path.join(x, y + ".ebuild")
                full_path = os.path.join(repo_settings.repodir, relative_path)
                if not vcs_settings.vcs_preserves_mtime:
-                       if ebuild_path not in new_ebuilds and \
-                               ebuild_path not in modified_ebuilds:
+                       if ebuild_path not in changed.new_ebuilds and \
+                               ebuild_path not in changed.ebuilds:
                                pkg.mtime = None
                try:
                        # All ebuilds should have utf_8 encoding.
@@ -1947,7 +1868,7 @@ else:
                        checkdir_relative = os.path.join(".", checkdir_relative)
 
                        changelog_path = os.path.join(checkdir_relative, 
"ChangeLog")
-                       changelog_modified = changelog_path in 
modified_changelogs
+                       changelog_modified = changelog_path in 
changed.changelogs
                        if changelog_modified and options.echangelog != 'force':
                                continue
 

diff --git a/pym/repoman/scan.py b/pym/repoman/scan.py
index 575069f..051f170 100644
--- a/pym/repoman/scan.py
+++ b/pym/repoman/scan.py
@@ -2,8 +2,13 @@
 import logging
 import os
 import sys
+from itertools import chain
+
+from portage import cvstree
 
 from repoman.errors import caterror
+from repoman._subprocess import repoman_popen
+
 
 def scan(repolevel, reposplit, startdir, categories, repo_settings):
        scanlist = []
@@ -48,3 +53,125 @@ def scan(repolevel, reposplit, startdir, categories, 
repo_settings):
                "Found the following packages to scan:\n%s" % 
'\n'.join(scanlist))
 
        return scanlist
+
+
+class Changes(object):
+       '''Class object to scan and hold the resultant data
+       for all changes to process.
+
+       Basic plan is move the spaghetti code here, refactor the code
+       to split it into separate functions for each cvs type.
+       Later refactoring can then move the individual scan_ functions
+       to their respective VCS plugin module.
+       Leaving this class as the manager class which runs the correct VCS 
plugin.
+       This will ease future addition of new VCS types.
+       '''
+
+
+       def __init__(self, options):
+               self.options = options
+               self._reset()
+
+
+       def _reset(self):
+               self.new_ebuilds = set()
+               self.ebuilds = set()
+               self.changelogs = set()
+               self.changed = []
+               self.new = []
+               self.removed = []
+
+
+       def scan(self, vcs_settings):
+               self._reset()
+
+               vcscheck = getattr(self, 'scan_%s' % vcs_settings.vcs)
+               vcscheck()
+
+               if vcs_settings.vcs:
+                       self.new_ebuilds.update(x for x in self.new if 
x.endswith(".ebuild"))
+                       self.ebuilds.update(x for x in self.changed if 
x.endswith(".ebuild"))
+                       self.changelogs.update(
+                               x for x in chain(self.changed, self.new)
+                               if os.path.basename(x) == "ChangeLog")
+
+
+       def scan_cvs(self):
+               tree = cvstree.getentries("./", recursive=1)
+               self.changed = cvstree.findchanged(tree, recursive=1, 
basedir="./")
+               self.new = cvstree.findnew(tree, recursive=1, basedir="./")
+               if self.options.if_modified == "y":
+                       self.removed = cvstree.findremoved(tree, recursive=1, 
basedir="./")
+               del tree
+
+
+       def scan_svn(self):
+               with repoman_popen("svn status") as f:
+                       svnstatus = f.readlines()
+               self.changed = [
+                       "./" + elem.split()[-1:][0]
+                       for elem in svnstatus
+                       if elem and elem[:1] in "MR"]
+               self.new = [
+                       "./" + elem.split()[-1:][0]
+                       for elem in svnstatus
+                       if elem.startswith("A")]
+               if self.options.if_modified == "y":
+                       self.removed = [
+                               "./" + elem.split()[-1:][0]
+                               for elem in svnstatus
+                               if elem.startswith("D")]
+
+
+       def scan_git(self):
+               with repoman_popen(
+                       "git diff-index --name-only "
+                       "--relative --diff-filter=M HEAD") as f:
+                       changed = f.readlines()
+               self.changed = ["./" + elem[:-1] for elem in changed]
+               del changed
+
+               with repoman_popen(
+                       "git diff-index --name-only "
+                       "--relative --diff-filter=A HEAD") as f:
+                       new = f.readlines()
+               self.new = ["./" + elem[:-1] for elem in new]
+               if self.options.if_modified == "y":
+                       with repoman_popen(
+                               "git diff-index --name-only "
+                               "--relative --diff-filter=D HEAD") as f:
+                               removed = f.readlines()
+                       self.removed = ["./" + elem[:-1] for elem in removed]
+                       del removed
+
+
+       def scan_bzr(self):
+               with repoman_popen("bzr status -S .") as f:
+                       bzrstatus = f.readlines()
+               self.changed = [
+                       "./" + elem.split()[-1:][0].split('/')[-1:][0]
+                       for elem in bzrstatus
+                       if elem and elem[1:2] == "M"]
+               self.new = [
+                       "./" + elem.split()[-1:][0].split('/')[-1:][0]
+                       for elem in bzrstatus
+                       if elem and (elem[1:2] == "NK" or elem[0:1] == "R")]
+               if self.options.if_modified == "y":
+                       self.removed = [
+                               "./" + elem.split()[-3:-2][0].split('/')[-1:][0]
+                               for elem in bzrstatus
+                               if elem and (elem[1:2] == "K" or elem[0:1] == 
"R")]
+
+
+       def scan_hg(self):
+               with repoman_popen("hg status --no-status --modified .") as f:
+                       changed = f.readlines()
+               self.changed = ["./" + elem.rstrip() for elem in changed]
+               with repoman_popen("hg status --no-status --added .") as f:
+                       new = f.readlines()
+               self.new = ["./" + elem.rstrip() for elem in new]
+               if self.options.if_modified == "y":
+                       with repoman_popen("hg status --no-status --removed .") 
as f:
+                               removed = f.readlines()
+                       self.removed = ["./" + elem.rstrip() for elem in 
removed]
+               del changed, new, removed

Reply via email to