Debian Codegraph @

util/standalone-tools-icons.py

raw
1 #!/usr/bin/env python3
2 from glob import glob
3 from subprocess import call
4 from tempfile import mkdtemp
5 import os
6 import re
7 import sys
8
9
10 appname = "DisplayCAL"
11 feeduri = "http://displaycal.net/0install/%s.xml" % appname
12
13
14 def script2pywname(script):
15 """Convert all-lowercase script name to mixed-case pyw name"""
16 a2b = {
17 appname + "-3dlut-maker": appname + "-3DLUT-maker",
18 appname + "-vrml-to-x3d-converter": appname + "-VRML-to-X3D-converter",
19 }
20 pyw = appname + script[len(appname):]
21 return a2b.get(pyw, pyw)
22
23
24 def installer(action="install"):
25 if action not in ("install", "uninstall"):
26 raise ValueError("Invalid action %r" % action)
27 root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
28 if action == "install":
29 tmpdir = mkdtemp()
30 tmpfilenames = []
31 try:
32 for desktopfilename in glob(
33 os.path.join(root, "misc", "%s-*.desktop" % appname.lower())
34 ):
35 desktopbasename = os.path.basename(desktopfilename)
36 scriptname = re.sub(r"\.desktop$", "", desktopbasename)
37 if action == "install":
38 try:
39 for size in [16, 22, 24, 32, 48, 256]:
40 call(
41 [
42 "xdg-icon-resource",
43 action,
44 "--noupdate",
45 "--novendor",
46 "--size",
47 str(size),
48 os.path.join(
49 root,
50 appname,
51 "theme",
52 "icons",
53 "%sx%s" % (size, size),
54 scriptname + ".png",
55 ),
56 ]
57 )
58 except EnvironmentError as exception:
59 exception.filename = "xdg-icon-resource"
60 raise exception
61 with open(desktopfilename) as desktopfile:
62 contents = desktopfile.read()
63 cmdname = script2pywname(scriptname)
64 cmd = re.sub(r"^%s-" % appname, "run-", cmdname)
65 for pattern, repl in [
66 ("Exec=.+", "Exec=0launch --command=%s -- %s %%f" % (cmd, feeduri))
67 ]:
68 contents = re.sub(pattern, repl, contents)
69 tmpfilename = os.path.join(tmpdir, desktopbasename)
70 tmpfilenames.append(tmpfilename)
71 with open(tmpfilename, "w") as tmpfile:
72 tmpfile.write(contents)
73 try:
74 call(
75 [
76 "xdg-desktop-menu",
77 action,
78 "--noupdate",
79 "--novendor",
80 tmpfilename,
81 ]
82 )
83 except EnvironmentError as exception:
84 exception.filename = "xdg-desktop-menu"
85 raise exception
86 else:
87 try:
88 for size in [16, 22, 24, 32, 48, 256]:
89 call(
90 [
91 "xdg-icon-resource",
92 action,
93 "--noupdate",
94 "--size",
95 str(size),
96 scriptname,
97 ]
98 )
99 except EnvironmentError as exception:
100 exception.filename = "xdg-icon-resource"
101 raise exception
102 try:
103 call(["xdg-desktop-menu", action, "--noupdate", desktopfilename])
104 except EnvironmentError as exception:
105 exception.filename = "xdg-desktop-menu"
106 raise exception
107 finally:
108 if action == "install":
109 try:
110 for tmpfilename in tmpfilenames:
111 if tmpfilename and os.path.isfile(tmpfilename):
112 os.unlink(tmpfilename)
113 os.rmdir(tmpdir)
114 except Exception as exception:
115 import warnings
116
117 warnings.warn(exception, Warning)
118 if os.geteuid() == 0:
119 data_dirs = os.getenv("XDG_DATA_DIRS", "/usr/local/share:/usr/share").split(
120 os.pathsep
121 )
122 else:
123 data_dirs = os.getenv(
124 "XDG_DATA_HOME", os.path.expandvars("$HOME/.local/share")
125 ).split(os.pathsep)
126 for data_dir in data_dirs:
127 call(["touch", "--no-create", data_dir.rstrip("/") + "/icons/hicolor"])
128 call(["xdg-icon-resource", "forceupdate"])
129 call(["xdg-desktop-menu", "forceupdate"])
130
131
132 if __name__ == "__main__":
133 if len(sys.argv) == 2:
134 try:
135 installer(sys.argv[1])
136 except Exception as exception:
137 print(exception)
138 else:
139 print("Usage: %s install | uninstall" % os.path.basename(__file__))