Skip to content

Missing Warning on Creating Bag of a Bag #186

Description

@theatischbein

When creating a bag of bag I would expect a warning or confirmation if I really want to create a bag of a bag.

In general it should be possible to create a bag of a bag, but I would not consider it a default case.

Reproduction

bag = bagit.make_bag('/tmp/test_bag', {'Contact-Name': 'Thea'})

This creates a bag with all info, manifest files and the data directory

/tmp/test_bag
| - bag-info.txt
| - bagit.txt
| - data
| - manifest-sha256.txt
| - manifest-sha512.txt
| - tagmanifest-sha256.txt
| - tagmanifest-sha512.txt

If I run make_bag on the same folder again

bag_again = bagit.make_bag('/tmp/test_bag', {'Contact-Name': 'Thea})

this creates a bag with all info, manifest files and the data directory of the first level bag

/tmp/test_bag
| - bag-info.txt
| - bagit.txt
| - data
    | - bag-info.txt
    | - bagit.txt
    | - data
    | - manifest-sha256.txt
    | - manifest-sha512.txt
    | - tagmanifest-sha256.txt
    | - tagmanifest-sha512.txt
| - manifest-sha256.txt
| - manifest-sha512.txt
| - tagmanifest-sha256.txt
| - tagmanifest-sha512.txt

Expection

I would expect a warning or even a confirmation, e.g.

>>> bag = bagit.make_bag('/tmp/test_bag', {'Contact-Name': 'Thea'})

>>> bag_again = bagit.make_bag('/tmp/test_bag', {'Contact-Name': 'Thea'})
WARNING: You are creating a bag of a bag. Do you want to continue? [N/y] 

Realization

To achieve this behaviour we would need to detect, if a folder is already a bag.
One way could be to use the class constructor of Bag and catch the case for valid bag, but continue on Manifest not found.

Example for the make_bag function:

https://github.com/LibraryOfCongress/bagit-python/blob/master/bagit.py#L141C1-L143C3

def make_bag(
    bag_dir, bag_info=None, processes=1, checksums=None, checksum=None, encoding="utf-8"
):

    # ....
    try:
        bagit.Bag(bag_dir)
        while True:
            ans = input("WARNING: You are creating a bag of a bag. Do you want to continue? [N/y]")
            if ans.lower() == 'y':
                break
            elif not ans or ans.lower() == 'n':
                raise UserWarning('Aborted')
    
    except Exception as e:
        pass

    # ...

Activity

  1. kieranjol commented on May 31, 2025

    @kieranjol
    Contributor
  2. theatischbein commented on May 31, 2025

    @theatischbein
    Author

    I really like the override flag!

    If wanted I could also created a PR, but wanted to discuss it here first.

  3. theatischbein commented on May 31, 2025

    @theatischbein
    Author

    A second thought:
    Maybe it is bad to rely on user input. Especially for integration into automated or parallel scripts.

    What do you think of

    • raising a bagit Error "Directory is already a bag"
    • providing a flag for allowing the creating of a bag of bag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions