Java Manifest File Parser
The `java-manifest` library provides functionality to encode and decode Java's `META-INF/MANIFEST.MF` files using Python. It represents a manifest as a list of dictionaries, where each dictionary corresponds to a section within the manifest. The library is currently at version 1.1.0, with its last release in July 2020, indicating a low release cadence.
Common errors
-
The last line of my manifest data is missing or incorrectly parsed after loading.
cause The input manifest string or file does not end with a newline character, which is required by the Java manifest specification for proper parsing of the final entry.fixEnsure that the manifest string or file you are processing or creating explicitly ends with a newline character (`\n`). -
TypeError: ... object is not iterable (or similar type error) when dumping data.
cause You are attempting to dump a manifest where a value in one of the dictionary sections is not a `str` or `bool` type, and no custom `encoder` function has been provided to handle the conversion. The `java-manifest` library expects attribute values to be strings or booleans.fixBefore dumping, convert all non-string/non-boolean values in your manifest data (list of dictionaries) to their string representations, or provide a custom `encoder` function to `java_manifest.dumps()` that handles the specific data types you are using.
Warnings
- gotcha Java manifest files, by specification, must end with a newline character. If the input manifest string or file lacks a trailing newline, the last attribute-value pair might be silently dropped or parsed incorrectly by this library or Java tools.
- gotcha The Java manifest format strictly treats all attribute values as strings. While the library supports `str` and `bool` values in the Python representation, attempting to dump manifests with other structured data types (e.g., lists, dictionaries, integers) for values will fail without custom handling.
Install
-
pip install java-manifest
Imports
- java_manifest
import java_manifest
Quickstart
import java_manifest
manifest_str = """Manifest-Version: 1.0
Created-By: My Build Tool
Application-Name: ExampleApp
Name: com/example/app/MainClass.class
MD5-Digest: AB12CD34EF56GH78IJ90KL12MN34OP56
"""
# Load manifest from a string
manifest_data = java_manifest.loads(manifest_str)
print("Loaded Manifest Data:")
for section in manifest_data:
print(section)
# Add a new attribute to the first section
if manifest_data:
manifest_data[0]["New-Attribute"] = "SomeValue"
# Dump manifest back to a string
updated_manifest_str = java_manifest.dumps(manifest_data)
print("\nUpdated Manifest String:")
print(updated_manifest_str)