updated on Thu Jan 26 16:09:46 UTC 2012
[aur-mirror.git] / eclipse-motivational-splash / setupsplash.py
blob7a5729f3e62673983ae1a57c0a20382f4dc9142f
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # vim:set ts=2 sw=2 et:
4 # Alexander Rødseth <rodseth@gmail.com>
5 # BSD license
6 # 20102011
8 import os
9 import os.path
10 from sys import exit, argv
11 from time import localtime
12 from subprocess import getstatusoutput
14 ver = "Eclipse motivational splash screen setup v 1.1"
15 instmsg = "Installing Eclipse motivational splash image..."
16 req = "Must run as root"
17 progdir = "/usr/share/eclipse-motivational-splash"
19 def ls(path): #: list
20 return os.listdir(os.path.expanduser(path))
22 def lsd(path): #: list
23 return [e[1] for e in sorted([(os.path.getmtime(os.path.join(path, f)), os.path.join(path, f)) for f in ls(path)])]
25 def install_splash(newsplashfile, msg=instmsg):
26 status, splashdata = getstatusoutput("pacman -Ql eclipse")
27 splash = ""
28 for line in splashdata.split("\n"):
29 if line.endswith("splash.bmp"):
30 splash = line
31 break
32 try:
33 splashfile = splash.split()[1]
34 except IndexError:
35 print("Unable to find eclipse splash image (got %s)" % (splash))
36 exit(1)
37 esplashdir = os.path.dirname(splashfile)
38 today = "-".join(map(str, localtime()[0:6]))
39 backupfile = os.path.join(progdir, "splash-backup-%s.bmp" % (today))
40 if os.path.exists(splashfile):
41 if 0 != getstatusoutput("mv -f %s %s" % (splashfile, backupfile))[0]:
42 print(ver + "\n" + req)
43 exit(1)
44 print(msg, end=" ")
45 if 0 != getstatusoutput("cp %s %s" % (newsplashfile, splashfile))[0]:
46 print()
47 print("Unable to install splash image to %s/" % (esplashdir))
48 exit(1)
49 else:
50 splashdir = os.path.dirname(splashfile)
51 if 0 != getstatusoutput("mkdir -p %s" % (splashdir))[0]:
52 print(ver + "\n" + req)
53 exit(1)
54 print(msg, end=" ")
55 if 0 != getstatusoutput("cp %s %s" % (newsplashfile, splashfile))[0]:
56 print()
57 print("Unable to install splash image to %s/" % (esplashdir))
58 exit(1)
59 print("done")
61 def main():
62 newsplashfile = os.path.join(progdir, "splash.bmp")
63 arguments = argv[1:]
64 if arguments:
65 if arguments[0] == "-r":
66 status, splashdata = getstatusoutput("pacman -Ql eclipse")
67 # if eclipse is removed, nothing to do
68 if status != 0:
69 print("done")
70 exit(0)
71 else:
72 # if eclipse is here, move oldest splash backup as the new splash
73 last = [f for f in lsd(progdir) if "backup" in f][:1]
74 if last:
75 lastfile = last[0]
76 install_splash(lastfile, instmsg.replace("Installing", "Resetting"))
77 else:
78 # Directory probably doesn't exist, nothing to do
79 print("done")
80 exit(0)
81 else:
82 install_splash(newsplashfile)
84 if __name__ == "__main__":
85 main()