#!/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' _BUILTIN_THEMES = ['simple', 'notmyidea'] 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_themes(args.verbose) else: 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_symlink: if args.verbose: print('Linking themes...') for i in args.to_symlink: symlink(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_themes(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): 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.\nYou cannot remove a builtin theme with this script, remove it by hand if you want.') elif 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) if os.path.exists(theme_path): err(path + ' : already exists') else: if v: print("Copying `{p}' to `{t}' ...".format(p=path, t=theme_path)) 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) if os.path.exists(theme_path): err(path + ' : already exists') else: if v: print("Linking `{p}' to `{t}' ...".format(p=path, t=theme_path)) 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()