Show More
@@ -88,21 +88,54 b' class ProfileDir(LoggingConfigurable):' | |||||
88 | def _log_dir_changed(self, name, old, new): |
|
88 | def _log_dir_changed(self, name, old, new): | |
89 | self.check_log_dir() |
|
89 | self.check_log_dir() | |
90 |
|
90 | |||
|
91 | def _mkdir(self, path, mode=None): | |||
|
92 | """ensure a directory exists at a given path | |||
|
93 | ||||
|
94 | This is a version of os.mkdir, with the following differences: | |||
|
95 | ||||
|
96 | - returns True if it created the directory, False otherwise | |||
|
97 | - ignores EEXIST, protecting against race conditions where | |||
|
98 | the dir may have been created in between the check and | |||
|
99 | the creation | |||
|
100 | - sets permissions if requested and the dir already exists | |||
|
101 | """ | |||
|
102 | if os.path.exists(path): | |||
|
103 | if mode and os.stat(path).st_mode != mode: | |||
|
104 | try: | |||
|
105 | os.chmod(path, mode) | |||
|
106 | except OSError: | |||
|
107 | self.log.warn( | |||
|
108 | "Could not set permissions on %s", | |||
|
109 | path | |||
|
110 | ) | |||
|
111 | return False | |||
|
112 | try: | |||
|
113 | if mode: | |||
|
114 | os.mkdir(path, mode) | |||
|
115 | else: | |||
|
116 | os.mkdir(path) | |||
|
117 | except OSError as e: | |||
|
118 | if e.errno == errno.EEXIST: | |||
|
119 | return False | |||
|
120 | else: | |||
|
121 | raise | |||
|
122 | ||||
|
123 | return True | |||
|
124 | ||||
91 | def check_log_dir(self): |
|
125 | def check_log_dir(self): | |
92 |
|
|
126 | self._mkdir(self.log_dir) | |
93 | os.mkdir(self.log_dir) |
|
|||
94 |
|
127 | |||
95 | def _startup_dir_changed(self, name, old, new): |
|
128 | def _startup_dir_changed(self, name, old, new): | |
96 | self.check_startup_dir() |
|
129 | self.check_startup_dir() | |
97 |
|
130 | |||
98 | def check_startup_dir(self): |
|
131 | def check_startup_dir(self): | |
99 |
|
|
132 | self._mkdir(self.startup_dir) | |
100 | os.mkdir(self.startup_dir) |
|
133 | ||
101 | readme = os.path.join(self.startup_dir, 'README') |
|
134 | readme = os.path.join(self.startup_dir, 'README') | |
102 | src = os.path.join(get_ipython_package_dir(), u'config', u'profile', u'README_STARTUP') |
|
135 | src = os.path.join(get_ipython_package_dir(), u'config', u'profile', u'README_STARTUP') | |
103 |
|
136 | |||
104 | if not os.path.exists(src): |
|
137 | if not os.path.exists(src): | |
105 |
self.log.warn("Could not copy README_STARTUP to startup dir. Source file %s does not exist." |
|
138 | self.log.warn("Could not copy README_STARTUP to startup dir. Source file %s does not exist.", src) | |
106 |
|
139 | |||
107 | if os.path.exists(src) and not os.path.exists(readme): |
|
140 | if os.path.exists(src) and not os.path.exists(readme): | |
108 | shutil.copy(src, readme) |
|
141 | shutil.copy(src, readme) | |
@@ -111,25 +144,13 b' class ProfileDir(LoggingConfigurable):' | |||||
111 | self.check_security_dir() |
|
144 | self.check_security_dir() | |
112 |
|
145 | |||
113 | def check_security_dir(self): |
|
146 | def check_security_dir(self): | |
114 |
|
|
147 | self._mkdir(self.security_dir, 0o40700) | |
115 | os.mkdir(self.security_dir, 0o700) |
|
|||
116 | else: |
|
|||
117 | try: |
|
|||
118 | os.chmod(self.security_dir, 0o700) |
|
|||
119 | except OSError: |
|
|||
120 | self.log.warn("Could not set security dir permissions to private.") |
|
|||
121 |
|
148 | |||
122 | def _pid_dir_changed(self, name, old, new): |
|
149 | def _pid_dir_changed(self, name, old, new): | |
123 | self.check_pid_dir() |
|
150 | self.check_pid_dir() | |
124 |
|
151 | |||
125 | def check_pid_dir(self): |
|
152 | def check_pid_dir(self): | |
126 |
|
|
153 | self._mkdir(self.pid_dir, 0o40700) | |
127 | os.mkdir(self.pid_dir, 0o700) |
|
|||
128 | else: |
|
|||
129 | try: |
|
|||
130 | os.chmod(self.pid_dir, 0o700) |
|
|||
131 | except OSError: |
|
|||
132 | self.log.warn("Could not set pid dir permissions to private.") |
|
|||
133 |
|
154 | |||
134 | def check_dirs(self): |
|
155 | def check_dirs(self): | |
135 | self.check_security_dir() |
|
156 | self.check_security_dir() |
General Comments 0
You need to be logged in to leave comments.
Login now