예제 #1
0
    def test_provides_prime(self):
        file1_path = os.path.join(self.project.prime_dir, "file1")
        file2_path = os.path.join(self.project.prime_dir, "file2")

        self.part1.mark_prime_done({"file1"}, set(), set())
        self.part2.mark_prime_done({"file2"}, set(), set())

        open(file1_path, "w").close()
        open(file2_path, "w").close()

        self.assertThat(
            inspection.provides(file1_path, self.project, self.parts),
            Equals({self.part1}),
        )
        self.assertThat(
            inspection.provides(file2_path, self.project, self.parts),
            Equals({self.part2}),
        )
예제 #2
0
    def test_provides_dir(self):
        dir_path = os.path.join(self.project.stage_dir, "dir")
        file1_path = os.path.join(dir_path, "file1")
        file2_path = os.path.join(dir_path, "file2")

        self.part1.mark_stage_done({os.path.join("dir", "file1")}, {"dir"})
        self.part2.mark_stage_done({os.path.join("dir", "file2")}, {"dir"})

        for file_path in (file1_path, file2_path):
            os.makedirs(os.path.dirname(file_path), exist_ok=True)
            open(file_path, "w").close()

        self.assertThat(
            inspection.provides(dir_path, self.project, self.parts),
            Equals({self.part1, self.part2}),
        )
예제 #3
0
def inspect(*, provides: str, latest_step: bool, use_json: bool, **kwargs):
    """Inspect the current state of the project.

    By default, this command will print a tabulated summary of the current state of the
    project.
    """
    count = 0
    for option in (provides, latest_step):
        if option:
            count += 1

    if count > 1:
        raise errors.SnapcraftEnvironmentError(
            "Only one of --provides or --latest-step may be supplied")

    # Silence the plugin loader logger
    logging.getLogger(
        "snapcraft.internal.pluginhandler._plugin_loader").setLevel(
            logging.ERROR)
    project = get_project(**kwargs)
    config = project_loader.load_config(project)

    if provides:
        providing_parts = inspection.provides(provides, config.project,
                                              config.all_parts)
        providing_part_names = sorted([p.name for p in providing_parts])
        if use_json:
            print(
                json.dumps(
                    {
                        "path": provides,
                        "parts": providing_part_names
                    },
                    sort_keys=True,
                    indent=4,
                ))
        else:
            echo.info("This path was provided by the following {}:".format(
                formatting_utils.pluralize(providing_part_names, "part",
                                           "parts")))
            echo.info("\n".join(providing_part_names))

    elif latest_step:
        part, step, timestamp = inspection.latest_step(config.all_parts)
        directory = part.working_directory_for_step(step)
        if use_json:
            print(
                json.dumps(
                    {
                        "part": part.name,
                        "step": step.name,
                        "directory": directory,
                        "timestamp": timestamp,
                    },
                    sort_keys=True,
                    indent=4,
                ))
        else:
            echo.info(
                "The latest step that was run is the {!r} step of the {!r} part, "
                "which ran at {}. The working directory for this step is {!r}".
                format(
                    step.name,
                    part.name,
                    datetime.fromtimestamp(timestamp).strftime("%c"),
                    directory,
                ))

    else:
        summary = inspection.lifecycle_status(config)
        if use_json:
            summary_dict = dict()
            for part_summary in summary:
                part_name = part_summary.pop("part")
                summary_dict[part_name] = part_summary
            print(json.dumps(summary_dict, sort_keys=True, indent=4))
        else:
            print(tabulate(summary, headers="keys"))