Mercurial > piecrust2
annotate piecrust/commands/builtin/themes.py @ 1188:a7c43131d871
bake: Fix file write flushing problem with Python 3.8+
Writing the cache files fails in Python 3.8 because it looks like flushing
behaviour has changed. We need to explicitly flush. And even then, in very
rare occurrences, it looks like it can still run into racing conditions,
so we do a very hacky and ugly "retry" loop when fetching cached data :(
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 15 Jun 2021 22:36:23 -0700 |
parents | 727110ea112a |
children |
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 logging |
879
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
4 from piecrust import THEME_DIR, THEME_CONFIG_PATH, THEME_INFO_PATH |
273
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 from piecrust.commands.base import ChefCommand |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 logger = logging.getLogger(__name__) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 |
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 class ThemesCommand(ChefCommand): |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 def __init__(self): |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 super(ThemesCommand, self).__init__() |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 self.name = 'themes' |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 self.description = "Manage the themes for the current website." |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 def setupParser(self, parser, app): |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
18 subparsers = parser.add_subparsers() |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 p = subparsers.add_parser( |
879
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
20 'info', |
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
21 help="Provides information about the current theme.") |
488
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
22 p.set_defaults(sub_func=self._info) |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
23 |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
24 p = subparsers.add_parser( |
879
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
25 'override', |
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
26 help="Copies the current theme to the website for " |
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
27 "customization.") |
273
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
28 p.set_defaults(sub_func=self._overrideTheme) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
29 |
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
|
30 p = subparsers.add_parser( |
879
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
31 'link', |
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
32 help="Makes a given theme the active one for the current " |
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
33 "website by creating a symbolic link to it from the " |
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
34 "'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
|
35 p.add_argument( |
879
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
36 'theme_dir', |
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
37 help="The directory of the theme to link.") |
747
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
38 p.set_defaults(sub_func=self._linkTheme) |
488
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
39 |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
40 p = subparsers.add_parser( |
879
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
41 'unlink', |
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
42 help="Removes the currently active theme for the website. " |
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
43 "This removes the symbolic link to the theme, if any, or " |
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
44 "deletes the theme folder if it was copied locally.") |
747
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
45 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
|
46 |
273
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 def checkedRun(self, ctx): |
879
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
48 from piecrust.pathutil import SiteNotFoundError |
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
49 |
747
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
50 if ctx.app.root_dir is None: |
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
51 raise SiteNotFoundError(theme=ctx.app.theme_site) |
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
52 |
475
c5df200354e8
themes: Fix crash when invoking command with no sub-command.
Ludovic Chabant <ludovic@chabant.com>
parents:
273
diff
changeset
|
53 if not hasattr(ctx.args, 'sub_func'): |
765
549e21789ad9
themes: No parameters shoudl make the help text show up.
Ludovic Chabant <ludovic@chabant.com>
parents:
747
diff
changeset
|
54 ctx.parser.parse_args(['themes', '--help']) |
549e21789ad9
themes: No parameters shoudl make the help text show up.
Ludovic Chabant <ludovic@chabant.com>
parents:
747
diff
changeset
|
55 return |
273
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
56 ctx.args.sub_func(ctx) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
57 |
488
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
58 def _info(self, ctx): |
879
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
59 import yaml |
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
60 |
747
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
61 theme_dir = ctx.app.theme_dir |
488
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
62 if not os.path.exists(theme_dir): |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
63 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
|
64 elif theme_dir.startswith(ctx.app.root_dir): |
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
65 if os.path.islink(theme_dir): |
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
66 target = os.readlink(theme_dir) |
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
67 target = os.path.join(os.path.dirname(theme_dir), target) |
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
68 logger.info("Using local theme, from: %s" % target) |
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
69 else: |
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
70 logger.info("Using local theme.") |
488
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
71 else: |
747
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
72 logger.info("Using theme from: %s" % theme_dir) |
273
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
73 |
747
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
74 info_path = os.path.join(theme_dir, THEME_CONFIG_PATH) |
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
75 if os.path.exists(info_path): |
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
76 info = None |
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
77 with open(info_path, 'r', encoding='utf8') as fp: |
1164
727110ea112a
core: Remove more YAML deprecation warnings.
Ludovic Chabant <ludovic@chabant.com>
parents:
891
diff
changeset
|
78 theme_cfg = yaml.safe_load(fp.read()) |
747
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
79 if isinstance(theme_cfg, dict): |
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
80 info = theme_cfg.get('theme') |
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
81 if info: |
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
82 logger.info("Theme info:") |
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
83 for k, v in info.items(): |
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
84 logger.info(" - %s: %s" % (str(k), str(v))) |
273
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
85 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
86 def _overrideTheme(self, ctx): |
879
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
87 import shutil |
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
88 |
273
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
89 theme_dir = ctx.app.theme_dir |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
90 if not theme_dir: |
747
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
91 logger.error("There is no theme currently applied.") |
273
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
92 return 1 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
93 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
94 copies = [] |
747
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
95 app_dir = ctx.app.root_dir |
273
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
96 for dirpath, dirnames, filenames in os.walk(theme_dir): |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
97 rel_dirpath = os.path.relpath(dirpath, theme_dir) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
98 for name in filenames: |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
99 if (dirpath == theme_dir and |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
100 name in [THEME_CONFIG_PATH, THEME_INFO_PATH]): |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
101 continue |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
102 src_path = os.path.join(dirpath, name) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
103 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
|
104 copies.append((src_path, dst_path)) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
105 |
891
f13d618cfec6
themes: Allow keeping local overrides when copying a theme locally.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
106 conflicts = set() |
273
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
107 for c in copies: |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
108 if os.path.exists(c[1]): |
891
f13d618cfec6
themes: Allow keeping local overrides when copying a theme locally.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
109 conflicts.add(c[1]) |
273
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
110 if conflicts: |
891
f13d618cfec6
themes: Allow keeping local overrides when copying a theme locally.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
111 logger.warning("Some website files override theme files:") |
273
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
112 for c in conflicts: |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
113 logger.warning(os.path.relpath(c, app_dir)) |
891
f13d618cfec6
themes: Allow keeping local overrides when copying a theme locally.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
114 logger.warning("") |
f13d618cfec6
themes: Allow keeping local overrides when copying a theme locally.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
115 logger.warning("The local website files will be preserved, and " |
f13d618cfec6
themes: Allow keeping local overrides when copying a theme locally.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
116 "the conflicting theme files won't be copied " |
f13d618cfec6
themes: Allow keeping local overrides when copying a theme locally.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
117 "locally.") |
273
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
118 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
119 for c in copies: |
891
f13d618cfec6
themes: Allow keeping local overrides when copying a theme locally.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
120 if not c[1] in conflicts: |
f13d618cfec6
themes: Allow keeping local overrides when copying a theme locally.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
121 logger.info(os.path.relpath(c[1], app_dir)) |
f13d618cfec6
themes: Allow keeping local overrides when copying a theme locally.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
122 os.makedirs(os.path.dirname(c[1]), exist_ok=True) |
f13d618cfec6
themes: Allow keeping local overrides when copying a theme locally.
Ludovic Chabant <ludovic@chabant.com>
parents:
879
diff
changeset
|
123 shutil.copy2(c[0], c[1]) |
273
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
124 |
747
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
125 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
|
126 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
|
127 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
|
128 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
|
129 |
747
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
130 msg = ("A theme already exists, and will be deleted. " |
879
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
131 "Are you sure? [Y/n]") |
747
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
132 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
|
133 |
747
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
134 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
|
135 try: |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
136 os.symlink(ctx.args.theme_dir, theme_dir) |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
137 except (NotImplementedError, OSError) as ex: |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
138 if ctx.args.link_only: |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
139 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
|
140 return 1 |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
141 |
747
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
142 def _unlinkTheme(self, ctx): |
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
143 msg = ("The active theme is local. Are you sure you want " |
879
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
144 "to delete the theme directory? [Y/n]") |
747
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
145 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
|
146 |
747
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
147 def _doUnlinkTheme(self, root_dir, delete_message): |
879
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
148 import shutil |
58ae026b4c31
chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents:
765
diff
changeset
|
149 |
747
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
150 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
|
151 |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
152 if os.path.islink(theme_dir): |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
153 logger.debug("Unlinking: %s" % theme_dir) |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
154 os.unlink(theme_dir) |
747
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
155 return True |
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
156 |
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
157 if os.path.isdir(theme_dir): |
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
158 logger.warning(delete_message) |
488
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
159 ans = input() |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
160 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
|
161 return 1 |
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 logger.debug("Deleting: %s" % theme_dir) |
488
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
164 shutil.rmtree(theme_dir) |
747
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
165 |
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
166 return True |
488
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
167 |
747
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
168 return False |
5336e146ac8d
themes: Simplify `themes` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
488
diff
changeset
|
169 |