Пример #1
0
def _ecr_login(project, registry):
    command = ExternalCommandBuilder('aws', project,
                                     Reactor.current_instance())
    command.use_argument('ecr')
    command.use_argument('get-authorization-token')
    command.use_argument('--output')
    command.use_argument('text')
    command.use_argument('--query')
    command.use_argument('authorizationData[].authorizationToken')
    res = command.run("{}/{}".format(prepare_reports_directory(project),
                                     'docker_ecr_get_token'))
    if res.exit_code > 0:
        raise Exception("Error getting token")
    pass_token = base64.b64decode(res.report_lines[0])
    split = pass_token.split(":")
    command = ExternalCommandBuilder('docker', project,
                                     Reactor.current_instance())
    command.use_argument('login')
    command.use_argument('-u')
    command.use_argument('{0}').formatted_with(split[0])
    command.use_argument('-p')
    command.use_argument('{0}').formatted_with(split[1])
    command.use_argument('{0}').formatted_with(registry)
    res = command.run("{}/{}".format(prepare_reports_directory(project),
                                     'docker_ecr_docker_login'))
    if res.exit_code > 0:
        raise Exception("Error authenticating")
Пример #2
0
 def prepare_reactor(self):
     logger = StdOutLogger(threshold=Logger.DEBUG)
     execution_manager = ExecutionManager(logger)
     reactor = Reactor(logger, execution_manager)
     print(self.tmp_directory)
     reactor.prepare_build(project_directory=self.tmp_directory)
     return reactor
Пример #3
0
 def setUp(self):
     self.old_reactor = Reactor.current_instance()
     self.plugin_loader_mock = Mock(PluginLoader)
     self.logger = Mock()
     self.execution_manager = Mock(ExecutionManager)
     self.reactor = Reactor(
         self.logger, self.execution_manager, self.plugin_loader_mock)
Пример #4
0
 def setUp(self):
     self.old_reactor = Reactor.current_instance()
     self.plugin_loader_mock = Mock(PluginLoader)
     self.logger = Mock()
     self.execution_manager = Mock(ExecutionManager)
     self.reactor = Reactor(
         self.logger, self.execution_manager, self.plugin_loader_mock)
Пример #5
0
def do_docker_package(project, logger):
    project.set_property_if_unset("docker_package_build_dir",
                                  "src/main/docker")
    project.set_property_if_unset("docker_package_build_image", project.name)
    project.set_property_if_unset("docker_package_build_version",
                                  project.version)
    report_dir = prepare_reports_directory(project)
    dist_dir = prepare_dist_directory(project)
    assert_can_execute(["docker", "--version"],
                       prerequisite="docker",
                       caller="docker_package",
                       env=None)
    # is true if user set verbose in build.py or from command line
    verbose = project.get_property("verbose")
    project.set_property_if_unset("docker_package_verbose_output", verbose)
    temp_build_img = 'pyb-temp-{}:{}'.format(project.name, project.version)
    build_img = get_build_img(project)
    logger.info("Executing primary stage docker build for image - {}.".format(
        build_img))
    # docker build --build-arg buildVersion=${BUILD_NUMBER} -t ${BUILD_IMG} src/
    command = ExternalCommandBuilder('docker', project,
                                     Reactor.current_instance())
    command.use_argument('build')
    command.use_argument('--build-arg')
    command.use_argument('buildVersion={0}').formatted_with_property(
        'docker_package_build_version')
    command.use_argument('-t')
    command.use_argument('{0}').formatted_with(temp_build_img)
    command.use_argument('{0}').formatted_with_property(
        'docker_package_build_dir')
    result = command.run("{}/{}".format(report_dir, 'docker_package_build'))
    if result.exit_code != 0:
        logger.error(result.error_report_lines)
        raise Exception("Error building primary stage docker image")
    write_docker_build_file(project=project,
                            logger=logger,
                            build_image=temp_build_img,
                            dist_dir=dist_dir)
    copy_dist_file(project=project, dist_dir=dist_dir, logger=logger)
    logger.info(
        "Executing secondary stage docker build for image - {}.".format(
            build_img))
    command = ExternalCommandBuilder('docker', project,
                                     Reactor.current_instance())
    command.use_argument('build')
    command.use_argument('-t')
    command.use_argument('{0}').formatted_with(build_img)
    command.use_argument('{0}').formatted_with(dist_dir)
    result = command.run("{}/{}".format(report_dir, 'docker_package_img'))
    if result.exit_code != 0:
        logger.error(result.error_report_lines)
        raise Exception("Error building docker image")
    logger.info(
        "Finished build docker image - {} - with dist file - {}".format(
            build_img, dist_dir))
Пример #6
0
def _run_tag_cmd(project, local_img, remote_img, logger):
    logger.info("Tagging local docker image {} - {}".format(
        local_img, remote_img))
    report_dir = prepare_reports_directory(project)
    command = ExternalCommandBuilder('docker', project,
                                     Reactor.current_instance())
    command.use_argument('tag')
    command.use_argument('{0}').formatted_with(local_img)
    command.use_argument('{0}').formatted_with(remote_img)
    command.run("{}/{}".format(report_dir, 'docker_push_tag'))
Пример #7
0
def get_all_dependencies_for_task(task):
    """
    Returns a list containing all tasks required by the given
    task function (but not the given task itself)
    """
    from pybuilder.reactor import Reactor
    task_name = task.__name__
    execution_manager = Reactor.current_instance().execution_manager
    task_and_all_dependencies = execution_manager.collect_all_transitive_tasks([task_name])
    return [dependency for dependency in task_and_all_dependencies if dependency.name != task_name]
Пример #8
0
def _create_ecr_registry(fq_artifact, project):
    command = ExternalCommandBuilder('aws', project,
                                     Reactor.current_instance())
    command.use_argument('ecr')
    command.use_argument('describe-repositories')
    command.use_argument('--repository-names')
    command.use_argument('{0}').formatted_with(fq_artifact)
    res = command.run("{}/{}".format(prepare_reports_directory(project),
                                     'docker_ecr_registry_discover'))
    if res.exit_code > 0:
        command = ExternalCommandBuilder('aws', project,
                                         Reactor.current_instance())
        command.use_argument('ecr')
        command.use_argument('create-repository')
        command.use_argument('--repository-name')
        command.use_argument('{0}').formatted_with(fq_artifact)
        res = command.run("{}/{}".format(prepare_reports_directory(project),
                                         'docker_ecr_registry_create'))
        if res.exit_code > 0:
            raise Exception("Unable to create ecr registry")
Пример #9
0
def get_all_dependencies_for_task(task):
    """
    Returns a list containing all tasks required by the given
    task function (but not the given task itself)
    """
    from pybuilder.reactor import Reactor

    task_name = task.__name__
    execution_manager = Reactor.current_instance().execution_manager
    task_and_all_dependencies = execution_manager.collect_all_transitive_tasks([task_name])
    return [dependency for dependency in task_and_all_dependencies if dependency.name != task_name]
Пример #10
0
def _run_push_cmd(project, remote_img, logger):
    logger.info("Pushing remote docker image - {}".format(remote_img))
    report_dir = prepare_reports_directory(project)
    command = ExternalCommandBuilder('docker', project,
                                     Reactor.current_instance())
    command.use_argument('push')
    command.use_argument('{0}').formatted_with(remote_img)
    res = command.run("{}/{}".format(report_dir, 'docker_push_tag'))
    if res.exit_code > 0:
        logger.info(res.error_report_lines)
        raise Exception(
            "Error pushing image to remote registry - {}".format(remote_img))
Пример #11
0
def build_with_pyb(target_dir, artifact_name, artifact_filter, config_settings=None, metadata_directory=None):
    from pybuilder.cli import main
    # verbose, debug, skip all optional...
    if main("-v", "-X", "-o", "--reset-plugins", "clean", "publish"):
        raise RuntimeError("PyBuilder build failed")

    from pybuilder.reactor import Reactor
    from pybuilder.plugins.python.distutils_plugin import _get_generated_artifacts
    reactor = Reactor.current_instance()
    project = reactor.project
    logger = reactor.logger
    dist_dir, artifacts = _get_generated_artifacts(project, logger)

    filtered = list(filter(artifact_filter, artifacts))
    if len(filtered) > 1:
        raise RuntimeError("Multiple project %ss found, don't know which to install: %r" % (artifact_name, filtered,))
    if not filtered:
        raise RuntimeError("Project did not generate any %ss install: %r" % (artifact_name, artifacts,))
    artifact = filtered[0]
    shutil.copy2(artifact, target_dir)

    return os.path.basename(artifact)
Пример #12
0
def use_plugin(name, version=None, plugin_module_name=None):
    from pybuilder.reactor import Reactor
    reactor = Reactor.current_instance()
    if reactor is not None:
        reactor.require_plugin(name, version, plugin_module_name)
Пример #13
0
 def tearDown(self):
     Reactor._set_current_instance(self.old_reactor)
Пример #14
0
class ReactorTest(unittest.TestCase):
    def setUp(self):
        self.old_reactor = Reactor.current_instance()
        self.plugin_loader_mock = Mock(PluginLoader)
        self.logger = Mock()
        self.execution_manager = Mock(ExecutionManager)
        self.reactor = Reactor(
            self.logger, self.execution_manager, self.plugin_loader_mock)

    def tearDown(self):
        Reactor._set_current_instance(self.old_reactor)

    def test_should_return_tasks_from_execution_manager_when_calling_get_tasks(self):
        self.execution_manager.tasks = ["spam"]
        self.assertEquals(["spam"], self.reactor.get_tasks())

    def test_should_raise_exception_when_importing_plugin_and_plugin_not_found(self):
        self.plugin_loader_mock.load_plugin.side_effect = MissingPluginException("not_found")

        self.assertRaises(
            MissingPluginException, self.reactor.import_plugin, "not_found")

        self.plugin_loader_mock.load_plugin.assert_called_with(ANY, "not_found", None, None)

    def test_should_collect_single_task(self):
        def task():
            pass

        setattr(task, TASK_ATTRIBUTE, True)

        module = ModuleType("mock_module")
        module.task = task

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        self.assertEquals(len(self.execution_manager.register_task.call_args_list), 1)
        self.assertTrue(isinstance(self.execution_manager.register_task.call_args[0][0], Task) and len(
            self.execution_manager.register_task.call_args[0]) == 1)
        self.assertEquals(self.execution_manager.register_task.call_args[0][0].name, "task")

    def test_should_collect_single_task_with_overridden_name(self):
        def task():
            pass

        setattr(task, TASK_ATTRIBUTE, True)
        setattr(task, NAME_ATTRIBUTE, "overridden_name")

        module = ModuleType("mock_module")
        module.task = task

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        self.assertEquals(len(self.execution_manager.register_task.call_args_list), 1)
        self.assertTrue(isinstance(self.execution_manager.register_task.call_args[0][0], Task) and len(
            self.execution_manager.register_task.call_args[0]) == 1)
        self.assertEquals(self.execution_manager.register_task.call_args[0][0].name, "overridden_name")

    def test_should_collect_multiple_tasks(self):
        def task():
            pass

        setattr(task, TASK_ATTRIBUTE, True)

        def task2():
            pass

        setattr(task2, TASK_ATTRIBUTE, True)

        module = ModuleType("mock_module")
        module.task = task
        module.task2 = task2

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        self.assertEquals(len(self.execution_manager.register_task.call_args_list), 2)
        for call_args in self.execution_manager.register_task.call_args_list:
            self.assertTrue(isinstance(call_args[0][0], Task) and len(call_args[0]) == 1)

    def test_task_dependencies(self):
        import pybuilder.reactor

        with patch("pybuilder.reactor.Task"):
            @task
            def task1():
                pass

            @task
            @depends(task1)
            def task2():
                pass

            @task
            def task3():
                pass

            @task
            @depends(optional(task3))
            @dependents("task6")
            def task4():
                pass

            @task
            @dependents("task6", optional(task3))
            def task5():
                pass

            @task
            @depends(task1, optional(task2))
            def task6():
                pass

            module = ModuleType("mock_module")
            module.task1 = task1
            module.task2 = task2
            module.task3 = task3
            module.task4 = task4
            module.task5 = task5
            module.task6 = task6

            self.reactor.collect_tasks_and_actions_and_initializers(module)

            pybuilder.reactor.Task.assert_has_calls([call("task1", task1, [], ''),
                                                     call("task2", task2, [TaskDependency(task1)], ''),
                                                     call("task3", task3, [TaskDependency(task5, True)], ''),
                                                     call("task4", task4, [TaskDependency(task3, True)], ''),
                                                     call("task5", task5, [], ''),
                                                     call("task6", task6,
                                                          [TaskDependency(task1), TaskDependency(task2, True),
                                                           TaskDependency(task4), TaskDependency(task5)], '')])

    def test_task_dependencies_with_post_definition_injections(self):
        import pybuilder.reactor

        with patch("pybuilder.reactor.Task"):
            @task
            def task1():
                pass

            @task
            @depends(task1)
            def task2():
                pass

            @task
            @depends(task1)
            @dependents(task2)
            def task3():
                pass

            module1 = ModuleType("mock_module_one")
            module1.task1 = task1
            module1.task2 = task2

            module2 = ModuleType("mock_module_two")
            module2.task3 = task3

            self.reactor.collect_tasks_and_actions_and_initializers(module1)
            pybuilder.reactor.Task.assert_has_calls([call("task1", task1, [], ''),
                                                     call("task2", task2, [TaskDependency(task1)], '')])

            self.reactor.collect_tasks_and_actions_and_initializers(module2)
            pybuilder.reactor.Task.assert_has_calls([call("task3", task3, [TaskDependency(task1)], '')])
            self.execution_manager.register_late_task_dependencies.assert_has_calls(
                [call({}), call({"task2": [TaskDependency(task3)]})])

    def test_task_dependencies_with_post_definition_injections_custom_names(self):
        import pybuilder.reactor

        with patch("pybuilder.reactor.Task"):
            @task
            def task1():
                pass

            @task
            @depends(task1)
            def task2():
                pass

            @task("task_3")
            @depends(task1)
            @dependents(task2)
            def task3():
                pass

            module1 = ModuleType("mock_module_one")
            module1.task1 = task1
            module1.task2 = task2

            module2 = ModuleType("mock_module_two")
            module2.task3 = task3

            self.reactor.collect_tasks_and_actions_and_initializers(module1)
            pybuilder.reactor.Task.assert_has_calls([call("task1", task1, [], ''),
                                                     call("task2", task2, [TaskDependency(task1)], '')])

            self.reactor.collect_tasks_and_actions_and_initializers(module2)
            pybuilder.reactor.Task.assert_has_calls([call("task_3", task3, [TaskDependency(task1)], '')])
            self.execution_manager.register_late_task_dependencies.assert_has_calls(
                [call({}), call({"task2": [TaskDependency("task_3")]})])

    def test_should_collect_single_before_action(self):
        @before("spam")
        def action():
            pass

        module = ModuleType("mock_module")
        module.task = action

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        self.assertEquals(self.execution_manager.register_action.call_count, 1)
        self.assertTrue(isinstance(self.execution_manager.register_action.call_args[0][0], Action) and
                        len(self.execution_manager.register_action.call_args[0]) == 1)

    def test_should_collect_single_after_action(self):
        @after("spam")
        def action():
            pass

        module = ModuleType("mock_module")
        module.task = action

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        self.assertEquals(self.execution_manager.register_action.call_count, 1)
        self.assertTrue(isinstance(self.execution_manager.register_action.call_args[0][0], Action) and
                        len(self.execution_manager.register_action.call_args[0]) == 1)

    def test_should_collect_single_after_action_with_only_once_flag(self):
        @after("spam", only_once=True)
        def action():
            pass

        module = ModuleType("mock_module")
        module.task = action

        def register_action(action):
            if not action.only_once:
                raise AssertionError("Action is not marked as only_once")

        self.execution_manager.register_action = register_action

        self.reactor.collect_tasks_and_actions_and_initializers(module)

    def test_should_collect_single_after_action_with_teardown_flag(self):
        @after("spam", teardown=True)
        def action():
            pass

        module = ModuleType("mock_module")
        module.task = action

        self.reactor.collect_tasks_and_actions_and_initializers(module)

    def test_should_collect_single_initializer(self):
        def init():
            pass

        setattr(init, INITIALIZER_ATTRIBUTE, True)

        module = ModuleType("mock_module")
        module.task = init

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        self.assertEquals(self.execution_manager.register_initializer.call_count, 1)
        self.assertTrue(isinstance(self.execution_manager.register_initializer.call_args[0][0], Initializer) and
                        len(self.execution_manager.register_initializer.call_args[0]) == 1)

    def test_should_collect_single_initializer_with_environments(self):
        def init():
            pass

        setattr(init, INITIALIZER_ATTRIBUTE, True)
        setattr(init, ENVIRONMENTS_ATTRIBUTE, ["any_environment"])

        module = ModuleType("mock_module")
        module.task = init

        class ExecutionManagerMock(object):
            def register_initializer(self, initializer):
                self.initializer = initializer

            def register_late_task_dependencies(self, dependencies):
                pass

        execution_manager_mock = ExecutionManagerMock()
        self.reactor.execution_manager = execution_manager_mock

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        self.assertEquals(
            execution_manager_mock.initializer.environments, ["any_environment"])

    @patch("pybuilder.reactor.os.path.exists", return_value=False)
    @patch("pybuilder.reactor.os.path.abspath", return_value="spam")
    def test_should_raise_when_verifying_project_directory_and_directory_does_not_exist(self,
                                                                                        os_path_abspath,
                                                                                        os_path_exists):
        self.assertRaises(
            PyBuilderException, self.reactor.verify_project_directory, "spam", "eggs")

        os_path_abspath.assert_called_with("spam")
        os_path_exists.assert_called_with("spam")

    @patch("pybuilder.reactor.os.path.isdir", return_value=False)
    @patch("pybuilder.reactor.os.path.exists", return_value=True)
    @patch("pybuilder.reactor.os.path.abspath", return_value="spam")
    def test_should_raise_when_verifying_project_directory_and_directory_is_not_a_directory(self,
                                                                                            os_path_abspath,
                                                                                            os_path_exists,
                                                                                            os_path_isdir):
        self.assertRaises(
            PyBuilderException, self.reactor.verify_project_directory, "spam", "eggs")

        os_path_abspath.assert_called_with("spam")
        os_path_exists.assert_called_with("spam")
        os_path_isdir.assert_called_with("spam")

    @patch("pybuilder.reactor.os.path.join", side_effect=lambda *x: "/".join(x))
    @patch("pybuilder.reactor.os.path.isdir", return_value=True)
    @patch("pybuilder.reactor.os.path.exists", side_effect=lambda x: True if x == "spam" else False)
    @patch("pybuilder.reactor.os.path.abspath", return_value="spam")
    def test_should_raise_when_verifying_project_directory_and_build_descriptor_does_not_exist(self,
                                                                                               os_path_abspath,
                                                                                               os_path_exists,
                                                                                               os_path_isdir,
                                                                                               os_path_join):
        self.assertRaises(
            PyBuilderException, self.reactor.verify_project_directory, "spam", "eggs")

        os_path_abspath.assert_called_with("spam")
        os_path_exists.assert_has_calls([call("spam"), call("spam/eggs")])
        os_path_isdir.assert_called_with("spam")
        os_path_join.assert_called_with("spam", "eggs")

    @patch("pybuilder.reactor.os.path.isfile", return_value=False)
    @patch("pybuilder.reactor.os.path.join", side_effect=lambda *x: "/".join(x))
    @patch("pybuilder.reactor.os.path.isdir", return_value=True)
    @patch("pybuilder.reactor.os.path.exists", return_value=True)
    @patch("pybuilder.reactor.os.path.abspath", return_value="spam")
    def test_should_raise_when_verifying_project_directory_and_build_descriptor_is_not_a_file(self,
                                                                                              os_path_abspath,
                                                                                              os_path_exists,
                                                                                              os_path_isdir,
                                                                                              os_path_join,
                                                                                              os_path_isfile):
        self.assertRaises(
            PyBuilderException, self.reactor.verify_project_directory, "spam", "eggs")

        os_path_abspath.assert_called_with("spam")
        os_path_exists.assert_has_calls([call("spam"), call("spam/eggs")])
        os_path_isdir.assert_called_with("spam")
        os_path_join.assert_called_with("spam", "eggs")
        os_path_isfile.assert_called_with("spam/eggs")

    @patch("pybuilder.reactor.os.path.isfile", return_value=True)
    @patch("pybuilder.reactor.os.path.join", side_effect=lambda *x: "/".join(x))
    @patch("pybuilder.reactor.os.path.isdir", return_value=True)
    @patch("pybuilder.reactor.os.path.exists", return_value=True)
    @patch("pybuilder.reactor.os.path.abspath", return_value="/spam")
    def test_should_return_directory_and_full_path_of_descriptor_when_verifying_project_directory(self,
                                                                                                  os_path_abspath,
                                                                                                  os_path_exists,
                                                                                                  os_path_isdir,
                                                                                                  os_path_join,
                                                                                                  os_path_isfile):
        self.assertEquals(
            ("/spam", "/spam/eggs"), self.reactor.verify_project_directory("spam", "eggs"))

        os_path_abspath.assert_called_with("spam")
        os_path_exists.assert_has_calls([call("/spam"), call("/spam/eggs")])
        os_path_isdir.assert_called_with("/spam")
        os_path_join.assert_called_with("/spam", "eggs")
        os_path_isfile.assert_called_with("/spam/eggs")

    @patch("pybuilder.reactor.imp.load_source", side_effect=ImportError("spam"))
    def test_should_raise_when_loading_project_module_and_import_raises_exception(self, imp_load_source):
        self.assertRaises(
            PyBuilderException, self.reactor.load_project_module, "spam")

        imp_load_source.assert_called_with("build", "spam")

    @patch("pybuilder.reactor.imp.load_source", return_value=Mock())
    def test_should_return_module_when_loading_project_module_and_import_raises_exception(self, imp_load_source):
        self.assertTrue(imp_load_source.return_value is self.reactor.load_project_module("spam"))

        imp_load_source.assert_called_with("build", "spam")

    def test_ensure_project_attributes_are_set_when_instantiating_project(self):
        module = ModuleType("mock_module")

        module.version = "version"
        module.default_task = "default_task"
        module.summary = "summary"
        module.home_page = "home_page"
        module.description = "description"
        module.authors = "authors"
        module.license = "license"
        module.url = "url"

        self.reactor.project = Mock()
        self.reactor.project_module = module

        self.reactor.apply_project_attributes()

        self.assertEquals("version", self.reactor.project.version)
        self.assertEquals("default_task", self.reactor.project.default_task)
        self.assertEquals("summary", self.reactor.project.summary)
        self.assertEquals("home_page", self.reactor.project.home_page)
        self.assertEquals("description", self.reactor.project.description)
        self.assertEquals("authors", self.reactor.project.authors)
        self.assertEquals("license", self.reactor.project.license)
        self.assertEquals("url", self.reactor.project.url)

    def test_ensure_project_name_is_set_from_attribute_when_instantiating_project(self):
        module = ModuleType("mock_module")
        module.name = "mock_module"

        self.reactor.project = Mock()
        self.reactor.project_module = module
        self.reactor.apply_project_attributes()

        self.assertEquals("mock_module", self.reactor.project.name)

    def test_should_import_plugin_only_once(self):
        plugin_module = ModuleType("mock_module")
        self.plugin_loader_mock.load_plugin.return_value = plugin_module

        self.reactor.require_plugin("spam")
        self.reactor.require_plugin("spam")

        self.assertEquals(["spam"], self.reactor.get_plugins())

        self.plugin_loader_mock.load_plugin.assert_called_with(ANY, "spam", None, None)

    def test_ensure_project_properties_are_logged_when_calling_log_project_properties(self):
        project = Project("spam")
        project.set_property("spam", "spam")
        project.set_property("eggs", "eggs")

        self.reactor.project = project
        self.reactor.log_project_properties()

        call_args = self.logger.debug.call_args
        self.assertEquals(call_args[0][0], "Project properties: %s")
        self.assertTrue("basedir : spam" in call_args[0][1])
        self.assertTrue("eggs : eggs" in call_args[0][1])
        self.assertTrue("spam : spam" in call_args[0][1])

    def test_should_raise_exception_when_project_is_not_valid(self):
        self.reactor.project = Mock(properties={})
        self.reactor.project.validate.return_value = ["spam"]

        self.assertRaises(ProjectValidationFailedException, self.reactor.build)

    def test_prepare_tasks(self):
        self.reactor.project = Project("spam")

        self.reactor.project.default_task = ["a", "b"]

        self.assertEquals(self.reactor._prepare_tasks(["c"]), ["c"])
        self.assertEquals(self.reactor._prepare_tasks(["+c"]), ["a", "b", "c"])
        self.assertEquals(self.reactor._prepare_tasks(["+c", '^c']), ["a", "b"])
        self.assertEquals(self.reactor._prepare_tasks(["^b"]), ["a"])
        self.assertEquals(self.reactor._prepare_tasks(["^a"]), ["b"])
        self.assertEquals(self.reactor._prepare_tasks(["^d"]), ["a", "b"])
        self.assertEquals(self.reactor._prepare_tasks(["+c", "d"]), ["d", "c"])
        self.assertEquals(self.reactor._prepare_tasks(["+c", "d", "^b"]), ["d", "c"])
        self.assertEquals(self.reactor._prepare_tasks(["+c", "+", "^"]), ["+", "^", "c"])
        self.assertEquals(self.reactor._prepare_tasks([]), ["a", "b"])

        self.reactor.project.default_task = []

        self.assertEquals(self.reactor._prepare_tasks(["c"]), ["c"])
        self.assertEquals(self.reactor._prepare_tasks(["+c"]), ["c"])
        self.assertEquals(self.reactor._prepare_tasks(["+c", "^d"]), ["c"])
        self.assertEquals(self.reactor._prepare_tasks(["+c", "d", "^d"]), ["c"])
        self.assertEquals(self.reactor._prepare_tasks(["^c", "c"]), [])
        self.assertEquals(self.reactor._prepare_tasks(["+c", "^c"]), [])
        self.assertEquals(self.reactor._prepare_tasks(["+c", "d"]), ["d", "c"])
        self.assertEquals(self.reactor._prepare_tasks(["+c", "+"]), ["+", "c"])

        self.assertRaises(PyBuilderException, self.reactor._prepare_tasks, [])
Пример #15
0
class ReactorTest(unittest.TestCase):
    def setUp(self):
        self.old_reactor = Reactor.current_instance()
        self.plugin_loader_mock = Mock(PluginLoader)
        self.logger = Mock()
        self.execution_manager = Mock(ExecutionManager)
        self.reactor = Reactor(
            self.logger, self.execution_manager, self.plugin_loader_mock)

    def tearDown(self):
        Reactor._set_current_instance(self.old_reactor)

    def test_should_return_tasks_from_execution_manager_when_calling_get_tasks(self):
        self.execution_manager.tasks = ["spam"]
        self.assertEqual(["spam"], self.reactor.get_tasks())

    def test_should_raise_exception_when_importing_plugin_and_plugin_not_found(self):
        self.plugin_loader_mock.load_plugin.side_effect = MissingPluginException("not_found")

        self.assertRaises(
            MissingPluginException, self.reactor.import_plugin, "not_found")

        self.plugin_loader_mock.load_plugin.assert_called_with(ANY, "not_found", None, None)

    def test_should_collect_single_task(self):
        def task():
            pass

        setattr(task, TASK_ATTRIBUTE, True)

        module = ModuleType("mock_module")
        module.task = task

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        self.assertEqual(len(self.execution_manager.register_task.call_args_list), 1)
        self.assertTrue(isinstance(self.execution_manager.register_task.call_args[0][0], Task) and len(
            self.execution_manager.register_task.call_args[0]) == 1)
        self.assertEqual(self.execution_manager.register_task.call_args[0][0].name, "task")

    def test_should_collect_single_task_with_overridden_name(self):
        def task():
            pass

        setattr(task, TASK_ATTRIBUTE, True)
        setattr(task, NAME_ATTRIBUTE, "overridden_name")

        module = ModuleType("mock_module")
        module.task = task

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        self.assertEqual(len(self.execution_manager.register_task.call_args_list), 1)
        self.assertTrue(isinstance(self.execution_manager.register_task.call_args[0][0], Task) and len(
            self.execution_manager.register_task.call_args[0]) == 1)
        self.assertEqual(self.execution_manager.register_task.call_args[0][0].name, "overridden_name")

    def test_should_collect_multiple_tasks(self):
        def task():
            pass

        setattr(task, TASK_ATTRIBUTE, True)

        def task2():
            pass

        setattr(task2, TASK_ATTRIBUTE, True)

        module = ModuleType("mock_module")
        module.task = task
        module.task2 = task2

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        self.assertEqual(len(self.execution_manager.register_task.call_args_list), 2)
        for call_args in self.execution_manager.register_task.call_args_list:
            self.assertTrue(isinstance(call_args[0][0], Task) and len(call_args[0]) == 1)

    def test_task_dependencies(self):
        import pybuilder.reactor

        with patch("pybuilder.reactor.Task"):
            @task
            def task1():
                pass

            @task
            @depends(task1)
            def task2():
                pass

            @task
            def task3():
                pass

            @task
            @depends(optional(task3))
            @dependents("task6")
            def task4():
                pass

            @task
            @dependents("task6", optional(task3))
            def task5():
                pass

            @task
            @depends(task1, optional(task2))
            def task6():
                pass

            module = ModuleType("mock_module")
            module.task1 = task1
            module.task2 = task2
            module.task3 = task3
            module.task4 = task4
            module.task5 = task5
            module.task6 = task6

            self.reactor.collect_tasks_and_actions_and_initializers(module)

            pybuilder.reactor.Task.assert_has_calls([call("task1", task1, [], ''),
                                                     call("task2", task2, [TaskDependency(task1)], ''),
                                                     call("task3", task3, [TaskDependency(task5, True)], ''),
                                                     call("task4", task4, [TaskDependency(task3, True)], ''),
                                                     call("task5", task5, [], ''),
                                                     call("task6", task6,
                                                          [TaskDependency(task1), TaskDependency(task2, True),
                                                           TaskDependency(task4), TaskDependency(task5)], '')])

    def test_task_dependencies_with_post_definition_injections(self):
        import pybuilder.reactor

        with patch("pybuilder.reactor.Task"):
            @task
            def task1():
                pass

            @task
            @depends(task1)
            def task2():
                pass

            @task
            @depends(task1)
            @dependents(task2)
            def task3():
                pass

            module1 = ModuleType("mock_module_one")
            module1.task1 = task1
            module1.task2 = task2

            module2 = ModuleType("mock_module_two")
            module2.task3 = task3

            self.reactor.collect_tasks_and_actions_and_initializers(module1)
            pybuilder.reactor.Task.assert_has_calls([call("task1", task1, [], ''),
                                                     call("task2", task2, [TaskDependency(task1)], '')])

            self.reactor.collect_tasks_and_actions_and_initializers(module2)
            pybuilder.reactor.Task.assert_has_calls([call("task3", task3, [TaskDependency(task1)], '')])
            self.execution_manager.register_late_task_dependencies.assert_has_calls(
                [call({}), call({"task2": [TaskDependency(task3)]})])

    def test_task_dependencies_with_post_definition_injections_custom_names(self):
        import pybuilder.reactor

        with patch("pybuilder.reactor.Task"):
            @task
            def task1():
                pass

            @task
            @depends(task1)
            def task2():
                pass

            @task("task_3")
            @depends(task1)
            @dependents(task2)
            def task3():
                pass

            module1 = ModuleType("mock_module_one")
            module1.task1 = task1
            module1.task2 = task2

            module2 = ModuleType("mock_module_two")
            module2.task3 = task3

            self.reactor.collect_tasks_and_actions_and_initializers(module1)
            pybuilder.reactor.Task.assert_has_calls([call("task1", task1, [], ''),
                                                     call("task2", task2, [TaskDependency(task1)], '')])

            self.reactor.collect_tasks_and_actions_and_initializers(module2)
            pybuilder.reactor.Task.assert_has_calls([call("task_3", task3, [TaskDependency(task1)], '')])
            self.execution_manager.register_late_task_dependencies.assert_has_calls(
                [call({}), call({"task2": [TaskDependency("task_3")]})])

    def test_should_collect_single_before_action(self):
        @before("spam")
        def action():
            pass

        module = ModuleType("mock_module")
        module.task = action

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        self.assertEqual(self.execution_manager.register_action.call_count, 1)
        self.assertTrue(isinstance(self.execution_manager.register_action.call_args[0][0], Action) and
                        len(self.execution_manager.register_action.call_args[0]) == 1)

    def test_should_collect_single_after_action(self):
        @after("spam")
        def action():
            pass

        module = ModuleType("mock_module")
        module.task = action

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        self.assertEqual(self.execution_manager.register_action.call_count, 1)
        self.assertTrue(isinstance(self.execution_manager.register_action.call_args[0][0], Action) and
                        len(self.execution_manager.register_action.call_args[0]) == 1)

    def test_should_collect_single_after_action_with_only_once_flag(self):
        @after("spam", only_once=True)
        def action():
            pass

        module = ModuleType("mock_module")
        module.task = action

        def register_action(action):
            if not action.only_once:
                raise AssertionError("Action is not marked as only_once")

        self.execution_manager.register_action = register_action

        self.reactor.collect_tasks_and_actions_and_initializers(module)

    def test_should_collect_single_after_action_with_teardown_flag(self):
        @after("spam", teardown=True)
        def action():
            pass

        module = ModuleType("mock_module")
        module.task = action

        self.reactor.collect_tasks_and_actions_and_initializers(module)

    def test_should_collect_single_initializer(self):
        def init():
            pass

        setattr(init, INITIALIZER_ATTRIBUTE, True)

        module = ModuleType("mock_module")
        module.task = init

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        self.assertEqual(self.execution_manager.register_initializer.call_count, 1)
        self.assertTrue(isinstance(self.execution_manager.register_initializer.call_args[0][0], Initializer) and
                        len(self.execution_manager.register_initializer.call_args[0]) == 1)

    def test_should_collect_single_initializer_with_environments(self):
        def init():
            pass

        setattr(init, INITIALIZER_ATTRIBUTE, True)
        setattr(init, ENVIRONMENTS_ATTRIBUTE, ["any_environment"])

        module = ModuleType("mock_module")
        module.task = init

        class ExecutionManagerMock(object):
            def register_initializer(self, initializer):
                self.initializer = initializer

            def register_late_task_dependencies(self, dependencies):
                pass

        execution_manager_mock = ExecutionManagerMock()
        self.reactor.execution_manager = execution_manager_mock

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        self.assertEqual(
            execution_manager_mock.initializer.environments, ["any_environment"])

    @patch("pybuilder.reactor.os.path.exists", return_value=False)
    @patch("pybuilder.reactor.os.path.abspath", return_value="spam")
    def test_should_raise_when_verifying_project_directory_and_directory_does_not_exist(self,
                                                                                        os_path_abspath,
                                                                                        os_path_exists):
        self.assertRaises(
            PyBuilderException, self.reactor.verify_project_directory, "spam", "eggs")

        os_path_abspath.assert_called_with("spam")
        os_path_exists.assert_called_with("spam")

    @patch("pybuilder.reactor.os.path.isdir", return_value=False)
    @patch("pybuilder.reactor.os.path.exists", return_value=True)
    @patch("pybuilder.reactor.os.path.abspath", return_value="spam")
    def test_should_raise_when_verifying_project_directory_and_directory_is_not_a_directory(self,
                                                                                            os_path_abspath,
                                                                                            os_path_exists,
                                                                                            os_path_isdir):
        self.assertRaises(
            PyBuilderException, self.reactor.verify_project_directory, "spam", "eggs")

        os_path_abspath.assert_called_with("spam")
        os_path_exists.assert_called_with("spam")
        os_path_isdir.assert_called_with("spam")

    @patch("pybuilder.reactor.os.path.join", side_effect=lambda *x: "/".join(x))
    @patch("pybuilder.reactor.os.path.isdir", return_value=True)
    @patch("pybuilder.reactor.os.path.exists", side_effect=lambda x: True if x == "spam" else False)
    @patch("pybuilder.reactor.os.path.abspath", return_value="spam")
    def test_should_raise_when_verifying_project_directory_and_build_descriptor_does_not_exist(self,
                                                                                               os_path_abspath,
                                                                                               os_path_exists,
                                                                                               os_path_isdir,
                                                                                               os_path_join):
        self.assertRaises(
            PyBuilderException, self.reactor.verify_project_directory, "spam", "eggs")

        os_path_abspath.assert_called_with("spam")
        os_path_exists.assert_has_calls([call("spam"), call("spam/eggs")])
        os_path_isdir.assert_called_with("spam")
        os_path_join.assert_called_with("spam", "eggs")

    @patch("pybuilder.reactor.os.path.isfile", return_value=False)
    @patch("pybuilder.reactor.os.path.join", side_effect=lambda *x: "/".join(x))
    @patch("pybuilder.reactor.os.path.isdir", return_value=True)
    @patch("pybuilder.reactor.os.path.exists", return_value=True)
    @patch("pybuilder.reactor.os.path.abspath", return_value="spam")
    def test_should_raise_when_verifying_project_directory_and_build_descriptor_is_not_a_file(self,
                                                                                              os_path_abspath,
                                                                                              os_path_exists,
                                                                                              os_path_isdir,
                                                                                              os_path_join,
                                                                                              os_path_isfile):
        self.assertRaises(
            PyBuilderException, self.reactor.verify_project_directory, "spam", "eggs")

        os_path_abspath.assert_called_with("spam")
        os_path_exists.assert_has_calls([call("spam"), call("spam/eggs")])
        os_path_isdir.assert_called_with("spam")
        os_path_join.assert_called_with("spam", "eggs")
        os_path_isfile.assert_called_with("spam/eggs")

    @patch("pybuilder.reactor.os.path.isfile", return_value=True)
    @patch("pybuilder.reactor.os.path.join", side_effect=lambda *x: "/".join(x))
    @patch("pybuilder.reactor.os.path.isdir", return_value=True)
    @patch("pybuilder.reactor.os.path.exists", return_value=True)
    @patch("pybuilder.reactor.os.path.abspath", return_value="/spam")
    def test_should_return_directory_and_full_path_of_descriptor_when_verifying_project_directory(self,
                                                                                                  os_path_abspath,
                                                                                                  os_path_exists,
                                                                                                  os_path_isdir,
                                                                                                  os_path_join,
                                                                                                  os_path_isfile):
        self.assertEqual(
            ("/spam", "/spam/eggs"), self.reactor.verify_project_directory("spam", "eggs"))

        os_path_abspath.assert_called_with("spam")
        os_path_exists.assert_has_calls([call("/spam"), call("/spam/eggs")])
        os_path_isdir.assert_called_with("/spam")
        os_path_join.assert_called_with("/spam", "eggs")
        os_path_isfile.assert_called_with("/spam/eggs")

    @patch("pybuilder.reactor.imp.load_source", side_effect=ImportError("spam"))
    def test_should_raise_when_loading_project_module_and_import_raises_exception(self, imp_load_source):
        self.assertRaises(
            PyBuilderException, self.reactor.load_project_module, "spam")

        imp_load_source.assert_called_with("build", "spam")

    @patch("pybuilder.reactor.imp.load_source", return_value=Mock())
    def test_should_return_module_when_loading_project_module_and_import_raises_exception(self, imp_load_source):
        self.assertTrue(imp_load_source.return_value is self.reactor.load_project_module("spam"))

        imp_load_source.assert_called_with("build", "spam")

    def test_ensure_project_attributes_are_set_when_instantiating_project(self):
        module = ModuleType("mock_module")

        module.version = "version"
        module.default_task = "default_task"
        module.summary = "summary"
        module.home_page = "home_page"
        module.description = "description"
        module.authors = "authors"
        module.license = "license"
        module.url = "url"

        self.reactor.project = Mock()
        self.reactor.project_module = module

        self.reactor.apply_project_attributes()

        self.assertEqual("version", self.reactor.project.version)
        self.assertEqual("default_task", self.reactor.project.default_task)
        self.assertEqual("summary", self.reactor.project.summary)
        self.assertEqual("home_page", self.reactor.project.home_page)
        self.assertEqual("description", self.reactor.project.description)
        self.assertEqual("authors", self.reactor.project.authors)
        self.assertEqual("license", self.reactor.project.license)
        self.assertEqual("url", self.reactor.project.url)

    def test_ensure_project_name_is_set_from_attribute_when_instantiating_project(self):
        module = ModuleType("mock_module")
        module.name = "mock_module"

        self.reactor.project = Mock()
        self.reactor.project_module = module
        self.reactor.apply_project_attributes()

        self.assertEqual("mock_module", self.reactor.project.name)

    def test_should_import_plugin_only_once(self):
        plugin_module = ModuleType("mock_module")
        self.plugin_loader_mock.load_plugin.return_value = plugin_module

        self.reactor.require_plugin("spam")
        self.reactor.require_plugin("spam")

        self.assertEqual(["spam"], self.reactor.get_plugins())

        self.plugin_loader_mock.load_plugin.assert_called_with(ANY, "spam", None, None)

    def test_ensure_project_properties_are_logged_when_calling_log_project_properties(self):
        project = Project("spam")
        project.set_property("spam", "spam")
        project.set_property("eggs", "eggs")

        self.reactor.project = project
        self.reactor.log_project_properties()

        call_args = self.logger.debug.call_args
        self.assertEqual(call_args[0][0], "Project properties: %s")
        self.assertTrue("basedir : spam" in call_args[0][1])
        self.assertTrue("eggs : eggs" in call_args[0][1])
        self.assertTrue("spam : spam" in call_args[0][1])

    def test_should_raise_exception_when_project_is_not_valid(self):
        self.reactor.project = Mock(properties={})
        self.reactor.project.validate.return_value = ["spam"]

        self.assertRaises(ProjectValidationFailedException, self.reactor.build)

    def test_prepare_tasks(self):
        self.reactor.project = Project("spam")

        self.reactor.project.default_task = ["a", "b"]

        self.assertEqual(self.reactor._prepare_tasks(["c"]), ["c"])
        self.assertEqual(self.reactor._prepare_tasks(["+c"]), ["a", "b", "c"])
        self.assertEqual(self.reactor._prepare_tasks(["+c", '^c']), ["a", "b"])
        self.assertEqual(self.reactor._prepare_tasks(["^b"]), ["a"])
        self.assertEqual(self.reactor._prepare_tasks(["^a"]), ["b"])
        self.assertEqual(self.reactor._prepare_tasks(["^d"]), ["a", "b"])
        self.assertEqual(self.reactor._prepare_tasks(["+c", "d"]), ["d", "c"])
        self.assertEqual(self.reactor._prepare_tasks(["+c", "d", "^b"]), ["d", "c"])
        self.assertEqual(self.reactor._prepare_tasks(["+c", "+", "^"]), ["+", "^", "c"])
        self.assertEqual(self.reactor._prepare_tasks([]), ["a", "b"])

        self.reactor.project.default_task = []

        self.assertEqual(self.reactor._prepare_tasks(["c"]), ["c"])
        self.assertEqual(self.reactor._prepare_tasks(["+c"]), ["c"])
        self.assertEqual(self.reactor._prepare_tasks(["+c", "^d"]), ["c"])
        self.assertEqual(self.reactor._prepare_tasks(["+c", "d", "^d"]), ["c"])
        self.assertEqual(self.reactor._prepare_tasks(["^c", "c"]), [])
        self.assertEqual(self.reactor._prepare_tasks(["+c", "^c"]), [])
        self.assertEqual(self.reactor._prepare_tasks(["+c", "d"]), ["d", "c"])
        self.assertEqual(self.reactor._prepare_tasks(["+c", "+"]), ["+", "c"])

        self.assertRaises(PyBuilderException, self.reactor._prepare_tasks, [])
Пример #16
0
def use_plugin(name):
    from pybuilder.reactor import Reactor

    reactor = Reactor.current_instance()
    if reactor is not None:
        reactor.require_plugin(name)
Пример #17
0
        It allows to set properties before other plugins
        and download it from needed PyPi index"""
    # workaround for command line properties
    # until https://github.com/pybuilder/pybuilder/pull/515
    if not project.get_property('pypi_server'):
        # try to get property from command line arguments
        for arg in sys.argv:
            if str(arg).startswith('pypi_server='):
                project.set_property('pypi_server',
                                     str(arg).replace('pypi_server=', ''))
                break
    if project.get_property('pypi_server'):
        use_pypi_server_plugin(project, logger)


init_pypi_server_plugin(Reactor.current_instance().project,
                        Reactor.current_instance().logger)


@before("prepare", only_once=True)
def reinitialize_pypi_server_plugin(project, logger):
    """ Re-init properties if `pypi_server` was changed on initialization
        stage"""
    if project.get_property('pypi_server'):
        if (project.get_property('pypi_server') !=
                project.get_property('pypi_server_on_import_plugin')):
            logger.warn(
                "Property `pypi_server` defined on initialize stage. "
                "Please use command line `pyb ... -P pypi_server=...`, "
                "otherwise some packages could be downloaded "
                "from default PyPi index.")
Пример #18
0
 def tearDown(self):
     Reactor._set_current_instance(self.old_reactor)
Пример #19
0
class ReactorTest(unittest.TestCase):
    def setUp(self):
        self.plugin_loader_mock = mock(PluginLoader)
        self.logger = mock()
        self.execution_manager = mock(ExecutionManager)
        self.reactor = Reactor(self.logger, self.execution_manager,
                               self.plugin_loader_mock)

    def tearDown(self):
        unstub()

    def test_should_return_tasks_from_execution_manager_when_calling_get_tasks(
            self):
        self.execution_manager.tasks = ["spam"]
        self.assertEquals(["spam"], self.reactor.get_tasks())

    def test_should_raise_exception_when_importing_plugin_and_plugin_not_found(
            self):
        when(self.plugin_loader_mock).load_plugin(
            any(), "not_found",
            any()).thenRaise(MissingPluginException("not_found"))

        self.assertRaises(MissingPluginException, self.reactor.import_plugin,
                          "not_found")

        verify(self.plugin_loader_mock).load_plugin(any(), "not_found", None)

    def test_should_collect_single_task(self):
        def task():
            pass

        setattr(task, TASK_ATTRIBUTE, True)

        module = mock()
        module.task = task

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        verify(self.execution_manager).register_task(TaskNameMatcher("task"))

    def test_should_collect_single_task_with_overridden_name(self):
        def task():
            pass

        setattr(task, TASK_ATTRIBUTE, True)
        setattr(task, NAME_ATTRIBUTE, "overridden_name")

        module = mock()
        module.task = task

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        verify(self.execution_manager).register_task(
            TaskNameMatcher("overridden_name"))

    def test_should_collect_multiple_tasks(self):
        def task():
            pass

        setattr(task, TASK_ATTRIBUTE, True)

        def task2():
            pass

        setattr(task2, TASK_ATTRIBUTE, True)

        module = mock()
        module.task = task
        module.task2 = task2

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        verify(self.execution_manager, times(2)).register_task(any(Task))

    def test_task_dependencies(self):
        import pybuilder.reactor

        when(pybuilder.reactor).Task().thenReturn(mock())

        @task
        def task1():
            pass

        @task
        @depends(task1)
        def task2():
            pass

        @task
        def task3():
            pass

        @task
        @depends(optional(task3))
        @dependents("task6")
        def task4():
            pass

        @task
        @dependents("task6", optional(task3))
        def task5():
            pass

        @task
        @depends(task1, optional(task2))
        def task6():
            pass

        module = mock()
        module.task1 = task1
        module.task2 = task2
        module.task3 = task3
        module.task4 = task4
        module.task5 = task5
        module.task6 = task6

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        inorder.verify(pybuilder.reactor).Task("task1", task1, [], '')
        inorder.verify(pybuilder.reactor).Task("task2", task2,
                                               [TaskDependency(task1)], '')
        inorder.verify(pybuilder.reactor).Task("task3", task3,
                                               [TaskDependency(task5, True)],
                                               '')
        inorder.verify(pybuilder.reactor).Task("task4", task4,
                                               [TaskDependency(task3, True)],
                                               '')
        inorder.verify(pybuilder.reactor).Task("task5", task5, [], '')
        inorder.verify(pybuilder.reactor).Task("task6", task6, [
            TaskDependency(task1),
            TaskDependency(task2, True),
            TaskDependency(task4),
            TaskDependency(task5)
        ], '')

    def test_should_collect_single_before_action(self):
        @before("spam")
        def action():
            pass

        module = mock()
        module.task = action

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        verify(self.execution_manager).register_action(any(Action))

    def test_should_collect_single_after_action(self):
        @after("spam")
        def action():
            pass

        module = mock()
        module.task = action

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        verify(self.execution_manager).register_action(any(Action))

    def test_should_collect_single_after_action_with_only_once_flag(self):
        @after("spam", only_once=True)
        def action():
            pass

        module = mock()
        module.task = action

        def register_action(action):
            if not action.only_once:
                raise AssertionError("Action is not marked as only_once")

        self.execution_manager.register_action = register_action

        self.reactor.collect_tasks_and_actions_and_initializers(module)

    def test_should_collect_single_after_action_with_teardown_flag(self):
        @after("spam", teardown=True)
        def action():
            pass

        module = mock()
        module.task = action

        self.reactor.collect_tasks_and_actions_and_initializers(module)

    def test_should_collect_single_initializer(self):
        def init():
            pass

        setattr(init, INITIALIZER_ATTRIBUTE, True)

        module = mock()
        module.task = init

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        verify(self.execution_manager).register_initializer(any(Initializer))

    def test_should_collect_single_initializer_with_environments(self):
        def init():
            pass

        setattr(init, INITIALIZER_ATTRIBUTE, True)
        setattr(init, ENVIRONMENTS_ATTRIBUTE, ["any_environment"])

        module = mock()
        module.task = init

        class ExecutionManagerMock(object):
            def register_initializer(self, initializer):
                self.initializer = initializer

        execution_manager_mock = ExecutionManagerMock()
        self.reactor.execution_manager = execution_manager_mock

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        self.assertEquals(execution_manager_mock.initializer.environments,
                          ["any_environment"])

    def test_should_raise_exception_when_verifying_project_directory_and_directory_does_not_exist(
            self):
        when(os.path).abspath("spam").thenReturn("spam")
        when(os.path).exists("spam").thenReturn(False)

        self.assertRaises(PyBuilderException,
                          self.reactor.verify_project_directory, "spam",
                          "eggs")

        verify(os.path).abspath("spam")
        verify(os.path).exists("spam")

    def test_should_raise_exception_when_verifying_project_directory_and_directory_is_not_a_directory(
            self):
        when(os.path).abspath("spam").thenReturn("spam")
        when(os.path).exists("spam").thenReturn(True)
        when(os.path).isdir("spam").thenReturn(False)

        self.assertRaises(PyBuilderException,
                          self.reactor.verify_project_directory, "spam",
                          "eggs")

        verify(os.path).abspath("spam")
        verify(os.path).exists("spam")
        verify(os.path).isdir("spam")

    def test_should_raise_exception_when_verifying_project_directory_and_build_descriptor_does_not_exist(
            self):
        when(os.path).abspath("spam").thenReturn("spam")
        when(os.path).exists("spam").thenReturn(True)
        when(os.path).isdir("spam").thenReturn(True)
        when(os.path).join("spam", "eggs").thenReturn("spam/eggs")
        when(os.path).exists("spam/eggs").thenReturn(False)

        self.assertRaises(PyBuilderException,
                          self.reactor.verify_project_directory, "spam",
                          "eggs")

        verify(os.path).abspath("spam")
        verify(os.path).exists("spam")
        verify(os.path).isdir("spam")
        verify(os.path).join("spam", "eggs")
        verify(os.path).exists("spam/eggs")

    def test_should_raise_exception_when_verifying_project_directory_and_build_descriptor_is_not_a_file(
            self):
        when(os.path).abspath("spam").thenReturn("spam")
        when(os.path).exists("spam").thenReturn(True)
        when(os.path).isdir("spam").thenReturn(True)
        when(os.path).join("spam", "eggs").thenReturn("spam/eggs")
        when(os.path).exists("spam/eggs").thenReturn(True)
        when(os.path).isfile("spam/eggs").thenReturn(False)

        self.assertRaises(PyBuilderException,
                          self.reactor.verify_project_directory, "spam",
                          "eggs")

        verify(os.path).abspath("spam")
        verify(os.path).exists("spam")
        verify(os.path).isdir("spam")
        verify(os.path).join("spam", "eggs")
        verify(os.path).exists("spam/eggs")
        verify(os.path).isfile("spam/eggs")

    def test_should_return_directory_and_full_path_of_descriptor_when_verifying_project_directory(
            self):
        when(os.path).abspath("spam").thenReturn("/spam")
        when(os.path).exists("/spam").thenReturn(True)
        when(os.path).isdir("/spam").thenReturn(True)
        when(os.path).join("/spam", "eggs").thenReturn("/spam/eggs")
        when(os.path).exists("/spam/eggs").thenReturn(True)
        when(os.path).isfile("/spam/eggs").thenReturn(True)

        self.assertEquals(
            ("/spam", "/spam/eggs"),
            self.reactor.verify_project_directory("spam", "eggs"))

        verify(os.path).abspath("spam")
        verify(os.path).exists("/spam")
        verify(os.path).isdir("/spam")
        verify(os.path).join("/spam", "eggs")
        verify(os.path).exists("/spam/eggs")
        verify(os.path).isfile("/spam/eggs")

    def test_should_raise_exception_when_loading_project_module_and_import_raises_exception(
            self):
        when(imp).load_source("build", "spam").thenRaise(ImportError("spam"))

        self.assertRaises(PyBuilderException, self.reactor.load_project_module,
                          "spam")

        verify(imp).load_source("build", "spam")

    def test_should_return_module_when_loading_project_module_and_import_raises_exception(
            self):
        module = mock()
        when(imp).load_source("build", "spam").thenReturn(module)

        self.assertEquals(module, self.reactor.load_project_module("spam"))

        verify(imp).load_source("build", "spam")

    def test_ensure_project_attributes_are_set_when_instantiating_project(
            self):
        module = mock(version="version",
                      default_task="default_task",
                      summary="summary",
                      home_page="home_page",
                      description="description",
                      authors="authors",
                      license="license",
                      url="url")

        self.reactor.project = mock()
        self.reactor.project_module = module

        self.reactor.apply_project_attributes()

        self.assertEquals("version", self.reactor.project.version)
        self.assertEquals("default_task", self.reactor.project.default_task)
        self.assertEquals("summary", self.reactor.project.summary)
        self.assertEquals("home_page", self.reactor.project.home_page)
        self.assertEquals("description", self.reactor.project.description)
        self.assertEquals("authors", self.reactor.project.authors)
        self.assertEquals("license", self.reactor.project.license)
        self.assertEquals("url", self.reactor.project.url)

    def test_ensure_project_name_is_set_from_attribute_when_instantiating_project(
            self):
        module = mock(name="name")

        self.reactor.project = mock()
        self.reactor.project_module = module
        self.reactor.apply_project_attributes()

        self.assertEquals("name", self.reactor.project.name)

    def test_should_import_plugin_only_once(self):
        plugin_module = mock()
        when(self.plugin_loader_mock).load_plugin(
            any(), "spam", any()).thenReturn(plugin_module)

        self.reactor.require_plugin("spam")
        self.reactor.require_plugin("spam")

        self.assertEquals(["spam"], self.reactor.get_plugins())

        verify(self.plugin_loader_mock).load_plugin(any(), "spam", None)

    def test_ensure_project_properties_are_logged_when_calling_log_project_properties(
            self):
        project = Project("spam")
        project.set_property("spam", "spam")
        project.set_property("eggs", "eggs")

        self.reactor.project = project
        self.reactor.log_project_properties()

        verify(self.logger).debug("Project properties: %s",
                                  contains("basedir : spam"))
        verify(self.logger).debug("Project properties: %s",
                                  contains("eggs : eggs"))
        verify(self.logger).debug("Project properties: %s",
                                  contains("spam : spam"))

    def test_should_raise_exception_when_project_is_not_valid(self):
        self.reactor.project = mock(properties={})
        when(self.reactor.project).validate().thenReturn(["spam"])

        self.assertRaises(ProjectValidationFailedException, self.reactor.build)
Пример #20
0
def use_plugin(name):
    from pybuilder.reactor import Reactor
    reactor = Reactor.current_instance()
    if reactor is not None:
        reactor.require_plugin(name)
Пример #21
0
 def prepare_reactor(self):
     logger = StdOutLogger(threshold=Logger.DEBUG)
     execution_manager = ExecutionManager(logger)
     reactor = Reactor(logger, execution_manager)
     reactor.prepare_build(project_directory=self.tmp_directory)
     return reactor
Пример #22
0
def use_plugin(name, version=None, plugin_module_name=None):
    from pybuilder.reactor import Reactor
    reactor = Reactor.current_instance()
    if reactor is not None:
        reactor.require_plugin(name, version, plugin_module_name)
Пример #23
0
 def setUp(self):
     self.plugin_loader_mock = mock(PluginLoader)
     self.logger = mock()
     self.execution_manager = mock(ExecutionManager)
     self.reactor = Reactor(self.logger, self.execution_manager,
                            self.plugin_loader_mock)
Пример #24
0
class ReactorTest(unittest.TestCase):
    def setUp(self):
        self.plugin_loader_mock = mock(PluginLoader)
        self.logger = mock()
        self.execution_manager = mock(ExecutionManager)
        self.reactor = Reactor(
            self.logger, self.execution_manager, self.plugin_loader_mock)

    def tearDown(self):
        unstub()

    def test_should_return_tasks_from_execution_manager_when_calling_get_tasks(self):
        self.execution_manager.tasks = ["spam"]
        self.assertEquals(["spam"], self.reactor.get_tasks())

    def test_should_raise_exception_when_importing_plugin_and_plugin_not_found(self):
        when(self.plugin_loader_mock).load_plugin(
            any(), "not_found", any(), any()).thenRaise(MissingPluginException("not_found"))

        self.assertRaises(
            MissingPluginException, self.reactor.import_plugin, "not_found")

        verify(self.plugin_loader_mock).load_plugin(any(), "not_found", None, None)

    def test_should_collect_single_task(self):
        def task():
            pass

        setattr(task, TASK_ATTRIBUTE, True)

        module = mock()
        module.task = task

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        verify(self.execution_manager).register_task(TaskNameMatcher("task"))

    def test_should_collect_single_task_with_overridden_name(self):
        def task():
            pass

        setattr(task, TASK_ATTRIBUTE, True)
        setattr(task, NAME_ATTRIBUTE, "overridden_name")

        module = mock()
        module.task = task

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        verify(self.execution_manager).register_task(
            TaskNameMatcher("overridden_name"))

    def test_should_collect_multiple_tasks(self):
        def task():
            pass

        setattr(task, TASK_ATTRIBUTE, True)

        def task2():
            pass

        setattr(task2, TASK_ATTRIBUTE, True)

        module = mock()
        module.task = task
        module.task2 = task2

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        verify(self.execution_manager, times(2)).register_task(any(Task))

    def test_task_dependencies(self):
        import pybuilder.reactor

        when(pybuilder.reactor).Task().thenReturn(mock())

        @task
        def task1():
            pass

        @task
        @depends(task1)
        def task2():
            pass

        @task
        def task3():
            pass

        @task
        @depends(optional(task3))
        @dependents("task6")
        def task4():
            pass

        @task
        @dependents("task6", optional(task3))
        def task5():
            pass

        @task
        @depends(task1, optional(task2))
        def task6():
            pass

        module = mock()
        module.task1 = task1
        module.task2 = task2
        module.task3 = task3
        module.task4 = task4
        module.task5 = task5
        module.task6 = task6

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        inorder.verify(pybuilder.reactor).Task("task1", task1, [], '')
        inorder.verify(pybuilder.reactor).Task("task2", task2, [TaskDependency(task1)], '')
        inorder.verify(pybuilder.reactor).Task("task3", task3, [TaskDependency(task5, True)], '')
        inorder.verify(pybuilder.reactor).Task("task4", task4, [TaskDependency(task3, True)], '')
        inorder.verify(pybuilder.reactor).Task("task5", task5, [], '')
        inorder.verify(pybuilder.reactor).Task("task6", task6, [TaskDependency(task1), TaskDependency(task2, True),
                                                                TaskDependency(task4), TaskDependency(task5)], '')

    def test_should_collect_single_before_action(self):
        @before("spam")
        def action():
            pass

        module = mock()
        module.task = action

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        verify(self.execution_manager).register_action(any(Action))

    def test_should_collect_single_after_action(self):
        @after("spam")
        def action():
            pass

        module = mock()
        module.task = action

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        verify(self.execution_manager).register_action(any(Action))

    def test_should_collect_single_after_action_with_only_once_flag(self):
        @after("spam", only_once=True)
        def action():
            pass

        module = mock()
        module.task = action

        def register_action(action):
            if not action.only_once:
                raise AssertionError("Action is not marked as only_once")

        self.execution_manager.register_action = register_action

        self.reactor.collect_tasks_and_actions_and_initializers(module)

    def test_should_collect_single_after_action_with_teardown_flag(self):
        @after("spam", teardown=True)
        def action():
            pass

        module = mock()
        module.task = action

        self.reactor.collect_tasks_and_actions_and_initializers(module)

    def test_should_collect_single_initializer(self):
        def init():
            pass

        setattr(init, INITIALIZER_ATTRIBUTE, True)

        module = mock()
        module.task = init

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        verify(self.execution_manager).register_initializer(any(Initializer))

    def test_should_collect_single_initializer_with_environments(self):
        def init():
            pass

        setattr(init, INITIALIZER_ATTRIBUTE, True)
        setattr(init, ENVIRONMENTS_ATTRIBUTE, ["any_environment"])

        module = mock()
        module.task = init

        class ExecutionManagerMock(object):
            def register_initializer(self, initializer):
                self.initializer = initializer

        execution_manager_mock = ExecutionManagerMock()
        self.reactor.execution_manager = execution_manager_mock

        self.reactor.collect_tasks_and_actions_and_initializers(module)

        self.assertEquals(
            execution_manager_mock.initializer.environments, ["any_environment"])

    def test_should_raise_exception_when_verifying_project_directory_and_directory_does_not_exist(self):
        when(os.path).abspath("spam").thenReturn("spam")
        when(os.path).exists("spam").thenReturn(False)

        self.assertRaises(
            PyBuilderException, self.reactor.verify_project_directory, "spam", "eggs")

        verify(os.path).abspath("spam")
        verify(os.path).exists("spam")

    def test_should_raise_exception_when_verifying_project_directory_and_directory_is_not_a_directory(self):
        when(os.path).abspath("spam").thenReturn("spam")
        when(os.path).exists("spam").thenReturn(True)
        when(os.path).isdir("spam").thenReturn(False)

        self.assertRaises(
            PyBuilderException, self.reactor.verify_project_directory, "spam", "eggs")

        verify(os.path).abspath("spam")
        verify(os.path).exists("spam")
        verify(os.path).isdir("spam")

    def test_should_raise_exception_when_verifying_project_directory_and_build_descriptor_does_not_exist(self):
        when(os.path).abspath("spam").thenReturn("spam")
        when(os.path).exists("spam").thenReturn(True)
        when(os.path).isdir("spam").thenReturn(True)
        when(os.path).join("spam", "eggs").thenReturn("spam/eggs")
        when(os.path).exists("spam/eggs").thenReturn(False)

        self.assertRaises(
            PyBuilderException, self.reactor.verify_project_directory, "spam", "eggs")

        verify(os.path).abspath("spam")
        verify(os.path).exists("spam")
        verify(os.path).isdir("spam")
        verify(os.path).join("spam", "eggs")
        verify(os.path).exists("spam/eggs")

    def test_should_raise_exception_when_verifying_project_directory_and_build_descriptor_is_not_a_file(self):
        when(os.path).abspath("spam").thenReturn("spam")
        when(os.path).exists("spam").thenReturn(True)
        when(os.path).isdir("spam").thenReturn(True)
        when(os.path).join("spam", "eggs").thenReturn("spam/eggs")
        when(os.path).exists("spam/eggs").thenReturn(True)
        when(os.path).isfile("spam/eggs").thenReturn(False)

        self.assertRaises(
            PyBuilderException, self.reactor.verify_project_directory, "spam", "eggs")

        verify(os.path).abspath("spam")
        verify(os.path).exists("spam")
        verify(os.path).isdir("spam")
        verify(os.path).join("spam", "eggs")
        verify(os.path).exists("spam/eggs")
        verify(os.path).isfile("spam/eggs")

    def test_should_return_directory_and_full_path_of_descriptor_when_verifying_project_directory(self):
        when(os.path).abspath("spam").thenReturn("/spam")
        when(os.path).exists("/spam").thenReturn(True)
        when(os.path).isdir("/spam").thenReturn(True)
        when(os.path).join("/spam", "eggs").thenReturn("/spam/eggs")
        when(os.path).exists("/spam/eggs").thenReturn(True)
        when(os.path).isfile("/spam/eggs").thenReturn(True)

        self.assertEquals(
            ("/spam", "/spam/eggs"), self.reactor.verify_project_directory("spam", "eggs"))

        verify(os.path).abspath("spam")
        verify(os.path).exists("/spam")
        verify(os.path).isdir("/spam")
        verify(os.path).join("/spam", "eggs")
        verify(os.path).exists("/spam/eggs")
        verify(os.path).isfile("/spam/eggs")

    def test_should_raise_exception_when_loading_project_module_and_import_raises_exception(self):
        when(imp).load_source("build", "spam").thenRaise(ImportError("spam"))

        self.assertRaises(
            PyBuilderException, self.reactor.load_project_module, "spam")

        verify(imp).load_source("build", "spam")

    def test_should_return_module_when_loading_project_module_and_import_raises_exception(self):
        module = mock()
        when(imp).load_source("build", "spam").thenReturn(module)

        self.assertEquals(module, self.reactor.load_project_module("spam"))

        verify(imp).load_source("build", "spam")

    def test_ensure_project_attributes_are_set_when_instantiating_project(self):
        module = mock(version="version",
                      default_task="default_task",
                      summary="summary",
                      home_page="home_page",
                      description="description",
                      authors="authors",
                      license="license",
                      url="url")

        self.reactor.project = mock()
        self.reactor.project_module = module

        self.reactor.apply_project_attributes()

        self.assertEquals("version", self.reactor.project.version)
        self.assertEquals("default_task", self.reactor.project.default_task)
        self.assertEquals("summary", self.reactor.project.summary)
        self.assertEquals("home_page", self.reactor.project.home_page)
        self.assertEquals("description", self.reactor.project.description)
        self.assertEquals("authors", self.reactor.project.authors)
        self.assertEquals("license", self.reactor.project.license)
        self.assertEquals("url", self.reactor.project.url)

    def test_ensure_project_name_is_set_from_attribute_when_instantiating_project(self):
        module = mock(name="name")

        self.reactor.project = mock()
        self.reactor.project_module = module
        self.reactor.apply_project_attributes()

        self.assertEquals("name", self.reactor.project.name)

    def test_should_import_plugin_only_once(self):
        plugin_module = mock()
        when(self.plugin_loader_mock).load_plugin(
            any(), "spam", any()).thenReturn(plugin_module)

        self.reactor.require_plugin("spam")
        self.reactor.require_plugin("spam")

        self.assertEquals(["spam"], self.reactor.get_plugins())

        verify(self.plugin_loader_mock).load_plugin(any(), "spam", None, None)

    def test_ensure_project_properties_are_logged_when_calling_log_project_properties(self):
        project = Project("spam")
        project.set_property("spam", "spam")
        project.set_property("eggs", "eggs")

        self.reactor.project = project
        self.reactor.log_project_properties()

        verify(self.logger).debug(
            "Project properties: %s", contains("basedir : spam"))
        verify(self.logger).debug(
            "Project properties: %s", contains("eggs : eggs"))
        verify(self.logger).debug(
            "Project properties: %s", contains("spam : spam"))

    def test_should_raise_exception_when_project_is_not_valid(self):
        self.reactor.project = mock(properties={})
        when(self.reactor.project).validate().thenReturn(["spam"])

        self.assertRaises(ProjectValidationFailedException, self.reactor.build)
Пример #25
0
def init_reactor(logger):
    execution_manager = ExecutionManager(logger)
    reactor = Reactor(logger, execution_manager)
    return reactor
Пример #26
0
    else:
        raise
except subprocess.CalledProcessError as e:
    if e.returncode == 127:
        install_pyb()
    else:
        sys.exit(e.returncode)

try:
    from pybuilder.cli import main
    # verbose, debug, skip all optional...
    if main("-v", "-X", "-o", "--reset-plugins", "clean", "package"):
        raise RuntimeError("PyBuilder build failed")

    from pybuilder.reactor import Reactor
    reactor = Reactor.current_instance()
    project = reactor.project
    dist_dir = project.expand_path("$dir_dist")

    for src_file in glob.glob(os.path.join(dist_dir, "*")):
        file_name = os.path.basename(src_file)
        target_file_name = os.path.join(script_dir, file_name)
        if os.path.exists(target_file_name):
            if os.path.isdir(target_file_name):
                shutil.rmtree(target_file_name)
            else:
                os.remove(target_file_name)
        shutil.move(src_file, script_dir)
    setup_args = sys.argv[1:]
    subprocess.check_call([sys.executable, "setup.py"] + setup_args,
                          cwd=script_dir)
Пример #27
0
 def setUp(self):
     self.plugin_loader_mock = mock(PluginLoader)
     self.logger = mock()
     self.execution_manager = mock(ExecutionManager)
     self.reactor = Reactor(
         self.logger, self.execution_manager, self.plugin_loader_mock)
Пример #28
0
        project.set_property_if_unset(key, DEFAULT_PROPERTIES[key])
        for arg in sys.argv:
            if str(arg).startswith(key + '='):
                project.set_property(key, str(arg).replace(key + '=', ''))
    # set project.name
    project.name = _get_repo_name(project, _get_repo_path(project))
    # set project.version
    set_version_from_git_tag(project, logger)
    # save current properties
    for key in DEFAULT_PROPERTIES:
        project.set_property_if_unset(key + SAVED_PROP_SUFFIX,
                                      project.get_property(key))


# if we're in working project - update version according git tag
if Reactor.current_instance():
    force_semver_git_tag_plugin(Reactor.current_instance().project,
                                Reactor.current_instance().logger)


@init
def initialize_semver_git_tag(project):
    """ Init default plugin project properties. """
    project.plugin_depends_on('GitPython')
    project.plugin_depends_on('semver')
    # Part for develop increment - 'major', 'minor' or 'patch'
    project.set_property_if_unset('semver_git_tag_increment_part', 'patch')
    # Git repository directory path.
    # If None parent directory for build.py will be used
    project.set_property_if_unset('semver_git_tag_repo_dir', None)
    # Relative path with name of changelog file.