コード例 #1
0
    def generate(self, project):
        LOG.debug("Generate started")
        root = project.root / "cmd"

        # project folder structure
        src = root / "resource"
        format_paths = []

        LOG.debug("Writing Types")
        models = resolve_models(project.schema)
        template = self.env.get_template("types.go.tple")
        path = src / "{}.go".format("model")
        contents = template.render(models=models)
        project.overwrite(path, contents)
        format_paths.append(path)

        path = root / "main.go"
        LOG.debug("Writing project: %s", path)
        template = self.env.get_template("main.go.tple")
        importpath = Path(project.settings["importpath"])
        contents = template.render(path=importpath / "cmd" / "resource")
        project.overwrite(path, contents)
        format_paths.append(path)

        # named files must all be in one directory
        for path in format_paths:
            try:
                subprocess_run(["go", "fmt", path], cwd=root, check=True)
            except (FileNotFoundError, CalledProcessError) as e:
                raise DownstreamError("go fmt failed") from e
コード例 #2
0
    def _build(self, base_path):
        LOG.debug("Dependencies build started from '%s'", base_path)

        # TODO: We should use the build logic from SAM CLI library, instead:
        # https://github.com/awslabs/aws-sam-cli/blob/master/samcli/lib/build/app_builder.py
        command = self._make_build_command(base_path, self._build_command)
        if self._use_docker:
            command = command + " --use-container"
        command = command + " " + MAIN_HANDLER_FUNCTION

        LOG.debug("command is '%s'", command)

        LOG.warning("Starting build.")
        try:
            completed_proc = subprocess_run(  # nosec
                ["/bin/bash", "-c", command],
                stdout=PIPE,
                stderr=PIPE,
                cwd=base_path,
                check=True,
            )
        except (FileNotFoundError, CalledProcessError) as e:
            raise DownstreamError("local build failed") from e

        LOG.debug("--- build stdout:\n%s", completed_proc.stdout)
        LOG.debug("--- build stderr:\n%s", completed_proc.stderr)
        LOG.debug("Dependencies build finished")
コード例 #3
0
    def generate(self, project):
        LOG.debug("Generate started")
        root = project.root / "cmd"

        # project folder structure
        src = root / "resource"
        format_paths = []

        LOG.debug("Writing Types")
        models = resolve_models(project.schema)
        template = self.env.get_template("types.go.tple")
        path = src / "{}.go".format("model")
        contents = template.render(models=models)
        project.overwrite(path, contents)
        format_paths.append(path)

        path = root / "main.go"
        LOG.debug("Writing project: %s", path)
        template = self.env.get_template("main.go.tple")
        importpath = Path(project.settings["importpath"])
        contents = template.render(path=importpath / "cmd" / "resource")
        project.overwrite(path, contents)
        format_paths.append(path)

        # Makefile
        path = project.root / "Makefile"
        LOG.debug("Writing Makefile: %s", path)
        template = self.env.get_template("Makefile")
        contents = template.render()
        project.overwrite(path, contents)

        # named files must all be in one directory
        for path in format_paths:
            try:
                subprocess_run(["go", "fmt", path],
                               cwd=root,
                               check=True,
                               capture_output=True)
            except (FileNotFoundError, CalledProcessError) as e:
                raise DownstreamError("go fmt failed") from e

        # Update settings as needed
        need_to_write = False
        for key, new in DEFAULT_SETTINGS.items():
            old = project.settings.get(key)

            if project.settings.get(key) != new:
                LOG.debug(f"{key} version change from {old} to {new}")
                project.settings[key] = new
                need_to_write = True

                if key == "pluginVersion":
                    # Display any upgrade messages
                    print(*check_version(old), sep="\n")

        if need_to_write:
            project.write_settings()
コード例 #4
0
    def _docker_build(cls, external_path):
        cls._check_for_support_lib_sdist(external_path)

        internal_path = PurePosixPath("/project")
        command = " ".join(cls._make_pip_command(internal_path))
        LOG.debug("command is '%s'", command)

        volumes = {
            str(external_path): {
                "bind": str(internal_path),
                "mode": "rw"
            }
        }
        image = f"lambci/lambda:build-{cls.RUNTIME}"
        LOG.warning(
            "Starting Docker build. This may take several minutes if the "
            "image '%s' needs to be pulled first.",
            image,
        )
        docker_client = docker.from_env()
        try:
            logs = docker_client.containers.run(
                image=image,
                command=command,
                auto_remove=True,
                volumes=volumes,
                stream=True,
                user=f"{os.geteuid()}:{os.getgid()}",
            )
        except RequestsConnectionError as e:
            # it seems quite hard to reliably extract the cause from
            # ConnectionError. we replace it with a friendlier error message
            # and preserve the cause for debug traceback
            cause = RequestsConnectionError(
                "Could not connect to docker - is it running?")
            cause.__cause__ = e
            raise DownstreamError("Error running docker build") from cause
        except (ContainerError, ImageLoadError, APIError) as e:
            raise DownstreamError("Error running docker build") from e
        LOG.debug("Build running. Output:")
        for line in logs:
            LOG.debug(line.rstrip(b"\n").decode("utf-8"))
コード例 #5
0
    def _pip_build(cls, base_path):
        command = cls._make_pip_command(base_path)
        LOG.debug("command is '%s'", command)

        LOG.warning("Starting pip build.")
        try:
            completed_proc = subprocess_run(  # nosec
                command, stdout=PIPE, stderr=PIPE, cwd=base_path, check=True
            )
        except (FileNotFoundError, CalledProcessError) as e:
            raise DownstreamError("pip build failed") from e

        LOG.debug("--- pip stdout:\n%s", completed_proc.stdout)
        LOG.debug("--- pip stderr:\n%s", completed_proc.stderr)
コード例 #6
0
 def raise_exception(_args):
     raise DownstreamError("ignored") from Exception(ERROR_MSG)