~ubuntu-branches/ubuntu/lucid/foomatic-gui/lucid

« back to all changes in this revision

Viewing changes to foomatic/pysmb.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lawrence
  • Date: 2004-06-23 18:54:20 UTC
  • Revision ID: james.westby@ubuntu.com-20040623185420-kzvwuo1wzk4si5su
Tags: 0.6.7
* Add GNOME/KDE desktop menu entry.
* Ensure the correct destination is selected in the TreeView when
  reconfiguring an existing queue.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
## redhat-config-printer
 
4
## CUPS backend
 
5
 
 
6
## Copyright (C) 2002, 2003 Red Hat, Inc.
 
7
## Copyright (C) 2002, 2003 Tim Waugh <twaugh@redhat.com>
 
8
 
 
9
## This program is free software; you can redistribute it and/or modify
 
10
## it under the terms of the GNU General Public License as published by
 
11
## the Free Software Foundation; either version 2 of the License, or
 
12
## (at your option) any later version.
 
13
 
 
14
## This program is distributed in the hope that it will be useful,
 
15
## but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
## GNU General Public License for more details.
 
18
 
 
19
## You should have received a copy of the GNU General Public License
 
20
## along with this program; if not, write to the Free Software
 
21
## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
22
 
 
23
## $Id: pysmb.py,v 1.2 2003/05/20 10:07:00 lordsutch Exp $
 
24
 
 
25
import os
 
26
import signal
 
27
import sys
 
28
 
 
29
nmblookup = "/usr/bin/nmblookup"
 
30
smbclient = "/usr/bin/smbclient"
 
31
 
 
32
def get_host_list ():
 
33
    """Return a dict, indexed by SMB name, of host dicts for this network.
 
34
 
 
35
    A host dict has 'NAME' and 'IP' keys, and may have a 'GROUP' key."""
 
36
 
 
37
    hosts = {}
 
38
    if not os.access (nmblookup, os.X_OK):
 
39
        return hosts
 
40
 
 
41
    ips = []
 
42
    signal.signal (signal.SIGCHLD, signal.SIG_DFL)
 
43
    for l in os.popen ("LANG=C %s '*'" % nmblookup, 'r').readlines ():
 
44
        if l.endswith (" *<00>\n"):
 
45
            ips.append (l.split (" ")[0])
 
46
 
 
47
    for ip in ips:
 
48
        name = None
 
49
        dict = { 'IP': ip }
 
50
        signal.signal (signal.SIGCHLD, signal.SIG_DFL)
 
51
        for l in os.popen ("LANG=C %s -A '%s'" % (nmblookup, ip),
 
52
                           'r').readlines ():
 
53
            l = l.strip ()
 
54
            if l.find (" <00> ") == -1:
 
55
                continue
 
56
 
 
57
            try:
 
58
                val = l.split (" ")[0]
 
59
                if val.find ("~") != -1:
 
60
                    continue
 
61
 
 
62
                if l.find (" <GROUP> ") != -1 and not dict.has_key ('GROUP'):
 
63
                    dict['GROUP'] = l.split (" ")[0]
 
64
                elif not dict.has_key ('NAME'):
 
65
                    name = l.split (" ")[0]
 
66
                    dict['NAME'] = name
 
67
            except:
 
68
                pass
 
69
 
 
70
        if not name:
 
71
            continue
 
72
 
 
73
        hosts[name] = dict
 
74
 
 
75
    return hosts
 
76
 
 
77
def get_host_info (smbname):
 
78
    """Given an SMB name, returns a host dict for it."""
 
79
    dict = { 'NAME': smbname, 'IP': '', 'GROUP': '' }
 
80
    for l in os.popen ("LANG=C %s -S '%s'" % (nmblookup, smbname),
 
81
                       'r').readlines ():
 
82
        l = l.strip ()
 
83
        if l.endswith ("<00>"):
 
84
            dict['IP'] = l.split (" ")[0]
 
85
            continue
 
86
 
 
87
        if l.find (" <00> ") == -1:
 
88
            continue
 
89
 
 
90
        if l.find (" <GROUP> ") != -1:
 
91
            dict['GROUP'] = l.split (" ")[0]
 
92
        else:
 
93
            name = l.split (" ")[0]
 
94
            dict['NAME'] = name
 
95
 
 
96
    return dict
 
97
 
 
98
def get_printer_list (host):
 
99
    """Given a host dict, returns a dict of printer shares for that host.
 
100
    The value for a printer share name is its comment."""
 
101
 
 
102
    printers = {}
 
103
    if not os.access (smbclient, os.X_OK):
 
104
        return printers
 
105
 
 
106
    str = "LANG=C %s -N -L '%s'" % (smbclient, host['NAME'])
 
107
    if host.has_key ('IP'):
 
108
        str += " -I '%s'" % host['IP']
 
109
 
 
110
    if host.has_key ('GROUP'):
 
111
        str += " -W '%s'" % host['GROUP']
 
112
 
 
113
    signal.signal (signal.SIGCHLD, signal.SIG_DFL)
 
114
    section = 0
 
115
    for l in os.popen (str, 'r'):
 
116
        l = l.strip ()
 
117
        if l == "":
 
118
            continue
 
119
 
 
120
        if l[0] == '-':
 
121
            section += 1
 
122
            if section > 1:
 
123
                break
 
124
 
 
125
            continue
 
126
 
 
127
        if section != 1:
 
128
            continue
 
129
 
 
130
        share = l[:l.find (" ")]
 
131
        rest = l[len (share):].strip ()
 
132
        end = rest.find (" ")
 
133
        if end == -1:
 
134
            type = rest
 
135
            comment = ""
 
136
        else:
 
137
            type = rest[:rest.find (" ")]
 
138
            comment = rest[len (type):].strip ()
 
139
 
 
140
        if type == "Printer":
 
141
            printers[share] = comment
 
142
 
 
143
    return printers
 
144
 
 
145
def printer_share_accessible (share, group = None, user = None, passwd = None):
 
146
    """Returns None if the share is inaccessible.  Otherwise,
 
147
    returns a dict with 'GROUP' associated with the workgroup name
 
148
    of the server."""
 
149
 
 
150
    if not os.access (smbclient, os.X_OK):
 
151
        return None
 
152
 
 
153
    args = [ smbclient, share ]
 
154
    if passwd:
 
155
        args.append (passwd)
 
156
 
 
157
    if group:
 
158
        args.extend (["-W", group])
 
159
 
 
160
    args.extend (["-N", "-P", "-c", "quit"])
 
161
    if user:
 
162
        args.extend (["-U", user])
 
163
 
 
164
    read, write = os.pipe ()
 
165
    signal.signal (signal.SIGCHLD, signal.SIG_DFL)
 
166
    pid = os.fork ()
 
167
    if pid == 0:
 
168
        os.close (read)
 
169
        if write != 1:
 
170
            os.dup2 (write, 1)
 
171
 
 
172
        os.environ['LANG'] = 'C'
 
173
        os.execv (args[0], args)
 
174
        sys.exit (1)
 
175
 
 
176
    # Parent
 
177
    dict = { 'GROUP': ''}
 
178
    os.close (write)
 
179
    for l in os.fdopen (read, 'r').readlines ():
 
180
        if l.startswith ("Domain=[") and l.find ("]") != -1:
 
181
            dict['GROUP'] = l[len("Domain=["):].split ("]")[0]
 
182
            break
 
183
 
 
184
    pid, status = os.waitpid (pid, 0)
 
185
    if status:
 
186
        return None
 
187
 
 
188
    return dict
 
189
 
 
190
if __name__ == '__main__':
 
191
    hosts = get_host_list ()
 
192
    print get_printer_list (hosts[hosts.keys ()[0]])