forked from deepindeed2022/pyrecipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
93 lines (76 loc) · 2.57 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import pwd
import stat
import time
import os.path
import logging
def root_dir():
root_dir = os.path.split(os.path.realpath(__file__))[0]
return root_dir
def get_logger(name):
def local_date():
return str(time.strftime("%Y-%m-%d", time.localtime()))
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename=os.path.join(root_dir(), 'log', '%s-%s.log' % (name, local_date())),
filemode='a+')
logger = logging.getLogger(name)
return logger
def elapsetime(func):
'''wapper for elapse time for function
'''
def wapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
end = time.time()
print("execute %s used time:%.2f s" % (func.__name__, (end - start)))
return result
return wapper
def elapsetime(ostream=sys.stdout):
'''wapper for elapse time for function'''
def decorator(func):
def wapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
end = time.time()
print >> ostream, "execute %s used time:%.2f s" % (func.__name__, (end - start))
return result
return wapper
return decorator
def is_readable(path, user):
user_info = pwd.getpwnam(user)
uid = user_info.pw_uid
gid = user_info.pw_gid
s = os.stat(path)
mode = s[stat.ST_MODE]
return (((s[stat.ST_UID] == uid) and (mode & stat.S_IRUSR > 0)) or \
((s[stat.ST_GID] == gid) and (mode & stat.S_IRGRP > 0)) or \
(mode & stat.S_IROTH > 0))
def is_writable(path, user):
user_info = pwd.getpwnam(user)
uid = user_info.pw_uid
gid = user_info.pw_gid
s = os.stat(path)
mode = s[stat.ST_MODE]
return (((s[stat.ST_UID] == uid) and (mode & stat.S_IWUSR > 0)) or \
((s[stat.ST_GID] == gid) and (mode & stat.S_IWGRP > 0)) or \
(mode & stat.S_IWOTH > 0))
def is_executable(path, user):
user_info = pwd.getpwnam(user)
uid = user_info.pw_uid
gid = user_info.pw_gid
s = os.stat(path)
mode = s[stat.ST_MODE]
return (((s[stat.ST_UID] == uid) and (mode & stat.S_IXUSR > 0)) or \
((s[stat.ST_GID] == gid) and (mode & stat.S_IXGRP > 0)) or \
(mode & stat.S_IXOTH > 0))
a = open('log.txt', 'a+')
@elapsetime(a)
def test():
print "hello"
if __name__ == '__main__':
test()