annotate piecrust/commands/builtin/themes.py @ 747:5336e146ac8d

themes: Simplify `themes` command.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 09 Jun 2016 22:25:16 -0700
parents a00750896316
children 549e21789ad9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
273
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import os
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import os.path
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import shutil
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 import logging
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 import yaml
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 from piecrust import (
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 RESOURCES_DIR, THEME_DIR, THEME_CONFIG_PATH, THEME_INFO_PATH)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 from piecrust.commands.base import ChefCommand
747
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
9 from piecrust.pathutil import SiteNotFoundError
273
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 logger = logging.getLogger(__name__)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 class ThemesCommand(ChefCommand):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 def __init__(self):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 super(ThemesCommand, self).__init__()
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 self.name = 'themes'
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 self.description = "Manage the themes for the current website."
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 def setupParser(self, parser, app):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 subparsers = parser.add_subparsers()
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 p = subparsers.add_parser(
488
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
24 'info',
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
25 help="Provides information about the current theme.")
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
26 p.set_defaults(sub_func=self._info)
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
27
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
28 p = subparsers.add_parser(
273
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 'override',
747
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
30 help="Copies the current theme to the website for "
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
31 "customization.")
273
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 p.set_defaults(sub_func=self._overrideTheme)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33
480
d5885c6d64bd themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents: 475
diff changeset
34 p = subparsers.add_parser(
747
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
35 'link',
488
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
36 help="Makes a given theme the active one for the current "
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
37 "website by creating a symbolic link to it from the "
747
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
38 "'theme' directory.")
480
d5885c6d64bd themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents: 475
diff changeset
39 p.add_argument(
d5885c6d64bd themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents: 475
diff changeset
40 'theme_dir',
d5885c6d64bd themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents: 475
diff changeset
41 help="The directory of the theme to link.")
747
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
42 p.set_defaults(sub_func=self._linkTheme)
488
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
43
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
44 p = subparsers.add_parser(
747
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
45 'unlink',
488
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
46 help="Removes the currently active theme for the website. "
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
47 "This removes the symbolic link to the theme, if any, or "
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
48 "deletes the theme folder if it was copied locally.")
747
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
49 p.set_defaults(sub_func=self._unlinkTheme)
480
d5885c6d64bd themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents: 475
diff changeset
50
273
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51 def checkedRun(self, ctx):
747
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
52 if ctx.app.root_dir is None:
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
53 raise SiteNotFoundError(theme=ctx.app.theme_site)
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
54
475
c5df200354e8 themes: Fix crash when invoking command with no sub-command.
Ludovic Chabant <ludovic@chabant.com>
parents: 273
diff changeset
55 if not hasattr(ctx.args, 'sub_func'):
488
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
56 ctx.args = ctx.parser.parse_args(['themes', 'info'])
273
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57 ctx.args.sub_func(ctx)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58
488
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
59 def _info(self, ctx):
747
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
60 theme_dir = ctx.app.theme_dir
488
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
61 if not os.path.exists(theme_dir):
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
62 logger.info("Using default theme, from: %s" % ctx.app.theme_dir)
747
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
63 elif theme_dir.startswith(ctx.app.root_dir):
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
64 if os.path.islink(theme_dir):
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
65 target = os.readlink(theme_dir)
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
66 target = os.path.join(os.path.dirname(theme_dir), target)
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
67 logger.info("Using local theme, from: %s" % target)
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
68 else:
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
69 logger.info("Using local theme.")
488
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
70 else:
747
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
71 logger.info("Using theme from: %s" % theme_dir)
273
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72
747
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
73 info_path = os.path.join(theme_dir, THEME_CONFIG_PATH)
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
74 if os.path.exists(info_path):
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
75 info = None
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
76 with open(info_path, 'r', encoding='utf8') as fp:
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
77 theme_cfg = yaml.load(fp.read())
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
78 if isinstance(theme_cfg, dict):
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
79 info = theme_cfg.get('theme')
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
80 if info:
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
81 logger.info("Theme info:")
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
82 for k, v in info.items():
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
83 logger.info(" - %s: %s" % (str(k), str(v)))
273
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 def _overrideTheme(self, ctx):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86 theme_dir = ctx.app.theme_dir
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87 if not theme_dir:
747
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
88 logger.error("There is no theme currently applied.")
273
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89 return 1
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
90
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91 copies = []
747
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
92 app_dir = ctx.app.root_dir
273
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93 for dirpath, dirnames, filenames in os.walk(theme_dir):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
94 rel_dirpath = os.path.relpath(dirpath, theme_dir)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 for name in filenames:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 if (dirpath == theme_dir and
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 name in [THEME_CONFIG_PATH, THEME_INFO_PATH]):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98 continue
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
99 src_path = os.path.join(dirpath, name)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100 dst_path = os.path.join(app_dir, rel_dirpath, name)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101 copies.append((src_path, dst_path))
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
102
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 conflicts = []
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104 for c in copies:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105 if os.path.exists(c[1]):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
106 conflicts.append(c[1])
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
107 if conflicts:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
108 logger.warning("Some website files will be overwritten:")
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
109 for c in conflicts:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
110 logger.warning(os.path.relpath(c, app_dir))
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
111 logger.warning("Are you sure? [Y/n]")
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
112 ans = input()
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
113 if len(ans) > 0 and ans.lower() not in ['y', 'yes']:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
114 return 1
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
115
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
116 for c in copies:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
117 logger.info(os.path.relpath(c[1], app_dir))
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
118 if not os.path.exists(os.path.dirname(c[1])):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
119 os.makedirs(os.path.dirname(c[1]))
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
120 shutil.copy2(c[0], c[1])
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
121
747
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
122 def _linkTheme(self, ctx):
480
d5885c6d64bd themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents: 475
diff changeset
123 if not os.path.isdir(ctx.args.theme_dir):
d5885c6d64bd themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents: 475
diff changeset
124 logger.error("Invalid theme directory: %s" % ctx.args.theme_dir)
d5885c6d64bd themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents: 475
diff changeset
125 return 1
d5885c6d64bd themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents: 475
diff changeset
126
747
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
127 msg = ("A theme already exists, and will be deleted. "
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
128 "Are you sure? [Y/n]")
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
129 self._doUnlinkTheme(ctx.app.root_dir, msg)
480
d5885c6d64bd themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents: 475
diff changeset
130
747
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
131 theme_dir = os.path.join(ctx.app.root_dir, THEME_DIR)
488
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
132 try:
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
133 os.symlink(ctx.args.theme_dir, theme_dir)
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
134 except (NotImplementedError, OSError) as ex:
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
135 if ctx.args.link_only:
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
136 logger.error("Couldn't symlink the theme: %s" % ex)
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
137 return 1
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
138
747
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
139 def _unlinkTheme(self, ctx):
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
140 msg = ("The active theme is local. Are you sure you want "
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
141 "to delete the theme directory? [Y/n]")
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
142 self._doUnlinkTheme(ctx.app.root_dir, msg)
480
d5885c6d64bd themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents: 475
diff changeset
143
747
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
144 def _doUnlinkTheme(self, root_dir, delete_message):
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
145 theme_dir = os.path.join(root_dir, THEME_DIR)
488
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
146
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
147 if os.path.islink(theme_dir):
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
148 logger.debug("Unlinking: %s" % theme_dir)
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
149 os.unlink(theme_dir)
747
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
150 return True
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
151
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
152 if os.path.isdir(theme_dir):
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
153 logger.warning(delete_message)
488
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
154 ans = input()
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
155 if len(ans) > 0 and ans.lower() not in ['y', 'yes']:
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
156 return 1
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
157
747
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
158 logger.debug("Deleting: %s" % theme_dir)
488
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
159 shutil.rmtree(theme_dir)
747
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
160
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
161 return True
488
a00750896316 themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 480
diff changeset
162
747
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
163 return False
5336e146ac8d themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 488
diff changeset
164