Skip to content
Snippets Groups Projects
Verified Commit 1f98c67f authored by Antoine R. Dumont's avatar Antoine R. Dumont
Browse files

Add optional clean up round-trip to remove empty folders

parent ca235a08
No related branches found
No related tags found
No related merge requests found
......@@ -188,7 +188,8 @@ def compute_tree_metadata(dirname, ls_hashes):
def walk_and_compute_sha1_from_directory(rootdir,
dir_ok_fn=lambda dirpath: True,
with_root_tree=True):
with_root_tree=True,
remove_empty_folder=False):
"""Compute git sha1 from directory rootdir.
Args:
......@@ -232,6 +233,17 @@ def walk_and_compute_sha1_from_directory(rootdir,
dirname)),
dirnames))
if remove_empty_folder: # round-trip to remove empty folders
gen_dir = ((dp, filtfn(dp, dns), fns) for (dp, dns, fns)
in os.walk(rootdir, topdown=False)
if dir_ok_fn(dp))
for dirpath, dirnames, filenames in gen_dir:
if dirnames == [] and filenames == []:
if os.path.islink(dirpath):
os.remove(dirpath)
else:
os.removedirs(dirpath)
gen_dir = ((dp, filtfn(dp, dns), fns) for (dp, dns, fns)
in os.walk(rootdir, topdown=False)
if dir_ok_fn(dp))
......@@ -429,7 +441,8 @@ def __remove_paths_from_objects(objects, rootpaths,
def update_checksums_from(changed_paths, objects,
dir_ok_fn=lambda dirpath: True):
dir_ok_fn=lambda dirpath: True,
remove_empty_folder=False):
"""Given a list of changed paths, recompute the checksums only where
needed.
......@@ -459,7 +472,10 @@ def update_checksums_from(changed_paths, objects,
parent = os.path.dirname(path)
if parent == root: # ... recompute everything anyway
return walk_and_compute_sha1_from_directory(root, dir_ok_fn)
return walk_and_compute_sha1_from_directory(
root,
dir_ok_fn=dir_ok_fn,
remove_empty_folder=remove_empty_folder)
if changed_path['action'] == 'D': # (D)elete
paths_to_remove.add(path)
......@@ -484,11 +500,16 @@ def update_checksums_from(changed_paths, objects,
# rootdir no longer exists, recompute all
# folder could have been previously ignored
# (e.g. in svn case with ignore flag activated)
return walk_and_compute_sha1_from_directory(root,
dir_ok_fn)
hashes = walk_and_compute_sha1_from_directory(rootdir, dir_ok_fn,
with_root_tree=False)
return walk_and_compute_sha1_from_directory(
root,
dir_ok_fn=dir_ok_fn,
remove_empty_folder=remove_empty_folder)
hashes = walk_and_compute_sha1_from_directory(
rootdir,
dir_ok_fn=dir_ok_fn,
with_root_tree=False,
remove_empty_folder=remove_empty_folder)
# Then update the original objects with new
# checksums for the arborescence tree below rootdir
......
......@@ -146,6 +146,7 @@ class GitHashWalkArborescenceTree(unittest.TestCase):
"""
def setUp(self):
self.tmp_root_path = tempfile.mkdtemp().encode('utf-8')
self.maxDiff = None
start_path = os.path.dirname(__file__).encode('utf-8')
pkg_doc_linux_r11 = os.path.join(start_path,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment