Zum Inhalt

create_bags

Overview

The create_bags module packages each processed article into a BagIt archive suitable for archival deposit. The bag contains all article files (metadata, XML, etc.) inherited from the previous module, plus a bag-info.txt file with Dublin Core metadata (title, authors, publisher, license, etc.) and BagIt manifest files. The finished bag is zipped and the unzipped folder is removed.

Module ordering: create_bags should run after check_license_information in the pipeline. It reads the normalized license link from the license.json file produced by that module and uses it to populate the dc.rights field of the bag. If check_license_information did not run first, license.json will be missing and dc.rights will be written as an empty string (with a warning logged).

The module contains two primary Python files:

  1. flow.py
  2. tasks.py

flow.py

This file defines the main flow for bag creation. For each article, it fetches the journal's DDC classification (if not already cached), copies the previous module's contents into a temp directory, and delegates the actual bag creation to tasks.py.

create_bags()

  • Required Parameters:

  • name_of_config_file (str): Name of the configuration file. Default is "config-test_crossrefcrawler.json"

  • test_copy (bool): Flag to indicate if it is a test run. Default is False

  • Context for Use:

  • This function is the main entry point for the bag creation flow. It initializes the module-specific context, iterates over every article of every journal in the module context, and for each article calls tasks.get_ddc, tasks.create_bag_zip, and finally writes a history.csv entry. Articles whose previous module has not been updated since the last run are skipped and counted separately.

  • Example Usage:

  • python create_bags('config-test_crossrefcrawler.json', test_copy=False)

  • Example Output:

  • INFO: Module name: create_bags INFO: Set base path to: /mnt/nas/wdm-test INFO: Module context: [{'ISSN': '2948-2143', 'position': 3, 'previous_module': 'check_license_information', ...}] INFO: Start bag creation for ISSN: 2948-2143. INFO: Journal path: /mnt/nas-tmp/wdm-test/Automatische_Prozessierung/2948-2143--2026-06-03-1780516203 INFO: Article: /mnt/nas-tmp/.../s42467-023-00015-y INFO: latest previous module: /mnt/nas-tmp/.../check_license_information_2025-01-17 INFO: Processed 3 out of 4 articles in journal: /mnt/nas-tmp/.../2948-2143--2026-06-03-1780516203 INFO: Skipped 1 articles in journal: /mnt/nas-tmp/.../2948-2143--2026-06-03-1780516203. These articles have not been updated by the previous module.

tasks.py

This file contains the task definitions used in the bag creation flow. It handles fetching Dewey Decimal Classification (DDC) subjects for the journal, reading the normalized license link from license.json, assembling the Dublin Core metadata dictionary, and producing the final zipped BagIt archive.

get_ddc()

  • Required Parameters:

  • journal_dir (pathlib.Path): Path to the journal directory

  • issn (str): The journal's ISSN

  • Context for Use:

  • This function fetches DDC (Dewey Decimal Classification) subjects for the journal if a DDC file does not already exist. It calls ddc_utils.fetch_ddc(issn) to get the subjects from the external DDC service. If successful, subjects are written line-by-line to a DDC.txt file inside the journal directory, and the ISSN is removed from the missing-DDC tracking file. If unsuccessful or no subjects are returned, the ISSN is added to the missing-DDC file for later manual review.

  • Example Usage:

  • python get_ddc(journal_path, '2948-2143')

  • Example Output:

  • A file at <journal_dir>/DDC.txt containing subject codes, one per line. Log output:

    INFO: DDC information written successfully to DDC.txt file
    
    or (if not found):
    INFO: DDC information not found for journal with ISSN: 2948-2143
    

create_bag_zip()

  • Required Parameters:

  • module_name (str): Name of the current module

  • latest_previous_module (pathlib.Path): Path to the latest previous module directory
  • journal_dir (pathlib.Path): Path to the journal directory

  • Context for Use:

  • This is a thin orchestrator that calls create_bag to produce the BagIt directory, then zip_bag to compress it into a .zip archive, then remove_bag to delete the unzipped folder. Only the zipped archive remains after this function returns.

  • Example Usage:

  • python create_bag_zip('create_bags', latest_previous_module, journal_path)

  • Example Output:

  • A zipped bag at <temp_current_module_dir>/<bag_name>.zip, with the unzipped folder deleted.

create_bag()

  • Required Parameters:

  • module_name (str): Name of the current module

  • latest_previous_module (pathlib.Path): Path to the latest previous module directory
  • journal_dir (pathlib.Path): Path to the journal directory

  • Context for Use:

  • This is the core task of the module. It copies the temp module directory contents into a new bag directory, assembles the Dublin Core metadata dictionary (dc.title, dc.creator, dc.rights, etc.), and calls bagit.make_bag to convert the folder into a BagIt archive with proper manifest and payload files. The dc.rights field is populated from the normalized license.json produced by the upstream check_license_information module, not from the raw CrossRef metadata. None values in the metadata dict are converted to empty strings before being written to bag-info.txt.

  • Example Usage:

  • python create_bag('create_bags', latest_previous_module, journal_path)

  • Example Output:

  • A BagIt-format directory at <temp_current_module_dir>/<bag_name>/ containing:

    • bagit.txt — BagIt version marker
    • bag-info.txt — the Dublin Core metadata
    • manifest-sha256.txt — checksums of payload files
    • tagmanifest-sha256.txt — checksums of tag files
    • data/ — the actual article files

get_documentation_data()

  • Required Parameters:

  • module_name (str): Name of the current module

  • latest_previous_module (pathlib.Path): Path to the latest previous module directory
  • current_datetime (dict): Dictionary containing the current date and timestamp

  • Context for Use:

  • Builds the documentation data dictionary that will be written to history.csv for this article. The Input field lists every file name that ended up inside the bag's data/ directory (except the documentation directory itself), joined by the standard delimiter. The Outputs field is the zipped bag file name.

  • Example Usage:

  • python documentation_data = get_documentation_data('create_bags', latest_previous_module, current_datetime)

  • Example Output:

  • python { 'Date': '2026-06-03', 'Input': 'metadata-article.json;license.json;s42467-023-00015-y.xml', 'Outputs': ['s42467-023-00015-y_bag.zip'] }


The lower-level helper functions in tasks.py are listed below.

  • Required Parameters:

  • temp_current_module_dir (pathlib.Path): Path to the current temp module directory

  • Context for Use:

  • Reads the normalized license link from license.json (produced by the upstream check_license_information module) and returns the value of its licenseLink field. If the file is missing or malformed, an empty string is returned and a warning is logged — this ensures that pipelines lacking check_license_information still complete, just with an empty dc.rights field and a visible warning.

get_bag_dir()

  • Required Parameters:

  • module_name (str): Name of the current module

  • latest_previous_module (pathlib.Path): Path to the latest previous module directory

  • Context for Use:

  • Returns the path where the unzipped BagIt directory will be created. The bag name is derived from utils.get_bag_name.

read_ddc()

  • Required Parameters:

  • journal_dir (pathlib.Path): Path to the journal directory

  • Context for Use:

  • Reads the first line of DDC.txt if it exists in the journal directory and returns it as a string. Returns an empty list when the file is missing. Used by create_bag to populate dc.subject.

zip_bag()

  • Required Parameters:

  • bag_dir_path (pathlib.Path): Path to the unzipped BagIt directory

  • Context for Use:

  • Uses shutil.make_archive to create a .zip archive of the BagIt directory. If an archive with the same name already exists, it is replaced.

remove_bag()

  • Required Parameters:

  • bag_dir_path (pathlib.Path): Path to the unzipped BagIt directory

  • Context for Use:

  • Removes the unzipped BagIt directory using shutil.rmtree. Called after zip_bag so that only the zipped archive remains.

get_file_names_in_archive()

  • Required Parameters:

  • zipped_bag_path (pathlib.Path): Path to the zipped bag archive

  • Context for Use:

  • Introspects the contents of a zipped bag and returns the names of all files inside the data/ directory, excluding files in data/<DOCUMENTATION_DIR>. Used by get_documentation_data to build the Input field of the history.csv entry.