~bennabiy/+junk/python-xlib

« back to all changes in this revision

Viewing changes to .pc/fix-unix-socket-in-display.patch/Xlib/support/unix_connect.py

  • Committer: Package Import Robot
  • Author(s): Andrew Shadura, Ramkumar Ramachandra, Andrew Shadura
  • Date: 2015-08-13 08:14:19 UTC
  • mfrom: (6.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20150813081419-hdefinnghp2iydkx
Tags: 0.14+20091101-3
[ Ramkumar Ramachandra ]
* Remove useless debugging output (Closes: #565996)

[ Andrew Shadura ]
* Switch to 3.0 (quilt) format.
* Rename patches.
* Use debhelper 9 in its short form.
* Use pybuild.
* Bump Standards-Version.
* Don't build or install PostScript documentation and info files.
* Use system-provided texi2html instead of a shipped version
  (Closes: #795057).
* Update debian/copyright (Closes: #795057).
* Don't install Makefile or texi2html with the documentation.
* Set executable bit for examples.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Xlib.support.unix_connect -- Unix-type display connection functions
 
2
#
 
3
#    Copyright (C) 2000,2002 Peter Liljenberg <petli@ctrl-c.liu.se>
 
4
#
 
5
#    This program is free software; you can redistribute it and/or modify
 
6
#    it under the terms of the GNU General Public License as published by
 
7
#    the Free Software Foundation; either version 2 of the License, or
 
8
#    (at your option) any later version.
 
9
#
 
10
#    This program is distributed in the hope that it will be useful,
 
11
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
#    GNU General Public License for more details.
 
14
#
 
15
#    You should have received a copy of the GNU General Public License
 
16
#    along with this program; if not, write to the Free Software
 
17
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 
 
19
import re
 
20
import string
 
21
import os
 
22
import platform
 
23
import socket
 
24
 
 
25
# FCNTL is deprecated from Python 2.2, so only import it if we doesn't
 
26
# get the names we need.  Furthermore, FD_CLOEXEC seems to be missing
 
27
# in Python 2.2.
 
28
 
 
29
import fcntl
 
30
 
 
31
if hasattr(fcntl, 'F_SETFD'):
 
32
    F_SETFD = fcntl.F_SETFD
 
33
    if hasattr(fcntl, 'FD_CLOEXEC'):
 
34
        FD_CLOEXEC = fcntl.FD_CLOEXEC
 
35
    else:
 
36
        FD_CLOEXEC = 1
 
37
else:
 
38
    from FCNTL import F_SETFD, FD_CLOEXEC
 
39
 
 
40
 
 
41
from Xlib import error, xauth
 
42
 
 
43
uname = platform.uname()
 
44
if (uname[0] == 'Darwin') and ([int(x) for x in uname[2].split('.')] >= [9, 0]):
 
45
 
 
46
    display_re = re.compile(r'^([-a-zA-Z0-9._/]*):([0-9]+)(\.([0-9]+))?$')
 
47
 
 
48
else:
 
49
 
 
50
    display_re = re.compile(r'^([-a-zA-Z0-9._]*):([0-9]+)(\.([0-9]+))?$')
 
51
 
 
52
def get_display(display):
 
53
    # Use $DISPLAY if display isn't provided
 
54
    if display is None:
 
55
        display = os.environ.get('DISPLAY', '')
 
56
 
 
57
    m = display_re.match(display)
 
58
    if not m:
 
59
        raise error.DisplayNameError(display)
 
60
 
 
61
    name = display
 
62
    host = m.group(1)
 
63
    dno = int(m.group(2))
 
64
    screen = m.group(4)
 
65
    if screen:
 
66
        screen = int(screen)
 
67
    else:
 
68
        screen = 0
 
69
 
 
70
    return name, host, dno, screen
 
71
 
 
72
 
 
73
def get_socket(dname, host, dno):
 
74
    try:
 
75
        # Darwin funky socket
 
76
        if (uname[0] == 'Darwin') and host and host.startswith('/tmp/'):
 
77
            s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
 
78
            s.connect(dname)
 
79
 
 
80
        # If hostname (or IP) is provided, use TCP socket
 
81
        elif host:
 
82
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
83
            s.connect((host, 6000 + dno))
 
84
 
 
85
        # Else use Unix socket
 
86
        else:
 
87
            s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
 
88
            s.connect('/tmp/.X11-unix/X%d' % dno)
 
89
    except socket.error, val:
 
90
        raise error.DisplayConnectionError(dname, str(val))
 
91
 
 
92
    # Make sure that the connection isn't inherited in child processes
 
93
    fcntl.fcntl(s.fileno(), F_SETFD, FD_CLOEXEC)
 
94
 
 
95
    return s
 
96
 
 
97
 
 
98
def new_get_auth(sock, dname, host, dno):
 
99
    # Translate socket address into the xauth domain
 
100
    if (uname[0] == 'Darwin') and host and host.startswith('/tmp/'):
 
101
        family = xauth.FamilyLocal
 
102
        addr = socket.gethostname()
 
103
 
 
104
    elif host:
 
105
        family = xauth.FamilyInternet
 
106
 
 
107
        # Convert the prettyprinted IP number into 4-octet string.
 
108
        # Sometimes these modules are too damn smart...
 
109
        octets = string.split(sock.getpeername()[0], '.')
 
110
        addr = string.join(map(lambda x: chr(int(x)), octets), '')
 
111
    else:
 
112
        family = xauth.FamilyLocal
 
113
        addr = socket.gethostname()
 
114
 
 
115
    au = xauth.Xauthority()
 
116
    while 1:
 
117
        try:
 
118
            return au.get_best_auth(family, addr, dno)
 
119
        except error.XNoAuthError:
 
120
            pass
 
121
 
 
122
        # We need to do this to handle ssh's X forwarding.  It sets
 
123
        # $DISPLAY to localhost:10, but stores the xauth cookie as if
 
124
        # DISPLAY was :10.  Hence, if localhost and not found, try
 
125
        # again as a Unix socket.
 
126
        if family == xauth.FamilyInternet and addr == '\x7f\x00\x00\x01':
 
127
            family = xauth.FamilyLocal
 
128
            addr = socket.gethostname()
 
129
        else:
 
130
            return '', ''
 
131
 
 
132
 
 
133
def old_get_auth(sock, dname, host, dno):
 
134
    # Find authorization cookie
 
135
    auth_name = auth_data = ''
 
136
 
 
137
    try:
 
138
        # We could parse .Xauthority, but xauth is simpler
 
139
        # although more inefficient
 
140
        data = os.popen('xauth list %s 2>/dev/null' % dname).read()
 
141
 
 
142
        # If there's a cookie, it is of the format
 
143
        #      DISPLAY SCHEME COOKIE
 
144
        # We're interested in the two last parts for the
 
145
        # connection establishment
 
146
        lines = string.split(data, '\n')
 
147
        if len(lines) >= 1:
 
148
            parts = string.split(lines[0], None, 2)
 
149
            if len(parts) == 3:
 
150
                auth_name = parts[1]
 
151
                hexauth = parts[2]
 
152
                auth = ''
 
153
 
 
154
                # Translate hexcode into binary
 
155
                for i in range(0, len(hexauth), 2):
 
156
                    auth = auth + chr(string.atoi(hexauth[i:i+2], 16))
 
157
 
 
158
                auth_data = auth
 
159
    except os.error:
 
160
        pass
 
161
 
 
162
    return auth_name, auth_data
 
163
 
 
164
get_auth = new_get_auth