From ddad637bb34ea2b372f258a2786a13ace81a4a44 Mon Sep 17 00:00:00 2001 From: Skami18 Date: Sun, 8 May 2011 13:30:09 +0200 Subject: [PATCH] Added a theme manager. Will be useful to create triggers in Debian packages. --- tools/pelican-themes | 147 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100755 tools/pelican-themes diff --git a/tools/pelican-themes b/tools/pelican-themes new file mode 100755 index 00000000..a8b9ff1c --- /dev/null +++ b/tools/pelican-themes @@ -0,0 +1,147 @@ +#!/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()