Skip to content
Permalink
Browse files

Moved common functions into utils class

  • Loading branch information
simoncadman committed Feb 13, 2014
1 parent 30b4992 commit 6d9d5da45564b6bacac4165a5204934f469bb2d8
Showing with 75 additions and 52 deletions.
  1. +5 −38 backend.py
  2. +56 −0 ccputils.py
  3. +1 −1 deleteaccount.py
  4. +1 −1 dynamicppd.py
  5. +1 −1 listcloudprinters.py
  6. +1 −1 listdrivefiles.py
  7. +1 −1 reportissues.py
  8. +1 −1 setupcloudprint.py
  9. +5 −5 test_backend.py → test_ccputils.py
  10. +3 −3 upgrade.py
@@ -15,48 +15,15 @@
# 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 sys, os, subprocess, mimetypes, logging

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], stdout=subprocess.PIPE)
output = p.communicate()[0]
result = p.returncode
if result != 0:
return False
else:
return "PDF document" in output

def which(program):
import os
def is_exe(fpath):
return os.path.exists(fpath) and os.access(fpath, os.X_OK)

fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None

if __name__ == '__main__': # pragma: no cover
import sys, os, subprocess, mimetypes, logging
from ccputils import Utils

progname = 'cloudprint'

if len(sys.argv) == 2 and sys.argv[1] == 'version':
# line below is replaced on commit
CCPVersion = "20140213 231214"
CCPVersion = "20140213 232212"
print "CUPS Cloud Print CUPS Backend Version " + CCPVersion
sys.exit(0)

@@ -149,11 +116,11 @@ def is_exe(fpath):
pdfFile = printFile+".pdf"
ps2PdfName = "ps2pdf"
convertToPDFParams = [ps2PdfName, "-dPDFSETTINGS=/printer", printFile, pdfFile]
if which(ps2PdfName) == None:
if Utils.which(ps2PdfName) == None:
ps2PdfName = "pstopdf"
convertToPDFParams = [ps2PdfName, printFile, pdfFile]

if not fileIsPDF( printFile ):
if not Utils.fileIsPDF( printFile ):
sys.stderr.write( "INFO: Converting print job to PDF\n")
subprocess.call(convertToPDFParams)
logging.info("Converted to PDF as "+ pdfFile)
@@ -0,0 +1,56 @@
# 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

class Utils:

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], 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 which(program):
import os
def is_exe(fpath):
return os.path.exists(fpath) and os.access(fpath, os.X_OK)

fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None

which = staticmethod(which)
@@ -22,7 +22,7 @@

if len(sys.argv) == 2 and sys.argv[1] == 'version':
# line below is replaced on commit
CCPVersion = "20140213 231214"
CCPVersion = "20140213 232212"
print "CUPS Cloud Print Delete Account Script Version " + CCPVersion
sys.exit(0)

@@ -20,7 +20,7 @@

if len(sys.argv) == 2 and sys.argv[1] == 'version':
# line below is replaced on commit
CCPVersion = "20140213 231214"
CCPVersion = "20140213 232212"
print "CUPS Cloud Print Dynamic PPD Generator Version " + CCPVersion
sys.exit(0)

@@ -30,7 +30,7 @@

if len(sys.argv) == 2 and sys.argv[1] == 'version':
# line below is replaced on commit
CCPVersion = "20140213 231214"
CCPVersion = "20140213 232212"
print "CUPS Cloud Print Printer Lister Version " + CCPVersion
sys.exit(0)

@@ -22,7 +22,7 @@

if len(sys.argv) == 2 and sys.argv[1] == 'version':
# line below is replaced on commit
CCPVersion = "20140213 231214"
CCPVersion = "20140213 232212"
print "CUPS Cloud Print Printer Drive Lister Version " + CCPVersion
sys.exit(0)

@@ -21,7 +21,7 @@

if len(sys.argv) == 2 and sys.argv[1] == 'version':
# line below is replaced on commit
CCPVersion = "20140213 231214"
CCPVersion = "20140213 232212"
print "CUPS Cloud Print Issue Reporting Script Version " + CCPVersion
sys.exit(0)

@@ -22,7 +22,7 @@

if len(sys.argv) == 2 and sys.argv[1] == 'version':
# line below is replaced on commit
CCPVersion = "20140213 231214"
CCPVersion = "20140213 232212"
print "CUPS Cloud Print Setup Script Version " + CCPVersion
sys.exit(0)

@@ -14,16 +14,16 @@
# 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 backend
from ccputils import Utils

def test_fileIsPDFFails():
assert backend.fileIsPDF('testfiles/NotPdf.txt') == False
assert Utils.fileIsPDF('testfiles/NotPdf.txt') == False

def test_fileIsPDFSucceeds():
assert backend.fileIsPDF('testfiles/Test Page.pdf') == True
assert Utils.fileIsPDF('testfiles/Test Page.pdf') == True

def test_whichFails():
assert backend.which('dsaph9oaghd9ahdsadsadsadsadasd') == None
assert Utils.which('dsaph9oaghd9ahdsadsadsadsadasd') == None

def test_whichSucceeds():
assert backend.which('bash') in ( '/bin/bash', '/usr/bin/bash', '/usr/sbin/bash' )
assert Utils.which('bash') in ( '/bin/bash', '/usr/bin/bash', '/usr/sbin/bash' )
@@ -43,7 +43,7 @@
sys.stderr.write("If you are upgrading from version 20131013 or earlier you should be aware that the scripts have moved from /usr/lib/cloudprint-cups to /usr/share/cloudprint-cups\n")

# line below is replaced on commit
CCPVersion = "20140213 231214"
CCPVersion = "20140213 232212"

if len(sys.argv) == 2 and sys.argv[1] == 'version':
print "CUPS Cloud Print Upgrade Script Version " + CCPVersion
@@ -73,8 +73,8 @@
sys.stderr.write("\nRun: /usr/share/cloudprint-cups/setupcloudprint.py to setup your Google Credentials and add your printers to CUPS\n\n")
sys.exit(0)

from backend import which
if which('lpadmin') == None:
from ccputils import Utils
if Utils.which('lpadmin') == None:
sys.stderr.write("lpadmin command not found, you may need to run this script as root\n")
sys.exit(1)

0 comments on commit 6d9d5da

Please sign in to comment.
You can’t perform that action at this time.