File: screenshotMaker.py

package info (click to toggle)
gnome-app-install 0.5.5.1-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,364 kB
  • ctags: 448
  • sloc: python: 4,321; xml: 940; makefile: 42; sh: 9
file content (124 lines) | stat: -rwxr-xr-x 3,506 bytes parent folder | download
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
#!/usr/bin/python

import os
import os.path
import sys
import string
import time
import optparse
import subprocess
import copy
import tempfile
import apt
import glob

TIMEOUT=10

# FIXME:
# * add timeout 
# blacklist: orca, python2.5
#

def check_apps():
    l = ["/usr/bin/apt-get",
         "/usr/bin/convert",
         "/usr/bin/xvfb-run",
         "/usr/bin/xwd",
         ]
    for prog in l:
        if not os.path.exists(prog):
            print "Missing: %s" % prog
            return False
    return True

def take_screenshot(execCmd, outfile):
    p = subprocess.Popen(["xvfb-run",
                          "--server-args= -screen 0 1024x768x24",
                          "-f",os.path.expanduser("~/.Xauthority"),
                          "-w","5",
                            execCmd])
    # wait for the app to startup
    time.sleep(TIMEOUT)
    tmp = tempfile.NamedTemporaryFile()
    env = copy.copy(os.environ)
    env["DISPLAY"] = ":99"
    ret = subprocess.call(["xsetroot", "-solid", "red"], env=env)
    ret = subprocess.Popen(["gnome-settings-daemon"], env=env)
    time.sleep(5)
    ret = subprocess.call(["xwd", "-root", "-out", tmp.name], env=env)
    ret = subprocess.call(["convert", tmp.name, "-trim", outfile])
    time.sleep(1)
    ret = subprocess.call(["killall","Xvfb"])
    p.wait()
    return True

def process_dir(d):
    res = True
    for f in glob.glob(d+"/*.desktop"):
        print "Processing file: ",f
        res &= process_desktop_file(f)
    return res

def process_desktop_file(f):
    res = False
    execCmd = None
    package = None

    if not os.path.exists(f):
        return False

    for line in map(string.strip, open(f).readlines()):
        if line.startswith("X-AppInstall-Package="):
            package = line.split("=")[1]
        if line.startswith("Exec="):
            execCmd = line.split("=")[1]
            if (execCmd == "gksu" or
                execCmd == "gksudo"):
                print "%s needs gksu/gksudo" % f
                return False
            if " " in execCmd:
                execCmd = execCmd.split()[0]
    if package is None or execCmd is None:
        print "No package or exec line found?!? %s" % f
        return False
    outfile = "screenshots/%s.png" % os.path.basename(f)
    if cache.has_key(package) and cache[package].isInstalled:
        res = take_screenshot(execCmd, outfile)
        return res
    return False
    # FIXME:
    # install the package
    # res = take_screenshot(execCmd, outfile)
    # autoremove the package again
    # return res

if __name__ == "__main__":

    # kill old Xvfbs
    subprocess.call(["killall","Xvfb"])

    cache = apt.Cache()

    parser = optparse.OptionParser()
    parser.add_option("-v", "--verbose", dest="verbose",
                      action="store_true", default="False",
                      help="be verbose")
    parser.add_option("-d", "--direcroty", dest="dir",
                      default=None,
                      help="directory for the menu-data")
    parser.add_option("-f", "--file", dest="file",
                      default=None,
                      help="menu-data desktop file")
    (options, args) = parser.parse_args()

    if options.dir == None and options.file == None: 
        print "need a menu-data file (-f) or directory (-d option) "
        sys.exit(1)

    if not check_apps():
        sys.exit(1)

    if options.dir:
        process_dir(options.dir)
    if options.file:
        process_desktop_file(options.file)