summaryrefslogtreecommitdiff
path: root/insanity/environment.py
blob: fc0c549295873c883338e322a89578675d9c3f8c (plain)
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
#!/usr/bin/env python

# GStreamer QA system
#
#       environment.py
#
# Copyright (c) 2007, Edward Hervey <bilboed@bilboed.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301, USA.

"""
Environment-related methods and classes
"""

import cPickle
import subprocess
import os
import tempfile
import sys
import imp
import gobject
gobject.threads_init()
from insanity.log import debug, exception

def _tupletostr(atup):
    return ".".join([str(x) for x in atup])

# TODO : methods/classes to retrieve/process environment
#
# examples:
#   env variablse
#   pluggable env retrievers
#   Application should be able to add information of its own
def _pollSubProcess(process, resfile, callback):
    res = process.poll()
    if res == None:
        return True
    # get dictionnary from resultfile
    try:
        wmf = open(resfile, "rb")
        resdict = cPickle.load(wmf)
        wmf.close()
        os.remove(resfile)
    except:
        exception("Couldn't get pickle from file %s", resfile)
        resdict = {}
    # call callback with dictionnary
    callback(resdict)
    return False

def collectEnvironment(environ, callback):
    """
    Using the given environment variables, spawn a new process to collect
    various environment information.

    Returns a dictionnary of information.

    When the information collection is done, the given callback will be called
    with the dictionnary of information as it's sole argument.
    """
    resfile, respath = tempfile.mkstemp()
    os.close(resfile)
    thispath = os.path.abspath(__file__)
    # The compiled module suffix can be ".pyc" or ".pyo":
    suffixes = [s[0] for s in imp.get_suffixes()
                if s[2] == imp.PY_COMPILED]
    for suffix in suffixes:
        if thispath.endswith(suffix):
            thispath = thispath[:-len(suffix)] + ".py"
            break
    pargs = [sys.executable, thispath, respath]

    try:
        debug("spawning subprocess %r", pargs)
        proc = subprocess.Popen(pargs, env=environ)
    except:
        exception("Spawning remote process (%s) failed" % (" ".join(pargs),))
        os.remove(respath)
        callback({})
    else:
        gobject.timeout_add(500, _pollSubProcess, proc, respath, callback)

def _getGObjectEnvironment():
    d = {}
    d["pygobject-path"] = gobject.__path__[0]
    d["pygobject-file"] = gobject.__file__
    d["glib-version"] = _tupletostr(gobject.glib_version)
    d["pygobject-version"] = _tupletostr(gobject.pygobject_version)
    d["pygtk-version"] = _tupletostr(gobject.pygtk_version)
    return d

def _privateCollectEnvironment():
    """
    Method called from the subprocess to collect environment
    """
    # we first get the system environment variables
    res = os.environ.copy()
    res["uname"] = ' '.join(os.uname())
    res.update(_getGObjectEnvironment())
    return res

if __name__ == "__main__":
    # args : <outputfile>
    d = _privateCollectEnvironment()
    mf = open(sys.argv[1], "wb+")
    cPickle.dump(d, mf)
    mf.close()