Skip to content

Commit

Permalink
Add get_dir_hash method. #10
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiocaccamo committed Dec 30, 2022
1 parent d343f91 commit 0ffc4a5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -66,6 +66,7 @@ import fsutil
- [`extract_zip_file`](#extract_zip_file)
- [`get_dir_creation_date`](#get_dir_creation_date)
- [`get_dir_creation_date_formatted`](#get_dir_creation_date_formatted)
- [`get_dir_hash`](#get_dir_hash)
- [`get_dir_last_modified_date`](#get_dir_last_modified_date)
- [`get_dir_last_modified_date_formatted`](#get_dir_last_modified_date_formatted)
- [`get_dir_size`](#get_dir_size)
Expand Down Expand Up @@ -311,6 +312,14 @@ date = fsutil.get_dir_creation_date(path)
date_str = fsutil.get_dir_creation_date_formatted(path, format='%Y-%m-%d %H:%M:%S')
```

#### `get_dir_hash`

```python
# Get the hash of the directory at the given path using
# the specified algorithm function (md5 by default).
hash = fsutil.get_dir_hash(path)
```

#### `get_dir_last_modified_date`

```python
Expand Down
18 changes: 17 additions & 1 deletion fsutil/__init__.py
Expand Up @@ -435,6 +435,22 @@ def get_dir_creation_date_formatted(path, format="%Y-%m-%d %H:%M:%S"):
return date.strftime(format)


def get_dir_hash(path, func="md5"):
"""
Get the hash of the directory at the given path using
the specified algorithm function (md5 by default).
"""
assert_dir(path)
hash = hashlib.new(func)
files = search_files(path)
for file in sorted(files):
file_hash = get_file_hash(file, func=func)
file_hash_b = bytes(file_hash, "utf-8")
hash.update(file_hash_b)
hash_hex = hash.hexdigest()
return hash_hex


def get_dir_last_modified_date(path):
"""
Get the directory last modification date.
Expand Down Expand Up @@ -523,7 +539,7 @@ def get_file_extension(path):

def get_file_hash(path, func="md5"):
"""
Get the hash of the file at the gived path using
Get the hash of the file at the given path using
the specified algorithm function (md5 by default).
"""
assert_file(path)
Expand Down
16 changes: 16 additions & 0 deletions tests/test.py
Expand Up @@ -490,6 +490,22 @@ def test_get_dir_creation_date_formatted(self):
creation_date_re = re.compile(r"^[\d]{4}\/[\d]{2}\/[\d]{2}$")
self.assertTrue(creation_date_re.match(creation_date_str))

def test_get_dir_hash(self):
f1_path = self.temp_path("x/a/b/f1.txt")
f2_path = self.temp_path("x/a/b/f2.txt")
f3_path = self.temp_path("x/j/k/f3.txt")
f4_path = self.temp_path("x/j/k/f4.txt")
f5_path = self.temp_path("x/y/z/f5.txt")
f6_path = self.temp_path("x/y/z/f6.txt")
fsutil.create_file(f1_path, content="hello world 1")
fsutil.create_file(f2_path, content="hello world 2")
fsutil.create_file(f3_path, content="hello world 3")
fsutil.create_file(f4_path, content="hello world 4")
fsutil.create_file(f5_path, content="hello world 5")
fsutil.create_file(f6_path, content="hello world 6")
hash = fsutil.get_dir_hash(self.temp_path("x/"))
self.assertEqual(hash, "eabe619c41f0c4611b7b9746bededfcb")

def test_get_dir_last_modified_date(self):
path = self.temp_path("a/b/c.txt")
fsutil.create_file(path, content="Hello")
Expand Down

0 comments on commit 0ffc4a5

Please sign in to comment.