forked from getpelican/pelican
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpelican_themes.py
executable file
·292 lines (247 loc) · 8 KB
/
pelican_themes.py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env python
import argparse
import os
import shutil
import sys
def err(msg, die=None):
"""Print an error message and exits if an exit code is given"""
sys.stderr.write(msg + "\n")
if die:
sys.exit(die if isinstance(die, int) else 1)
try:
import pelican
except ImportError:
err(
"Cannot import pelican.\nYou must "
"install Pelican in order to run this script.",
-1,
)
global _THEMES_PATH
_THEMES_PATH = os.path.join(
os.path.dirname(os.path.abspath(pelican.__file__)), "themes"
)
__version__ = "0.2"
_BUILTIN_THEMES = ["simple", "notmyidea"]
def main():
"""Main function"""
parser = argparse.ArgumentParser(description="""Install themes for Pelican""")
excl = parser.add_mutually_exclusive_group()
excl.add_argument(
"-l",
"--list",
dest="action",
action="store_const",
const="list",
help="Show the themes already installed and exit",
)
excl.add_argument(
"-p",
"--path",
dest="action",
action="store_const",
const="path",
help="Show the themes path and exit",
)
excl.add_argument(
"-V",
"--version",
action="version",
version=f"pelican-themes v{__version__}",
help="Print the version of this script",
)
parser.add_argument(
"-i",
"--install",
dest="to_install",
nargs="+",
metavar="theme path",
help="The themes to install",
)
parser.add_argument(
"-r",
"--remove",
dest="to_remove",
nargs="+",
metavar="theme name",
help="The themes to remove",
)
parser.add_argument(
"-U",
"--upgrade",
dest="to_upgrade",
nargs="+",
metavar="theme path",
help="The themes to upgrade",
)
parser.add_argument(
"-s",
"--symlink",
dest="to_symlink",
nargs="+",
metavar="theme path",
help="Same as `--install', but create a symbolic link instead of "
"copying the theme. Useful for theme development",
)
parser.add_argument(
"-c",
"--clean",
dest="clean",
action="store_true",
help="Remove the broken symbolic links of the theme path",
)
parser.add_argument(
"-v", "--verbose", dest="verbose", action="store_true", help="Verbose output"
)
args = parser.parse_args()
to_install = args.to_install or args.to_upgrade
to_sym = args.to_symlink or args.clean
if args.action:
if args.action == "list":
list_themes(args.verbose)
elif args.action == "path":
print(_THEMES_PATH)
elif to_install or args.to_remove or to_sym:
if args.to_remove:
if args.verbose:
print("Removing themes...")
for i in args.to_remove:
remove(i, v=args.verbose)
if args.to_install:
if args.verbose:
print("Installing themes...")
for i in args.to_install:
install(i, v=args.verbose)
if args.to_upgrade:
if args.verbose:
print("Upgrading themes...")
for i in args.to_upgrade:
install(i, v=args.verbose, u=True)
if args.to_symlink:
if args.verbose:
print("Linking themes...")
for i in args.to_symlink:
symlink(i, v=args.verbose)
if args.clean:
if args.verbose:
print("Cleaning the themes directory...")
clean(v=args.verbose)
else:
print("No argument given... exiting.")
def themes():
"""Returns the list of the themes"""
for i in os.listdir(_THEMES_PATH):
e = os.path.join(_THEMES_PATH, i)
if os.path.isdir(e):
if os.path.islink(e):
yield (e, os.readlink(e))
else:
yield (e, None)
def list_themes(v=False):
"""Display the list of the themes"""
for theme_path, link_target in themes():
if not v:
theme_path = os.path.basename(theme_path)
if link_target:
if v:
print(theme_path + (" (symbolic link to `" + link_target + "')"))
else:
print(theme_path + "@")
else:
print(theme_path)
def remove(theme_name, v=False):
"""Removes a theme"""
theme_name = theme_name.replace("/", "")
target = os.path.join(_THEMES_PATH, theme_name)
if theme_name in _BUILTIN_THEMES:
err(
theme_name + " is a builtin theme.\n"
"You cannot remove a builtin theme with this script, "
"remove it by hand if you want."
)
elif os.path.islink(target):
if v:
print("Removing link `" + target + "'")
os.remove(target)
elif os.path.isdir(target):
if v:
print("Removing directory `" + target + "'")
shutil.rmtree(target)
elif os.path.exists(target):
err(target + " : not a valid theme")
else:
err(target + " : no such file or directory")
def install(path, v=False, u=False):
"""Installs a theme"""
if not os.path.exists(path):
err(path + " : no such file or directory")
elif not os.path.isdir(path):
err(path + " : not a directory")
else:
theme_name = os.path.basename(os.path.normpath(path))
theme_path = os.path.join(_THEMES_PATH, theme_name)
exists = os.path.exists(theme_path)
if exists and not u:
err(path + " : already exists")
elif exists:
remove(theme_name, v)
install(path, v)
else:
if v:
print(f"Copying '{path}' to '{theme_path}' ...")
try:
shutil.copytree(path, theme_path)
try:
if os.name == "posix":
for root, dirs, files in os.walk(theme_path):
for d in dirs:
dname = os.path.join(root, d)
os.chmod(dname, 493) # 0o755
for f in files:
fname = os.path.join(root, f)
os.chmod(fname, 420) # 0o644
except OSError as e:
err(
"Cannot change permissions of files "
f"or directory in `{theme_path}':\n{e!s}",
die=False,
)
except Exception as e:
err(f"Cannot copy `{path}' to `{theme_path}':\n{e!s}")
def symlink(path, v=False):
"""Symbolically link a theme"""
path = os.path.realpath(path)
if not os.path.exists(path):
err(path + " : no such file or directory")
elif not os.path.isdir(path):
err(path + " : not a directory")
else:
theme_name = os.path.basename(os.path.normpath(path))
theme_path = os.path.join(_THEMES_PATH, theme_name)
if os.path.exists(theme_path):
err(path + " : already exists")
else:
if v:
print(f"Linking `{path}' to `{theme_path}' ...")
try:
os.symlink(path, theme_path)
except Exception as e:
err(f"Cannot link `{path}' to `{theme_path}':\n{e!s}")
def is_broken_link(path):
"""Returns True if the path given as is a broken symlink"""
path = os.path.realpath(path)
return not os.path.exists(path)
def clean(v=False):
"""Removes the broken symbolic links"""
c = 0
for path in os.listdir(_THEMES_PATH):
path = os.path.join(_THEMES_PATH, path)
if os.path.islink(path) and is_broken_link(path):
if v:
print(f"Removing {path}")
try:
os.remove(path)
except OSError:
print(f"Error: cannot remove {path}")
else:
c += 1
print(f"\nRemoved {c} broken links")