Skip to content

Commit f6aaab3

Browse files
committed
Refactor to mixin
1 parent 23290a8 commit f6aaab3

File tree

1 file changed

+22
-59
lines changed

1 file changed

+22
-59
lines changed

readthedocs/doc_builder/environments.py

Lines changed: 22 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def pre_run_command(self):
292292
The command that will be executed can be accessed as
293293
the last element of ``self.commands``.
294294
"""
295-
pass
295+
super(BaseEnvironment, self).pre_run_command(kwargs)
296296

297297
def post_run_command(self):
298298
"""
@@ -301,7 +301,7 @@ def post_run_command(self):
301301
The command that was executed can be accessed as
302302
the last element of ``self.commands``.
303303
"""
304-
pass
304+
super(BaseEnvironment, self).post_run_command()
305305

306306
def run(self, *cmd, **kwargs):
307307
"""Shortcut to run command from environment."""
@@ -315,6 +315,7 @@ def run_command_class(self, cls, cmd, warn_only=False, **kwargs):
315315
:param cmd: command (as a list) to execute in this environment
316316
:param warn_only: don't raise an exception on command failure
317317
"""
318+
self.pre_run_command(kwargs)
318319
# Remove PATH from env, and set it to bin_path if it isn't passed in
319320
env_path = self.environment.pop('BIN_PATH', None)
320321
if 'bin_path' not in kwargs and env_path:
@@ -326,7 +327,6 @@ def run_command_class(self, cls, cmd, warn_only=False, **kwargs):
326327
# ``*BuildEnvironment``
327328
build_cmd = cls(cmd, **kwargs)
328329
self.commands.append(build_cmd)
329-
self.pre_run_command()
330330
build_cmd.run()
331331
self.post_run_command()
332332

@@ -347,10 +347,21 @@ def run_command_class(self, cls, cmd, warn_only=False, **kwargs):
347347
return build_cmd
348348

349349

350-
class LocalEnvironment(BaseEnvironment):
350+
class PostCommandRecordMixin(object):
351351

352-
# TODO: BuildCommand name doesn't make sense here, should be just Command
353-
command_class = BuildCommand
352+
# record, force_success, warn_only
353+
def pre_run_command(self, kwargs):
354+
# kwargs.update({
355+
# 'build_env': self,
356+
# })
357+
self.record = kwargs.pop('record', True)
358+
self.record_as_success = kwargs.pop('record_as_success', False)
359+
if not self.record:
360+
pass
361+
# kwargs['warn_only'] = True
362+
if self.record_as_success:
363+
self.record = True
364+
# kwargs['warn_only'] = True
354365

355366
def post_run_command(self):
356367
command = self.commands[-1]
@@ -359,29 +370,14 @@ def post_run_command(self):
359370
if self.record:
360371
command.save()
361372

362-
def run(self, *cmd, **kwargs):
363-
self.record = kwargs.pop('record', False)
364-
self.record_as_success = kwargs.pop('record_as_success', False)
365-
if not self.record:
366-
kwargs['warn_only'] = True
367-
if self.record_as_success:
368-
self.record = True
369-
kwargs['warn_only'] = True
370-
return super(LocalEnvironment, self).run(*cmd, **kwargs)
371373

372-
# record, force_success, warn_only
373-
def run_command_class(self, *cmd, **kwargs): # noqa
374-
self.record = kwargs.pop('record', False)
375-
self.record_as_success = kwargs.pop('record_as_success', False)
376-
if not self.record:
377-
kwargs['warn_only'] = True
378-
if self.record_as_success:
379-
self.record = True
380-
kwargs['warn_only'] = True
381-
return super(LocalEnvironment, self).run_command_class(*cmd, **kwargs)
374+
class LocalEnvironment(BaseEnvironment, PostCommandRecordMixin):
382375

376+
# TODO: BuildCommand name doesn't make sense here, should be just Command
377+
command_class = BuildCommand
383378

384-
class BuildEnvironment(BaseEnvironment):
379+
380+
class BuildEnvironment(BaseEnvironment, PostCommandRecordMixin):
385381

386382
"""
387383
Base build environment.
@@ -459,39 +455,6 @@ def handle_exception(self, exc_type, exc_value, _):
459455
self.failure = exc_value
460456
return True
461457

462-
def post_run_command(self):
463-
command = self.commands[-1]
464-
if self.record_as_success:
465-
command.exit_code = 0
466-
if self.record:
467-
command.save()
468-
469-
def run(self, *cmd, **kwargs):
470-
kwargs.update({
471-
'build_env': self,
472-
})
473-
self.record = kwargs.pop('record', self.record)
474-
self.record_as_success = kwargs.pop('record_as_success', False)
475-
if not self.record:
476-
kwargs['warn_only'] = True
477-
if self.record_as_success:
478-
self.record = True
479-
kwargs['warn_only'] = True
480-
return super(BuildEnvironment, self).run(*cmd, **kwargs)
481-
482-
def run_command_class(self, *cmd, **kwargs): # noqa
483-
kwargs.update({
484-
'build_env': self,
485-
})
486-
self.record = kwargs.pop('record', True)
487-
self.record_as_success = kwargs.pop('record_as_success', False)
488-
if not self.record:
489-
kwargs['warn_only'] = True
490-
if self.record_as_success:
491-
self.record = True
492-
kwargs['warn_only'] = True
493-
return super(BuildEnvironment, self).run_command_class(*cmd, **kwargs)
494-
495458
@property
496459
def successful(self):
497460
"""Is build completed, without top level failures or failing commands.""" # noqa

0 commit comments

Comments
 (0)