workspace_bagger: use pushd_popd
@@ -10,7 +10,15 @@ import sys
from pkg_resources import get_distribution
from bagit import Bag, make_manifests # pylint: disable=no-name-in-module
-from ocrd_utils import VERSION, getLogger, abspath, is_local_filename, unzip_file_to_dir
+from ocrd_utils import (
+ pushd_popd,
+ getLogger,
+ abspath,
+ is_local_filename,
+ unzip_file_to_dir,
+
+ VERSION,
+)
from ocrd_validators.constants import BAGIT_TXT, TMP_BAGIT_PREFIX, OCRD_BAGIT_PROFILE_URL
from .workspace import Workspace
@@ -43,9 +51,8 @@ class WorkspaceBagger():
# Remove temporary bagdir
rmtree(bagdir)
- def _log_or_raise(self, msg, oldpwd):
+ def _log_or_raise(self, msg):
if self.strict:
- chdir(oldpwd)
raise(Exception(msg))
else:
log.info(msg)
@@ -54,8 +61,7 @@ class WorkspaceBagger():
mets = workspace.mets
# TODO allow filtering by fileGrp@USE and such
- oldpwd = getcwd()
- chdir(workspace.directory)
+ with pushd_popd(workspace.directory):
for f in mets.find_files():
log.info("Resolving %s (%s)", f.url, ocrd_manifestation_depth)
if is_local_filename(f.url):
@@ -64,10 +70,10 @@ class WorkspaceBagger():
# elif is_local_filename(join(workspace.directory, 'data', f.url)):
# f.url = abspath(join(workspace.directory, 'data', f.url))
elif ocrd_manifestation_depth != 'full':
- self._log_or_raise("Not fetching non-local files, skipping %s" % f.url, oldpwd)
+ self._log_or_raise("Not fetching non-local files, skipping %s" % f.url)
continue
elif not f.url.startswith('http'):
- self._log_or_raise("Not an http URL: %s" % f.url, oldpwd)
+ self._log_or_raise("Not an http URL: %s" % f.url)
continue
log.info("Resolved %s", f.url)
@@ -83,7 +89,6 @@ class WorkspaceBagger():
chdir(bagdir)
total_bytes, total_files = make_manifests('data', processes, algorithms=['sha512'])
- chdir(oldpwd)
return total_bytes, total_files
def _set_bag_info(self, bag, total_bytes, total_files, ocrd_identifier, ocrd_manifestation_depth, ocrd_base_version_checksum):
