| 38 | def discoverOtherBackends(self) : |
| 39 | """Discovers the other CUPS backends. |
| 40 | |
| 41 | Executes each existing backend in turn in device enumeration mode. |
| 42 | Returns the list of available backends. |
| 43 | """ |
| 44 | # Unfortunately this method can't output any debug information |
| 45 | # to stdout or stderr, else CUPS considers that the device is |
| 46 | # not available. |
| 47 | available = [] |
| 48 | (directory, myname) = os.path.split(sys.argv[0]) |
| 49 | tmpdir = tempfile.gettempdir() |
| 50 | lockfilename = os.path.join(tmpdir, "%s..LCK" % myname) |
| 51 | if os.path.exists(lockfilename) : |
| 52 | lockfile = open(lockfilename, "r") |
| 53 | pid = int(lockfile.read()) |
| 54 | lockfile.close() |
| 55 | try : |
| 56 | # see if the pid contained in the lock file is still running |
| 57 | os.kill(pid, 0) |
| 58 | except OSError, e : |
| 59 | if e.errno != errno.EPERM : |
| 60 | # process doesn't exist anymore |
| 61 | os.remove(lockfilename) |
| 62 | |
| 63 | if not os.path.exists(lockfilename) : |
| 64 | lockfile = open(lockfilename, "w") |
| 65 | lockfile.write("%i" % os.getpid()) |
| 66 | lockfile.close() |
| 67 | allbackends = [ os.path.join(directory, b) \ |
| 68 | for b in os.listdir(directory) |
| 69 | if os.access(os.path.join(directory, b), os.X_OK) \ |
| 70 | and (b != myname)] |
| 71 | for backend in allbackends : |
| 72 | answer = os.popen(backend, "r") |
| 73 | try : |
| 74 | devices = [line.strip() for line in answer.readlines()] |
| 75 | except : |
| 76 | devices = [] |
| 77 | status = answer.close() |
| 78 | if status is None : |
| 79 | for d in devices : |
| 80 | # each line is of the form : |
| 81 | # 'xxxx xxxx "xxxx xxx" "xxxx xxx"' |
| 82 | # so we have to decompose it carefully |
| 83 | fdevice = cStringIO.StringIO(d) |
| 84 | tokenizer = shlex.shlex(fdevice) |
| 85 | tokenizer.wordchars = tokenizer.wordchars + \ |
| 86 | r".:,?!~/\_$*-+={}[]()#" |
| 87 | arguments = [] |
| 88 | while 1 : |
| 89 | token = tokenizer.get_token() |
| 90 | if token : |
| 91 | arguments.append(token) |
| 92 | else : |
| 93 | break |
| 94 | fdevice.close() |
| 95 | try : |
| 96 | (devicetype, device, name, fullname) = arguments |
| 97 | except ValueError : |
| 98 | pass # ignore this 'bizarre' device |
| 99 | else : |
| 100 | if name.startswith('"') and name.endswith('"') : |
| 101 | name = name[1:-1] |
| 102 | if fullname.startswith('"') and fullname.endswith('"') : |
| 103 | fullname = fullname[1:-1] |
| 104 | available.append('%s cupsoftee:%s "CupsOfTee+%s" "CupsOfTee managed %s"' \ |
| 105 | % (devicetype, device, name, fullname)) |
| 106 | os.remove(lockfilename) |
| 107 | return available |
| 108 | |
80 | | def discoverOtherBackends(self) : |
81 | | """Discovers the other CUPS backends. |
82 | | |
83 | | Executes each existing backend in turn in device enumeration mode. |
84 | | Returns the list of available backends. |
85 | | """ |
86 | | self.logDebug("Entering device enumeration loop.") |
87 | | available = [] |
88 | | tmpdir = tempfile.gettempdir() |
89 | | lockfilename = os.path.join(tmpdir, "cupsoftee..LCK") |
90 | | if os.path.exists(lockfilename) : |
91 | | self.logInfo("Lock file present.") |
92 | | locked = 1 |
93 | | lockfile = open(lockfilename, "r") |
94 | | pid = int(lockfile.read()) |
95 | | lockfile.close() |
96 | | try : |
97 | | # see if the pid contained in the lock file is still running |
98 | | os.kill(pid, 0) |
99 | | except OSError, e : |
100 | | if e.errno != errno.EPERM : |
101 | | # process doesn't exist anymore |
102 | | self.logInfo("Removing stalled lock.") |
103 | | os.remove(lockfilename) |
104 | | locked = 0 |
105 | | if locked : |
106 | | self.logInfo("No device enumeration.") |
107 | | |
108 | | if not os.path.exists(lockfilename) : |
109 | | lockfile = open(lockfilename, "w") |
110 | | lockfile.write("%i" % os.getpid()) |
111 | | lockfile.close() |
112 | | (directory, myname) = os.path.split(sys.argv[0]) |
113 | | for backend in [ os.path.join(directory, b) \ |
114 | | for b in os.listdir(directory) |
115 | | if os.path.isfile(os.path.join(directory, b))\ |
116 | | and (b != myname)] : |
117 | | answer = os.popen(backend, "r") |
118 | | try : |
119 | | devices = [line.strip() for line in answer.readlines()] |
120 | | except : |
121 | | devices = [] |
122 | | status = answer.close() |
123 | | if status is None : |
124 | | for d in devices : |
125 | | # each line is of the form : |
126 | | # 'xxxx xxxx "xxxx xxx" "xxxx xxx"' |
127 | | # so we have to decompose it carefully |
128 | | fdevice = cStringIO.StringIO(d) |
129 | | tokenizer = shlex.shlex(fdevice) |
130 | | tokenizer.wordchars = tokenizer.wordchars + \ |
131 | | r".:,?!~/\_$*-+={}[]()#" |
132 | | arguments = [] |
133 | | while 1 : |
134 | | token = tokenizer.get_token() |
135 | | if token : |
136 | | arguments.append(token) |
137 | | else : |
138 | | break |
139 | | fdevice.close() |
140 | | try : |
141 | | (devicetype, device, name, fullname) = arguments |
142 | | except ValueError : |
143 | | pass # ignore this 'bizarre' device |
144 | | else : |
145 | | if name.startswith('"') and name.endswith('"') : |
146 | | name = name[1:-1] |
147 | | if fullname.startswith('"') and fullname.endswith('"') : |
148 | | fullname = fullname[1:-1] |
149 | | available.append('%s cupsoftee:%s "CupsOfTee+%s" "CupsOfTee managed %s"' \ |
150 | | % (devicetype, device, name, fullname)) |
151 | | os.remove(lockfilename) |
152 | | self.logDebug("Exiting device enumeration loop.") |
153 | | return available |
154 | | |