diff options
| author | Till Maas <opensource@till.name> | 2009-07-19 02:40:36 +0200 |
|---|---|---|
| committer | Till Maas <opensource@till.name> | 2009-07-19 02:40:36 +0200 |
| commit | e06ead01cad2ff56bbac89c0765ff44b19cd035f (patch) | |
| tree | 6e122290d78395132674f32687671457c41479c0 | |
| parent | 46379924e912a5ddcaafc823733eab0f3cdecfee (diff) | |
| download | cnucnu-e06ead01cad2ff56bbac89c0765ff44b19cd035f.tar.gz cnucnu-e06ead01cad2ff56bbac89c0765ff44b19cd035f.tar.xz cnucnu-e06ead01cad2ff56bbac89c0765ff44b19cd035f.zip | |
integrate default config in Config class, use it
| -rw-r--r-- | .gitignore | 1 | ||||
| -rwxr-xr-x | cnucnu.py | 37 | ||||
| -rw-r--r-- | cnucnu.yaml.sample | 21 | ||||
| -rw-r--r-- | lib/cnucnu/bugzilla_reporter.py | 20 | ||||
| -rwxr-xr-x | lib/cnucnu/config.py | 107 | ||||
| -rw-r--r-- | lib/cnucnu/package_list.py | 38 | ||||
| -rwxr-xr-x | lib/cnucnu/tests/config_test.py | 60 | ||||
| -rwxr-xr-x | lib/cnucnu/wiki.py | 6 | ||||
| -rwxr-xr-x | runtests.sh | 1 |
9 files changed, 221 insertions, 70 deletions
@@ -1,5 +1,6 @@ *.pyc .*.swp cnucnu.yaml +cnucnu.yaml.sample *.log *.pickle @@ -20,7 +20,7 @@ import sys sys.path.insert(0, './lib') -from cnucnu import config +from cnucnu.config import Config from cnucnu.package_list import Repository, PackageList, Package from cnucnu.checkshell import CheckShell from cnucnu.bugzilla_reporter import BugzillaReporter @@ -79,15 +79,33 @@ if __name__ == '__main__': parser = OptionParser() parser.add_option("", "--shell", dest="action", help="Interactive shell", action="store_const", const="shell") - parser.add_option("", "--config", dest="config_filename", help="config_filename, e.g. for bugzilla credentials", default="./cnucnu.yaml") + parser.add_option("", "--config", dest="config_filename", help="config_filename, e.g. for bugzilla credentials") parser.add_option("", "--create-bugs", dest="action", help="file bugs for outdated packages", action="store_const", const="create-bugs") parser.add_option("", "--fm-outdated-all", dest="action", help="compare all packages in rawhide with freshmeat", action="store_const", const="fm-outdated-all") + parser.add_option("", "--dump-default-config", dest="yaml_file", help="dumps default config to ./cnucnu.yaml.sample") + parser.add_option("", "--dry-run", dest="dry_run", help="Do not file or change bugs", default=False) (options, args) = parser.parse_args() + if options.yaml_file: + ofile = open(options.yaml_file, "wb") + config = Config() + ofile.write(config.yaml) + ofile.close() + sys.exit(0) + + yaml_file = options.config_filename + yaml_data = "" + if not yaml_file: + try: + f = open("./cnucnu.yaml", "rb") + yaml_data = f.read() + f.close() + except IOError: + pass - conf = config.Config(options.config_filename) + config = Config(yaml_file=yaml_file, yaml_data=yaml_data) if options.action == "shell": - shell = CheckShell(config=conf) + shell = CheckShell(config=config) while True: try: if not shell.cmdloop(): @@ -97,16 +115,17 @@ if __name__ == '__main__': print repr(ex) break elif options.action == "create-bugs": - bugzilla_config = conf.bugzilla_config + bugzilla_config = config.bugzilla_config br = BugzillaReporter(bugzilla_config) - pl = PackageList() + repo = Repository(**config.config["repo"]) + pl = PackageList(repo=repo, **config.config["package list"]) for p in pl: print "testing: %s" % p.name try: if p.upstream_newer: if p.name not in ['abook', 'crm114', 'crossvc', 'ctorrent', 'ekg2', 'emacs-auctex', 'fdupes', 'hping3', 'libtlen', 'mysqltuner']: - br.report_outdated(p, dry_run=False) + br.report_outdated(p, dry_run=options.dry_run) except Exception, e: pprint(e) elif options.action == "fm-outdated-all": @@ -124,7 +143,3 @@ if __name__ == '__main__': plist = PackageList(repo=repo) packages = plist.packages analyse_packages(packages) - - - - diff --git a/cnucnu.yaml.sample b/cnucnu.yaml.sample deleted file mode 100644 index d4983f1..0000000 --- a/cnucnu.yaml.sample +++ /dev/null @@ -1,21 +0,0 @@ -cnucnu: - -bugzilla: - user: upstream-release-monitoring@fedoraproject.org - password: - base url: https://bugzilla.redhat.com - url: "%(base url)s/xmlrpc.cgi" - bug url prefix: "%(base url)s/show_bug.cgi?id=" - product: Fedora - version: rawhide - keywords: FutureFeature - bug status: ASSIGNED - summary template: "%%(name)s-%%(latest_upstream)s is available" - - -repo: - path: http://download.fedora.redhat.com/pub/fedora/linux/development/source/SRPMS - name: Fedora Rawhide - - -# vim: filetype=yaml diff --git a/lib/cnucnu/bugzilla_reporter.py b/lib/cnucnu/bugzilla_reporter.py index 5fa5f81..ced1aad 100644 --- a/lib/cnucnu/bugzilla_reporter.py +++ b/lib/cnucnu/bugzilla_reporter.py @@ -35,20 +35,14 @@ class BugzillaReporter(object): # if not, then it is silently ignored # 'status': 'ASSIGNED', } - summary_template = "%(name)s-%(latest_upstream)s is available" - description_template = \ -"""Latest upstream release: %(latest_upstream)s -Current version in %(repo_name)s: %(repo_version)s -URL: %(url)s - -More information about the service that created this bug can be found at: -https://fedoraproject.org/wiki/Using_FEver_to_track_upstream_changes""" def __init__(self, config): rpc_conf = filter_dict(config, ["url", "user", "password"]) bz = Bugzilla(**rpc_conf) self.bz = bz - self.bz.login() + + if "password" in rpc_conf and rpc_conf["password"]: + self.bz.login() self.bugzilla_config = config self.base_query['product'] = config['product'] @@ -77,8 +71,8 @@ https://fedoraproject.org/wiki/Using_FEver_to_track_upstream_changes""" open = self.get_open(package) if not open: bug = {'component': package.name, - 'summary': self.summary_template % package, - 'description': self.description_template % package + 'summary': self.config["summary template"] % package, + 'description': self.config["description template"] % package } bug.update(self.new_bug) @@ -102,8 +96,8 @@ https://fedoraproject.org/wiki/Using_FEver_to_track_upstream_changes""" if bug_version != package.latest_upstream: # :TODO: comment creation untested - update = {'short_desc': self.summary_template % package, - 'comment': self.description_template % package + update = {'short_desc': self.config["summary template"] % package, + 'comment': self.config["summary template"] % package } res = self.bz._update_bugs(open_bug.bug_id, update) print res diff --git a/lib/cnucnu/config.py b/lib/cnucnu/config.py index bcf6afe..c7431a0 100755 --- a/lib/cnucnu/config.py +++ b/lib/cnucnu/config.py @@ -20,20 +20,107 @@ from helper import pprint, filter_dict import yaml +DEFAULT_YAML=""" +bugzilla: + user: upstream-release-monitoring@fedoraproject.org + password: + base url: https://bugzilla.redhat.com + url: "%(base url)s/xmlrpc.cgi" + bug url prefix: "%(base url)s/show_bug.cgi?id=" + product: Fedora + version: rawhide + keywords: FutureFeature + bug status: ASSIGNED + explanation url: 'https://fedoraproject.org/wiki/Using_FEver_to_track_upstream_changes' + + summary template: "%%(name)s-%%(latest_upstream)s is available" + description template: 'Latest upstream release: %%(latest_upstream)s + + Current version in %%(repo_name)s: %%(repo_version)s + + URL: %%(url)s + + More information about the service that created this bug can be found at: + + %(explanation url)s' + + +repo: + path: 'http://download.fedora.redhat.com/pub/fedora/linux/development/source/SRPMS' + name: Fedora Rawhide +package list: + mediawiki: + base url: 'https://fedoraproject.org/w/' + page: Using_FEver_to_track_upstream_changes + + +# vim: filetype=yaml +""" + class Config(object): - def __init__(self, yaml_filename): - file = open(yaml_filename, "rb") - self.config = yaml.load(file.read()) + def __init__(self, yaml_file=None, yaml=None, yaml_data=None, config=None, load_default=True): + # TODO: remove yaml option + if yaml_data: + yaml = yaml_data + self.config = {} + + # TODO? + self.__getitem__ = self.config.__getitem__ + + if load_default: + self.update_yaml(DEFAULT_YAML) + if yaml_file: + self.update_yaml_file(yaml_file) + elif yaml: + self.update_yaml(yaml) + elif config: + self.update(config) + + self._bugzilla_config = {} + + def update_yaml_file(self, new_yaml_file, old=None): + if not old: + old = self.config + file = open(new_yaml_file, "rb") + new_yaml = file.read() file.close() + old = self.update_yaml(new_yaml, old) + return old + + def update_yaml(self, new_yaml, old=None): + if not old: + old = self.config + new = yaml.load(new_yaml) + old = self.update(new, old) + return old + + def update(self, new, old=None): + if not old: + old = self.config + for k, v in new.items(): + if k in old.keys(): + if isinstance(old[k], dict): + old[k] = self.update(new[k], old[k]) + else: + old[k] = new[k] + else: + old[k] = new[k] self._bugzilla_config = {} + return old + + +# D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k] +# (if E has keys else: for (k, v) in E: D[k] = v) then: for k in F: D[k] = F[k] + @property def bugzilla_config(self): if not self._bugzilla_config: b = self.config["bugzilla"] for c, v in b.items(): - b[c] = v % b + if isinstance(v, str): + b[c] = v % b self._bugzilla_config = b return self._bugzilla_config @@ -43,9 +130,16 @@ class Config(object): rpc_conf = filter_dict(self.bugzilla_config, ["url", "user", "password"]) return rpc_conf + @property + def yaml(self): + return yaml.dump(self.config, indent=4, default_flow_style=False) + + if __name__ == '__main__': - cf = Config('../../cnucnu.yaml') + cf = Config() + cf.update_yaml_file('../../cnucnu.yaml') + print "Global config" pprint(cf.config) @@ -54,3 +148,6 @@ if __name__ == '__main__': print "\nBugzilla class config" pprint(cf.bugzilla_class_conf) + + print + print cf.yaml diff --git a/lib/cnucnu/package_list.py b/lib/cnucnu/package_list.py index 6640e83..6bddec5 100644 --- a/lib/cnucnu/package_list.py +++ b/lib/cnucnu/package_list.py @@ -25,6 +25,7 @@ sys.path.insert(0, '../../lib') import cnucnu.errors as cc_errors from cnucnu.helper import rpm_cmp +from cnucnu.config import Config class Package(object): @@ -145,16 +146,18 @@ class Package(object): class Repository: - def __init__(self, package_list=None, repoid=None): - self.repofrompath = None + def __init__(self, package_list=None, name="", path=""): + if not (name and path): + c = Config().config["repo"] + name = c["name"] + path = c["path"] - if repoid: - self.repoid = repoid - self.name = repoid - else: - self.repoid = "cnucnu-rawhide" - self.repofrompath = "cnucnu-rawhide,http://download.fedora.redhat.com/pub/fedora/linux/development/source/SRPMS" - self.name = "Fedora Rawhide" + import string + self.name = name + self.path = path + self.repoid = "cnucnu-%s" % "".join(c for c in name if c in string.letters) + + self.repofrompath = "%s,%s" % (self.repoid, self.path) self.package_list = package_list self._package_version_list = None @@ -186,13 +189,17 @@ class Repository: class PackageList: - def __init__(self, repo=None, wiki_page="Using_FEver_to_track_upstream_changes", packages=None): + def __init__(self, repo=None, mediawiki=False, packages=None): if not repo: repo = Repository() - if not packages: - import cnucnu.wiki as wiki - w = wiki.Wiki() - page_text = w.get_pagesource(wiki_page) + + if not mediawiki: + mediawiki = Config().config["package list"]["mediawiki"] + if not packages and mediawiki: + + from wiki import MediaWiki + w = MediaWiki(base_url=mediawiki["base url"]) + page_text = w.get_pagesource(mediawiki["page"]) import re package_line = re.compile(' \\* ([^ ]*) (.*) ([^ \n]*)\n') @@ -217,6 +224,3 @@ class PackageList: if p.name == key: return p raise KeyError("Package %s not found" % key) - - - diff --git a/lib/cnucnu/tests/config_test.py b/lib/cnucnu/tests/config_test.py new file mode 100755 index 0000000..5b1e97a --- /dev/null +++ b/lib/cnucnu/tests/config_test.py @@ -0,0 +1,60 @@ +#!/usr/bin/python +# vim: fileencoding=utf8 foldmethod=marker +#{{{ License header: GPLv2+ +# This file is part of cnucnu. +# +# Cnucnu is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# +# Cnucnu is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with cnucnu. If not, see <http://www.gnu.org/licenses/>. +#}}} + +import unittest + +import sys +sys.path.insert(0, '../..') +sys.path.insert(0, '..') +sys.path.insert(0, './lib') + + +from cnucnu.config import Config + +class ConfigTest(unittest.TestCase): + + def testCreateConfig(self): + c = Config() + c = Config(yaml="{}") + c = Config(config={}) + + def testSimpleUpdate(self): + old = {0: 0, "d": {0: 0}} + new = {1: 1, "d": {1: 1}} + expected = {0: 0, 1: 1, "d": {0: 0, 1: 1}} + + c = Config(config=old, load_default=False) + c.update(new) + + self.assertEqual(c.config, expected) + + def testComplexUpdate(self): + old = {0: 0, "d": {0: 0, 1: 1, 2: 2}, 2: {}} + new = {1: 1, "d": {1: {0: 0}, 2: None}} + expected = {0: 0, 1: 1, "d": {0: 0, 1: {0: 0}, 2: None}, 2: {}} + + c = Config(config=old, load_default=False) + c.update(new) + + self.assertEqual(c.config, expected) + +if __name__ == "__main__": + suite = unittest.TestLoader().loadTestsFromTestCase(ConfigTest) + unittest.TextTestRunner(verbosity=2).run(suite) + #unittest.main() diff --git a/lib/cnucnu/wiki.py b/lib/cnucnu/wiki.py index fb14696..7b138d4 100755 --- a/lib/cnucnu/wiki.py +++ b/lib/cnucnu/wiki.py @@ -19,9 +19,9 @@ import fedora.client -class Wiki(fedora.client.Wiki): +class MediaWiki(fedora.client.Wiki): def __init__(self, base_url='https://fedoraproject.org/w/', *args, **kw): - super(Wiki, self).__init__(base_url, *args, **kw) + super(MediaWiki, self).__init__(base_url, *args, **kw) def json_request(self, method="api.php", req_params=None, auth=False, **kwargs): if req_params: @@ -45,5 +45,5 @@ class Wiki(fedora.client.Wiki): if __name__ == '__main__': - wiki = Wiki() + wiki = MediaWiki(base_url='https://fedoraproject.org/w/') print wiki.get_pagesource("Using_FEver_to_track_upstream_changes") diff --git a/runtests.sh b/runtests.sh index 41b99ed..3b12705 100755 --- a/runtests.sh +++ b/runtests.sh @@ -19,3 +19,4 @@ lib/cnucnu/tests/package_list_test.py lib/cnucnu/tests/mail_test.py +lib/cnucnu/tests/config_test.py |
