-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
68 lines (48 loc) · 1.77 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- coding: utf-8 -*-
#
# hashtool - hashing of entire trees of files
# Copyright (C) 2015 - Felipe Machado
#
#
# Python future imports
from __future__ import print_function
from __future__ import unicode_literals
# Dependencies
from builtins import str
# Python standard library
import json
import gzip
import re
import os.path
###############################################################################
def loadHashDBFile( filename ):
if filename[-3:] == ".gz":
f = gzip.open( filename, 'rb' )
else:
f = open( filename, 'rb' )
return json.loads( str( f.read(), "utf-8" ) )
###############################################################################
###############################################################################
data_size_suffixes = [ "Bytes", "KB", "MB", "GB", "TB" ]
def formatDataSize( size_in_bytes ):
size_in_bytes = float( size_in_bytes )
count = 0
while size_in_bytes > 1000 and count < len( data_size_suffixes ) - 1:
size_in_bytes /= 1000
count += 1
return str( size_in_bytes ) + " " + data_size_suffixes[ count ]
###############################################################################
###############################################################################
ignored_files = [ ".DS_Store", "._.DS_Store" ]
ignore_regexes = [ "\.\_.+" ]
def shouldIgnore( file_path, prefix ):
if prefix is not None and not file_path.startswith( prefix ):
return True
for name in ignored_files:
if os.path.basename( file_path ) == name:
return True
for name in ignore_regexes:
if re.match( name, os.path.basename( file_path ) ):
return True
return False
###############################################################################