Esempio n. 1
0
    def run(self, assignment: CompilationAssignment) -> List[Path]:
        """Aggregate contents."""

        log.debug(f"aggregating {self.contents_relative_path} to {self.destination_path}")

        files.replace_directory(self.destination_path)
        copied_paths = []

        # First compile assignment-wide assets
        assignment_contents_path = assignment.path.joinpath(self.contents_relative_path)
        if assignment_contents_path.exists():
            files.copy_directory(assignment_contents_path, self.destination_path)
            copied_paths.append(self.destination_path)

        directory_name = self.directory_name or (lambda p: p.relative_path)

        # Copy per problem, enable filtration
        for problem in filter(self.filter_problems, assignment.problems):
            problem_contents_path = problem.path.joinpath(self.contents_relative_path)
            if problem_contents_path.exists():
                problem_destination_path = self.destination_path.joinpath(directory_name(problem))
                files.copy_directory(problem_contents_path, problem_destination_path, merge=True)
                copied_paths.append(problem_destination_path)

        return copied_paths
Esempio n. 2
0
    def compile_watch(cls, assignment_path: Path, artifacts_path: Path,
                      custom_template_path: Path, options: dict):
        """Compile once and watch for changes."""

        configuration = Configuration(
            assignment_path=assignment_path,
            artifacts_path=artifacts_path,
            custom_template_path=custom_template_path,
            options=options)
        target = CurriculaTarget(configuration)

        observer = Observer()
        observer.schedule(TargetHandler(assignment_path, target),
                          str(assignment_path),
                          recursive=True)
        observer.start()
        log.debug(f"waiting for changes...")

        try:
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            observer.stop()

        observer.join()
Esempio n. 3
0
    def run(self, assignment: CompilationAssignment, context: Context) -> Path:
        """Render the template returning the written path."""

        log.debug(f"building {self.readme_relative_path}/README.md to {self.destination_path}")
        template_path = self.template_relative_path.joinpath(Templates.ASSIGNMENT)
        template = context.environment.get_template(f"template:{template_path.as_posix()}")
        with self.destination_path.open("w") as file:
            file.write(template.render(assignment=assignment))
        return self.destination_path
Esempio n. 4
0
    def compile(self):
        """Rebuild with the src_path as modified."""

        log.debug(
            f"""recompiling for change to {",".join(p.parts[-1] for p in self.paths_modified)}"""
        )

        try:
            self.target.compile(paths_modified=self.paths_modified)
        except CompilationException as exception:
            log.error(exception.message)
        except Exception:
            traceback.print_exc()

        self.paths_modified.clear()
        log.debug(f"waiting for changes...")
Esempio n. 5
0
    def run(self, assignment: CompilationAssignment):
        """Merge directory contents."""

        log.debug(f"merging directories {self.contents_relative_path} to {self.destination_path}")

        files.replace_directory(self.destination_path)

        # First copy any assignment-wide resources
        assignment_contents_path = assignment.path.joinpath(self.contents_relative_path)
        if assignment_contents_path.is_dir():
            files.copy_directory(assignment_contents_path, self.destination_path)

        # Overwrite with problem contents, enable filtration
        for problem in filter(self.filter_problems, assignment.problems):
            problem_contents_path = problem.path.joinpath(self.contents_relative_path)
            if problem_contents_path.is_dir():
                files.copy_directory(problem_contents_path, self.destination_path, merge=True)
    def compile(self, **context_options) -> TargetResult:
        """Generate context and compile."""

        log.info(
            f"building {self.configuration.assignment_path} to {self.configuration.artifacts_path}"
        )

        # Validate first
        validate(self.configuration.assignment_path)

        # Load the assignment object
        log.debug("loading assignment")
        assignment = CompilationAssignment.read(
            self.configuration.assignment_path)

        # Set up templating
        problem_template_paths = {
            f"problem/{problem.short}": problem.path
            for problem in assignment.problems
        }
        environment = jinja2_create_environment(
            default_template_path=root.joinpath("template"),
            assignment_path=self.configuration.assignment_path,
            problem_paths=problem_template_paths,
            custom_template_path=self.configuration.custom_template_path)
        environment.filters.update(get_readme=get_readme,
                                   has_readme=has_readme)
        environment.globals["configuration"] = self.configuration

        # Define context
        log.debug("setting context")
        context = Context(environment=environment, **context_options)

        # Check if an index file was edited
        if context.paths_modified:
            for item in (assignment, *assignment.problems):
                if item.index_path.resolve() in context.paths_modified:
                    context.indices_modified = True
                    break

        # Create output directory
        self.configuration.artifacts_path.mkdir(exist_ok=True, parents=True)

        # Run units
        return super().compile(assignment, context)
Esempio n. 7
0
    def main(cls, parser: argparse.ArgumentParser, options: dict):
        """Format a bunch of reports."""

        grading_path: Path = Path(options["grading"]).absolute()
        if not grading_path.is_dir():
            raise PluginException("grading artifact does not exist!")

        from . import create_format_environment

        assignment = GradingAssignment.read(grading_path)
        custom_template_path = Path(options.get("template") or grading_path.joinpath("report.md"))
        environment = create_format_environment(custom_template_path=custom_template_path)

        if len(options["reports"]) == 1:
            log.debug(f"formatting report with template {custom_template_path}")
            report_path = Path(options.get("reports")[0])
            cls.format_single(assignment, report_path, environment, options)
        else:
            log.debug(f"formatting reports with template {custom_template_path}")
            cls.format_batch(assignment, map(Path, options.get("reports")), environment, options)