Skip to content

refactor: assign each workflow a default build directory #428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 41 additions & 17 deletions aws_lambda_builders/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from collections import namedtuple
from enum import Enum
from typing import Dict, Optional

from aws_lambda_builders.binary_path import BinaryPath
from aws_lambda_builders.path_resolver import PathResolver
Expand Down Expand Up @@ -38,6 +39,12 @@ class BuildMode(object):
RELEASE = "release"


class BuildDirectory(Enum):
SCRATCH = "scratch"
ARTIFACTS = "artifacts"
SOURCE = "source"


class BuildInSourceSupport(Enum):
"""
Enum to define a workflow's support for building in source.
Expand Down Expand Up @@ -140,14 +147,13 @@ def __new__(mcs, name, bases, class_dict):
if not isinstance(cls.CAPABILITY, Capability):
raise ValueError("Workflow '{}' must register valid capabilities".format(cls.NAME))

# All workflows must define valid default and supported values for build in source
if (
not isinstance(cls.BUILD_IN_SOURCE_SUPPORT, BuildInSourceSupport)
or cls.BUILD_IN_SOURCE_BY_DEFAULT not in cls.BUILD_IN_SOURCE_SUPPORT.value
):
raise ValueError(
"Workflow '{}' must define valid default and supported values for build in source".format(cls.NAME)
)
# All workflows must define supported values for build in source
if not isinstance(cls.BUILD_IN_SOURCE_SUPPORT, BuildInSourceSupport):
raise ValueError("Workflow '{}' must define supported values for build in source".format(cls.NAME))

# All workflows must define default build directory
if not isinstance(cls.DEFAULT_BUILD_DIR, BuildDirectory):
raise ValueError("Workflow '{}' must define default build directory".format(cls.NAME))

LOG.debug("Registering workflow '%s' with capability '%s'", cls.NAME, cls.CAPABILITY)
DEFAULT_REGISTRY[cls.CAPABILITY] = cls
Expand All @@ -173,12 +179,12 @@ class BaseWorkflow(object, metaclass=_WorkflowMetaClass):
# Optional list of manifests file/folder names supported by this workflow.
SUPPORTED_MANIFESTS = []

# Whether the workflow builds in source by default, each workflow should define this.
# (some workflows build in temporary or artifact directories by default)
BUILD_IN_SOURCE_BY_DEFAULT = None
# Support for building in source, each workflow should define this.
BUILD_IN_SOURCE_SUPPORT = None

# The directory where the workflow builds/installs by default, each workflow should define this.
DEFAULT_BUILD_DIR = None

def __init__(
self,
source_dir,
Expand Down Expand Up @@ -261,21 +267,39 @@ def __init__(
self.is_building_layer = is_building_layer
self.experimental_flags = experimental_flags if experimental_flags else []

self.build_in_source = build_in_source
# this represents where the build/install happens, not the final output directory (that's the artifacts_dir)
self.build_dir = self._select_build_dir(build_in_source)

# Actions are registered by the subclasses as they seem fit
self.actions = []
self._binaries = {}
Comment on lines +273 to +275
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just moved these up, didn't add these


def _select_build_dir(self, build_in_source: Optional[bool]) -> str:
"""
Returns the build directory for the workflow.
"""

should_build_in_source = build_in_source
if build_in_source not in self.BUILD_IN_SOURCE_SUPPORT.value:
# assign default value
should_build_in_source = self.DEFAULT_BUILD_DIR == BuildDirectory.SOURCE

# only show warning if an unsupported value was explicitly passed in
if build_in_source is not None:
LOG.warning(
'Workflow %s does not support value "%s" for building in source. Using default value "%s".',
self.NAME,
build_in_source,
self.BUILD_IN_SOURCE_BY_DEFAULT,
should_build_in_source,
)
self.build_in_source = self.BUILD_IN_SOURCE_BY_DEFAULT

# Actions are registered by the subclasses as they seem fit
self.actions = []
self._binaries = {}
build_directory_mapping = {
BuildDirectory.SCRATCH: self.scratch_dir,
BuildDirectory.ARTIFACTS: self.artifacts_dir,
BuildDirectory.SOURCE: self.source_dir,
}

return self.source_dir if should_build_in_source else build_directory_mapping.get(self.DEFAULT_BUILD_DIR)

def is_supported(self):
"""
Expand Down
23 changes: 7 additions & 16 deletions aws_lambda_builders/workflows/custom_make/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
ProvidedMakeWorkflow
"""
from aws_lambda_builders.workflows.custom_make.validator import CustomMakeRuntimeValidator
from aws_lambda_builders.workflow import BaseWorkflow, Capability, BuildInSourceSupport
from aws_lambda_builders.workflow import BaseWorkflow, Capability, BuildInSourceSupport, BuildDirectory
from aws_lambda_builders.actions import CopySourceAction
from aws_lambda_builders.path_resolver import PathResolver
from .actions import CustomMakeAction
Expand All @@ -23,7 +23,7 @@ class CustomMakeWorkflow(BaseWorkflow):

EXCLUDED_FILES = (".aws-sam", ".git")

BUILD_IN_SOURCE_BY_DEFAULT = False
DEFAULT_BUILD_DIR = BuildDirectory.SCRATCH
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.OPTIONALLY_SUPPORTED

def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtime=None, osutils=None, **kwargs):
Expand All @@ -35,7 +35,6 @@ def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtim
self.os_utils = OSUtils()

options = kwargs.get("options") or {}
build_in_source = kwargs.get("build_in_source")

build_logical_id = options.get("build_logical_id", None)
if not build_logical_id:
Expand All @@ -47,10 +46,8 @@ def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtim

subprocess_make = SubProcessMake(make_exe=self.binaries["make"].binary_path, osutils=self.os_utils)

# an explicitly definied working directory should take precedence
working_directory = options.get("working_directory") or self._select_working_directory(
source_dir, scratch_dir, build_in_source
)
# an explicitly defined working directory should take precedence
working_directory = options.get("working_directory") or self.build_dir

make_action = CustomMakeAction(
artifacts_dir,
Expand All @@ -63,18 +60,12 @@ def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtim

self.actions = []

if not self.build_in_source:
# if we're building on scratch_dir, we have to first copy the source there
self.actions.append(CopySourceAction(source_dir, scratch_dir, excludes=self.EXCLUDED_FILES))
if self.build_dir != source_dir:
# if we're not building in the source directory, we have to first copy the source
self.actions.append(CopySourceAction(source_dir, self.build_dir, excludes=self.EXCLUDED_FILES))

self.actions.append(make_action)

def _select_working_directory(self, source_dir: str, scratch_dir: str, build_in_source: bool):
Copy link
Contributor Author

@torresxb1 torresxb1 Jan 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to the changes in this PR, we can remove the logic of choosing a working/build directory based on the build_in_source parameter. Similarly, when we implement building in source for other workflows we won't have to define this kind of logic either because the parent Workflow class now handles setting the build_dir.

"""
Returns the directory where the make action should be executed
"""
return source_dir if build_in_source else scratch_dir

def get_resolvers(self):
return [PathResolver(runtime="provided", binary="make", executable_search_paths=self.executable_search_paths)]

Expand Down
4 changes: 2 additions & 2 deletions aws_lambda_builders/workflows/dotnet_clipackage/workflow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
.NET Core CLI Package Workflow
"""
from aws_lambda_builders.workflow import BaseWorkflow, Capability, BuildInSourceSupport
from aws_lambda_builders.workflow import BaseWorkflow, BuildDirectory, Capability, BuildInSourceSupport

from .actions import GlobalToolInstallAction, RunPackageAction
from .dotnetcli import SubprocessDotnetCLI
Expand All @@ -19,7 +19,7 @@ class DotnetCliPackageWorkflow(BaseWorkflow):

CAPABILITY = Capability(language="dotnet", dependency_manager="cli-package", application_framework=None)

BUILD_IN_SOURCE_BY_DEFAULT = True
DEFAULT_BUILD_DIR = BuildDirectory.SOURCE
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.EXCLUSIVELY_SUPPORTED

def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtime=None, mode=None, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions aws_lambda_builders/workflows/go_modules/workflow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Go Modules Workflow
"""
from aws_lambda_builders.workflow import BaseWorkflow, Capability, BuildInSourceSupport
from aws_lambda_builders.workflow import BaseWorkflow, BuildDirectory, Capability, BuildInSourceSupport

from .actions import GoModulesBuildAction
from .builder import GoModulesBuilder
Expand All @@ -15,7 +15,7 @@ class GoModulesWorkflow(BaseWorkflow):

CAPABILITY = Capability(language="go", dependency_manager="modules", application_framework=None)

BUILD_IN_SOURCE_BY_DEFAULT = True
DEFAULT_BUILD_DIR = BuildDirectory.SOURCE
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.EXCLUSIVELY_SUPPORTED

def __init__(
Expand Down
4 changes: 2 additions & 2 deletions aws_lambda_builders/workflows/java_gradle/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import hashlib
import os
from aws_lambda_builders.actions import CleanUpAction
from aws_lambda_builders.workflow import BaseWorkflow, Capability, BuildInSourceSupport
from aws_lambda_builders.workflow import BaseWorkflow, BuildDirectory, Capability, BuildInSourceSupport
from aws_lambda_builders.workflows.java.actions import JavaCopyDependenciesAction, JavaMoveDependenciesAction
from aws_lambda_builders.workflows.java.utils import OSUtils

Expand All @@ -25,7 +25,7 @@ class JavaGradleWorkflow(BaseWorkflow):

INIT_FILE = "lambda-build-init.gradle"

BUILD_IN_SOURCE_BY_DEFAULT = False
DEFAULT_BUILD_DIR = BuildDirectory.SCRATCH
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.NOT_SUPPORTED

def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions aws_lambda_builders/workflows/java_maven/workflow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Java Maven Workflow
"""
from aws_lambda_builders.workflow import BaseWorkflow, Capability, BuildInSourceSupport
from aws_lambda_builders.workflow import BaseWorkflow, BuildDirectory, Capability, BuildInSourceSupport
from aws_lambda_builders.actions import CopySourceAction, CleanUpAction
from aws_lambda_builders.workflows.java.actions import JavaCopyDependenciesAction, JavaMoveDependenciesAction
from aws_lambda_builders.workflows.java.utils import OSUtils
Expand All @@ -28,7 +28,7 @@ class JavaMavenWorkflow(BaseWorkflow):

EXCLUDED_FILES = (".aws-sam", ".git")

BUILD_IN_SOURCE_BY_DEFAULT = False
DEFAULT_BUILD_DIR = BuildDirectory.SCRATCH
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.NOT_SUPPORTED

def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions aws_lambda_builders/workflows/nodejs_npm/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging

from aws_lambda_builders.path_resolver import PathResolver
from aws_lambda_builders.workflow import BaseWorkflow, Capability, BuildInSourceSupport
from aws_lambda_builders.workflow import BaseWorkflow, BuildDirectory, Capability, BuildInSourceSupport
from aws_lambda_builders.actions import (
CopySourceAction,
CleanUpAction,
Expand Down Expand Up @@ -42,7 +42,7 @@ class NodejsNpmWorkflow(BaseWorkflow):

CONFIG_PROPERTY = "aws_sam"

BUILD_IN_SOURCE_BY_DEFAULT = False
DEFAULT_BUILD_DIR = BuildDirectory.ARTIFACTS
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.NOT_SUPPORTED

def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtime=None, osutils=None, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pathlib import Path
from typing import List

from aws_lambda_builders.workflow import BaseWorkflow, Capability, BuildInSourceSupport
from aws_lambda_builders.workflow import BaseWorkflow, BuildDirectory, Capability, BuildInSourceSupport
from aws_lambda_builders.actions import (
CopySourceAction,
CleanUpAction,
Expand Down Expand Up @@ -44,7 +44,7 @@ class NodejsNpmEsbuildWorkflow(BaseWorkflow):

CONFIG_PROPERTY = "aws_sam"

BUILD_IN_SOURCE_BY_DEFAULT = False
DEFAULT_BUILD_DIR = BuildDirectory.SCRATCH
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.NOT_SUPPORTED

def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtime=None, osutils=None, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions aws_lambda_builders/workflows/python_pip/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
import logging

from aws_lambda_builders.workflow import BaseWorkflow, BuildInSourceSupport, Capability
from aws_lambda_builders.workflow import BaseWorkflow, BuildDirectory, BuildInSourceSupport, Capability
from aws_lambda_builders.actions import CopySourceAction, CleanUpAction, LinkSourceAction
from aws_lambda_builders.workflows.python_pip.validator import PythonRuntimeValidator
from aws_lambda_builders.path_resolver import PathResolver
Expand Down Expand Up @@ -67,7 +67,7 @@ class PythonPipWorkflow(BaseWorkflow):

PYTHON_VERSION_THREE = "3"

BUILD_IN_SOURCE_BY_DEFAULT = False
DEFAULT_BUILD_DIR = BuildDirectory.SCRATCH
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.NOT_SUPPORTED

def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtime=None, osutils=None, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions aws_lambda_builders/workflows/ruby_bundler/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
import logging

from aws_lambda_builders.workflow import BaseWorkflow, BuildInSourceSupport, Capability
from aws_lambda_builders.workflow import BaseWorkflow, BuildDirectory, BuildInSourceSupport, Capability
from aws_lambda_builders.actions import CopySourceAction, CopyDependenciesAction, CleanUpAction
from .actions import RubyBundlerInstallAction, RubyBundlerVendorAction
from .utils import OSUtils
Expand All @@ -25,7 +25,7 @@ class RubyBundlerWorkflow(BaseWorkflow):

EXCLUDED_FILES = (".aws-sam", ".git")

BUILD_IN_SOURCE_BY_DEFAULT = False
DEFAULT_BUILD_DIR = BuildDirectory.ARTIFACTS
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.NOT_SUPPORTED

def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtime=None, osutils=None, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import os

from aws_lambda_builders.workflow import BaseWorkflow, BuildInSourceSupport, Capability
from aws_lambda_builders.workflow import BaseWorkflow, BuildDirectory, BuildInSourceSupport, Capability
from aws_lambda_builders.actions import BaseAction, Purpose


Expand Down Expand Up @@ -34,7 +34,7 @@ class WriteHelloWorkflow(BaseWorkflow):

NAME = "WriteHelloWorkflow"
CAPABILITY = Capability(language="python", dependency_manager="test", application_framework="test")
BUILD_IN_SOURCE_BY_DEFAULT = False
DEFAULT_BUILD_DIR = BuildDirectory.SCRATCH
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.OPTIONALLY_SUPPORTED

def __init__(self, source_dir, artifacts_dir, *args, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from parameterized import parameterized, param

from aws_lambda_builders.builder import LambdaBuilder
from aws_lambda_builders.workflow import BuildInSourceSupport, Capability, BaseWorkflow
from aws_lambda_builders.workflow import BuildDirectory, BuildInSourceSupport, Capability, BaseWorkflow
from aws_lambda_builders.registry import DEFAULT_REGISTRY


Expand Down Expand Up @@ -71,7 +71,7 @@ class MyWorkflow(BaseWorkflow):
CAPABILITY = Capability(
language=self.lang, dependency_manager=self.lang_framework, application_framework=self.app_framework
)
BUILD_IN_SOURCE_BY_DEFAULT = False
DEFAULT_BUILD_DIR = BuildDirectory.SCRATCH
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.OPTIONALLY_SUPPORTED

def __init__(
Expand Down
Loading