utils.py 2.57 KB
Newer Older
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Copyright (C) 2015 GRNET S.A.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

16
17
18
import os
import hashlib
import datetime
19
import threading
20
import watchdog.utils
21

Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
22
23
from agkyra.syncer.common import OBJECT_DIRSEP

24

Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
25
26
27
28
29
30
31
32
def to_local_sep(filename):
    return filename.replace(OBJECT_DIRSEP, os.path.sep)


def to_standard_sep(filename):
    return filename.replace(os.path.sep, OBJECT_DIRSEP)


33
def join_path(dirpath, filename):
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
34
35
36
37
38
39
40
    return os.path.join(dirpath, to_local_sep(filename))


def join_objname(prefix, filename):
    if prefix != "":
        prefix = prefix.rstrip(OBJECT_DIRSEP) + OBJECT_DIRSEP
    return prefix + filename
41
42


Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
43
44
45
46
47
48
49
50
def normalize_standard_suffix(path):
    return path.rstrip(OBJECT_DIRSEP) + OBJECT_DIRSEP


def normalize_local_suffix(path):
    return path.rstrip(os.path.sep) + os.path.sep


51
52
53
54
55
56
57
58
59
60
61
62
63
def hash_string(s):
    return hashlib.sha256(s).hexdigest()


def time_stamp():
    return datetime.datetime.now().strftime("%s.%f")


def younger_than(tstamp, seconds):
    now = datetime.datetime.now()
    ts = datetime.datetime.fromtimestamp(int(float(tstamp)))
    delta = now - ts
    return delta < datetime.timedelta(seconds=seconds)
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84


BaseStoppableThread = watchdog.utils.BaseThread


class StoppableThread(BaseStoppableThread):
    def run_body(self):
        raise NotImplementedError()

    def run(self):
        while True:
            if not self.should_keep_running():
                return
            self.run_body()


def start_daemon(threadClass):
    thread = threadClass()
    thread.daemon = True
    thread.start()
    return thread
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102


class ThreadSafeDict(object):
    def __init__(self, *args, **kwargs):
        self._DICT = {}
        self._LOCK = threading.Lock()

    def lock(self):
        class Lock(object):
            def __enter__(this):
                self._LOCK.acquire()
                return self._DICT

            def __exit__(this, exctype, value, traceback):
                self._LOCK.release()
                if value is not None:
                    raise value
        return Lock()