From 039f3d8a59c62d3ad14288ed1b6a97aab539731e Mon Sep 17 00:00:00 2001 From: John Bresnahan Date: Tue, 2 Apr 2013 11:09:24 -1000 Subject: [PATCH] Convert scripts to entry points The executable programs used by Glance have historically been scripts in the ./bin directory. This patch converts all of the scripts to entry_points. This change makes these programs python modules. Thus they can be imported and methods in them can be called just like any other module. This will allow the tests to call into these programs directly instead of having to fork out a process. The conf.py file in the doc tree was causing a name collision with the python module cmd. The glance/glance directory was being added to sys.path which made glance.cmd import with the name cmd. This patch also fixes that problem. blueprint: refactoring-better-faster-stronger-functional-tests Change-Id: I67ae14b7403af31a5944befcd2ec27a690e81f15 --- doc/source/conf.py | 2 +- glance/cmd/__init__.py | 16 ++++++++ bin/glance-api => glance/cmd/api.py | 2 +- .../cmd/cache_cleaner.py | 2 +- .../cmd/cache_manage.py | 2 +- .../cmd/cache_prefetcher.py | 2 +- .../cmd/cache_pruner.py | 2 +- bin/glance-control => glance/cmd/control.py | 6 ++- bin/glance-manage => glance/cmd/manage.py | 4 -- bin/glance-registry => glance/cmd/registry.py | 2 +- .../cmd/replicator.py | 2 +- bin/glance-scrubber => glance/cmd/scrubber.py | 8 ++-- glance/tests/functional/__init__.py | 9 +++-- .../test_bin_glance_cache_manage.py | 38 +++++++++---------- .../tests/functional/test_cache_middleware.py | 2 +- glance/tests/functional/test_glance_manage.py | 2 +- glance/tests/functional/test_scrubber.py | 6 +-- glance/tests/unit/test_glance_replicator.py | 15 +------- run_tests.sh | 2 +- setup.py | 21 +++++----- tox.ini | 1 - 21 files changed, 74 insertions(+), 72 deletions(-) create mode 100644 glance/cmd/__init__.py rename bin/glance-api => glance/cmd/api.py (98%) rename bin/glance-cache-cleaner => glance/cmd/cache_cleaner.py (98%) rename bin/glance-cache-manage => glance/cmd/cache_manage.py (99%) rename bin/glance-cache-prefetcher => glance/cmd/cache_prefetcher.py (98%) rename bin/glance-cache-pruner => glance/cmd/cache_pruner.py (98%) rename bin/glance-control => glance/cmd/control.py (99%) rename bin/glance-manage => glance/cmd/manage.py (99%) rename bin/glance-registry => glance/cmd/registry.py (98%) rename bin/glance-replicator => glance/cmd/replicator.py (99%) rename bin/glance-scrubber => glance/cmd/scrubber.py (93%) diff --git a/doc/source/conf.py b/doc/source/conf.py index 8b546d65e2..f926b61b47 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -33,7 +33,7 @@ import sys # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. -sys.path = [os.path.abspath('../../glance'), +sys.path = [ os.path.abspath('../..'), os.path.abspath('../../bin') ] + sys.path diff --git a/glance/cmd/__init__.py b/glance/cmd/__init__.py new file mode 100644 index 0000000000..c65c52d23a --- /dev/null +++ b/glance/cmd/__init__.py @@ -0,0 +1,16 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2013 OpenStack LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. diff --git a/bin/glance-api b/glance/cmd/api.py similarity index 98% rename from bin/glance-api rename to glance/cmd/api.py index f8f49be03a..d032ad17b5 100755 --- a/bin/glance-api +++ b/glance/cmd/api.py @@ -52,7 +52,7 @@ def fail(returncode, e): sys.exit(returncode) -if __name__ == '__main__': +def main(): try: config.parse_args() log.setup('glance') diff --git a/bin/glance-cache-cleaner b/glance/cmd/cache_cleaner.py similarity index 98% rename from bin/glance-cache-cleaner rename to glance/cmd/cache_cleaner.py index c26814c517..84f55ce409 100755 --- a/bin/glance-cache-cleaner +++ b/glance/cmd/cache_cleaner.py @@ -55,7 +55,7 @@ from glance.openstack.common import log CONF = cfg.CONF -if __name__ == '__main__': +def main(): try: config.parse_cache_args() log.setup('glance') diff --git a/bin/glance-cache-manage b/glance/cmd/cache_manage.py similarity index 99% rename from bin/glance-cache-manage rename to glance/cmd/cache_manage.py index 9fdcb11c56..70649d9270 100755 --- a/bin/glance-cache-manage +++ b/glance/cmd/cache_manage.py @@ -473,7 +473,7 @@ def user_confirm(prompt, default=False): return answer.lower() in ("yes", "y") -if __name__ == '__main__': +def main(): usage = """ %prog [options] [args] diff --git a/bin/glance-cache-prefetcher b/glance/cmd/cache_prefetcher.py similarity index 98% rename from bin/glance-cache-prefetcher rename to glance/cmd/cache_prefetcher.py index 914f62365d..06988f25b4 100755 --- a/bin/glance-cache-prefetcher +++ b/glance/cmd/cache_prefetcher.py @@ -47,7 +47,7 @@ import glance.store CONF = cfg.CONF -if __name__ == '__main__': +def main(): try: config.parse_cache_args() log.setup('glance') diff --git a/bin/glance-cache-pruner b/glance/cmd/cache_pruner.py similarity index 98% rename from bin/glance-cache-pruner rename to glance/cmd/cache_pruner.py index 9f6d3b4112..9cbed315d7 100755 --- a/bin/glance-cache-pruner +++ b/glance/cmd/cache_pruner.py @@ -47,7 +47,7 @@ from glance.openstack.common import log CONF = cfg.CONF -if __name__ == '__main__': +def main(): try: config.parse_cache_args() log.setup('glance') diff --git a/bin/glance-control b/glance/cmd/control.py similarity index 99% rename from bin/glance-control rename to glance/cmd/control.py index ebd2eb2228..77376202a1 100755 --- a/bin/glance-control +++ b/glance/cmd/control.py @@ -70,6 +70,8 @@ And command is one of: And CONFPATH is the optional configuration file to use.""" +exitcode = 0 + def gated_by(predicate): def wrap(f): @@ -281,8 +283,8 @@ def add_command_parsers(subparsers): parser.set_defaults(servers=['glance-' + s for s in ALL_SERVERS]) -if __name__ == '__main__': - exitcode = 0 +def main(): + global exitcode opts = [ cfg.SubCommandOpt('server', diff --git a/bin/glance-manage b/glance/cmd/manage.py similarity index 99% rename from bin/glance-manage rename to glance/cmd/manage.py index d86cb5474a..a5de5e887d 100755 --- a/bin/glance-manage +++ b/glance/cmd/manage.py @@ -130,7 +130,3 @@ def main(): CONF.command.func() except exception.GlanceException as e: sys.exit("ERROR: %s" % e) - - -if __name__ == '__main__': - main() diff --git a/bin/glance-registry b/glance/cmd/registry.py similarity index 98% rename from bin/glance-registry rename to glance/cmd/registry.py index d1ca2300ef..b45657062d 100755 --- a/bin/glance-registry +++ b/glance/cmd/registry.py @@ -45,7 +45,7 @@ from glance.common import wsgi from glance.openstack.common import log -if __name__ == '__main__': +def main(): try: config.parse_args() log.setup('glance') diff --git a/bin/glance-replicator b/glance/cmd/replicator.py similarity index 99% rename from bin/glance-replicator rename to glance/cmd/replicator.py index aada6c91ab..48af08f823 100755 --- a/bin/glance-replicator +++ b/glance/cmd/replicator.py @@ -670,7 +670,7 @@ def lookup_command(parser, command_name): return command -if __name__ == '__main__': +def main(): usage = """ %%prog [options] [args] diff --git a/bin/glance-scrubber b/glance/cmd/scrubber.py similarity index 93% rename from bin/glance-scrubber rename to glance/cmd/scrubber.py index 189a6a4171..21993c595c 100755 --- a/bin/glance-scrubber +++ b/glance/cmd/scrubber.py @@ -39,12 +39,12 @@ from oslo.config import cfg from glance.common import config from glance.openstack.common import log import glance.store -from glance.store import scrubber +import glance.store.scrubber CONF = cfg.CONF -if __name__ == '__main__': +def main(): CONF.register_cli_opt( cfg.BoolOpt('daemon', short='D', @@ -64,10 +64,10 @@ if __name__ == '__main__': glance.store.create_stores() glance.store.verify_default_store() - app = scrubber.Scrubber() + app = glance.store.scrubber.Scrubber() if CONF.daemon: - server = scrubber.Daemon(CONF.wakeup_time) + server = glance.store.scrubber.Daemon(CONF.wakeup_time) server.start(app) server.wait() else: diff --git a/glance/tests/functional/__init__.py b/glance/tests/functional/__init__.py index 0a24cd6abe..b52b47f5dc 100644 --- a/glance/tests/functional/__init__.py +++ b/glance/tests/functional/__init__.py @@ -68,7 +68,7 @@ class Server(object): self.conf_file_name = None self.conf_base = None self.paste_conf_base = None - self.server_control = './bin/glance-control' + self.server_control = 'glance-control' self.exec_env = None self.deployment_flavor = '' self.show_image_direct_url = False @@ -205,7 +205,7 @@ class Server(object): os.system('cp %s %s/tests.sqlite' % (db_location, self.test_dir)) else: - cmd = ('bin/glance-manage --config-file %s db_sync' + cmd = ('glance-manage --config-file %s db_sync' % conf_filepath) execute(cmd, no_venv=self.no_venv, exec_env=self.exec_env, expect_exit=True) @@ -696,8 +696,9 @@ class FunctionalTest(test_utils.BaseTestCase): def start_servers(self, **kwargs): """ - Starts the API and Registry servers (bin/glance-control api start - & bin/glance-control registry start) on unused ports. + Starts the API and Registry servers (glance-control api start + & glance-control registry start) on unused ports. glance-control + should be installed into the python path Any kwargs passed to this method will override the configuration value in the conf file used in starting the servers. diff --git a/glance/tests/functional/test_bin_glance_cache_manage.py b/glance/tests/functional/test_bin_glance_cache_manage.py index 205ccedd40..6768782309 100644 --- a/glance/tests/functional/test_bin_glance_cache_manage.py +++ b/glance/tests/functional/test_bin_glance_cache_manage.py @@ -70,7 +70,7 @@ class TestBinGlanceCacheManage(functional.FunctionalTest): """ Return True if supplied image ID is cached, False otherwise """ - cmd = "bin/glance-cache-manage --port=%d list-cached" % self.api_port + cmd = "glance-cache-manage --port=%d list-cached" % self.api_port exitcode, out, err = execute(cmd) @@ -81,7 +81,7 @@ class TestBinGlanceCacheManage(functional.FunctionalTest): """ Return True if supplied image ID is cached, False otherwise """ - cmd = "bin/glance-cache-manage --port=%d list-cached" % self.api_port + cmd = "glance-cache-manage --port=%d list-cached" % self.api_port exitcode, out, err = execute(cmd) @@ -99,7 +99,7 @@ class TestBinGlanceCacheManage(functional.FunctionalTest): registry_port = self.registry_port # Verify decent error message returned - cmd = "bin/glance-cache-manage --port=%d list-cached" % api_port + cmd = "glance-cache-manage --port=%d list-cached" % api_port exitcode, out, err = execute(cmd, raise_error=False) @@ -120,7 +120,7 @@ class TestBinGlanceCacheManage(functional.FunctionalTest): registry_port = self.registry_port # Verify no cached images - cmd = "bin/glance-cache-manage --port=%d list-cached" % api_port + cmd = "glance-cache-manage --port=%d list-cached" % api_port exitcode, out, err = execute(cmd) @@ -159,7 +159,7 @@ class TestBinGlanceCacheManage(functional.FunctionalTest): registry_port = self.registry_port # Verify no cached images - cmd = "bin/glance-cache-manage --port=%d list-cached" % api_port + cmd = "glance-cache-manage --port=%d list-cached" % api_port exitcode, out, err = execute(cmd) @@ -167,7 +167,7 @@ class TestBinGlanceCacheManage(functional.FunctionalTest): self.assertTrue('No cached images' in out.strip()) # Verify no queued images - cmd = "bin/glance-cache-manage --port=%d list-queued" % api_port + cmd = "glance-cache-manage --port=%d list-queued" % api_port exitcode, out, err = execute(cmd) @@ -182,7 +182,7 @@ class TestBinGlanceCacheManage(functional.FunctionalTest): ids[x] = self.add_image("Image%s" % x) # Queue second image and then cache it - cmd = "bin/glance-cache-manage --port=%d --force queue-image %s" % ( + cmd = "glance-cache-manage --port=%d --force queue-image %s" % ( api_port, ids[1]) exitcode, out, err = execute(cmd) @@ -190,7 +190,7 @@ class TestBinGlanceCacheManage(functional.FunctionalTest): self.assertEqual(0, exitcode) # Verify queued second image - cmd = "bin/glance-cache-manage --port=%d list-queued" % api_port + cmd = "glance-cache-manage --port=%d list-queued" % api_port exitcode, out, err = execute(cmd) @@ -219,7 +219,7 @@ metadata_encryption_key = %(metadata_encryption_key)s log_file = %(log_file)s """ % cache_file_options) - cmd = ("bin/glance-cache-prefetcher --config-file %s" % + cmd = ("glance-cache-prefetcher --config-file %s" % cache_config_filepath) exitcode, out, err = execute(cmd) @@ -228,7 +228,7 @@ log_file = %(log_file)s self.assertEqual('', out.strip(), out) # Verify no queued images - cmd = "bin/glance-cache-manage --port=%d list-queued" % api_port + cmd = "glance-cache-manage --port=%d list-queued" % api_port exitcode, out, err = execute(cmd) @@ -236,7 +236,7 @@ log_file = %(log_file)s self.assertTrue('No queued images' in out.strip()) # Verify second image now cached - cmd = "bin/glance-cache-manage --port=%d list-cached" % api_port + cmd = "glance-cache-manage --port=%d list-cached" % api_port exitcode, out, err = execute(cmd) @@ -244,7 +244,7 @@ log_file = %(log_file)s self.assertTrue(ids[1] in out, 'Image %s was not cached!' % ids[1]) # Queue third image and then delete it from queue - cmd = "bin/glance-cache-manage --port=%d --force queue-image %s" % ( + cmd = "glance-cache-manage --port=%d --force queue-image %s" % ( api_port, ids[2]) exitcode, out, err = execute(cmd) @@ -252,7 +252,7 @@ log_file = %(log_file)s self.assertEqual(0, exitcode) # Verify queued third image - cmd = "bin/glance-cache-manage --port=%d list-queued" % api_port + cmd = "glance-cache-manage --port=%d list-queued" % api_port exitcode, out, err = execute(cmd) @@ -260,7 +260,7 @@ log_file = %(log_file)s self.assertTrue(ids[2] in out, 'Image %s was not queued!' % ids[2]) # Delete the image from the queue - cmd = ("bin/glance-cache-manage --port=%d --force " + cmd = ("glance-cache-manage --port=%d --force " "delete-queued-image %s") % (api_port, ids[2]) exitcode, out, err = execute(cmd) @@ -268,7 +268,7 @@ log_file = %(log_file)s self.assertEqual(0, exitcode) # Verify no queued images - cmd = "bin/glance-cache-manage --port=%d list-queued" % api_port + cmd = "glance-cache-manage --port=%d list-queued" % api_port exitcode, out, err = execute(cmd) @@ -277,7 +277,7 @@ log_file = %(log_file)s # Queue all images for x in xrange(0, 4): - cmd = ("bin/glance-cache-manage --port=%d --force " + cmd = ("glance-cache-manage --port=%d --force " "queue-image %s") % (api_port, ids[x]) exitcode, out, err = execute(cmd) @@ -285,7 +285,7 @@ log_file = %(log_file)s self.assertEqual(0, exitcode) # Verify queued third image - cmd = "bin/glance-cache-manage --port=%d list-queued" % api_port + cmd = "glance-cache-manage --port=%d list-queued" % api_port exitcode, out, err = execute(cmd) @@ -293,7 +293,7 @@ log_file = %(log_file)s self.assertTrue('Found 3 queued images' in out) # Delete the image from the queue - cmd = ("bin/glance-cache-manage --port=%d --force " + cmd = ("glance-cache-manage --port=%d --force " "delete-all-queued-images") % (api_port) exitcode, out, err = execute(cmd) @@ -301,7 +301,7 @@ log_file = %(log_file)s self.assertEqual(0, exitcode) # Verify nothing in queue anymore - cmd = "bin/glance-cache-manage --port=%d list-queued" % api_port + cmd = "glance-cache-manage --port=%d list-queued" % api_port exitcode, out, err = execute(cmd) diff --git a/glance/tests/functional/test_cache_middleware.py b/glance/tests/functional/test_cache_middleware.py index e704b85c13..52ada69165 100644 --- a/glance/tests/functional/test_cache_middleware.py +++ b/glance/tests/functional/test_cache_middleware.py @@ -535,7 +535,7 @@ log_file = %(log_file)s self.verify_no_cached_images() - cmd = ("bin/glance-cache-prefetcher --config-file %s" % + cmd = ("glance-cache-prefetcher --config-file %s" % cache_config_filepath) exitcode, out, err = execute(cmd) diff --git a/glance/tests/functional/test_glance_manage.py b/glance/tests/functional/test_glance_manage.py index 79324bda54..478415ad13 100644 --- a/glance/tests/functional/test_glance_manage.py +++ b/glance/tests/functional/test_glance_manage.py @@ -43,7 +43,7 @@ class TestGlanceManage(functional.FunctionalTest): conf_file.write(self.connection) conf_file.flush() - cmd = ('bin/glance-manage --config-file %s db_sync' % + cmd = ('glance-manage --config-file %s db_sync' % self.conf_filepath) execute(cmd, raise_error=True) diff --git a/glance/tests/functional/test_scrubber.py b/glance/tests/functional/test_scrubber.py index 055cff1d04..19721ae617 100644 --- a/glance/tests/functional/test_scrubber.py +++ b/glance/tests/functional/test_scrubber.py @@ -120,7 +120,7 @@ class TestScrubber(functional.FunctionalTest): time.sleep(self.api_server.scrub_time) # scrub images and make sure they get deleted - cmd = ("bin/glance-scrubber --config-file %s" % + cmd = ("glance-scrubber --config-file %s" % self.scrubber_daemon.conf_file_name) exitcode, out, err = execute(cmd, raise_error=False) self.assertEqual(0, exitcode) @@ -180,7 +180,7 @@ class TestScrubber(functional.FunctionalTest): time.sleep(self.api_server.scrub_time) # call the scrubber to scrub images - cmd = ("bin/glance-scrubber --config-file %s" % + cmd = ("glance-scrubber --config-file %s" % self.scrubber_daemon.conf_file_name) exitcode, out, err = execute(cmd, raise_error=False) self.assertEqual(0, exitcode) @@ -318,7 +318,7 @@ class TestScrubber(functional.FunctionalTest): time.sleep(self.api_server.scrub_time) # run the scrubber app, and ensure it doesn't fall over - cmd = ("bin/glance-scrubber --config-file %s" % + cmd = ("glance-scrubber --config-file %s" % self.scrubber_daemon.conf_file_name) exitcode, out, err = execute(cmd, raise_error=False) self.assertEqual(0, exitcode) diff --git a/glance/tests/unit/test_glance_replicator.py b/glance/tests/unit/test_glance_replicator.py index 693971f7f9..7eb21c3d34 100644 --- a/glance/tests/unit/test_glance_replicator.py +++ b/glance/tests/unit/test_glance_replicator.py @@ -15,7 +15,6 @@ # under the License. import copy -import imp import json import os import StringIO @@ -26,19 +25,7 @@ import uuid import fixtures from glance.tests import utils as test_utils - - -TOPDIR = os.path.normpath(os.path.join( - os.path.dirname(os.path.abspath(__file__)), - os.pardir, - os.pardir, - os.pardir)) -GLANCE_REPLICATOR_PATH = os.path.join(TOPDIR, 'bin', 'glance-replicator') - -sys.dont_write_bytecode = True -glance_replicator = imp.load_source('glance_replicator', - GLANCE_REPLICATOR_PATH) -sys.dont_write_bytecode = False +from glance.cmd import replicator as glance_replicator IMG_RESPONSE_ACTIVE = { diff --git a/run_tests.sh b/run_tests.sh index d9e1de3c5c..073601f4f4 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -71,7 +71,7 @@ function run_pep8 { PEP8_EXCLUDE=".venv,.tox,dist,doc,openstack" PEP8_OPTIONS="--exclude=$PEP8_EXCLUDE --repeat" PEP8_IGNORE="--ignore=E125,E126,E711,E712" - PEP8_INCLUDE=". bin/*" + PEP8_INCLUDE="." ${wrapper} pep8 $PEP8_OPTIONS $PEP8_INCLUDE $PEP8_IGNORE } diff --git a/setup.py b/setup.py index e5dd11e777..ba7b99ade3 100644 --- a/setup.py +++ b/setup.py @@ -45,14 +45,15 @@ setuptools.setup( 'Environment :: No Input/Output (Daemon)', 'Environment :: OpenStack', ], - scripts=['bin/glance-api', - 'bin/glance-cache-prefetcher', - 'bin/glance-cache-pruner', - 'bin/glance-cache-manage', - 'bin/glance-cache-cleaner', - 'bin/glance-control', - 'bin/glance-manage', - 'bin/glance-registry', - 'bin/glance-replicator', - 'bin/glance-scrubber'], + entry_points={'console_scripts': + ['glance-api=glance.cmd.api:main', + 'glance-cache-prefetcher=glance.cmd.cache_prefetcher:main', + 'glance-cache-pruner = glance.cmd.cache_pruner:main', + 'glance-cache-manage = glance.cmd.cache_manage:main', + 'glance-cache-cleaner = glance.cmd.cache_cleaner:main', + 'glance-control = glance.cmd.control:main', + 'glance-manage = glance.cmd.manage:main', + 'glance-registry = glance.cmd.registry:main', + 'glance-replicator = glance.cmd.replicator:main', + 'glance-scrubber = glance.cmd.scrubber:main']}, py_modules=[]) diff --git a/tox.ini b/tox.ini index b9f8537415..4e514811e3 100644 --- a/tox.ini +++ b/tox.ini @@ -19,7 +19,6 @@ downloadcache = ~/cache/pip [testenv:pep8] commands = pep8 --ignore=E125,E126,E711,E712 --repeat --show-source --exclude=.venv,.tox,dist,doc,openstack . - pep8 --ignore=E125,E126,E711,E712 --repeat --show-source --filename=glance* bin [testenv:cover] setenv = NOSE_WITH_COVERAGE=1