LibraryOfCongress / bagit-python

Work with BagIt packages from Python.
http://libraryofcongress.github.io/bagit-python
218 stars 85 forks source link

Error when bag is created while working directory is inside dir to be bagged. #101

Closed nkrabben closed 6 years ago

nkrabben commented 6 years ago

If a user is bagging a nested directory (bagit.py ~/Desktop/bag_level1), and their current working directory is inside that directory (e.g. cwd = ~/Desktop/bag_level1/bag_level2), bagit.py fails with the following error.

...
line 233, in make_bag
    os.chdir(old_dir)
FileNotFoundError: [Errno 2] 
...

This happens because the current working directory is saved to old_dir, but then bag_level2 is moved to data/bag_level2

I have a couple of ideas on possible behavior.

  1. error out and warn the user to change directories before bagging
  2. fail to another location, e.g
    if not os.path.exists(old_dir):
    os.chdir(self.path)
    log('Working directory now here, because previous one doesn't exists)
acdha commented 6 years ago

My first inclination is a strong preference for the first option since it seems rare that this is actually something you'd intend to do and it can be very confusing if moving the current directory succeeds (i.e. on POSIX systems) or when it fails (i.e. Windows).

acdha commented 6 years ago

I just added a check for this but had a question which might be good for @johnscancella to answer re:bagit-java as well — do we consider it an error to attempt to bag the current directory? (i.e. should bagit.py . fail?)

johnscancella commented 6 years ago

I think bagit.py . seems perfectly reasonable. But than again I am not the average user. The old bagit-java CLI creates the bag and doesn't move you which is super confusing. But since this is the first mention of this I don't know how often this happens in practice.

acdha commented 6 years ago

Agreed. I want to preserve that because when Bagit 2 arrives bagit.py . is perfectly safe since it'd only be creating a .bagit directory without moving anything.