Show More
@@ -1,228 +1,232 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | An object for managing IPython profile directories. |
|
4 | 4 | |
|
5 | 5 | Authors: |
|
6 | 6 | |
|
7 | 7 | * Brian Granger |
|
8 | 8 | * Fernando Perez |
|
9 | 9 | * Min RK |
|
10 | 10 | |
|
11 | 11 | """ |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Copyright (C) 2008-2011 The IPython Development Team |
|
15 | 15 | # |
|
16 | 16 | # Distributed under the terms of the BSD License. The full license is in |
|
17 | 17 | # the file COPYING, distributed as part of this software. |
|
18 | 18 | #----------------------------------------------------------------------------- |
|
19 | 19 | |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | # Imports |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | |
|
24 | 24 | import os |
|
25 | 25 | import shutil |
|
26 | 26 | import sys |
|
27 | 27 | |
|
28 | 28 | from IPython.config.configurable import LoggingConfigurable |
|
29 | 29 | from IPython.config.loader import Config |
|
30 | 30 | from IPython.utils.path import get_ipython_package_dir, expand_path |
|
31 | 31 | from IPython.utils.traitlets import List, Unicode, Bool |
|
32 | 32 | |
|
33 | 33 | #----------------------------------------------------------------------------- |
|
34 | 34 | # Classes and functions |
|
35 | 35 | #----------------------------------------------------------------------------- |
|
36 | 36 | |
|
37 | 37 | |
|
38 | 38 | #----------------------------------------------------------------------------- |
|
39 | 39 | # Module errors |
|
40 | 40 | #----------------------------------------------------------------------------- |
|
41 | 41 | |
|
42 | 42 | class ProfileDirError(Exception): |
|
43 | 43 | pass |
|
44 | 44 | |
|
45 | 45 | |
|
46 | 46 | #----------------------------------------------------------------------------- |
|
47 | 47 | # Class for managing profile directories |
|
48 | 48 | #----------------------------------------------------------------------------- |
|
49 | 49 | |
|
50 | 50 | class ProfileDir(LoggingConfigurable): |
|
51 | 51 | """An object to manage the profile directory and its resources. |
|
52 | 52 | |
|
53 | 53 | The profile directory is used by all IPython applications, to manage |
|
54 | 54 | configuration, logging and security. |
|
55 | 55 | |
|
56 | 56 | This object knows how to find, create and manage these directories. This |
|
57 | 57 | should be used by any code that wants to handle profiles. |
|
58 | 58 | """ |
|
59 | 59 | |
|
60 | 60 | security_dir_name = Unicode('security') |
|
61 | 61 | log_dir_name = Unicode('log') |
|
62 | 62 | startup_dir_name = Unicode('startup') |
|
63 | 63 | pid_dir_name = Unicode('pid') |
|
64 | 64 | security_dir = Unicode(u'') |
|
65 | 65 | log_dir = Unicode(u'') |
|
66 | 66 | startup_dir = Unicode(u'') |
|
67 | 67 | pid_dir = Unicode(u'') |
|
68 | 68 | |
|
69 | 69 | location = Unicode(u'', config=True, |
|
70 | 70 | help="""Set the profile location directly. This overrides the logic used by the |
|
71 | 71 | `profile` option.""", |
|
72 | 72 | ) |
|
73 | 73 | |
|
74 | 74 | _location_isset = Bool(False) # flag for detecting multiply set location |
|
75 | 75 | |
|
76 | 76 | def _location_changed(self, name, old, new): |
|
77 | 77 | if self._location_isset: |
|
78 | 78 | raise RuntimeError("Cannot set profile location more than once.") |
|
79 | 79 | self._location_isset = True |
|
80 | 80 | if not os.path.isdir(new): |
|
81 | 81 | os.makedirs(new) |
|
82 | 82 | |
|
83 | 83 | # ensure config files exist: |
|
84 | 84 | self.security_dir = os.path.join(new, self.security_dir_name) |
|
85 | 85 | self.log_dir = os.path.join(new, self.log_dir_name) |
|
86 | 86 | self.startup_dir = os.path.join(new, self.startup_dir_name) |
|
87 | 87 | self.pid_dir = os.path.join(new, self.pid_dir_name) |
|
88 | 88 | self.check_dirs() |
|
89 | 89 | |
|
90 | 90 | def _log_dir_changed(self, name, old, new): |
|
91 | 91 | self.check_log_dir() |
|
92 | 92 | |
|
93 | 93 | def check_log_dir(self): |
|
94 | 94 | if not os.path.isdir(self.log_dir): |
|
95 | 95 | os.mkdir(self.log_dir) |
|
96 | 96 | |
|
97 | 97 | def _startup_dir_changed(self, name, old, new): |
|
98 | 98 | self.check_startup_dir() |
|
99 | 99 | |
|
100 | 100 | def check_startup_dir(self): |
|
101 | 101 | if not os.path.isdir(self.startup_dir): |
|
102 | 102 | os.mkdir(self.startup_dir) |
|
103 | 103 | readme = os.path.join(self.startup_dir, 'README') |
|
104 | 104 | src = os.path.join(get_ipython_package_dir(), u'config', u'profile', u'README_STARTUP') |
|
105 | if not os.path.exists(readme): | |
|
105 | ||
|
106 | if not os.path.exists(src): | |
|
107 | self.log.warn("Could not copy README_STARTUP to startup dir. Source file %s does not exist." % src) | |
|
108 | ||
|
109 | if os.path.exists(src) and not os.path.exists(readme): | |
|
106 | 110 | shutil.copy(src, readme) |
|
107 | 111 | |
|
108 | 112 | def _security_dir_changed(self, name, old, new): |
|
109 | 113 | self.check_security_dir() |
|
110 | 114 | |
|
111 | 115 | def check_security_dir(self): |
|
112 | 116 | if not os.path.isdir(self.security_dir): |
|
113 | 117 | os.mkdir(self.security_dir, 0o700) |
|
114 | 118 | else: |
|
115 | 119 | try: |
|
116 | 120 | os.chmod(self.security_dir, 0o700) |
|
117 | 121 | except OSError: |
|
118 | 122 | self.log.warn("Could not set security dir permissions to private.") |
|
119 | 123 | |
|
120 | 124 | def _pid_dir_changed(self, name, old, new): |
|
121 | 125 | self.check_pid_dir() |
|
122 | 126 | |
|
123 | 127 | def check_pid_dir(self): |
|
124 | 128 | if not os.path.isdir(self.pid_dir): |
|
125 | 129 | os.mkdir(self.pid_dir, 0o700) |
|
126 | 130 | else: |
|
127 | 131 | try: |
|
128 | 132 | os.chmod(self.pid_dir, 0o700) |
|
129 | 133 | except OSError: |
|
130 | 134 | self.log.warn("Could not set pid dir permissions to private.") |
|
131 | 135 | |
|
132 | 136 | def check_dirs(self): |
|
133 | 137 | self.check_security_dir() |
|
134 | 138 | self.check_log_dir() |
|
135 | 139 | self.check_pid_dir() |
|
136 | 140 | self.check_startup_dir() |
|
137 | 141 | |
|
138 | 142 | def copy_config_file(self, config_file, path=None, overwrite=False): |
|
139 | 143 | """Copy a default config file into the active profile directory. |
|
140 | 144 | |
|
141 | 145 | Default configuration files are kept in :mod:`IPython.config.default`. |
|
142 | 146 | This function moves these from that location to the working profile |
|
143 | 147 | directory. |
|
144 | 148 | """ |
|
145 | 149 | dst = os.path.join(self.location, config_file) |
|
146 | 150 | if os.path.isfile(dst) and not overwrite: |
|
147 | 151 | return False |
|
148 | 152 | if path is None: |
|
149 | 153 | path = os.path.join(get_ipython_package_dir(), u'config', u'profile', u'default') |
|
150 | 154 | src = os.path.join(path, config_file) |
|
151 | 155 | shutil.copy(src, dst) |
|
152 | 156 | return True |
|
153 | 157 | |
|
154 | 158 | @classmethod |
|
155 | 159 | def create_profile_dir(cls, profile_dir, config=None): |
|
156 | 160 | """Create a new profile directory given a full path. |
|
157 | 161 | |
|
158 | 162 | Parameters |
|
159 | 163 | ---------- |
|
160 | 164 | profile_dir : str |
|
161 | 165 | The full path to the profile directory. If it does exist, it will |
|
162 | 166 | be used. If not, it will be created. |
|
163 | 167 | """ |
|
164 | 168 | return cls(location=profile_dir, config=config) |
|
165 | 169 | |
|
166 | 170 | @classmethod |
|
167 | 171 | def create_profile_dir_by_name(cls, path, name=u'default', config=None): |
|
168 | 172 | """Create a profile dir by profile name and path. |
|
169 | 173 | |
|
170 | 174 | Parameters |
|
171 | 175 | ---------- |
|
172 | 176 | path : unicode |
|
173 | 177 | The path (directory) to put the profile directory in. |
|
174 | 178 | name : unicode |
|
175 | 179 | The name of the profile. The name of the profile directory will |
|
176 | 180 | be "profile_<profile>". |
|
177 | 181 | """ |
|
178 | 182 | if not os.path.isdir(path): |
|
179 | 183 | raise ProfileDirError('Directory not found: %s' % path) |
|
180 | 184 | profile_dir = os.path.join(path, u'profile_' + name) |
|
181 | 185 | return cls(location=profile_dir, config=config) |
|
182 | 186 | |
|
183 | 187 | @classmethod |
|
184 | 188 | def find_profile_dir_by_name(cls, ipython_dir, name=u'default', config=None): |
|
185 | 189 | """Find an existing profile dir by profile name, return its ProfileDir. |
|
186 | 190 | |
|
187 | 191 | This searches through a sequence of paths for a profile dir. If it |
|
188 | 192 | is not found, a :class:`ProfileDirError` exception will be raised. |
|
189 | 193 | |
|
190 | 194 | The search path algorithm is: |
|
191 | 195 | 1. ``os.getcwdu()`` |
|
192 | 196 | 2. ``ipython_dir`` |
|
193 | 197 | |
|
194 | 198 | Parameters |
|
195 | 199 | ---------- |
|
196 | 200 | ipython_dir : unicode or str |
|
197 | 201 | The IPython directory to use. |
|
198 | 202 | name : unicode or str |
|
199 | 203 | The name of the profile. The name of the profile directory |
|
200 | 204 | will be "profile_<profile>". |
|
201 | 205 | """ |
|
202 | 206 | dirname = u'profile_' + name |
|
203 | 207 | paths = [os.getcwdu(), ipython_dir] |
|
204 | 208 | for p in paths: |
|
205 | 209 | profile_dir = os.path.join(p, dirname) |
|
206 | 210 | if os.path.isdir(profile_dir): |
|
207 | 211 | return cls(location=profile_dir, config=config) |
|
208 | 212 | else: |
|
209 | 213 | raise ProfileDirError('Profile directory not found in paths: %s' % dirname) |
|
210 | 214 | |
|
211 | 215 | @classmethod |
|
212 | 216 | def find_profile_dir(cls, profile_dir, config=None): |
|
213 | 217 | """Find/create a profile dir and return its ProfileDir. |
|
214 | 218 | |
|
215 | 219 | This will create the profile directory if it doesn't exist. |
|
216 | 220 | |
|
217 | 221 | Parameters |
|
218 | 222 | ---------- |
|
219 | 223 | profile_dir : unicode or str |
|
220 | 224 | The path of the profile directory. This is expanded using |
|
221 | 225 | :func:`IPython.utils.genutils.expand_path`. |
|
222 | 226 | """ |
|
223 | 227 | profile_dir = expand_path(profile_dir) |
|
224 | 228 | if not os.path.isdir(profile_dir): |
|
225 | 229 | raise ProfileDirError('Profile directory not found: %s' % profile_dir) |
|
226 | 230 | return cls(location=profile_dir, config=config) |
|
227 | 231 | |
|
228 | 232 |
General Comments 0
You need to be logged in to leave comments.
Login now