Skip to content

Commit 05e0460

Browse files
committed
Move bin/bugzilla to bugzilla/_cli.py
Simplifies testing and let's us use entry_points, which are more standard
1 parent 69bb2a2 commit 05e0460

4 files changed

Lines changed: 33 additions & 53 deletions

File tree

bugzilla-cli

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
#!/bin/sh
1+
#!/usr/bin/env python3
22

3-
PYTHONPATH=. exec bin/bugzilla "$@"
3+
# This is a small wrapper script to simplify running the 'bugzilla'
4+
# cli tool from a git checkout
5+
6+
from bugzilla import _cli
7+
_cli.main()

bin/bugzilla renamed to bugzilla/_cli.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@
4545
format_field_re = re.compile("%{([a-z0-9_]+)(?::([^}]*))?}")
4646

4747
log = getLogger(bugzilla.__name__)
48-
handler = StreamHandler(sys.stderr)
49-
handler.setFormatter(Formatter(
50-
"[%(asctime)s] %(levelname)s (%(module)s:%(lineno)d) %(message)s",
51-
"%H:%M:%S"))
52-
log.addHandler(handler)
5348

5449

5550
################
@@ -107,6 +102,24 @@ def get_default_url():
107102
return DEFAULT_BZ
108103

109104

105+
def setup_logging(debug, verbose):
106+
handler = StreamHandler(sys.stderr)
107+
handler.setFormatter(Formatter(
108+
"[%(asctime)s] %(levelname)s (%(module)s:%(lineno)d) %(message)s",
109+
"%H:%M:%S"))
110+
log.addHandler(handler)
111+
112+
if debug:
113+
log.setLevel(DEBUG)
114+
elif verbose:
115+
log.setLevel(INFO)
116+
else:
117+
log.setLevel(WARN)
118+
119+
if _is_unittest_debug:
120+
log.setLevel(DEBUG)
121+
122+
110123
##################
111124
# Option parsing #
112125
##################
@@ -1027,20 +1040,11 @@ def _handle_login(opt, parser, args, action, bz):
10271040
sys.exit(0)
10281041

10291042

1030-
def main(unittest_bz_instance=None):
1043+
def _main(unittest_bz_instance):
10311044
parser = setup_parser()
10321045
opt, args = parser.parse_known_args()
10331046
action = opt.command
1034-
1035-
if opt.debug:
1036-
log.setLevel(DEBUG)
1037-
elif opt.verbose:
1038-
log.setLevel(INFO)
1039-
else:
1040-
log.setLevel(WARN)
1041-
1042-
if _is_unittest_debug:
1043-
log.setLevel(DEBUG)
1047+
setup_logging(opt.debug, opt.verbose)
10441048

10451049
log.debug("Launched with command line: %s", " ".join(sys.argv))
10461050

@@ -1114,9 +1118,9 @@ def main(unittest_bz_instance=None):
11141118
_format_output(bz, opt, buglist)
11151119

11161120

1117-
if __name__ == '__main__':
1121+
def main(unittest_bz_instance=None):
11181122
try:
1119-
main()
1123+
return _main(unittest_bz_instance)
11201124
except KeyboardInterrupt:
11211125
log.debug("", exc_info=True)
11221126
print("\nExited at user request.")

setup.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ def initialize_options(self):
146146
def finalize_options(self):
147147
pass
148148

149-
def _run(self):
149+
def run(self):
150150
import pylint.lint
151151
import pycodestyle
152152

153-
files = (["bugzilla/", "bin-bugzilla"] +
153+
files = (["bugzilla-cli", "bugzilla"] +
154154
glob.glob("examples/*.py") +
155155
glob.glob("tests/*.py"))
156156
output_format = sys.stdout.isatty() and "colorized" or "text"
@@ -174,16 +174,6 @@ def _run(self):
174174
]
175175
pylint.lint.Run(files + pylint_opts)
176176

177-
def run(self):
178-
os.link("bin/bugzilla", "bin-bugzilla")
179-
try:
180-
self._run()
181-
finally:
182-
try:
183-
os.unlink("bin-bugzilla")
184-
except:
185-
pass
186-
187177

188178
class RPMCommand(Command):
189179
description = "Build src and binary rpms."
@@ -238,7 +228,7 @@ def _parse_requirements(fname):
238228
'Programming Language :: Python :: 3.6',
239229
],
240230
packages = ['bugzilla'],
241-
scripts=['bin/bugzilla'],
231+
entry_points={'console_scripts': ['bugzilla = bugzilla._cli:main']},
242232
data_files=[('share/man/man1', ['bugzilla.1'])],
243233

244234
install_requires=_parse_requirements("requirements.txt"),

tests/__init__.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import atexit
55
import difflib
6-
import imp
76
import os
87
import shlex
98
import sys
@@ -15,26 +14,9 @@
1514
from StringIO import StringIO
1615
# pylint: enable=import-error
1716

18-
from bugzilla import Bugzilla, RHBugzilla
17+
from bugzilla import Bugzilla, RHBugzilla, _cli
1918

2019

21-
_cleanup = []
22-
23-
24-
def _import(name, path):
25-
_cleanup.append(path + "c")
26-
return imp.load_source(name, path)
27-
28-
29-
def _cleanup_cb():
30-
for f in _cleanup:
31-
if os.path.exists(f):
32-
os.unlink(f)
33-
34-
35-
atexit.register(_cleanup_cb)
36-
bugzillascript = _import("bugzillascript", "bin/bugzilla")
37-
3820
# This is overwritten by python setup.py test --redhat-url, and then
3921
# used in ro/rw tests
4022
REDHAT_URL = None
@@ -102,7 +84,7 @@ def clicomm(argv, bzinstance, returnmain=False, printcliout=False,
10284
print(" ".join(argv))
10385
print()
10486

105-
mainout = bugzillascript.main(unittest_bz_instance=bzinstance)
87+
mainout = _cli.main(unittest_bz_instance=bzinstance)
10688
except SystemExit as sys_e:
10789
ret = sys_e.code
10890

0 commit comments

Comments
 (0)