Mercurial > piecrust2
diff piecrust/commands/builtin/themes.py @ 480:d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Mon, 20 Jul 2015 22:56:45 -0700 |
parents | c5df200354e8 |
children | a00750896316 |
line wrap: on
line diff
--- a/piecrust/commands/builtin/themes.py Mon Jul 20 22:25:39 2015 -0700 +++ b/piecrust/commands/builtin/themes.py Mon Jul 20 22:56:45 2015 -0700 @@ -40,6 +40,15 @@ help="Copies a theme to the website for customization.") p.set_defaults(sub_func=self._overrideTheme) + p = subparsers.add_parser( + 'link', + help="Installs a theme as a link to an already existing " + "theme on disk.") + p.add_argument( + 'theme_dir', + help="The directory of the theme to link.") + p.set_defaults(sub_func=self._linkTheme) + def checkedRun(self, ctx): if not hasattr(ctx.args, 'sub_func'): ctx.parser.parse_args(['themes', '--help']) @@ -131,3 +140,24 @@ os.makedirs(os.path.dirname(c[1])) shutil.copy2(c[0], c[1]) + def _linkTheme(self, ctx): + if not os.path.isdir(ctx.args.theme_dir): + logger.error("Invalid theme directory: %s" % ctx.args.theme_dir) + return 1 + + theme_dir = os.path.join(ctx.app.root_dir, THEME_DIR) + + if os.path.islink(theme_dir): + logger.debug("Unlinking: %s" % theme_dir) + os.unlink(theme_dir) + elif os.path.isdir(theme_dir): + logger.warning("A theme already exists, and will be overwritten. " + "Are you sure? [Y/n]") + ans = input() + if len(ans) > 0 and ans.lower() not in ['y', 'yes']: + return 1 + + shutil.rmtree(theme_dir) + + os.symlink(ctx.args.theme_dir, theme_dir) +