1
0
mirror of https://github.com/gryf/slack-backup.git synced 2025-12-17 11:30:25 +01:00
Files
slack-backup/slack_backup/utils.py
gryf db8527e9af Added implementation for url_file_attachment option.
Using url_file_attachment user can specify if he like to change objects
like 'file_share' marked as external (in Slack servers point of view)
to attachments, so the only value would be remembered URL for the
"uploaded" document as an attachment. Or, treat it as is, and let the
slack-backup to produce file which contain the URLs and corresponding
local file names for such files.
2018-07-16 15:20:45 +02:00

58 lines
1.6 KiB
Python

"""
Some utils functions. Jsut to not copypaste the code around
"""
from datetime import datetime
import errno
import os
import logging
import tempfile
import hashlib
def makedirs(path):
"""Create if not exists - version of os.makedirs."""
try:
os.makedirs(path)
except OSError as err:
if err.errno != errno.EEXIST:
logging.error("Cannot create `%s'.", path)
raise
else:
if os.path.exists(path) and not os.path.isdir(path):
logging.error("Cannot create `%s'. There is some file on the "
"way; cannot proceed.", path)
raise
def get_temp_name(suffix='', prefix='tmp', dir=None, unlink=False):
"""Return temporary file name"""
fdesc, fname = tempfile.mkstemp(suffix=suffix, prefix=prefix, dir=dir)
os.close(fdesc)
if unlink:
os.unlink(fname)
return fname
def same_files(file1, file2):
"""
Compare files by calculating hash for each of them. Return True if hash is
identical, False otherwise
"""
with open(file1, 'rb') as fobj:
hash1 = hashlib.sha256(fobj.read())
with open(file2, 'rb') as fobj:
hash2 = hashlib.sha256(fobj.read())
return hash1.hexdigest() == hash2.hexdigest()
def fromtimestamp(timestamp):
"""
Return datetime object from provided timestamp. If timestamp argument is
falsy, datetime object placed in January 1970 will be retuned.
"""
if not timestamp:
return datetime.utcfromtimestamp(0)
return datetime.fromtimestamp(timestamp)