aboutsummaryrefslogtreecommitdiff
path: root/ratinox
blob: 6cdad0970ae1d92604b0cb2f5994b043a02bde64 (plain) (blame)
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
128
#!/usr/bin/env python

"""
Perform demented tasks with blind rodents.
"""

import subprocess, os, time, xvfbwrapper, shutil, argparse

class Ratinox(xvfbwrapper.Xvfb):

    xvfb_screen_name = "Xvfb_screen0"

    def __init__(self, **kwargs):
        if kwargs.pop("screencap", True) and "fbdir" in kwargs:
            self.screencap_file = os.path.join(kwargs["fbdir"], self.xvfb_screen_name)
        else:
            self.screencap_file = None
        xvfbwrapper.Xvfb.__init__(self, **kwargs)

    def __enter__(self):
        result = xvfbwrapper.Xvfb.__enter__(self)
        self.wm = subprocess.Popen(("ratpoison",))
        time.sleep(0.5)
        return result

    def __exit__(self, exc_type, exc_val, exc_tb):
        subprocess.check_call(("ratpoison", "-c", "quit"))
        self.wm.wait()
        self.wm = None
        xvfbwrapper.Xvfb.__exit__(self, exc_type, exc_val, exc_tb)

    def click(self, *coordinates):
        assert len(coordinates) in (0, 2)
        if coordinates:
            subprocess.check_call(("ratpoison", "-c", "ratwarp {:d} {:d}".format(*coordinates)))
        if self.screencap_file:
            shutil.copy(self.screencap_file, "{}.{}".format(self.screencap_file, time.time()))
        subprocess.check_call(("ratpoison", "-c", "ratclick"))

def snooze(how_long = 15):
    time.sleep(how_long)

def installer():

    with Ratinox(fbdir = "/cryptech-builder") as rat:
        print("DISPLAY={}".format(os.getenv("DISPLAY")))

        snooze()
        print("Starting XiLinx installer")
        xsetup = subprocess.Popen(("./xsetup",), cwd = "/Xilinx_ISE_DS_Lin_14.7_1015_1")

        snooze()
        print("First screen")
        rat.click(650, 610)

        snooze()
        print("Second screen")
        rat.click(250, 420)
        rat.click(250, 444)
        rat.click(650, 610)

        snooze()
        print("Third screen")
        rat.click(600, 560)
        rat.click(650, 610)

        snooze()
        print("Fourth screen")
        rat.click(300, 100)
        rat.click(650, 610)

        for ith in ("Fifth", "Sixth", "Seventh"):
            snooze()
            print(ith + " screen")
            rat.click()

        print("Waiting half an hour for XiLinx installer to run, ^C if you get bored")
        snooze(1800)

        # In theory we could use visgrep to check for the "finish" button.
        # In practice ... this is such a kludge, what's one more, let's just
        # try blindly clicking where the finish button should be and see
        # if that results in a usable image.

        print("Blindly clicking where finish button should be")
        rat.click(720, 610)

        print("xsetup exited with status {}".format(xsetup.wait()))

def licenser():

    with Ratinox(fbdir = "/cryptech-builder") as rat:
        print("DISPLAY={}".format(os.getenv("DISPLAY")))

        snooze()
        print("Starting XiLinx license manager")
        xlcm = ". /opt/Xilinx/14.7/ISE_DS/settings64.sh; /opt/Xilinx/14.7/ISE_DS/common/bin/lin64/xlcm -manage"
        xlcm = subprocess.Popen(xlcm, shell = True)

        snooze()
        print("First screen")
        rat.click(100, 116)

        snooze()
        print("Second screen")
        rat.click(220, 170)
        rat.click(680, 490)

        snooze()
        print("Third screen")
        rat.click(400, 360)
        rat.click(750, 650)

        print("xlcm exited with status {}".format(xlcm.wait()))

def main():

    dispatch = {"ise-install"  : installer,
                "license-user" : licenser }

    ap = argparse.ArgumentParser(description = __doc__)
    ap.add_argument("command", choices = tuple(sorted(dispatch)))
    args = ap.parse_args()

    dispatch[args.command]()

if __name__ == "__main__":
    main()