profiledir.py
225 lines
| 7.8 KiB
| text/x-python
|
PythonLexer
MinRK
|
r4024 | # encoding: utf-8 | ||
MinRK
|
r16486 | """An object for managing IPython profile directories.""" | ||
MinRK
|
r4024 | |||
MinRK
|
r16486 | # Copyright (c) IPython Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||||
MinRK
|
r4024 | |||
import os | ||||
import shutil | ||||
Paul Ivanov
|
r12266 | import errno | ||
Matthias Bussonnier
|
r25972 | from pathlib import Path | ||
MinRK
|
r4024 | |||
Min RK
|
r21253 | from traitlets.config.configurable import LoggingConfigurable | ||
Srinivas Reddy Thatiparthy
|
r25227 | from ..paths import get_ipython_package_dir | ||
from ..utils.path import expand_path, ensure_dir_exists | ||||
Min RK
|
r22340 | from traitlets import Unicode, Bool, observe | ||
MinRK
|
r4024 | |||
#----------------------------------------------------------------------------- | ||||
# Module errors | ||||
#----------------------------------------------------------------------------- | ||||
class ProfileDirError(Exception): | ||||
pass | ||||
#----------------------------------------------------------------------------- | ||||
# Class for managing profile directories | ||||
#----------------------------------------------------------------------------- | ||||
MinRK
|
r5197 | class ProfileDir(LoggingConfigurable): | ||
MinRK
|
r4024 | """An object to manage the profile directory and its resources. | ||
The profile directory is used by all IPython applications, to manage | ||||
configuration, logging and security. | ||||
This object knows how to find, create and manage these directories. This | ||||
should be used by any code that wants to handle profiles. | ||||
""" | ||||
security_dir_name = Unicode('security') | ||||
log_dir_name = Unicode('log') | ||||
MinRK
|
r5246 | startup_dir_name = Unicode('startup') | ||
MinRK
|
r4024 | pid_dir_name = Unicode('pid') | ||
MinRK
|
r12813 | static_dir_name = Unicode('static') | ||
MinRK
|
r4024 | security_dir = Unicode(u'') | ||
log_dir = Unicode(u'') | ||||
MinRK
|
r5246 | startup_dir = Unicode(u'') | ||
MinRK
|
r4024 | pid_dir = Unicode(u'') | ||
MinRK
|
r12813 | static_dir = Unicode(u'') | ||
MinRK
|
r4024 | |||
Min RK
|
r22340 | location = Unicode(u'', | ||
MinRK
|
r4024 | help="""Set the profile location directly. This overrides the logic used by the | ||
`profile` option.""", | ||||
Min RK
|
r22340 | ).tag(config=True) | ||
MinRK
|
r4024 | |||
_location_isset = Bool(False) # flag for detecting multiply set location | ||||
Min RK
|
r22340 | @observe('location') | ||
def _location_changed(self, change): | ||||
MinRK
|
r4024 | if self._location_isset: | ||
raise RuntimeError("Cannot set profile location more than once.") | ||||
self._location_isset = True | ||||
Min RK
|
r22340 | new = change['new'] | ||
MinRK
|
r16486 | ensure_dir_exists(new) | ||
James Porter
|
r13981 | |||
MinRK
|
r4024 | # ensure config files exist: | ||
self.security_dir = os.path.join(new, self.security_dir_name) | ||||
self.log_dir = os.path.join(new, self.log_dir_name) | ||||
MinRK
|
r5246 | self.startup_dir = os.path.join(new, self.startup_dir_name) | ||
MinRK
|
r4024 | self.pid_dir = os.path.join(new, self.pid_dir_name) | ||
MinRK
|
r12813 | self.static_dir = os.path.join(new, self.static_dir_name) | ||
MinRK
|
r4024 | self.check_dirs() | ||
Min RK
|
r22340 | |||
MinRK
|
r10351 | def _mkdir(self, path, mode=None): | ||
"""ensure a directory exists at a given path | ||||
James Porter
|
r13981 | |||
MinRK
|
r10351 | This is a version of os.mkdir, with the following differences: | ||
James Porter
|
r13981 | |||
MinRK
|
r10351 | - returns True if it created the directory, False otherwise | ||
- ignores EEXIST, protecting against race conditions where | ||||
the dir may have been created in between the check and | ||||
the creation | ||||
- sets permissions if requested and the dir already exists | ||||
""" | ||||
if os.path.exists(path): | ||||
if mode and os.stat(path).st_mode != mode: | ||||
try: | ||||
os.chmod(path, mode) | ||||
except OSError: | ||||
Pierre Gerold
|
r21888 | self.log.warning( | ||
MinRK
|
r10351 | "Could not set permissions on %s", | ||
path | ||||
) | ||||
return False | ||||
try: | ||||
if mode: | ||||
os.mkdir(path, mode) | ||||
else: | ||||
os.mkdir(path) | ||||
except OSError as e: | ||||
if e.errno == errno.EEXIST: | ||||
return False | ||||
else: | ||||
raise | ||||
James Porter
|
r13981 | |||
MinRK
|
r10351 | return True | ||
Min RK
|
r22340 | |||
@observe('log_dir') | ||||
def check_log_dir(self, change=None): | ||||
MinRK
|
r10351 | self._mkdir(self.log_dir) | ||
Min RK
|
r22340 | |||
@observe('startup_dir') | ||||
def check_startup_dir(self, change=None): | ||||
MinRK
|
r10351 | self._mkdir(self.startup_dir) | ||
MinRK
|
r5247 | readme = os.path.join(self.startup_dir, 'README') | ||
Min RK
|
r20870 | src = os.path.join(get_ipython_package_dir(), u'core', u'profile', u'README_STARTUP') | ||
Jussi Sainio
|
r10264 | |||
if not os.path.exists(src): | ||||
Matthias Bussonnier
|
r21937 | self.log.warning("Could not copy README_STARTUP to startup dir. Source file %s does not exist.", src) | ||
Jussi Sainio
|
r10264 | |||
if os.path.exists(src) and not os.path.exists(readme): | ||||
MinRK
|
r5247 | shutil.copy(src, readme) | ||
MinRK
|
r5246 | |||
Min RK
|
r22340 | @observe('security_dir') | ||
def check_security_dir(self, change=None): | ||||
MinRK
|
r10351 | self._mkdir(self.security_dir, 0o40700) | ||
MinRK
|
r4024 | |||
Min RK
|
r22340 | @observe('pid_dir') | ||
def check_pid_dir(self, change=None): | ||||
MinRK
|
r10351 | self._mkdir(self.pid_dir, 0o40700) | ||
MinRK
|
r4024 | |||
def check_dirs(self): | ||||
self.check_security_dir() | ||||
self.check_log_dir() | ||||
self.check_pid_dir() | ||||
MinRK
|
r5247 | self.check_startup_dir() | ||
MinRK
|
r4024 | |||
Matthias Bussonnier
|
r25972 | def copy_config_file(self, config_file: str, path: Path, overwrite=False) -> bool: | ||
MinRK
|
r4024 | """Copy a default config file into the active profile directory. | ||
Min RK
|
r21253 | Default configuration files are kept in :mod:`IPython.core.profile`. | ||
MinRK
|
r4024 | This function moves these from that location to the working profile | ||
directory. | ||||
""" | ||||
Matthias Bussonnier
|
r25972 | dst = Path(os.path.join(self.location, config_file)) | ||
if dst.exists() and not overwrite: | ||||
MinRK
|
r4122 | return False | ||
MinRK
|
r4024 | if path is None: | ||
Min RK
|
r20870 | path = os.path.join(get_ipython_package_dir(), u'core', u'profile', u'default') | ||
Matthias Bussonnier
|
r25972 | assert isinstance(path, Path) | ||
src = path / config_file | ||||
MinRK
|
r4024 | shutil.copy(src, dst) | ||
MinRK
|
r4122 | return True | ||
MinRK
|
r4024 | |||
@classmethod | ||||
def create_profile_dir(cls, profile_dir, config=None): | ||||
"""Create a new profile directory given a full path. | ||||
Parameters | ||||
---------- | ||||
profile_dir : str | ||||
The full path to the profile directory. If it does exist, it will | ||||
be used. If not, it will be created. | ||||
""" | ||||
return cls(location=profile_dir, config=config) | ||||
@classmethod | ||||
def create_profile_dir_by_name(cls, path, name=u'default', config=None): | ||||
"""Create a profile dir by profile name and path. | ||||
Parameters | ||||
---------- | ||||
path : unicode | ||||
The path (directory) to put the profile directory in. | ||||
name : unicode | ||||
The name of the profile. The name of the profile directory will | ||||
be "profile_<profile>". | ||||
""" | ||||
if not os.path.isdir(path): | ||||
raise ProfileDirError('Directory not found: %s' % path) | ||||
profile_dir = os.path.join(path, u'profile_' + name) | ||||
return cls(location=profile_dir, config=config) | ||||
@classmethod | ||||
def find_profile_dir_by_name(cls, ipython_dir, name=u'default', config=None): | ||||
"""Find an existing profile dir by profile name, return its ProfileDir. | ||||
This searches through a sequence of paths for a profile dir. If it | ||||
is not found, a :class:`ProfileDirError` exception will be raised. | ||||
The search path algorithm is: | ||||
Matthias Bussonnier
|
r27464 | 1. ``os.getcwd()`` # removed for security reason. | ||
MinRK
|
r4024 | 2. ``ipython_dir`` | ||
Parameters | ||||
---------- | ||||
ipython_dir : unicode or str | ||||
The IPython directory to use. | ||||
name : unicode or str | ||||
The name of the profile. The name of the profile directory | ||||
will be "profile_<profile>". | ||||
""" | ||||
dirname = u'profile_' + name | ||||
Matthias Bussonnier
|
r27464 | paths = [ipython_dir] | ||
MinRK
|
r4024 | for p in paths: | ||
profile_dir = os.path.join(p, dirname) | ||||
if os.path.isdir(profile_dir): | ||||
return cls(location=profile_dir, config=config) | ||||
else: | ||||
raise ProfileDirError('Profile directory not found in paths: %s' % dirname) | ||||
@classmethod | ||||
def find_profile_dir(cls, profile_dir, config=None): | ||||
"""Find/create a profile dir and return its ProfileDir. | ||||
This will create the profile directory if it doesn't exist. | ||||
Parameters | ||||
---------- | ||||
profile_dir : unicode or str | ||||
Matthias Bussonnier
|
r21439 | The path of the profile directory. | ||
MinRK
|
r4024 | """ | ||
profile_dir = expand_path(profile_dir) | ||||
if not os.path.isdir(profile_dir): | ||||
raise ProfileDirError('Profile directory not found: %s' % profile_dir) | ||||
return cls(location=profile_dir, config=config) | ||||