def create_project():
    project = Project("/")
    project.build_depends_on("testingframework")
    project.depends_on("sometool")
    project.depends_on("pyassert", url="https://github.com/downloads/halimath/pyassert/pyassert-0.2.2.tar.gz")
    project.name = "Spam and Eggs"
    project.version = "1.2.3"
    project.summary = "This is a simple integration-test for distutils plugin."
    project.description = "As you might have guessed we have nothing to say here."
    project.authors = [Author("Udo Juettner", "*****@*****.**"), Author("Michael Gruber", "*****@*****.**")]
    project.license = "WTFPL"
    project.url = "http://github.com/pybuilder/pybuilder"

    def return_dummy_list():
        return ["spam", "eggs"]

    project.list_scripts = return_dummy_list
    project.list_packages = return_dummy_list

    project.set_property("distutils_classifiers", ["Development Status :: 5 - Beta", "Environment :: Console"])
    project.install_file("dir", "file1")
    project.install_file("dir", "file2")
    project.include_file("spam", "eggs")

    return project
Beispiel #2
0
    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"))
class InstallRuntimeDependenciesTest(unittest.TestCase):
    def setUp(self):
        self.project = Project("unittest", ".")
        self.project.set_property("dir_install_logs", "any_directory")
        self.logger = mock(Logger)
        when(pythonbuilder.plugins.python.install_dependencies_plugin).execute_command(any_value(), any_value(),
            shell=True).thenReturn(0)

    def tearDown(self):
        unstub()

    def test_should_install_multiple_dependencies(self):
        self.project.depends_on("spam")
        self.project.depends_on("eggs")

        install_runtime_dependencies(self.logger, self.project)

        verify(pythonbuilder.plugins.python.install_dependencies_plugin).execute_command("pip install spam",
            any_value()
            , shell=True)
        verify(pythonbuilder.plugins.python.install_dependencies_plugin).execute_command("pip install eggs",
            any_value()
            , shell=True)
Beispiel #4
0
class Reactor(object):
    _current_instance = None

    @staticmethod
    def current_instance():
        return Reactor._current_instance

    def __init__(self, logger, execution_manager, plugin_loader=None):
        self.logger = logger
        self.execution_manager = execution_manager
        if not plugin_loader:
            self.plugin_loader = BuiltinPluginLoader(self.logger)
        else:
            self.plugin_loader = plugin_loader
        self._plugins = []
        self.project = None

    def require_plugin(self, plugin):
        if plugin not in self._plugins:
            try:
                self._plugins.append(plugin)
                self.import_plugin(plugin)
            except:
                self._plugins.remove(plugin)
                raise

    def get_plugins(self):
        return self._plugins

    def get_tasks(self):
        return self.execution_manager.tasks

    def validate_project(self):
        validation_messages = self.project.validate()
        if len(validation_messages) > 0:
            raise ProjectValidationFailedException(validation_messages)

    def prepare_build(self,
                      property_overrides={},
                      project_directory=".",
                      project_descriptor="build.py"):
        Reactor._current_instance = self

        project_directory, project_descriptor =\
        self.verify_project_directory(project_directory, project_descriptor)

        self.logger.debug("Loading project module from %s", project_descriptor)

        self.project = Project(basedir=project_directory)

        self.project_module = self.load_project_module(project_descriptor)

        self.apply_project_attributes()
        self.override_properties(property_overrides)

        self.logger.debug("Have loaded plugins %s", ", ".join(self._plugins))

        self.collect_tasks_and_actions_and_initializers(self.project_module)

        self.execution_manager.resolve_dependencies()

    def build(self, tasks=[], environments=[]):
        Reactor._current_instance = self

        if environments:
            self.logger.info("Activated environments: %s", ", ".join(environments))

        self.execution_manager.execute_initializers(environments, logger=self.logger, project=self.project)

        self.log_project_properties()

        self.validate_project()

        tasks = as_list(tasks)

        if not len(tasks):
            if self.project.default_task:
                tasks += as_list(self.project.default_task)
            else:
                raise PythonbuilderException("No default task given.")

        execution_plan = self.execution_manager.build_execution_plan(tasks)
        self.logger.debug("Execution plan is %s", ", ".join([task.name for task in execution_plan]))

        self.logger.info("Building %s version %s", self.project.name, self.project.version)
        self.logger.info("Executing build in %s", self.project.basedir)
        self.logger.info("Going to execute task%s %s",
            "s" if len(tasks) != 1 else "",
            ", ".join(tasks))

        task_execution_summaries =\
        self.execution_manager.execute_execution_plan(execution_plan,
            logger=self.logger,
            project=self.project,
            reactor=self)

        return BuildSummary(self.project, task_execution_summaries)

    def execute_task(self, task_name):
        execution_plan = self.execution_manager.build_execution_plan(task_name)

        self.execution_manager.execute_execution_plan(execution_plan,
            logger=self.logger,
            project=self.project,
            reactor=self)

    def override_properties(self, property_overrides):
        for property_override in property_overrides:
            self.project.set_property(property_override,
                property_overrides[property_override])

    def log_project_properties(self):
        formatted = ""
        for key in sorted(self.project.properties):
            formatted += "\n%40s : %s" % (key, self.project.get_property(key))
        self.logger.debug("Project properties: %s", formatted)

    def import_plugin(self, plugin):
        self.logger.debug("Loading plugin '%s'", plugin)
        plugin_module = self.plugin_loader.load_plugin(self.project, plugin)
        self.collect_tasks_and_actions_and_initializers(plugin_module)

    def collect_tasks_and_actions_and_initializers(self, project_module):
        for name in dir(project_module):
            candidate = getattr(project_module, name)

            if hasattr(candidate, NAME_ATTRIBUTE):
                name = getattr(candidate, NAME_ATTRIBUTE)
            elif hasattr(candidate, "__name__"):
                name = candidate.__name__
            description = getattr(candidate, DESCRIPTION_ATTRIBUTE) if hasattr(candidate, DESCRIPTION_ATTRIBUTE) else ""

            if hasattr(candidate, TASK_ATTRIBUTE) and getattr(candidate, TASK_ATTRIBUTE):
                dependencies = getattr(candidate, DEPENDS_ATTRIBUTE) if hasattr(candidate, DEPENDS_ATTRIBUTE) else None

                self.logger.debug("Found task %s", name)
                self.execution_manager.register_task(Task(name, candidate, dependencies, description))

            elif hasattr(candidate, ACTION_ATTRIBUTE) and getattr(candidate, ACTION_ATTRIBUTE):
                before = getattr(candidate, BEFORE_ATTRIBUTE) if hasattr(candidate, BEFORE_ATTRIBUTE) else None
                after = getattr(candidate, AFTER_ATTRIBUTE) if hasattr(candidate, AFTER_ATTRIBUTE) else None
                only_once = getattr(candidate, ONLY_ONCE_ATTRIBUTE) if hasattr(candidate,
                    ONLY_ONCE_ATTRIBUTE) else False

                self.logger.debug("Found action %s", name)
                self.execution_manager.register_action(Action(name, candidate, before, after, description, only_once))

            elif hasattr(candidate, INITIALIZER_ATTRIBUTE) and getattr(candidate, INITIALIZER_ATTRIBUTE):
                environments = []
                if hasattr(candidate, ENVIRONMENTS_ATTRIBUTE):
                    environments = getattr(candidate, ENVIRONMENTS_ATTRIBUTE)

                self.execution_manager.register_initializer(Initializer(name, candidate, environments, description))

    def apply_project_attributes(self):
        self.propagate_property("name")
        self.propagate_property("version")
        self.propagate_property("default_task")
        self.propagate_property("summary")
        self.propagate_property("home_page")
        self.propagate_property("description")
        self.propagate_property("authors")
        self.propagate_property("license")
        self.propagate_property("url")

    def propagate_property(self, property):
        if hasattr(self.project_module, property):
            value = getattr(self.project_module, property)
            setattr(self.project, property, value)

    def load_project_module(self, project_descriptor):
        try:
            return imp.load_source("build", project_descriptor)
        except ImportError as e:
            raise PythonbuilderException("Error importing project descriptor %s: %s" % (project_descriptor, e))

    def verify_project_directory(self, project_directory, project_descriptor):
        project_directory = os.path.abspath(project_directory)

        if not os.path.exists(project_directory):
            raise PythonbuilderException("Project directory does not exist: %s", project_directory)
        if not os.path.isdir(project_directory):
            raise PythonbuilderException("Project directory is not a directory: %s", project_directory)

        project_descriptor_full_path = os.path.join(project_directory, project_descriptor)

        if not os.path.exists(project_descriptor_full_path):
            raise PythonbuilderException("Project directory does not contain descriptor file: %s",
                project_descriptor_full_path)
        if not os.path.isfile(project_descriptor_full_path):
            raise PythonbuilderException("Project descriptor is not a file: %s", project_descriptor_full_path)

        return project_directory, project_descriptor_full_path
Beispiel #5
0
class ProjectTest (unittest.TestCase):
    def setUp (self):
        self.project = Project(basedir="/imaginary", name="Unittest")
    
    def test_should_pick_directory_name_for_project_name_when_name_is_not_given (self):
        try:
            when(os.path).basename("/imaginary").thenReturn("imaginary")
            
            project = Project(basedir="/imaginary")
            
            self.assertEquals("imaginary", project.name)
            verify(os.path).basename("/imaginary")
        finally:
            unstub()
    
    def test_get_property_should_return_default_value_when_property_is_not_set (self):
        self.assertEquals("spam", self.project.get_property("spam", "spam"))

    def test_get_property_should_return_property_value_when_property_is_set (self):
        self.project.set_property("spam", "eggs")
        self.assertEquals("eggs", self.project.get_property("spam", "spam"))

    def test_has_property_should_return_false_when_property_is_not_set (self):
        self.assertFalse(self.project.has_property("spam"))

    def test_has_property_should_return_true_when_property_is_set (self):
        self.project.set_property("spam", "eggs")
        self.assertTrue(self.project.has_property("spam"))
        
    def test_set_property_if_unset_should_set_property_when_property_is_not_set (self):
        self.project.set_property_if_unset("spam", "spam")
        self.assertEquals("spam", self.project.get_property("spam"))

    def test_set_property_if_unset_should_not_set_property_when_property_is_already_set (self):
        self.project.set_property("spam", "eggs")
        self.project.set_property_if_unset("spam", "spam")
        self.assertEquals("eggs", self.project.get_property("spam"))
    
    def test_expand_should_raise_exception_when_property_is_not_set (self):
        self.assertRaises(MissingPropertyException, self.project.expand, "$spam")

    def test_expand_should_return_expanded_string_when_property_is_set (self):
        self.project.set_property("spam", "eggs")
        self.assertEquals("eggs", self.project.expand("$spam"))

    def test_expand_should_return_expanded_string_when_two_properties_are_found_and_set (self):
        self.project.set_property("spam", "spam")
        self.project.set_property("eggs", "eggs")
        self.assertEquals("spam and eggs", self.project.expand("$spam and $eggs"))
        
    def test_expand_should_expand_property_with_value_being_an_property_expression (self):
        self.project.set_property("spam", "spam")
        self.project.set_property("eggs", "$spam")
        self.assertEquals("spam", self.project.expand("$eggs"))

    def test_expand_should_raise_exception_when_first_expansion_leads_to_property_reference_and_property_is_undefined (self):
        self.project.set_property("eggs", "$spam")
        self.assertRaises(MissingPropertyException, self.project.expand, "$eggs")
        
    def test_expand_path_should_return_expanded_path (self):
        self.project.set_property("spam", "spam")
        self.project.set_property("eggs", "eggs")
        self.assertEquals(os.path.join("/imaginary", "spam", "eggs"),
                          self.project.expand_path("$spam/$eggs"))

    def test_expand_path_should_return_expanded_path_and_additional_parts_when_additional_parts_are_given (self):
        self.project.set_property("spam", "spam")
        self.project.set_property("eggs", "eggs")
        self.assertEquals(os.path.join("/imaginary", "spam", "eggs", "foo", "bar"),
                          self.project.expand_path("$spam/$eggs", "foo", "bar"))
        
    def test_should_raise_exception_when_getting_mandatory_propert_and_property_is_not_found (self):
        self.assertRaises(MissingPropertyException, 
                          self.project.get_mandatory_property, "i_dont_exist")

    def test_should_return_property_value_when_getting_mandatory_propert_and_property_exists (self):
        self.project.set_property("spam", "spam")
        self.assertEquals("spam", self.project.get_mandatory_property("spam"))
        
    def test_should_add_runtime_dependency_with_name_only (self):
        self.project.depends_on("spam")
        self.assertEquals(1, len(self.project.dependencies))
        self.assertEquals("spam", self.project.dependencies[0].name)
        self.assertEquals(None, self.project.dependencies[0].version)

    def test_should_add_dependency_with_name_and_version (self):
        self.project.depends_on("spam", "0.7")
        self.assertEquals(1, len(self.project.dependencies))
        self.assertEquals("spam", self.project.dependencies[0].name)
        self.assertEquals("0.7", self.project.dependencies[0].version)

    def test_should_add_dependency_with_name_and_version_only_once (self):
        self.project.depends_on("spam", "0.7")
        self.project.depends_on("spam", "0.7")
        self.assertEquals(1, len(self.project.dependencies))
        self.assertEquals("spam", self.project.dependencies[0].name)
        self.assertEquals("0.7", self.project.dependencies[0].version)
class InstallDependencyTest(unittest.TestCase):
    def setUp(self):
        self.project = Project("unittest", ".")
        self.project.set_property("dir_install_logs", "any_directory")
        self.logger = mock(Logger)
        when(pythonbuilder.plugins.python.install_dependencies_plugin).execute_command(any_value(), any_value(),
            shell=True).thenReturn(0)

    def tearDown(self):
        unstub()

    def test_should_install_dependency_without_version(self):
        dependency = Dependency("spam")

        install_dependency(self.logger, self.project, dependency)

        verify(pythonbuilder.plugins.python.install_dependencies_plugin).execute_command("pip install spam", any_value()
            , shell=True)

    def test_should_install_dependency_using_custom_index_url(self):
        self.project.set_property("install_dependencies_index_url", "some_index_url")
        dependency = Dependency("spam")

        install_dependency(self.logger, self.project, dependency)

        verify(pythonbuilder.plugins.python.install_dependencies_plugin).execute_command(
            "pip install --index-url some_index_url spam", any_value()
            , shell=True)

    def test_should_not_use_extra_index_url_when_index_url_is_not_set(self):
        self.project.set_property("install_dependencies_extra_index_url", "some_index_url")
        dependency = Dependency("spam")

        install_dependency(self.logger, self.project, dependency)

        verify(pythonbuilder.plugins.python.install_dependencies_plugin).execute_command(
            "pip install spam", any_value()
            , shell=True)

    def test_should_not_use_index_and_extra_index_url_when_index_and_extra_index_url_are_set(self):
        self.project.set_property("install_dependencies_index_url", "some_index_url")
        self.project.set_property("install_dependencies_extra_index_url", "some_extra_index_url")
        dependency = Dependency("spam")

        install_dependency(self.logger, self.project, dependency)

        verify(pythonbuilder.plugins.python.install_dependencies_plugin).execute_command(
            "pip install --index-url some_index_url --extra-index-url some_extra_index_url spam", any_value()
            , shell=True)

    def test_should_use_mirrors_to_install_dependency(self):
        self.project.set_property("install_dependencies_use_mirrors", True)
        dependency = Dependency("spam")

        install_dependency(self.logger, self.project, dependency)

        verify(pythonbuilder.plugins.python.install_dependencies_plugin).execute_command(
            "pip install --use-mirrors spam", any_value()
            , shell=True)

    def test_should_upgrade_dependencies(self):
        self.project.set_property("install_dependencies_upgrade", True)
        dependency = Dependency("spam")

        install_dependency(self.logger, self.project, dependency)

        verify(pythonbuilder.plugins.python.install_dependencies_plugin).execute_command(
            "pip install --upgrade spam", any_value()
            , shell=True)

    def test_should_install_dependency_with_version(self):
        dependency = Dependency("spam", "0.1.2")

        install_dependency(self.logger, self.project, dependency)

        verify(pythonbuilder.plugins.python.install_dependencies_plugin).execute_command("pip install spam>=0.1.2",
            any_value()
            , shell=True)

    def test_should_install_dependency_with_version_and_operator(self):
        dependency = Dependency("spam", "==0.1.2")

        install_dependency(self.logger, self.project, dependency)

        verify(pythonbuilder.plugins.python.install_dependencies_plugin).execute_command("pip install spam==0.1.2",
            any_value()
            , shell=True)

    def test_should_install_dependency_with_url(self):
        dependency = Dependency("spam", url="some_url")

        install_dependency(self.logger, self.project, dependency)

        verify(pythonbuilder.plugins.python.install_dependencies_plugin).execute_command("pip install some_url",
            any_value()
            , shell=True)

    def test_should_install_dependency_with_url_even_if_version_is_given(self):
        dependency = Dependency("spam", version="0.1.2", url="some_url")

        install_dependency(self.logger, self.project, dependency)

        verify(pythonbuilder.plugins.python.install_dependencies_plugin).execute_command("pip install some_url",
            any_value()
            , shell=True)