Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bag to destination #178

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 84 additions & 5 deletions bagit.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from datetime import date
from functools import partial
from os.path import abspath, isdir, isfile, join
import shutil

try:
from importlib.metadata import version
Expand Down Expand Up @@ -144,7 +145,7 @@ def find_locale_dir():


def make_bag(
bag_dir, bag_info=None, processes=1, checksums=None, checksum=None, encoding="utf-8"
bag_dir, bag_info=None, processes=1, checksums=None, checksum=None, encoding="utf-8", destination=None, name=None
):
"""
Convert a given directory into a bag. You can pass in arbitrary
Expand All @@ -165,6 +166,42 @@ def make_bag(
checksums = DEFAULT_CHECKSUMS

bag_dir = os.path.abspath(bag_dir)

source_dir = None

# If a destination is provided, copy the directory to the new destination
if destination:
destination = os.path.abspath(destination)

if name != None:
bagdir = name
else:
bagdir = os.path.basename(bag_dir)

if not os.path.exists(destination):
os.makedirs(destination)

# Get the final path where the contents of bag_dir will be copied
destination_subdir = os.path.join(destination, bagdir)


if os.path.isfile(bag_dir):
# If it's a file, copy it directly to the destination
if not os.path.exists(destination_subdir):
os.makedirs(destination_subdir)
destination_file = os.path.join(destination_subdir, os.path.basename(bag_dir))
shutil.copy2(bag_dir, destination_file) # copy2 preserves metadata
else:
# Check if the destination subdirectory already exists
if not os.path.exists(destination_subdir):
# Copy the contents of the source bag_dir to the destination subdirectory
shutil.copytree(bag_dir, destination_subdir)
else:
raise FileExistsError(f"The directory '{destination_subdir}' already exists. Choose a different destination or delete the existing folder.")

source_dir = bag_dir
bag_dir = destination_subdir # Update bag_dir to point to the new location

cwd = os.path.abspath(os.path.curdir)

if cwd.startswith(bag_dir) and cwd != bag_dir:
Expand Down Expand Up @@ -240,9 +277,14 @@ def make_bag(
# original directory
os.chmod("data", os.stat(cwd).st_mode)

total_bytes, total_files = make_manifests(
"data", processes, algorithms=checksums, encoding=encoding
if source_dir != None:
total_bytes, total_files = make_manifests(
source_dir, processes, algorithms=checksums, encoding=encoding
)
else:
total_bytes, total_files = make_manifests(
"data", processes, algorithms=checksums, encoding=encoding
)

LOGGER.info(_("Creating bagit.txt"))
txt = """BagIt-Version: 0.97\nTag-File-Character-Encoding: UTF-8\n"""
Expand Down Expand Up @@ -1249,13 +1291,22 @@ def make_manifests(data_dir, processes, algorithms=DEFAULT_CHECKSUMS, encoding="

manifest_line_generator = partial(generate_manifest_lines, algorithms=algorithms)

# check if data_dir is a directory or a file
if os.path.isfile(data_dir):
# handle solitary file: create a single entry list
file_entries = [data_dir]
is_single_file = True
else:
file_entries = _walk(data_dir)
is_single_file = False

if processes > 1:
pool = multiprocessing.Pool(processes=processes)
checksums = pool.map(manifest_line_generator, _walk(data_dir))
checksums = pool.map(manifest_line_generator, file_entries)
pool.close()
pool.join()
else:
checksums = [manifest_line_generator(i) for i in _walk(data_dir)]
checksums = [manifest_line_generator(i) for i in file_entries]

# At this point we have a list of tuples which start with the algorithm name:
manifest_data = {}
Expand All @@ -1273,6 +1324,16 @@ def make_manifests(data_dir, processes, algorithms=DEFAULT_CHECKSUMS, encoding="

with open_text_file(manifest_filename, "w", encoding=encoding) as manifest:
for digest, filename, byte_count in values:
if is_single_file:

if data_dir != "data":
relative_path = os.path.basename(filename)
filename = os.path.join("data", relative_path)
else:
relative_path = os.path.relpath(filename, data_dir)
if data_dir != "data":
relative_path = os.path.relpath(filename, data_dir)
filename = os.path.join("data", relative_path)
manifest.write("%s %s\n" % (digest, _encode_filename(filename)))
num_files[algorithm] += 1
total_bytes[algorithm] += byte_count
Expand Down Expand Up @@ -1509,6 +1570,21 @@ def _make_parser():
),
)

parser.add_argument(
"--destination",
help=_(
"The directory where the bag will be created (default: the same as the"
" source directory)"
),
)

parser.add_argument(
"--name",
help=_(
"this is what the encolsing directory will be named. If not provided it will use tha name of the source directory"
),
)

checksum_args = parser.add_argument_group(
_("Checksum Algorithms"),
_(
Expand Down Expand Up @@ -1544,6 +1620,7 @@ def _make_parser():
),
)


return parser


Expand Down Expand Up @@ -1610,6 +1687,8 @@ def main():
bag_info=args.bag_info,
processes=args.processes,
checksums=args.checksums,
destination=args.destination,
name=args.name,
)
except Exception as exc:
LOGGER.error(
Expand Down
Binary file added locale/en/LC_MESSAGES/.DS_Store
Binary file not shown.