annotate piecrust/commands/builtin/themes.py @ 482:cce911be024d

serve: Fix a crash when matching taxonomy URLs with incorrect URLs. This is still around code that should have been fixed a while ago but hey, consider this a growing TODO.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 21 Jul 2015 22:42:05 -0700
parents d5885c6d64bd
children a00750896316
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
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 logger = logging.getLogger(__name__)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12
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 class ThemesCommand(ChefCommand):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 def __init__(self):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 super(ThemesCommand, self).__init__()
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 self.name = 'themes'
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 self.description = "Manage the themes for the current website."
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 def setupParser(self, parser, app):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 if app.root_dir is None:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 return
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 subparsers = parser.add_subparsers()
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 p = subparsers.add_parser(
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 'create',
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 help="Create a new theme for the current website.")
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 p.add_argument(
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 '--from-default',
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 action='store_true',
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 help=("Create a new theme by copying the default PieCrust "
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 "theme into the theme directory"))
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 p.add_argument(
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 'theme_name',
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 help=("The name of the theme"))
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 p.set_defaults(sub_func=self._createTheme)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 p = subparsers.add_parser(
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 'override',
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 help="Copies a theme to the website for customization.")
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 p.set_defaults(sub_func=self._overrideTheme)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42
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
43 p = subparsers.add_parser(
d5885c6d64bd themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents: 475
diff changeset
44 'link',
d5885c6d64bd themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents: 475
diff changeset
45 help="Installs a theme as a link to an already existing "
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 "theme on disk.")
d5885c6d64bd themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents: 475
diff changeset
47 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
48 '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
49 help="The directory of the theme to link.")
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 p.set_defaults(sub_func=self._linkTheme)
d5885c6d64bd themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents: 475
diff changeset
51
273
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 def checkedRun(self, ctx):
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'):
c5df200354e8 themes: Fix crash when invoking command with no sub-command.
Ludovic Chabant <ludovic@chabant.com>
parents: 273
diff changeset
54 ctx.parser.parse_args(['themes', '--help'])
c5df200354e8 themes: Fix crash when invoking command with no sub-command.
Ludovic Chabant <ludovic@chabant.com>
parents: 273
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
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 def _createTheme(self, ctx):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 theme_dir = os.path.join(ctx.app.root_dir, THEME_DIR)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 if os.path.exists(theme_dir):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 logger.warning("A theme already exists, and will be overwritten. "
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 "Are you sure? [Y/n]")
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 ans = input()
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64 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
65 return 1
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 shutil.rmtree(theme_dir)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69 try:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70 if ctx.args.from_default:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71 def reporting_copy2(src, dst):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72 rel_dst = os.path.relpath(dst, ctx.app.root_dir)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 logger.info(rel_dst)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74 shutil.copy2(src, dst)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76 default_theme_dir = os.path.join(RESOURCES_DIR, 'theme')
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77 shutil.copytree(default_theme_dir, theme_dir,
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78 copy_function=reporting_copy2)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79 return 0
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81 logger.info("Creating theme directory.")
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
82 os.makedirs(theme_dir)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84 logger.info("Creating theme_config.yml")
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 config_path = os.path.join(theme_dir, THEME_CONFIG_PATH)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86 with open(config_path, 'w', encoding='utf8') as fp:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87 fp.write('')
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89 logger.info("Creating theme_info.yml")
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
90 info_path = os.path.join(theme_dir, THEME_INFO_PATH)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91 with open(info_path, 'w', encoding='utf8') as fp:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 yaml.dump(
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 'name': ctx.args.theme_name or 'My New Theme',
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 'description': "A new PieCrust theme.",
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 'authors': ['Your Name Here <email or twitter>'],
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 'url': 'http://www.example.org'},
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98 fp,
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
99 default_flow_style=False)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100 return 0
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101 except:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
102 logger.error("Error occured, deleting theme directory.")
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 shutil.rmtree(theme_dir)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104 raise
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
106 def _overrideTheme(self, ctx):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
107 app_dir = ctx.app.root_dir
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
108 theme_dir = ctx.app.theme_dir
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
109 if not theme_dir:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
110 logger.error("There is not theme currently applied to override.")
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
111 return 1
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
112
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
113 copies = []
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
114 for dirpath, dirnames, filenames in os.walk(theme_dir):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
115 rel_dirpath = os.path.relpath(dirpath, theme_dir)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
116 for name in filenames:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
117 if (dirpath == theme_dir and
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
118 name in [THEME_CONFIG_PATH, THEME_INFO_PATH]):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
119 continue
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
120 src_path = os.path.join(dirpath, name)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
121 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
122 copies.append((src_path, dst_path))
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
123
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
124 conflicts = []
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
125 for c in copies:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
126 if os.path.exists(c[1]):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
127 conflicts.append(c[1])
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
128 if conflicts:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
129 logger.warning("Some website files will be overwritten:")
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
130 for c in conflicts:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
131 logger.warning(os.path.relpath(c, app_dir))
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
132 logger.warning("Are you sure? [Y/n]")
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
133 ans = input()
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
134 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
135 return 1
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
136
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
137 for c in copies:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
138 logger.info(os.path.relpath(c[1], app_dir))
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
139 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
140 os.makedirs(os.path.dirname(c[1]))
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
141 shutil.copy2(c[0], c[1])
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
142
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 def _linkTheme(self, ctx):
d5885c6d64bd themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents: 475
diff changeset
144 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
145 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
146 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
147
d5885c6d64bd themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents: 475
diff changeset
148 theme_dir = os.path.join(ctx.app.root_dir, 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
149
d5885c6d64bd themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents: 475
diff changeset
150 if os.path.islink(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
151 logger.debug("Unlinking: %s" % 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
152 os.unlink(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
153 elif os.path.isdir(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
154 logger.warning("A theme already exists, and will be overwritten. "
d5885c6d64bd themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents: 475
diff changeset
155 "Are you sure? [Y/n]")
d5885c6d64bd themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents: 475
diff changeset
156 ans = input()
d5885c6d64bd themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents: 475
diff changeset
157 if len(ans) > 0 and ans.lower() not in ['y', 'yes']:
d5885c6d64bd themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents: 475
diff changeset
158 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
159
d5885c6d64bd themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents: 475
diff changeset
160 shutil.rmtree(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
161
d5885c6d64bd themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents: 475
diff changeset
162 os.symlink(ctx.args.theme_dir, 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
163