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

self.path shouldn't be None #167

Open
wants to merge 3 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
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
*.pyc
*.py[cod]
bench-data
build
dist
MANIFEST
bagit.egg-info
.idea
.eggs/
.tox/
*~
*.mo
test.log
29 changes: 12 additions & 17 deletions bagit.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from collections import defaultdict
from datetime import date
from functools import partial
from os.path import abspath, isdir, isfile, join

from pkg_resources import DistributionNotFound, get_distribution

Expand Down Expand Up @@ -280,7 +279,7 @@ class Bag(object):
valid_files = ["bagit.txt", "fetch.txt"]
valid_directories = ["data"]

def __init__(self, path=None):
def __init__(self, path):
super(Bag, self).__init__()
self.tags = {}
self.info = {}
Expand All @@ -302,12 +301,8 @@ def __init__(self, path=None):

self.algorithms = []
self.tag_file_name = None
self.path = abspath(path)
if path:
# if path ends in a path separator, strip it off
if path[-1] == os.sep:
self.path = path[:-1]
self._open()
self.path = os.path.abspath(path)
self._open()

def __str__(self):
# FIXME: develop a more informative string representation for a Bag
Expand All @@ -331,7 +326,7 @@ def _open(self):
# the required version and encoding.
bagit_file_path = os.path.join(self.path, "bagit.txt")

if not isfile(bagit_file_path):
if not os.path.isfile(bagit_file_path):
raise BagError(_("Expected bagit.txt does not exist: %s") % bagit_file_path)

self.tags = tags = _load_tag_file(bagit_file_path)
Expand Down Expand Up @@ -380,13 +375,13 @@ def _open(self):
def manifest_files(self):
for filename in ["manifest-%s.txt" % a for a in CHECKSUM_ALGOS]:
f = os.path.join(self.path, filename)
if isfile(f):
if os.path.isfile(f):
yield f

def tagmanifest_files(self):
for filename in ["tagmanifest-%s.txt" % a for a in CHECKSUM_ALGOS]:
f = os.path.join(self.path, filename)
if isfile(f):
if os.path.isfile(f):
yield f

def compare_manifests_with_fs(self):
Expand Down Expand Up @@ -560,7 +555,7 @@ def fetch_entries(self):

fetch_file_path = os.path.join(self.path, "fetch.txt")

if isfile(fetch_file_path):
if os.path.isfile(fetch_file_path):
with open_text_file(
fetch_file_path, "r", encoding=self.encoding
) as fetch_file:
Expand Down Expand Up @@ -744,7 +739,7 @@ def _validate_structure(self):
def _validate_structure_payload_directory(self):
data_dir_path = os.path.join(self.path, "data")

if not isdir(data_dir_path):
if not os.path.isdir(data_dir_path):
raise BagValidationError(
_("Expected data directory %s does not exist") % data_dir_path
)
Expand Down Expand Up @@ -1291,14 +1286,14 @@ def make_manifests(data_dir, processes, algorithms=DEFAULT_CHECKSUMS, encoding="


def _make_tagmanifest_file(alg, bag_dir, encoding="utf-8"):
tagmanifest_file = join(bag_dir, "tagmanifest-%s.txt" % alg)
tagmanifest_file = os.path.join(bag_dir, "tagmanifest-%s.txt" % alg)
LOGGER.info(_("Creating %s"), tagmanifest_file)

checksums = []
for f in _find_tag_files(bag_dir):
if re.match(r"^tagmanifest-.+\.txt$", f):
continue
with open(join(bag_dir, f), "rb") as fh:
with open(os.path.join(bag_dir, f), "rb") as fh:
m = hashlib.new(alg)
while True:
block = fh.read(HASH_BLOCK_SIZE)
Expand All @@ -1308,7 +1303,7 @@ def _make_tagmanifest_file(alg, bag_dir, encoding="utf-8"):
checksums.append((m.hexdigest(), f))

with open_text_file(
join(bag_dir, tagmanifest_file), mode="w", encoding=encoding
os.path.join(bag_dir, tagmanifest_file), mode="w", encoding=encoding
) as tagmanifest:
for digest, filename in checksums:
tagmanifest.write("%s %s\n" % (digest, filename))
Expand All @@ -1324,7 +1319,7 @@ def _find_tag_files(bag_dir):
if filename.startswith("tagmanifest-"):
continue
# remove everything up to the bag_dir directory
p = join(dir_name, filename)
p = os.path.join(dir_name, filename)
yield os.path.relpath(p, bag_dir)


Expand Down