Skip to content

Commit 0ab1279

Browse files
committed
bugzilla: Dynamically set version number at connect time
This is the beginning of breaking away from hardcoding version numbers in class names, and requiring a new class for bugzilla version detection
1 parent 78fd831 commit 0ab1279

8 files changed

Lines changed: 51 additions & 36 deletions

File tree

bugzilla/base.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,7 @@ def _init_field_aliases(self):
313313
self._add_field_alias('last_change_time', 'delta_ts')
314314

315315
def _get_user_agent(self):
316-
ret = ('Python-urllib bugzilla.py/%s %s' %
317-
(__version__, str(self.__class__.__name__)))
318-
return ret
316+
return 'python-bugzilla/%s' % __version__
319317
user_agent = property(_get_user_agent)
320318

321319

@@ -438,6 +436,16 @@ def readconfig(self, configpath=None):
438436
else:
439437
log.debug("bugzillarc: unknown key=%s", key)
440438

439+
def _set_bz_version(self, version):
440+
try:
441+
self.bz_ver_major, self.bz_ver_minor = [
442+
int(i) for i in version.split(".")[0:2]]
443+
except:
444+
log.debug("version doesn't match expected format X.Y.Z, "
445+
"assuming 5.0", exc_info=True)
446+
self.bz_ver_major = 5
447+
self.bz_ver_minor = 0
448+
441449
def connect(self, url=None):
442450
'''
443451
Connect to the bugzilla instance with the given url.
@@ -469,6 +477,10 @@ def connect(self, url=None):
469477
log.info("user and password present - doing login()")
470478
self.login()
471479

480+
version = self._proxy.Bugzilla.version()["version"]
481+
log.debug("Bugzilla version string: %s", version)
482+
self._set_bz_version(version)
483+
472484
def disconnect(self):
473485
'''
474486
Disconnect from the given bugzilla instance.

bugzilla/bugzilla3.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@
1313

1414

1515
class Bugzilla3(BugzillaBase):
16-
bz_ver_major = 3
17-
bz_ver_minor = 0
16+
pass
1817

1918

2019
class Bugzilla32(Bugzilla3):
21-
bz_ver_minor = 2
20+
pass
2221

2322

2423
class Bugzilla34(Bugzilla32):
25-
bz_ver_minor = 4
24+
pass
2625

2726

2827
class Bugzilla36(Bugzilla34):
29-
bz_ver_minor = 6
28+
pass

bugzilla/bugzilla4.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212

1313

1414
class Bugzilla4(Bugzilla36):
15-
bz_ver_major = 4
16-
bz_ver_minor = 0
15+
pass
1716

1817

1918
class Bugzilla42(Bugzilla4):
20-
bz_ver_minor = 2
19+
pass
2120

2221

2322
class Bugzilla44(Bugzilla42):
24-
bz_ver_minor = 4
23+
pass

bugzilla/rhbugzilla.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from logging import getLogger
1313

14-
from .bugzilla4 import Bugzilla44 as _parent
14+
from .base import BugzillaBase as _parent
1515

1616
log = getLogger(__name__)
1717

tests/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
else:
1414
from StringIO import StringIO
1515

16+
from bugzilla.base import BugzillaBase
17+
from bugzilla import RHBugzilla
18+
1619

1720
_cleanup = []
1821

@@ -36,6 +39,19 @@ def _cleanup_cb():
3639
REDHAT_URL = None
3740

3841

42+
def make_bz(version, *args, **kwargs):
43+
cls = BugzillaBase
44+
if kwargs.pop("rhbz", False):
45+
cls = RHBugzilla
46+
if "cookiefile" not in kwargs:
47+
kwargs["cookiefile"] = None
48+
if "tokenfile" not in kwargs:
49+
kwargs["tokenfile"] = None
50+
bz = cls(*args, **kwargs)
51+
bz._set_bz_version(version) # pylint: disable=protected-access
52+
return bz
53+
54+
3955
def diff(orig, new):
4056
"""
4157
Return a unified diff string between the passed strings

tests/createbug.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@
1111

1212
import unittest
1313

14-
from bugzilla.bugzilla4 import Bugzilla4
15-
1614
import tests
1715

1816

19-
bz4 = Bugzilla4(cookiefile=None, tokenfile=None)
17+
bz4 = tests.make_bz("4.0.0")
2018

2119

2220
class CreatebugTest(unittest.TestCase):

tests/misc.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from __future__ import print_function
1313

1414
import atexit
15-
import logging
1615
import os
1716
import shutil
1817
import sys
@@ -64,11 +63,8 @@ class MiscAPI(unittest.TestCase):
6463
Test miscellaneous API bits
6564
"""
6665
def testUserAgent(self):
67-
b3 = bugzilla.Bugzilla3(url=None, cookiefile=None, tokenfile=None)
68-
rhbz = bugzilla.RHBugzilla(url=None, cookiefile=None, tokenfile=None)
69-
70-
self.assertTrue(b3.user_agent.endswith("Bugzilla3"))
71-
self.assertTrue(rhbz.user_agent.endswith("RHBugzilla"))
66+
b3 = tests.make_bz("3.0.0")
67+
self.assertTrue("python-bugzilla" in b3.user_agent)
7268

7369
def testCookies(self):
7470
cookiesbad = os.path.join(os.getcwd(), "tests/data/cookies-bad.txt")
@@ -83,7 +79,7 @@ def cleanup():
8379
shutil.copy(cookieslwp, cookiesnew)
8480

8581
# Mozilla should be converted inplace to LWP
86-
bugzilla.Bugzilla3(url=None, cookiefile=cookiesnew)
82+
tests.make_bz("3.0.0", cookiefile=cookiesnew)
8783

8884
def strip_comments(content):
8985
return [l for l in content.split("\n") if not l.startswith("#")]
@@ -93,18 +89,18 @@ def strip_comments(content):
9389

9490
# Make sure bad cookies raise an error
9591
try:
96-
bugzilla.Bugzilla3(url=None, cookiefile=cookiesbad)
92+
tests.make_bz("3.0.0", cookiefile=cookiesbad)
9793
raise AssertionError("Expected BugzillaError from parsing %s" %
9894
os.path.basename(cookiesbad))
9995
except bugzilla.BugzillaError:
10096
# Expected result
10197
pass
10298

10399
# Mozilla should 'just work'
104-
bugzilla.Bugzilla3(url=None, cookiefile=cookiesmoz)
100+
tests.make_bz("3.0.0", cookiefile=cookiesmoz)
105101

106102
def test_readconfig(self):
107-
bzapi = bugzilla.RHBugzilla(url=None)
103+
bzapi = tests.make_bz("4.4.0", rhbz=True)
108104
bzapi.url = "foo.example.com"
109105
temp = tempfile.NamedTemporaryFile(mode="w")
110106

@@ -147,8 +143,8 @@ def _testPostCompare(bz, indict, outexpect):
147143
bz.post_translation({}, outdict)
148144
self.assertTrue(outdict == outexpect)
149145

150-
bug3 = bugzilla.Bugzilla3(url=None, cookiefile=None, tokenfile=None)
151-
rhbz = bugzilla.RHBugzilla(url=None, cookiefile=None, tokenfile=None)
146+
bug3 = tests.make_bz("3.4.0")
147+
rhbz = tests.make_bz("4.4.0", rhbz=True)
152148

153149
test1 = {
154150
"component": ["comp1"],

tests/query.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,11 @@
1313
import os
1414
import unittest
1515

16-
import bugzilla
17-
from bugzilla.bugzilla3 import Bugzilla34
18-
from bugzilla.bugzilla4 import Bugzilla4
19-
from bugzilla.rhbugzilla import RHBugzilla4
20-
2116
import tests
2217

23-
bz34 = Bugzilla34(cookiefile=None, tokenfile=None)
24-
bz4 = Bugzilla4(cookiefile=None, tokenfile=None)
25-
rhbz4 = RHBugzilla4(cookiefile=None, tokenfile=None)
18+
bz34 = tests.make_bz("3.4.0")
19+
bz4 = tests.make_bz("4.0.0")
20+
rhbz4 = tests.make_bz("4.4.0", rhbz=True)
2621

2722

2823
class BZ34Test(unittest.TestCase):

0 commit comments

Comments
 (0)