77from django .conf import settings
88from binary_database_files import settings as _settings
99
10+
1011def is_fresh (name , content_hash ):
1112 """
1213 Returns true if the file exists on the local filesystem and matches the
@@ -34,19 +35,20 @@ def is_fresh(name, content_hash):
3435 local_content_hash = get_file_hash (fqfn )
3536 return local_content_hash == content_hash
3637
38+
3739def get_hash_fn (name ):
3840 """
3941 Returns the filename for the hash file.
4042 """
4143 fqfn = os .path .join (settings .MEDIA_ROOT , name )
4244 fqfn = os .path .normpath (fqfn )
43- dirs , fn = os .path .split (fqfn )
44- if not os .path .isdir (dirs ):
45- os .makedirs (dirs )
4645 fqfn_parts = os .path .split (fqfn )
46+ if not os .path .isdir (fqfn_parts [0 ]):
47+ os .makedirs (fqfn_parts [0 ])
4748 hash_fn = os .path .join (fqfn_parts [0 ], _settings .DB_FILES_DEFAULT_HASH_FN_TEMPLATE % fqfn_parts [1 ])
4849 return hash_fn
4950
51+
5052def write_file (name , content , overwrite = False ):
5153 """
5254 Writes the given content to the relative filename under the MEDIA_ROOT.
@@ -55,9 +57,9 @@ def write_file(name, content, overwrite=False):
5557 fqfn = os .path .normpath (fqfn )
5658 if os .path .isfile (fqfn ) and not overwrite :
5759 return
58- dirs , fn = os .path .split (fqfn )
59- if not os .path .isdir (dirs ):
60- os .makedirs (dirs )
60+ fqfn_parts = os .path .split (fqfn )
61+ if not os .path .isdir (fqfn_parts [ 0 ] ):
62+ os .makedirs (fqfn_parts [ 0 ] )
6163 open (fqfn , 'wb' ).write (content )
6264
6365 # Cache hash.
@@ -74,14 +76,15 @@ def write_file(name, content, overwrite=False):
7476 uname = getattr (settings , 'DATABASE_FILES_USER' , None )
7577 gname = getattr (settings , 'DATABASE_FILES_GROUP' , None )
7678 if gname :
77- gname = ':' + gname
79+ gname = ':' + gname
7880 if uname :
79- os .system ('chown -RL %s%s "%s"' % (uname , gname , dirs ))
81+ os .system ('chown -RL %s%s "%s"' % (uname , gname , fqfn_parts [ 0 ] ))
8082
8183 # Set permissions.
8284 perms = getattr (settings , 'DATABASE_FILES_PERMS' , None )
8385 if perms :
84- os .system ('chmod -R %s "%s"' % (perms , dirs ))
86+ os .system ('chmod -R %s "%s"' % (perms , fqfn_parts [0 ]))
87+
8588
8689def get_file_hash (fin , force_encoding = None , encoding = None , errors = None , chunk_size = 128 ):
8790 """
@@ -109,6 +112,7 @@ def get_file_hash(fin, force_encoding=None, encoding=None, errors=None, chunk_si
109112 h .update (text )
110113 return h .hexdigest ()
111114
115+
112116def get_text_hash_0004 (text ):
113117 """
114118 Returns the hash of the given text.
@@ -119,6 +123,7 @@ def get_text_hash_0004(text):
119123 h .update (text .encode ('utf-8' , 'replace' ))
120124 return h .hexdigest ()
121125
126+
122127def get_text_hash (text , force_encoding = None , encoding = None , errors = None ):
123128 """
124129 Returns the hash of the given text.
0 commit comments