This repository has been archived by the owner on Aug 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
setupcloudprint.py
executable file
·167 lines (147 loc) · 6.28 KB
/
setupcloudprint.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
#! /usr/bin/env python2
# CUPS Cloudprint - Print via Google Cloud Print
# Copyright (C) 2011 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 cups, os, json, sys
from auth import Auth
from printer import Printer
if len(sys.argv) == 2 and sys.argv[1] == 'version':
# line below is replaced on commit
CCPVersion = "20131017 211222"
print "CUPS Cloud Print Setup Script Version " + CCPVersion
sys.exit(0)
if os.path.exists(Auth.config):
try:
content_file = open(Auth.config, 'r')
content = content_file.read()
data = json.loads(content)
except:
# remove old config file
print("Deleting old configuration file: " + Auth.config)
os.remove(Auth.config)
while True:
requestors, storage = Auth.SetupAuth(True)
print "You currently have these accounts configured: "
for requestor in requestors:
print requestor.getAccount()
answer = raw_input("Add more accounts (Y/N)? ")
if not ( answer.startswith("Y") or answer.startswith("y") ):
break
else:
Auth.AddAccount(storage)
for requestor in requestors:
addedCount = 0
connection = cups.Connection()
cupsprinters = connection.getPrinters()
prefix = ""
printer = Printer(requestor)
printers = printer.getPrinters()
if printers == None:
print("Sorry, no printers were found on your Google Cloud Print account.")
continue
answer = raw_input("Add all Google Cloud Print printers to local CUPS install from " + requestor.getAccount() + " (Y/N)? ")
if not ( answer.startswith("Y") or answer.startswith("y") ):
answer = 1
print("Not adding printers automatically")
while int(answer) != 0:
i=0
for printeritem in printers:
i+=1
print str(i) + ") " + printeritem['displayName']
maxprinterid = i
answer = raw_input("Add printer (1-" + str(maxprinterid) + ", 0 to cancel)? ")
try:
answer = int(answer)
except ValueError:
answer = 0
if answer != 0:
if answer >= 1 and answer <= maxprinterid:
ccpprinter = printers[answer-1]
print "Adding " + printers[answer-1]['displayName']
prefixanswer = raw_input("Use a prefix for name of printer (Y/N)? ")
if ( prefixanswer.startswith("Y") or prefixanswer.startswith("y") ):
prefix = raw_input("Prefix ( e.g. GCP- )? ")
if prefix == "":
print("Not using prefix")
printername = prefix + ccpprinter['name'].encode('ascii', 'replace')
uri = printer.printerNameToUri(ccpprinter['account'], ccpprinter['name'].encode('ascii', 'replace'))
found = False
for cupsprinter in cupsprinters:
if cupsprinters[cupsprinter]['device-uri'] == uri:
found = True
if found == True:
print "\nPrinter with " + printername +" already exists\n"
else:
printer.addPrinter(printername, uri, connection)
else:
print "\nPrinter " + str(answer) + " not found\n"
continue
prefixanswer = raw_input("Use a prefix for names of created printers (Y/N)? ")
if ( prefixanswer.startswith("Y") or prefixanswer.startswith("y") ):
prefix = raw_input("Prefix ( e.g. GCP- )? ")
if prefix == "":
print("Not using prefix")
for ccpprinter in printers:
uri = printer.printerNameToUri(ccpprinter['account'], ccpprinter['name'].encode('ascii', 'replace'))
found = False
for cupsprinter in cupsprinters:
if cupsprinters[cupsprinter]['device-uri'] == uri:
found = True
if found == False:
printername = prefix + ccpprinter['name']
# check if printer name already exists
foundbyname = False
for ccpprinter2 in cupsprinters:
if printer.sanitizePrinterName(cupsprinters[ccpprinter2]['printer-info']) == printer.sanitizePrinterName(printername):
foundbyname = True
if ( foundbyname ) :
answer = raw_input("Printer " + printer.sanitizePrinterName(printername) + " already exists, supply another name (Y/N)? ")
if ( answer.startswith("Y") or answer.startswith("y") ):
printername = raw_input("New printer name? ")
else:
answer = raw_input("Overwrite " + printer.sanitizePrinterName(printername) + " with new printer (Y/N)? ")
if ( answer.startswith("N") or answer.startswith("n") ):
printername = ""
if printername != "":
printer.addPrinter(printername, uri, connection)
addedCount+=1
if addedCount > 0:
print("Added " + str(addedCount) + " new printers to CUPS")
else:
print("No new printers to add")
printeruris = []
printer = Printer(requestors)
printers = printer.getPrinters()
for foundprinter in printers:
printeruris.append(printer.printerNameToUri(foundprinter['account'], foundprinter['name'].encode('ascii', 'replace')))
# check for printers to prune
prunePrinters = []
connection = cups.Connection()
cupsprinters = connection.getPrinters()
for cupsprinter in cupsprinters:
if cupsprinters[cupsprinter]['device-uri'].startswith( printer.PROTOCOL ):
if cupsprinters[cupsprinter]['device-uri'] not in printeruris:
prunePrinters.append(cupsprinter)
if len( prunePrinters ) > 0 :
print("Found " + str(len( prunePrinters )) + " printers which no longer exist on cloud print:")
for printer in prunePrinters:
print(printer)
answer = raw_input("Remove (Y/N)? ")
if answer.startswith("Y") or answer.startswith("y"):
for printer in prunePrinters:
connection.deletePrinter(printer)
print("Deleted",printer)
else:
print("Not removing old printers")