コード例 #1
0
    def new_project(self, project_root: str) -> 'RuntimeConfig':
        """Given a new project root, read in its project dictionary, supply the
        existing project's profile info, and create a new project file.

        :param project_root: A filepath to a dbt project.
        :raises DbtProfileError: If the profile is invalid.
        :raises DbtProjectError: If project is missing or invalid.
        :returns: The new configuration.
        """
        # copy profile
        profile = Profile(**self.to_profile_info())
        profile.validate()

        # load the new project and its packages. Don't pass cli variables.
        renderer = DbtProjectYamlRenderer(generate_target_context(profile, {}))

        project = Project.from_project_root(
            project_root,
            renderer,
            verify_version=getattr(self.args, 'version_check', False),
        )

        cfg = self.from_parts(
            project=project,
            profile=profile,
            args=deepcopy(self.args),
        )
        # force our quoting back onto the new project.
        cfg.quoting = deepcopy(self.quoting)
        return cfg
コード例 #2
0
    def collect_parts(
        cls: Type['RuntimeConfig'], args: Any
    ) -> Tuple[Project, Profile]:
        # profile_name from the project
        project_root = args.project_dir if args.project_dir else os.getcwd()
        version_check = getattr(args, 'version_check', False)
        partial = Project.partial_load(
            project_root,
            verify_version=version_check
        )

        # build the profile using the base renderer and the one fact we know
        cli_vars: Dict[str, Any] = parse_cli_vars(getattr(args, 'vars', '{}'))
        profile_renderer = ProfileRenderer(generate_base_context(cli_vars))
        profile_name = partial.render_profile_name(profile_renderer)

        profile = cls._get_rendered_profile(
            args, profile_renderer, profile_name
        )

        # get a new renderer using our target information and render the
        # project
        ctx = generate_target_context(profile, cli_vars)
        project_renderer = DbtProjectYamlRenderer(ctx)
        project = partial.render(project_renderer)
        return (project, profile)
コード例 #3
0
def get_project(git_path):
    phony_profile = Profile.from_raw_profiles(raw_profiles=PHONY_PROFILE,
                                              profile_name='hubcap',
                                              renderer=ProfileRenderer({}))

    ctx = generate_target_context(phony_profile, cli_vars={})
    renderer = DbtProjectYamlRenderer(ctx)
    return Project.from_project_root(git_path, renderer)
コード例 #4
0
def project_from_dict(project, profile, packages=None, cli_vars='{}'):
    from dbt.context.target import generate_target_context
    from dbt.config import Project, ConfigRenderer
    from dbt.utils import parse_cli_vars
    if not isinstance(cli_vars, dict):
        cli_vars = parse_cli_vars(cli_vars)

    renderer = ConfigRenderer(generate_target_context(profile, cli_vars))

    project_root = project.pop('project-root', os.getcwd())

    return Project.render_from_dict(project_root, project, packages, renderer)
コード例 #5
0
    def __init__(
        self,
        results,
        project,
        root_project,
        macro_manifest,
    ) -> None:
        super().__init__(results, project, root_project, macro_manifest)
        all_v_2 = (self.root_project.config_version == 2
                   and self.project.config_version == 2)
        if all_v_2:
            ctx = generate_schema_yml(self.root_project,
                                      self.project.project_name)
        else:
            ctx = generate_target_context(self.root_project,
                                          self.root_project.cli_vars)

        self.raw_renderer = SchemaYamlRenderer(ctx)
        self.config_generator = ContextConfigGenerator(self.root_project)
コード例 #6
0
ファイル: resolver.py プロジェクト: abhinav-spoj1996/fivetran
def resolve_packages(
    packages: List[PackageContract], config: RuntimeConfig
) -> List[PinnedPackage]:
    pending = PackageListing.from_contracts(packages)
    final = PackageListing()

    ctx = generate_target_context(config, config.cli_vars)
    renderer = DbtProjectYamlRenderer(ctx, config.config_version)

    while pending:
        next_pending = PackageListing()
        # resolve the dependency in question
        for package in pending:
            final.incorporate(package)
            target = final[package].resolved().fetch_metadata(config, renderer)
            next_pending.update_from(target.packages)
        pending = next_pending

    resolved = final.resolved()
    _check_for_duplicate_project_names(resolved, config, renderer)
    return resolved
コード例 #7
0
    def run(self):
        system.make_directory(self.config.modules_path)
        packages = self.config.packages.packages
        if not packages:
            logger.info('Warning: No packages were found in packages.yml')
            return

        with downloads_directory():
            final_deps = resolve_packages(packages, self.config)

            renderer = DbtProjectYamlRenderer(
                generate_target_context(self.config, self.config.cli_vars))

            for package in final_deps:
                logger.info('Installing {}', package)
                package.install(self.config, renderer)
                logger.info('  Installed from {}\n',
                            package.nice_version_name())

                self.track_package_install(package_name=package.name,
                                           source_type=package.source_type(),
                                           version=package.get_version())
コード例 #8
0
    def _load_project(self):
        if not os.path.exists(self.project_path):
            self.project_fail_details = FILE_NOT_FOUND
            return red('ERROR not found')

        if self.profile is None:
            ctx = generate_base_context(self.cli_vars)
        else:
            ctx = generate_target_context(self.profile, self.cli_vars)

        renderer = DbtProjectYamlRenderer(ctx)

        try:
            self.project = Project.from_project_root(
                self.project_dir,
                renderer,
                verify_version=getattr(self.args, 'version_check', False),
            )
        except dbt.exceptions.DbtConfigError as exc:
            self.project_fail_details = str(exc)
            return red('ERROR invalid')

        return green('OK found and valid')
コード例 #9
0
ファイル: schemas.py プロジェクト: chrisp-data/all_data
    def __init__(
        self,
        project,
        manifest,
        root_project,
    ) -> None:
        super().__init__(project, manifest, root_project)
        all_v_2 = (self.root_project.config_version == 2
                   and self.project.config_version == 2)
        if all_v_2:
            ctx = generate_schema_yml(self.root_project,
                                      self.project.project_name)
        else:
            ctx = generate_target_context(self.root_project,
                                          self.root_project.cli_vars)

        self.raw_renderer = SchemaYamlRenderer(ctx)

        internal_package_names = get_adapter_package_names(
            self.root_project.credentials.type)
        self.macro_resolver = MacroResolver(self.manifest.macros,
                                            self.root_project.project_name,
                                            internal_package_names)
コード例 #10
0
    def _parse_generic_test(
        self,
        target: Testable,
        test: Dict[str, Any],
        tags: List[str],
        column_name: Optional[str],
    ) -> ParsedSchemaTestNode:

        render_ctx = generate_target_context(self.root_project,
                                             self.root_project.cli_vars)
        try:
            builder = TestBuilder(
                test=test,
                target=target,
                column_name=column_name,
                package_name=target.package_name,
                render_ctx=render_ctx,
            )
        except CompilationException as exc:
            context = _trimmed(str(target))
            msg = ('Invalid test config given in {}:'
                   '\n\t{}\n\t@: {}'.format(target.original_file_path, exc.msg,
                                            context))
            raise CompilationException(msg) from exc
        original_name = os.path.basename(target.original_file_path)
        compiled_path = get_pseudo_test_path(
            builder.compiled_name,
            original_name,
            'schema_test',
        )
        fqn_path = get_pseudo_test_path(
            builder.fqn_name,
            original_name,
            'schema_test',
        )
        # the fqn for tests actually happens in the test target's name, which
        # is not necessarily this package's name
        fqn = self.get_fqn(fqn_path, builder.fqn_name)

        config = self.initial_config(fqn)

        metadata = {
            'namespace': builder.namespace,
            'name': builder.name,
            'kwargs': builder.args,
        }
        tags = sorted(set(itertools.chain(tags, builder.tags())))
        if 'schema' not in tags:
            tags.append('schema')

        node = self.create_test_node(
            target=target,
            path=compiled_path,
            config=config,
            fqn=fqn,
            tags=tags,
            name=builder.fqn_name,
            raw_sql=builder.build_raw_sql(),
            column_name=column_name,
            test_metadata=metadata,
        )
        self.render_update(node, config)
        return node
コード例 #11
0
ファイル: test_context.py プロジェクト: chrisp-data/all_data
def test_target_context():
    profile = profile_from_dict(PROFILE_DATA, 'test')
    ctx = target.generate_target_context(profile, {})
    assert_has_keys(REQUIRED_TARGET_KEYS, MAYBE_KEYS, ctx)