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