forked from simoncadman/CUPS-Cloud-Print
/
ccputils.py
130 lines (109 loc) · 4.4 KB
/
ccputils.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
# CUPS Cloudprint - Print via Google Cloud Print
# Copyright (C) 2014 Simon Cadman
#
# 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/>.
import subprocess, os, logging, sys, grp
class Utils:
logpath = '/var/log/cups/cloudprint_log'
def FixFilePermissions(filename):
filePermissions = True
fileOwnerships = True
currentStat = None
if os.path.exists(filename):
currentStat = os.stat(filename)
if currentStat == None or currentStat.st_mode != 0100660:
try:
os.chmod(filename, 0100660)
except:
filePermissions = False
sys.stderr.write("DEBUG: Cannot alter "+ filename +" file permissions\n")
pass
if currentStat == None or currentStat.st_gid != Utils.GetLPID():
try:
os.chown(filename, -1, Utils.GetLPID())
except:
fileOwnerships = False
sys.stderr.write("DEBUG: Cannot alter "+ filename +" file ownership\n")
pass
return filePermissions, fileOwnerships
FixFilePermissions = staticmethod(FixFilePermissions)
def SetupLogging(logpath=None):
returnValue = True
logformat = "%(asctime)s|%(levelname)s|%(message)s"
dateformat = "%Y-%m-%d %H:%M:%S"
if logpath == None:
logpath = Utils.logpath
try:
logging.basicConfig(filename=logpath,level=logging.INFO,format=logformat,datefmt=dateformat)
Utils.FixFilePermissions(logpath)
except:
logging.basicConfig(level=logging.INFO,format=logformat,datefmt=dateformat)
logging.error("Unable to write to log file "+ logpath)
returnValue = False
return returnValue
SetupLogging = staticmethod(SetupLogging)
def fileIsPDF ( filename ) :
"""Check if a file is or isnt a PDF
Args:
filename: string, name of the file to check
Returns:
boolean: True = is a PDF, False = not a PDF.
"""
result = 0
p = subprocess.Popen(["file", filename.lstrip('-')], stdout=subprocess.PIPE)
output = p.communicate()[0]
result = p.returncode
if result != 0:
return False
else:
return "PDF document" in output
fileIsPDF = staticmethod(fileIsPDF)
def is_exe(fpath):
return os.path.exists(fpath) and os.access(fpath, os.X_OK)
is_exe = staticmethod(is_exe)
def which(program):
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if Utils.is_exe(exe_file):
return exe_file
return None
which = staticmethod(which)
def GetLPID(default='lp', alternative='cups', useFiles=True):
blacklistedGroups = [ 'adm', 'wheel', 'root' ]
blacklistedGroupIds = []
for group in blacklistedGroups:
try:
blacklistedGroupIds.append( grp.getgrnam(group).gr_gid )
except:
pass
if useFiles:
# check files in order
for cupsConfigFile in [ '/var/log/cups/access_log', '/etc/cups/ppd', '/usr/local/etc/cups/ppd' ]:
if os.path.exists(cupsConfigFile):
if os.stat(cupsConfigFile).st_gid not in blacklistedGroupIds:
return os.stat(cupsConfigFile).st_gid
# try lp first, then cups
lpgrp = None
try:
lpgrp = grp.getgrnam(default)
except:
try:
lpgrp = grp.getgrnam(alternative)
except:
pass
if lpgrp == None:
return None
else:
return lpgrp.gr_gid
GetLPID = staticmethod(GetLPID)