xwrapper.py 4.01 KB
Newer Older
1
# xwrapper.py - wrapper around X applications
2
# Copyright (C) 2004 - 2010  Gaute Hope <eg at gaute dot vetsj dot com>
3
#
4 5 6 7 8 9 10 11 12 13 14 15
# 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 2 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 Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
Gaute Hope's avatar
Gaute Hope committed
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

#python
import sys
import os
import pwd


# g-s modules
import config
import crontab


##
## I18N
##
import gettext
gettext.install(config.GETTEXT_PACKAGE(), config.GNOMELOCALEDIR(), unicode=1)

35
def check_X (display, xauth):
Gaute Hope's avatar
Gaute Hope committed
36
    # Checking if I can use X
37
    os.putenv ('DISPLAY', display)
38
    os.putenv ('XAUTHORITY', xauth)
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

    try:
        import pygtk
        pygtk.require("2.0")

    except:
        pass

    try:
        import gtk

    except:
        print _("You need to install pyGTK or GTKv2,\n"
                "or set your PYTHONPATH correctly.\n"
                "try: export PYTHONPATH= ")
        sys.exit(1)

    try:
Gaute Hope's avatar
Gaute Hope committed
57
        gtk.init_check ()
58

Gaute Hope's avatar
Gaute Hope committed
59
    except Exception as e:
60
        print _("Could not open a connection to X!")
61 62 63 64 65 66 67 68 69 70 71 72
        print e
        sys.exit (1)

poscorrect_isset = os.getenv ("POSIXLY_CORRECT", False)
manual_poscorrect = False
if poscorrect_isset == False:
    os.putenv ("POSIXLY_CORRECT", "enabled")
    manual_poscorrect = True

if (len (sys.argv) < 2):
    print _("Wrong number of arguments.")

73
    print _("Wrapper script for Gnome-schedule (http://gnome-schedule.sf.net) for applications to be run from crontab or at under X. Use through gnome-schedule.")
74 75 76 77 78 79 80
    sys.exit (1)

if sys.argv[1] == "c":
    job_type = 0
    if (len (sys.argv) != 3):
        print _("Wrong number of arguments.")

81
        print _("Wrapper script for Gnome-schedule (http://gnome-schedule.sf.net) for applications to be run from crontab or at under X. Use through gnome-schedule.")
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
        sys.exit (1)

elif sys.argv[1] == "a":
    job_type = 1
else:
    print _("Unknown type of job.")
    sys.exit (1)

uid = os.geteuid ()
gid = os.getegid ()
user = pwd.getpwuid (uid)[0]
home_dir = pwd.getpwuid (uid)[5]
user_shell = pwd.getpwuid (uid)[6]
if uid == 0:
    is_root = True
else:
    is_root = False

# CRONTAB
if job_type == 0:

    try:
        job_id = int (sys.argv[2])
    except:
        print _("Invalid job id.")
        sys.exit (1)

    if job_id < 0:
        print _("Invalid job id.")
        sys.exit (1)

    c = crontab.Crontab (is_root, user, uid, gid, home_dir)
    success, ver, title, desc, output, display, command = c.get_job_data (job_id)

    if success == False:
        print _("Could not get job data, the task might have been created with an old version - try recreating the task.")
        sys.exit (1)

    if ver < 5:
        print _("Data file too old. Recreate task.")
        sys.exit (1)


    print _("Launching %s.." % title)
    if (output < 2):
127
        print _("output<2: Why am I launched?")
128 129 130 131 132
        sys.exit (1)
    if (len (display) < 2):
        print _("len(display)<2: No proper DISPLAY variable")
        sys.exit (1)

133
    # TODO: Can/Does this change ?
Gaute Hope's avatar
Gaute Hope committed
134
    xauth = home_dir + "/.Xauthority"
135

136
    check_X (display, xauth)
137 138 139 140

    # Execute task
    sh = os.popen ("/bin/sh -s", 'w')
    sh.write ("export DISPLAY=" + display + "\n")
141
    sh.write ("export XAUTHORITY=" + xauth + "\n")
142 143 144 145 146 147 148 149
    sh.write (command + "\n")
    sh.close ()

    sys.exit ()

# AT
elif (job_type == 1):
    display = os.getenv ('DISPLAY')
Gaute Hope's avatar
Gaute Hope committed
150
    xauth = home_dir + "/.Xauthority"
Gaute Hope's avatar
Gaute Hope committed
151
    check_X (display, xauth)
152 153 154 155 156 157 158 159 160
    sys.exit (0) # All fine

else:
    print _("I will never be displayed.")
    sys.exit (1)

print _("xwrapper.py: completed")