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