예제 #1
0
 def _stripped_search_order(self,
                            template_file,
                            mailing_list=None,
                            language=None):
     # Return the search path order for a given template, possibly using
     # the mailing list and the language as context.  Note that this only
     # returns the search path, and does not check for whether the paths
     # exist or not.
     #
     # Replace the tempdir prefix with a placeholder for more readable and
     # reproducible tests.  Essentially the paths below are rooted at
     # $var_dir, except those files that live within Mailman's source
     # tree.  The former will use /v/ as the root and the latter will use
     # /m/ as the root.
     with ExitStack() as resources:
         in_tree = str(
             resources.enter_context(resource_path('mailman',
                                                   'templates')).parent)
         raw_search_order = search(resources, template_file, mailing_list,
                                   language)
     for path in raw_search_order:
         if path.startswith(self.var_dir):
             path = '/v' + path[len(self.var_dir):]
         elif path.startswith(in_tree):
             path = '/m' + path[len(in_tree):]
         else:
             # This will cause tests to fail, so keep the full bogus
             # pathname for better debugging.
             pass
         yield path
예제 #2
0
def run(
    dryrun: bool = False,
    cores: int = 4,
    keepgoing: bool = False,
    unlock: bool = False,
    printdag: bool = False,
    printfilegraph: bool = False,
    targets=None,
    workdir=None,
):
    # snakemake sets up its own logging, and this cannot be easily changed
    # (setting keep_logger=True crashes), so remove our own log handler
    # for now
    logger.root.handlers = []
    with resource_path('blr', 'Snakefile') as snakefile_path:
        success = snakemake(
            snakefile_path,
            snakemakepath='snakemake',
            dryrun=dryrun,
            cores=cores,
            keepgoing=keepgoing,
            unlock=unlock,
            printshellcmds=True,
            printdag=printdag,
            printfilegraph=printfilegraph,
            targets=targets,
            workdir=workdir,
        )
    if not success:
        raise SnakemakeError()
예제 #3
0
    def validate(self, xsd_path: Path) -> None:
        """
        Validates the contents of a record against a given XSD schema

        The external `xmllint` binary is used to validate records as the `lxml` methods did not easily support relative
        paths for schemas that use imports/includes (which includes the ISO 19115 family).

        Schemas are loaded from a Zip archive to allow the `importlib.path` method to be used (which only supports
        files). This Zip is extracted to a temporary directory managed by Python. The current record object is written
        to the same temporary directory to easily pass to the `xmllint` binary.

        The `xmllint` binary only returns a 0 exit code if the record validates successfully. Therefore, any other exit
        code can be considered a validation failure, and returned as a `RecordValidationError` exception.

        It is assumed this method will be overridden in concrete implementations of this class. Specifically it's
        assumed the `xsd_path` parameter will be hard coded to a schema suitable for the standard each class implements.

        :type xsd_path: Path
        :param xsd_path: Path relative to `bas_metadata_library.schemas.xsd` to the schema to validate against
        """
        with resource_path("bas_metadata_library.schemas.xsd",
                           "xsd-archive.zip") as schemas_archive_path:
            with ZipFile(str(schemas_archive_path)) as schemas_archive:
                with TemporaryDirectory() as schemas_archive_directory_path:
                    schemas_archive.extractall(
                        path=schemas_archive_directory_path)
                    schema_path = Path(
                        schemas_archive_directory_path).joinpath(xsd_path)

                    document_path = Path(
                        schemas_archive_directory_path).joinpath("record.xml")
                    validation_document: MetadataRecord = deepcopy(self)
                    with open(document_path, mode="w") as document_file:
                        document_data = validation_document.generate_xml_document(
                        ).decode()
                        document_file.write(document_data)

                    try:
                        # Exempting Bandit security issue (using subprocess)
                        # Checking for untrusted input is not a concern for this library, rather those implementing
                        # this library should ensure it is used in a way that is secure (i.e. it is context dependent).
                        #
                        # Use `capture_output=True` in future when we can use Python 3.7+
                        subprocess.run(  # nosec
                            args=[
                                "xmllint", "--noout", "--schema",
                                str(schema_path),
                                str(document_path)
                            ],
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            check=True,
                        )
                    except subprocess.CalledProcessError as e:
                        raise RecordValidationError(
                            f"Record validation failed: {e.stderr.decode()}")
예제 #4
0
    def __init__(self, **kwargs: dict):
        super().__init__(**kwargs)

        self.config = kwargs

        with resource_path(
            "bas_metadata_library.schemas.dist", "iec_pas_61174_1_v1.json"
        ) as configuration_schema_file_path:
            with open(configuration_schema_file_path) as configuration_schema_file:
                configuration_schema_data = json.load(configuration_schema_file)
        self.schema = configuration_schema_data
예제 #5
0
    def __init__(self):
        """Initialize index database.

        Will copy the package's version to user's home folder at init,
        so that user doesn't need to edit file in package to add new indices.

        Adding new index URLs to the package's config file pds_indices_db.toml
        is highly encouraged via pull request.
        """
        if not self.fpath.exists():
            with resource_path("planetarypy.pdstools.data", self.fname) as p:
                self.config = self.read_from_file(p)
        else:
            self.config = self.read_from_file()