summaryrefslogtreecommitdiffstats
path: root/iutil.py
blob: 0cca8728a0fad2b00ea9597b06fd0d478e241a42 (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
121
122
123
124
125
126
127

import types, os, sys, isys, select, string

def getArch ():
    arch = os.uname ()[4]
    if (len (arch) == 4 and arch[0] == 'i' and
        arch[2:4] == "86"):
        arch = "i386"

    if arch == "sparc64":
        arch = "sparc"

    return arch

def getfd(filespec, readOnly = 0):
    if type(filespec) == types.IntType:
	return filespec
    if filespec == None:
	filespec = "/dev/null"

    flags = os.O_RDWR | os.O_CREAT
    if (readOnly):
	flags = os.O_RDONLY
    return os.open(filespec, flags)

def execWithRedirect(command, argv, stdin = 0, stdout = 1, stderr = 2,	
		     searchPath = 0, root = '/'):
    stdin = getfd(stdin)
    if stdout == stderr:
	stdout = getfd(stdout)
	stderr = stdout
    else:
	stdout = getfd(stdout)
	stderr = getfd(stderr)

    if not os.access (root + command, os.X_OK):
        raise RuntimeError, command + " can not be run"

    childpid = os.fork()
    if (not childpid):
        if (root != '/'): isys.chroot (root)

	if type(stdin) == type("a"):
	    stdin == os.open(stdin, os.O_RDONLY)
	if type(stdout) == type("a"):
	    stdout == os.open(stdout, os.O_RDWR)
	if type(stderr) == type("a"):
	    stderr = os.open(stderr, os.O_RDWR)

	if stdin != 0:
	    os.dup2(stdin, 0)
	    os.close(stdin)
	if stdout != 1:
	    os.dup2(stdout, 1)
	    if stdout != stderr:
		os.close(stdout)
	if stderr != 2:
	    os.dup2(stderr, 2)
	    os.close(stderr)

	if (searchPath):
	    os.execvp(command, argv)
	else:
	    os.execv(command, argv)

	sys.exit(1)
    (pid, status) = os.waitpid(childpid, 0)

    return status

def execWithCapture(command, argv, searchPath = 0, root = '/', stdin = 0):

    if not os.access (root + command, os.X_OK):
        raise RuntimeError, command + " can not be run"
    
    (read, write) = os.pipe()

    childpid = os.fork()
    if (not childpid):
        if (root != '/'): isys.chroot (root)
	os.dup2(write, 1)

	if stdin:
	    os.dup2(stdin, 0)
	    os.close(stdin)

	if (searchPath):
	    os.execvp(command, argv)
	else:
	    os.execv(command, argv)

	sys.exit(1)

    os.close(write)

    rc = ""
    s = "1"
    while (s):
	select.select([read], [], [])
	s = os.read(read, 1000)
	rc = rc + s

    os.close(read)

    os.waitpid(childpid, 0)

    return rc

def copyFile(source, to):
    f = os.open(source, os.O_RDONLY)
    t = os.open(to, os.O_RDWR | os.O_TRUNC | os.O_CREAT)

    count = os.read(f, 16384)
    while (count):
	os.write(t, count)
	count = os.read(f, 16384)
	
    os.close(f)
    os.close(t)

def memInstalled():
    f = open("/proc/meminfo", "r")
    mem = f.readlines()[1]
    del f

    fields = string.split(mem)
    return int(fields[1]) / 1024