-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtimekprcommon.py
169 lines (143 loc) · 4.19 KB
/
timekprcommon.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python
""" Common variables and definitions for timekpr.
Copyright / License: See COPYRIGHT.txt
"""
try:
# python3
import configparser
except ImportError:
# python2.x
import ConfigParser as configparser
from os.path import isfile, getmtime
from os import geteuid
from time import strftime, localtime
from timekprpam import *
def getversion():
return '0.3.5'
def checkifadmin():
if geteuid() != 0:
exit('Error: You need to have administrative privileges to run timekpr')
def getvariables(DEVACTIVE):
#Read timekpr.conf
fconf = '/etc/timekpr.conf'
if DEVACTIVE:
fconf = './etc/timekpr.conf'
if not isfile(fconf):
exit('Error: Could not find configuration file %s' % fconf)
conf = configparser.ConfigParser()
try:
conf.read(fconf)
except configparser.ParsingError:
exit('Error: Could not parse the configuration file properly %s' % fconf)
#Creating a dictionary file
var = dict()
#VARIABLES
#VERSION GRACEPERIOD POLLTIME DEBUGME LOCKLASTS LOGFILE TIMEKPRDIR TIMEKPRWORK TIMEKPRSHARED
#Exits or sets default if not found
try:
var['VERSION'] = conf.get("general", "version")
except configparser.NoOptionError:
exit('Error: Could not detect variable version in configuration file %s' % fconf)
if var['VERSION'] < '0.2.0':
exit('Error: You have an old /etc/timekpr.conf - remove and reinstall timekpr')
try:
var['GRACEPERIOD'] = int(conf.get("variables", "graceperiod"))
except configparser.NoOptionError:
var['GRACEPERIOD'] = 120
try:
var['POLLTIME'] = int(conf.get("variables", "polltime"))
except configparser.NoOptionError:
var['POLLTIME'] = 45
try:
var['LOCKLASTS'] = conf.get("variables", "locklasts")
except configparser.NoOptionError:
var['LOCKLASTS'] = '1 hour'
try:
var['DEBUGME'] = conf.get("variables", "debugme")
except configparser.NoOptionError:
var['DEBUGME'] = 'True'
try:
var['LOGFILE'] = conf.get("directories", "logfile")
except configparser.NoOptionError:
var['LOGFILE'] = '/var/log/timekpr.log'
try:
var['TIMEKPRDIR'] = conf.get("directories", "timekprdir")
except configparser.NoOptionError:
var['TIMEKPRDIR'] = '/etc/timekpr'
try:
var['TIMEKPRWORK'] = conf.get("directories", "timekprwork")
except configparser.NoOptionError:
var['TIMEKPRWORK'] = '/var/lib/timekpr'
try:
var['TIMEKPRSHARED'] = conf.get("directories", "timekprshared")
except configparser.NoOptionError:
var['TIMEKPRSHARED'] = '/usr/share/timekpr'
if DEVACTIVE:
var['TIMEKPRSHARED'] = './gui'
return var
def getcmdoutput(cmd):
#TODO: timekpr-gui.py: Use it for "/etc/init.d/timekpr status" and a button enable/disable
from os import popen
#Execute a command, returns its output
out = popen(cmd)
return out.read()
def fromtoday(fname):
# Returns True if a file was last modified today
fdate = strftime("%Y%m%d", localtime(getmtime(fname)))
today = strftime("%Y%m%d")
return fdate == today
def islate(bto, allowfile):
# Get current day index and hour of day
index = int(strftime("%w"))
hour = int(strftime("%H"))
if (hour > bto[index]):
if isfile(allowfile):
if not fromtoday(allowfile):
return True
else:
return False
else:
return True
else:
return False
def ispasttime(limits, time):
index = int(strftime("%w"))
if (time > limits[index]):
return True
else:
return False
def isearly(bfrom, allowfile):
# Get current day index and hour of day
index = int(strftime("%w"))
hour = int(strftime("%H"))
if (hour < bfrom[index]):
if isfile(allowfile):
if not fromtoday(allowfile):
return True
else:
return False
else:
return True
else:
return False
def isrestricteduser(username, limit):
if not isuserlimited(username) and limit == 86400:
return False
else:
return True
def readusersettings(user, conffile):
#Returns limits and from/to allowed hours
if isfile(conffile):
fhandle = open(conffile)
limits = fhandle.readline() #Read 1st line
limits = re.compile('(\d+)').findall(limits)
lims = list(map(int, limits))
else:
lims = [ 86400, 86400, 86400, 86400, 86400, 86400, 86400 ]
bfromandto = getuserlimits(user)
bfromtemp = bfromandto[0]
#Using map instead of for i in ...
bfrom = list(map(int, bfromtemp))
btotemp = bfromandto[1]
bto = list(map(int, btotemp))
return lims, bfrom, bto