mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
147 lines
4.1 KiB
Python
Executable file
147 lines
4.1 KiB
Python
Executable file
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import os, sys, shutil
|
|
import argparse
|
|
|
|
|
|
def err(msg, die=None):
|
|
sys.stderr.write(str(msg) + '\n')
|
|
if die:
|
|
sys.exit((die if type(die) is int else 1))
|
|
|
|
try:
|
|
import pelican
|
|
|
|
global themes_path
|
|
themes_path = os.path.join(
|
|
os.path.dirname(
|
|
os.path.abspath(
|
|
pelican.__file__
|
|
)
|
|
),
|
|
'themes'
|
|
)
|
|
except:
|
|
err('Cannot import pelican.\nYou must install Pelican in order to run this script.', -1)
|
|
|
|
|
|
__version__ = '0.1'
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="""Install themes for Pelican""")
|
|
|
|
parser.add_argument('-l', '--list', dest='list', action="store_true",
|
|
help="Show the themes already installed")
|
|
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('-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('-v', '--verbose', dest='verbose', action="store_true",
|
|
help="Verbose output")
|
|
parser.add_argument('--version', action='version', version=__version__,
|
|
help='Print the version of this script')
|
|
args = parser.parse_args()
|
|
|
|
|
|
if args.list:
|
|
list(args.verbose)
|
|
else:
|
|
|
|
if args.to_install:
|
|
if args.verbose:
|
|
print('Installing themes...')
|
|
|
|
for i in args.to_install:
|
|
install(i, v=args.verbose)
|
|
|
|
if args.to_symlink:
|
|
if args.verbose:
|
|
print('Linking themes...')
|
|
|
|
for i in args.to_symlink:
|
|
symlink(i, v=args.verbose)
|
|
|
|
if args.to_remove:
|
|
if args.verbose:
|
|
print('Removing themes...')
|
|
|
|
for i in args.to_remove:
|
|
remove(i, v=args.verbose)
|
|
|
|
|
|
|
|
def 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.path.realpath(e))
|
|
else:
|
|
yield (e, None)
|
|
|
|
|
|
def list(v=False):
|
|
for t, l in themes():
|
|
if not v:
|
|
t = os.path.basename(t)
|
|
if l:
|
|
if v:
|
|
print(t + (" (symbolic link to `" + l + "')"))
|
|
else:
|
|
print(t + '@')
|
|
else:
|
|
print(t)
|
|
|
|
|
|
def remove(theme_name, v=False):
|
|
|
|
target = os.path.join(themes_path, theme_name)
|
|
|
|
|
|
if not os.path.exists(target):
|
|
err(target + ' : no such file or directory')
|
|
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)
|
|
else:
|
|
err(target + ' : not a valid theme')
|
|
|
|
|
|
def install(path, v=False):
|
|
if not os.path.exists(path):
|
|
err(path + ' : no such file or directory')
|
|
elif not os.path.isdir(path):
|
|
err(path + ' : no a directory')
|
|
else:
|
|
theme_name = os.path.basename(os.path.normpath(path))
|
|
theme_path = os.path.join(themes_path, theme_name)
|
|
try:
|
|
shutil.copytree(path, theme_path)
|
|
except Exception, e:
|
|
err("Cannot copy `{p}' to `{t}':\n{e}".format(p=path, t=theme_path, e=str(e)))
|
|
|
|
def symlink(path, v=False):
|
|
if not os.path.exists(path):
|
|
err(path + ' : no such file or directory')
|
|
elif not os.path.isdir(path):
|
|
err(path + ' : no a directory')
|
|
else:
|
|
theme_name = os.path.basename(os.path.normpath(path))
|
|
theme_path = os.path.join(themes_path, theme_name)
|
|
try:
|
|
os.symlink(path, theme_path)
|
|
except Exception, e:
|
|
err("Cannot link `{p}' to `{t}':\n{e}".format(p=path, t=theme_path, e=str(e)))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|