From 7f3571d145cfbd18bbba371fd3a1e91c5e9cbdcf Mon Sep 17 00:00:00 2001 From: Qixiang Wan Date: May 18 2017 04:56:05 +0000 Subject: [PATCH 1/2] Fix some flake8 errors --- diff --git a/freshmaker/__init__.py b/freshmaker/__init__.py index 1227bbb..6e118e9 100644 --- a/freshmaker/__init__.py +++ b/freshmaker/__init__.py @@ -40,5 +40,3 @@ db = SQLAlchemy(app) conf = init_config(app) init_logging(conf) log = getLogger(__name__) - -from freshmaker import views diff --git a/freshmaker/handlers/image_builder.py b/freshmaker/handlers/image_builder.py index cd2b62a..63b674f 100644 --- a/freshmaker/handlers/image_builder.py +++ b/freshmaker/handlers/image_builder.py @@ -56,9 +56,9 @@ class DockerImageRebuildHandler(BaseHandler): try: task_id = self.build_image(repo_url=event.repo_url, - rev=event.rev, - branch=event.branch, - namespace=event.namespace) + rev=event.rev, + branch=event.branch, + namespace=event.namespace) self.record_build(event, event.repo, 'image', task_id) diff --git a/freshmaker/manage.py b/freshmaker/manage.py index 6b38a14..83c5198 100644 --- a/freshmaker/manage.py +++ b/freshmaker/manage.py @@ -26,11 +26,6 @@ import logging import os import ssl -import fedmsg.config -import moksha.hub -import moksha.hub.hub -import moksha.hub.reactor - from freshmaker import app, conf, db from freshmaker import models @@ -54,7 +49,7 @@ Usage: {0} [{1}] See also: freshmaker-manager(1)""".format(command, - '|'.join(help_args))) + '|'.join(help_args))) sys.exit(2) r = f(*args, **kwargs) return r @@ -98,6 +93,7 @@ def upgradedb(): with app.app_context(): flask_migrate.upgrade(directory=migrations_dir) + @console_script_help @manager.command def cleardb(): @@ -107,6 +103,7 @@ def cleardb(): models.ArtifactBuild.query.delete() db.session.commit() + @manager.command @console_script_help def generatelocalhostcert(): @@ -162,5 +159,6 @@ def runssl(host=conf.host, port=conf.port, debug=conf.debug): debug=debug ) + if __name__ == "__main__": manager.run() diff --git a/freshmaker/models.py b/freshmaker/models.py index a1287be..052b827 100644 --- a/freshmaker/models.py +++ b/freshmaker/models.py @@ -47,7 +47,7 @@ ARTIFACT_TYPES = { "rpm": 0, "image": 1, "module": 2, - } +} INVERSE_ARTIFACT_TYPES = {v: k for k, v in ARTIFACT_TYPES.items()} diff --git a/freshmaker/views.py b/freshmaker/views.py index 4eed73e..2e995de 100644 --- a/freshmaker/views.py +++ b/freshmaker/views.py @@ -21,12 +21,9 @@ # # Written by Jan Kaluza -import json -from flask import request, jsonify from flask.views import MethodView -from freshmaker import app, conf, log -from freshmaker import models, db +from freshmaker import app api_v1 = { 'freshmaker': { @@ -44,6 +41,7 @@ class FreshmakerAPI(MethodView): def get(self, id): return "Done", 200 + def register_api_v1(): """ Registers version 1 of MBS API. """ module_view = FreshmakerAPI.as_view('freshmaker') @@ -53,4 +51,5 @@ def register_api_v1(): view_func=module_view, **val['options']) + register_api_v1() diff --git a/tests/handlers/test_image_builder.py b/tests/handlers/test_image_builder.py index 75f18bf..04f3394 100644 --- a/tests/handlers/test_image_builder.py +++ b/tests/handlers/test_image_builder.py @@ -245,18 +245,17 @@ class TestRebuildWhenBodhiUpdateStable(BaseTestCase): buildContainer = session.buildContainer self.assertEqual(2, buildContainer.call_count) buildContainer.assert_has_calls([ - call('{}/container/{}?#{}'.format(conf.git_base_url, - 'testimage1', - last_commit_hash), - 'f25-container-candidate', - {'scratch': True, 'git_branch': 'f25'}), - call('{}/container/{}?#{}'.format(conf.git_base_url, - 'testimage2', - last_commit_hash), - 'f25-container-candidate', - {'scratch': True, 'git_branch': 'f25'}), - ], - any_order=True) + call('{}/container/{}?#{}'.format(conf.git_base_url, + 'testimage1', + last_commit_hash), + 'f25-container-candidate', + {'scratch': True, 'git_branch': 'f25'}), + call('{}/container/{}?#{}'.format(conf.git_base_url, + 'testimage2', + last_commit_hash), + 'f25-container-candidate', + {'scratch': True, 'git_branch': 'f25'}), + ], any_order=True) events = models.Event.query.all() self.assertEquals(len(events), 1) diff --git a/tests/helpers.py b/tests/helpers.py index 8235874..dd6e0cd 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -123,13 +123,15 @@ class DistGitMessage(FedMsgFactory): self.repo = repo self.branch = branch self.rev = rev - self.stats = {'files': {}, - 'total': { - 'additions': 0, - 'deletions': 0, - 'files': 0, - 'lines': 0, - }} + self.stats = { + 'files': {}, + 'total': { + 'additions': 0, + 'deletions': 0, + 'files': 0, + 'lines': 0, + } + } @property def inner_msg(self): diff --git a/tests/test_buildsys_handler.py b/tests/test_buildsys_handler.py index a33be7e..6c0e181 100644 --- a/tests/test_buildsys_handler.py +++ b/tests/test_buildsys_handler.py @@ -22,7 +22,7 @@ import os import sys import unittest -sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) # noqa from tests import helpers from freshmaker import events, db, models @@ -87,5 +87,6 @@ class BuildsysHandlerTest(helpers.FreshmakerTestCase): build = models.ArtifactBuild.query.all()[0] self.assertEqual(build.state, models.BUILD_STATES['done']) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_mbs_handler.py b/tests/test_mbs_handler.py index 2711cd6..6fc0a61 100644 --- a/tests/test_mbs_handler.py +++ b/tests/test_mbs_handler.py @@ -23,7 +23,7 @@ import sys import unittest import mock -sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) # noqa from tests import helpers from freshmaker import events, db, models @@ -373,5 +373,6 @@ class MBSHandlerTest(helpers.FreshmakerTestCase): handler.handle(event) handler.rebuild_module.assert_not_called() + if __name__ == '__main__': unittest.main() diff --git a/tests/test_models.py b/tests/test_models.py index e1d7e0f..3d89fd9 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -22,7 +22,7 @@ import unittest -from freshmaker import conf, db +from freshmaker import db from freshmaker.models import Event, ArtifactBuild @@ -41,7 +41,7 @@ class TestModels(unittest.TestCase): def test_creating_event_and_builds(self): event = Event.create(db.session, "test_msg_id") build = ArtifactBuild.create(db.session, event, "ed", "module", 1234) - build2 = ArtifactBuild.create(db.session, event, "mksh", "module", 1235, build) + ArtifactBuild.create(db.session, event, "mksh", "module", 1235, build) db.session.commit() db.session.expire_all() From d34b5002a853b8b8d29423a9caf8ffde139fc47b Mon Sep 17 00:00:00 2001 From: Qixiang Wan Date: May 18 2017 08:20:21 +0000 Subject: [PATCH 2/2] Fix bandit issue: B108:hardcoded_tmp_directory Replace hard-coded '/tmp' with tempfile.gettempdir() --- diff --git a/freshmaker/utils.py b/freshmaker/utils.py index c7c5622..12b80ff 100644 --- a/freshmaker/utils.py +++ b/freshmaker/utils.py @@ -110,9 +110,12 @@ def get_commit_hash(repo, revision='HEAD'): return _run_command(cmd, rundir=repo, return_output=True).strip() -def _run_command(command, logger=None, rundir='/tmp', output=subprocess.PIPE, error=subprocess.PIPE, env=None, return_output=False): +def _run_command(command, logger=None, rundir=None, output=subprocess.PIPE, error=subprocess.PIPE, env=None, return_output=False): """Run a command, return output if return_output is True. Error out if command exit with non-zero code.""" + if rundir is None: + rundir = tempfile.gettempdir() + if logger: logger.info("Running %s", subprocess.list2cmdline(command))