Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

swh:1:snp:02443124ed4ee0d8d724fefd38bf9b271361cc09
  • Code
  • Branches (3)
  • Releases (7)
    • Branches
    • Releases
    • HEAD
    • refs/heads/dev-mike
    • refs/heads/main
    • refs/tags/v2023.1
    • v2023.11
    • v2023.10
    • v2023.9
    • v2023.6
    • v2023.5
    • v2023.4
    • v2023.3
  • c2f3999
  • /
  • src
  • /
  • chemfeat
  • /
  • modules.py
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
  • revision
  • snapshot
  • release
content badge
swh:1:cnt:313b3e6c960cfacbbeb82eb2c35298ed56e87665
directory badge
swh:1:dir:7682504f4e2d513f5c7c3cad9f2a9b1197d4ad4c
revision badge
swh:1:rev:dd1769c283eb3b72bd00f13d91b42d5540212a7d
snapshot badge
swh:1:snp:02443124ed4ee0d8d724fefd38bf9b271361cc09
release badge
swh:1:rel:7785c16f6df373e238eccd76ee97a9a40860e2d5

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
  • revision
  • snapshot
  • release
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
Tip revision: dd1769c283eb3b72bd00f13d91b42d5540212a7d authored by Jan-Michael Rye on 25 September 2023, 12:12:20 UTC
Pad fingerprint indices with zeros
Tip revision: dd1769c
modules.py
#!/usr/bin/env python3
'''
Functions for dynamically importing modules.
'''

import logging
import pathlib
import pkgutil


LOGGER = logging.getLogger(__name__)


def import_modules(paths, path_log_msg=None):
    '''
    Import modules by paths.

    Args:
        paths:
            An iterable of file and directory paths. File paths will import
            single modules while directory paths will recursively import all
            modules contained in the directory.

        path_log_msg:
            An optional logging format string for logging messages about the
            given paths. If given, it should contain a single "%s" placeholder
            for the path argument.

    Returns:
        The set of loaded module names.
    '''
    paths = set(pathlib.Path(path).resolve() for path in paths)
    if not paths:
        LOGGER.warning('No paths passed to import_modules().')

    if path_log_msg:
        for path in sorted(paths):
            LOGGER.debug(path_log_msg, path)

    file_paths = set()
    dir_paths = set()
    for path in paths:
        if path.is_dir():
            dir_paths.add(path)
        else:
            file_paths.add(path)

    all_dir_paths = sorted(dir_paths | set(path.parent for path in file_paths))

    loaded = set()
    for loader, name, _is_pkg in pkgutil.walk_packages(path=all_dir_paths):
        path = pathlib.Path(loader.path) / f'{name}.py'
        if path in file_paths or any(path.is_relative_to(p) for p in dir_paths):
            LOGGER.debug('Loading module %s from %s', name, path)
            loader.find_module(name).load_module(name)
            loaded.add(name)

    return loaded

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API