| 1 |
# encoding: utf-8 |
| 2 |
""" |
| 3 |
An application for managing IPython profiles. |
| 4 |
|
| 5 |
To be invoked as the `ipython profile` subcommand. |
| 6 |
|
| 7 |
Authors: |
| 8 |
|
| 9 |
* Min RK |
| 10 |
|
| 11 |
""" |
| 12 |
|
| 13 |
#----------------------------------------------------------------------------- |
| 14 |
# Copyright (C) 2008 The IPython Development Team |
| 15 |
# |
| 16 |
# Distributed under the terms of the BSD License. The full license is in |
| 17 |
# the file COPYING, distributed as part of this software. |
| 18 |
#----------------------------------------------------------------------------- |
| 19 |
|
| 20 |
#----------------------------------------------------------------------------- |
| 21 |
# Imports |
| 22 |
#----------------------------------------------------------------------------- |
| 23 |
|
| 24 |
import os |
| 25 |
|
| 26 |
from traitlets.config.application import Application |
| 27 |
from IPython.core.application import ( |
| 28 |
BaseIPythonApplication, base_flags |
| 29 |
) |
| 30 |
from IPython.core.profiledir import ProfileDir |
| 31 |
from IPython.utils.importstring import import_item |
| 32 |
from IPython.paths import get_ipython_dir, get_ipython_package_dir |
| 33 |
from traitlets import Unicode, Bool, Dict, observe |
| 34 |
|
| 35 |
#----------------------------------------------------------------------------- |
| 36 |
# Constants |
| 37 |
#----------------------------------------------------------------------------- |
| 38 |
|
| 39 |
create_help = """Create an IPython profile by name |
| 40 |
|
| 41 |
Create an ipython profile directory by its name or |
| 42 |
profile directory path. Profile directories contain |
| 43 |
configuration, log and security related files and are named |
| 44 |
using the convention 'profile_<name>'. By default they are |
| 45 |
located in your ipython directory. Once created, you will |
| 46 |
can edit the configuration files in the profile |
| 47 |
directory to configure IPython. Most users will create a |
| 48 |
profile directory by name, |
| 49 |
`ipython profile create myprofile`, which will put the directory |
| 50 |
in `<ipython_dir>/profile_myprofile`. |
| 51 |
""" |
| 52 |
list_help = """List available IPython profiles |
| 53 |
|
| 54 |
List all available profiles, by profile location, that can |
| 55 |
be found in the current working directly or in the ipython |
| 56 |
directory. Profile directories are named using the convention |
| 57 |
'profile_<profile>'. |
| 58 |
""" |
| 59 |
profile_help = """Manage IPython profiles |
| 60 |
|
| 61 |
Profile directories contain |
| 62 |
configuration, log and security related files and are named |
| 63 |
using the convention 'profile_<name>'. By default they are |
| 64 |
located in your ipython directory. You can create profiles |
| 65 |
with `ipython profile create <name>`, or see the profiles you |
| 66 |
already have with `ipython profile list` |
| 67 |
|
| 68 |
To get started configuring IPython, simply do: |
| 69 |
|
| 70 |
$> ipython profile create |
| 71 |
|
| 72 |
and IPython will create the default profile in <ipython_dir>/profile_default, |
| 73 |
where you can edit ipython_config.py to start configuring IPython. |
| 74 |
|
| 75 |
""" |
| 76 |
|
| 77 |
_list_examples = "ipython profile list # list all profiles" |
| 78 |
|
| 79 |
_create_examples = """ |
| 80 |
ipython profile create foo # create profile foo w/ default config files |
| 81 |
ipython profile create foo --reset # restage default config files over current |
| 82 |
ipython profile create foo --parallel # also stage parallel config files |
| 83 |
""" |
| 84 |
|
| 85 |
_main_examples = """ |
| 86 |
ipython profile create -h # show the help string for the create subcommand |
| 87 |
ipython profile list -h # show the help string for the list subcommand |
| 88 |
|
| 89 |
ipython locate profile foo # print the path to the directory for profile 'foo' |
| 90 |
""" |
| 91 |
|
| 92 |
#----------------------------------------------------------------------------- |
| 93 |
# Profile Application Class (for `ipython profile` subcommand) |
| 94 |
#----------------------------------------------------------------------------- |
| 95 |
|
| 96 |
|
| 97 |
def list_profiles_in(path): |
| 98 |
"""list profiles in a given root directory""" |
| 99 |
profiles = [] |
| 100 |
|
| 101 |
# for python 3.6+ rewrite to: with os.scandir(path) as dirlist: |
| 102 |
files = os.scandir(path) |
| 103 |
for f in files: |
| 104 |
if f.is_dir() and f.name.startswith('profile_'): |
| 105 |
profiles.append(f.name.split('_', 1)[-1]) |
| 106 |
return profiles |
| 107 |
|
| 108 |
|
| 109 |
def list_bundled_profiles(): |
| 110 |
"""list profiles that are bundled with IPython.""" |
| 111 |
path = os.path.join(get_ipython_package_dir(), u'core', u'profile') |
| 112 |
profiles = [] |
| 113 |
|
| 114 |
# for python 3.6+ rewrite to: with os.scandir(path) as dirlist: |
| 115 |
files = os.scandir(path) |
| 116 |
for profile in files: |
| 117 |
if profile.is_dir() and profile.name != "__pycache__": |
| 118 |
profiles.append(profile.name) |
| 119 |
return profiles |
| 120 |
|
| 121 |
|
| 122 |
class ProfileLocate(BaseIPythonApplication): |
| 123 |
description = """print the path to an IPython profile dir""" |
| 124 |
|
| 125 |
def parse_command_line(self, argv=None): |
| 126 |
super(ProfileLocate, self).parse_command_line(argv) |
| 127 |
if self.extra_args: |
| 128 |
self.profile = self.extra_args[0] |
| 129 |
|
| 130 |
def start(self): |
| 131 |
print(self.profile_dir.location) |
| 132 |
|
| 133 |
|
| 134 |
class ProfileList(Application): |
| 135 |
name = u'ipython-profile' |
| 136 |
description = list_help |
| 137 |
examples = _list_examples |
| 138 |
|
| 139 |
aliases = Dict({ |
| 140 |
'ipython-dir' : 'ProfileList.ipython_dir', |
| 141 |
'log-level' : 'Application.log_level', |
| 142 |
}) |
| 143 |
flags = Dict(dict( |
| 144 |
debug = ({'Application' : {'log_level' : 0}}, |
| 145 |
"Set Application.log_level to 0, maximizing log output." |
| 146 |
) |
| 147 |
)) |
| 148 |
|
| 149 |
ipython_dir = Unicode(get_ipython_dir(), |
| 150 |
help=""" |
| 151 |
The name of the IPython directory. This directory is used for logging |
| 152 |
configuration (through profiles), history storage, etc. The default |
| 153 |
is usually $HOME/.ipython. This options can also be specified through |
| 154 |
the environment variable IPYTHONDIR. |
| 155 |
""" |
| 156 |
).tag(config=True) |
| 157 |
|
| 158 |
|
| 159 |
def _print_profiles(self, profiles): |
| 160 |
"""print list of profiles, indented.""" |
| 161 |
for profile in profiles: |
| 162 |
print(' %s' % profile) |
| 163 |
|
| 164 |
def list_profile_dirs(self): |
| 165 |
profiles = list_bundled_profiles() |
| 166 |
if profiles: |
| 167 |
print() |
| 168 |
print("Available profiles in IPython:") |
| 169 |
self._print_profiles(profiles) |
| 170 |
print() |
| 171 |
print(" The first request for a bundled profile will copy it") |
| 172 |
print(" into your IPython directory (%s)," % self.ipython_dir) |
| 173 |
print(" where you can customize it.") |
| 174 |
|
| 175 |
profiles = list_profiles_in(self.ipython_dir) |
| 176 |
if profiles: |
| 177 |
print() |
| 178 |
print("Available profiles in %s:" % self.ipython_dir) |
| 179 |
self._print_profiles(profiles) |
| 180 |
|
| 181 |
profiles = list_profiles_in(os.getcwd()) |
| 182 |
if profiles: |
| 183 |
print() |
| 184 |
print( |
| 185 |
"Profiles from CWD have been removed for security reason, see CVE-2022-21699:" |
| 186 |
) |
| 187 |
|
| 188 |
print() |
| 189 |
print("To use any of the above profiles, start IPython with:") |
| 190 |
print(" ipython --profile=<name>") |
| 191 |
print() |
| 192 |
|
| 193 |
def start(self): |
| 194 |
self.list_profile_dirs() |
| 195 |
|
| 196 |
|
| 197 |
create_flags = {} |
| 198 |
create_flags.update(base_flags) |
| 199 |
# don't include '--init' flag, which implies running profile create in other apps |
| 200 |
create_flags.pop('init') |
| 201 |
create_flags['reset'] = ({'ProfileCreate': {'overwrite' : True}}, |
| 202 |
"reset config files in this profile to the defaults.") |
| 203 |
create_flags['parallel'] = ({'ProfileCreate': {'parallel' : True}}, |
| 204 |
"Include the config files for parallel " |
| 205 |
"computing apps (ipengine, ipcontroller, etc.)") |
| 206 |
|
| 207 |
|
| 208 |
class ProfileCreate(BaseIPythonApplication): |
| 209 |
name = u'ipython-profile' |
| 210 |
description = create_help |
| 211 |
examples = _create_examples |
| 212 |
auto_create = Bool(True).tag(config=True) |
| 213 |
def _log_format_default(self): |
| 214 |
return "[%(name)s] %(message)s" |
| 215 |
|
| 216 |
def _copy_config_files_default(self): |
| 217 |
return True |
| 218 |
|
| 219 |
parallel = Bool(False, |
| 220 |
help="whether to include parallel computing config files" |
| 221 |
).tag(config=True) |
| 222 |
|
| 223 |
@observe('parallel') |
| 224 |
def _parallel_changed(self, change): |
| 225 |
parallel_files = [ 'ipcontroller_config.py', |
| 226 |
'ipengine_config.py', |
| 227 |
'ipcluster_config.py' |
| 228 |
] |
| 229 |
if change['new']: |
| 230 |
for cf in parallel_files: |
| 231 |
self.config_files.append(cf) |
| 232 |
else: |
| 233 |
for cf in parallel_files: |
| 234 |
if cf in self.config_files: |
| 235 |
self.config_files.remove(cf) |
| 236 |
|
| 237 |
def parse_command_line(self, argv): |
| 238 |
super(ProfileCreate, self).parse_command_line(argv) |
| 239 |
# accept positional arg as profile name |
| 240 |
if self.extra_args: |
| 241 |
self.profile = self.extra_args[0] |
| 242 |
|
| 243 |
flags = Dict(create_flags) |
| 244 |
|
| 245 |
classes = [ProfileDir] |
| 246 |
|
| 247 |
def _import_app(self, app_path): |
| 248 |
"""import an app class""" |
| 249 |
app = None |
| 250 |
name = app_path.rsplit('.', 1)[-1] |
| 251 |
try: |
| 252 |
app = import_item(app_path) |
| 253 |
except ImportError: |
| 254 |
self.log.info("Couldn't import %s, config file will be excluded", name) |
| 255 |
except Exception: |
| 256 |
self.log.warning('Unexpected error importing %s', name, exc_info=True) |
| 257 |
return app |
| 258 |
|
| 259 |
def init_config_files(self): |
| 260 |
super(ProfileCreate, self).init_config_files() |
| 261 |
# use local imports, since these classes may import from here |
| 262 |
from IPython.terminal.ipapp import TerminalIPythonApp |
| 263 |
apps = [TerminalIPythonApp] |
| 264 |
for app_path in ( |
| 265 |
'ipykernel.kernelapp.IPKernelApp', |
| 266 |
): |
| 267 |
app = self._import_app(app_path) |
| 268 |
if app is not None: |
| 269 |
apps.append(app) |
| 270 |
if self.parallel: |
| 271 |
from ipyparallel.apps.ipcontrollerapp import IPControllerApp |
| 272 |
from ipyparallel.apps.ipengineapp import IPEngineApp |
| 273 |
from ipyparallel.apps.ipclusterapp import IPClusterStart |
| 274 |
apps.extend([ |
| 275 |
IPControllerApp, |
| 276 |
IPEngineApp, |
| 277 |
IPClusterStart, |
| 278 |
]) |
| 279 |
for App in apps: |
| 280 |
app = App() |
| 281 |
app.config.update(self.config) |
| 282 |
app.log = self.log |
| 283 |
app.overwrite = self.overwrite |
| 284 |
app.copy_config_files=True |
| 285 |
app.ipython_dir=self.ipython_dir |
| 286 |
app.profile_dir=self.profile_dir |
| 287 |
app.init_config_files() |
| 288 |
|
| 289 |
def stage_default_config_file(self): |
| 290 |
pass |
| 291 |
|
| 292 |
|
| 293 |
class ProfileApp(Application): |
| 294 |
name = u'ipython profile' |
| 295 |
description = profile_help |
| 296 |
examples = _main_examples |
| 297 |
|
| 298 |
subcommands = Dict(dict( |
| 299 |
create = (ProfileCreate, ProfileCreate.description.splitlines()[0]), |
| 300 |
list = (ProfileList, ProfileList.description.splitlines()[0]), |
| 301 |
locate = (ProfileLocate, ProfileLocate.description.splitlines()[0]), |
| 302 |
)) |
| 303 |
|
| 304 |
def start(self): |
| 305 |
if self.subapp is None: |
| 306 |
print( |
| 307 |
"No subcommand specified. Must specify one of: " |
| 308 |
+ ", ".join(map(repr, self.subcommands)) |
| 309 |
+ ".\n" |
| 310 |
) |
| 311 |
self.print_description() |
| 312 |
self.print_subcommands() |
| 313 |
self.exit(1) |
| 314 |
else: |
| 315 |
return self.subapp.start() |