Skip to content

Commit 524c24c

Browse files
authored
Build: merge BaseEnvironment with BuildEnvironment (#10375)
We aren't using the LocalEnvironment class, only the BuildEnvironment, so there is no need to keep BaseEnvironment seperated from BuildEnvironment.
1 parent ccb24e9 commit 524c24c

File tree

2 files changed

+50
-86
lines changed

2 files changed

+50
-86
lines changed

readthedocs/doc_builder/environments.py

+49-85
Original file line numberDiff line numberDiff line change
@@ -400,22 +400,56 @@ def _escape_command(self, cmd):
400400
return command
401401

402402

403-
class BaseEnvironment:
403+
class BaseBuildEnvironment:
404404

405405
"""
406-
Base environment class.
406+
Base build environment.
407+
408+
Base class for wrapping command execution for build steps. This class is in
409+
charge of raising ``BuildAppError`` for internal application errors that
410+
should be communicated to the user as a general unknown error and
411+
``BuildUserError`` that will be exposed to the user with a proper message
412+
for them to debug by themselves since they are _not_ a Read the Docs issue.
407413
408-
Used to run arbitrary commands outside a build.
414+
:param project: Project that is being built
415+
:param version: Project version that is being built
416+
:param build: Build instance
417+
:param environment: shell environment variables
418+
:param record: whether or not record a build commands in the databse via
419+
the API. The only case where we want this to be `False` is when
420+
instantiating this class from `sync_repository_task` because it's a
421+
background task that does not expose commands to the user.
409422
"""
410423

411-
def __init__(self, project, environment=None):
412-
# TODO: maybe we can remove this Project dependency also
424+
def __init__(
425+
self,
426+
project=None,
427+
version=None,
428+
build=None,
429+
config=None,
430+
environment=None,
431+
record=True,
432+
**kwargs,
433+
):
413434
self.project = project
414435
self._environment = environment or {}
415436
self.commands = []
437+
self.version = version
438+
self.build = build
439+
self.config = config
440+
self.record = record
441+
442+
# TODO: remove these methods, we are not using LocalEnvironment anymore. We
443+
# need to find a way for tests to not require this anymore
444+
def __enter__(self):
445+
return self
446+
447+
def __exit__(self, exc_type, exc_value, tb):
448+
return
416449

417450
def record_command(self, command):
418-
pass
451+
if self.record:
452+
command.save()
419453

420454
def run(self, *cmd, **kwargs):
421455
"""Shortcut to run command from environment."""
@@ -447,15 +481,13 @@ def run_command_class(
447481

448482
# Remove PATH from env, and set it to bin_path if it isn't passed in
449483
environment = self._environment.copy()
450-
env_path = environment.pop('BIN_PATH', None)
451-
if 'bin_path' not in kwargs and env_path:
452-
kwargs['bin_path'] = env_path
453-
if 'environment' in kwargs:
454-
raise BuildAppError('environment can\'t be passed in via commands.')
455-
kwargs['environment'] = environment
456-
457-
# ``build_env`` is passed as ``kwargs`` when it's called from a
458-
# ``*BuildEnvironment``
484+
env_path = environment.pop("BIN_PATH", None)
485+
if "bin_path" not in kwargs and env_path:
486+
kwargs["bin_path"] = env_path
487+
if "environment" in kwargs:
488+
raise BuildAppError("environment can't be passed in via commands.")
489+
kwargs["environment"] = environment
490+
kwargs["build_env"] = self
459491
build_cmd = cls(cmd, **kwargs)
460492
build_cmd.run()
461493

@@ -497,82 +529,14 @@ def run_command_class(
497529
return build_cmd
498530

499531

500-
class LocalEnvironment(BaseEnvironment):
501-
502-
# TODO: BuildCommand name doesn't make sense here, should be just Command
503-
command_class = BuildCommand
504-
505-
506-
class BuildEnvironment(BaseEnvironment):
507-
508-
"""
509-
Base build environment.
510-
511-
Base class for wrapping command execution for build steps. This class is in
512-
charge of raising ``BuildAppError`` for internal application errors that
513-
should be communicated to the user as a general unknown error and
514-
``BuildUserError`` that will be exposed to the user with a proper message
515-
for them to debug by themselves since they are _not_ a Read the Docs issue.
516-
517-
:param project: Project that is being built
518-
:param version: Project version that is being built
519-
:param build: Build instance
520-
:param environment: shell environment variables
521-
:param record: whether or not record a build commands in the databse via
522-
the API. The only case where we want this to be `False` is when
523-
instantiating this class from `sync_repository_task` because it's a
524-
background task that does not expose commands to the user.
525-
"""
526-
527-
def __init__(
528-
self,
529-
project=None,
530-
version=None,
531-
build=None,
532-
config=None,
533-
environment=None,
534-
record=True,
535-
**kwargs,
536-
):
537-
super().__init__(project, environment)
538-
self.version = version
539-
self.build = build
540-
self.config = config
541-
self.record = record
542-
543-
# TODO: remove these methods, we are not using LocalEnvironment anymore. We
544-
# need to find a way for tests to not require this anymore
545-
def __enter__(self):
546-
return self
547-
548-
def __exit__(self, exc_type, exc_value, tb):
549-
return
550-
551-
def record_command(self, command):
552-
if self.record:
553-
command.save()
554-
555-
def run(self, *cmd, **kwargs):
556-
kwargs.update({
557-
'build_env': self,
558-
})
559-
return super().run(*cmd, **kwargs)
560-
561-
def run_command_class(self, *cmd, **kwargs): # pylint: disable=arguments-differ
562-
kwargs.update({
563-
'build_env': self,
564-
})
565-
return super().run_command_class(*cmd, **kwargs)
566-
567-
568-
class LocalBuildEnvironment(BuildEnvironment):
532+
class LocalBuildEnvironment(BaseBuildEnvironment):
569533

570534
"""Local execution build environment."""
571535

572536
command_class = BuildCommand
573537

574538

575-
class DockerBuildEnvironment(BuildEnvironment):
539+
class DockerBuildEnvironment(BaseBuildEnvironment):
576540

577541
"""
578542
Docker build environment, uses docker to contain builds.

readthedocs/vcs_support/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class BaseVCS:
3838
"""
3939
Base for VCS Classes.
4040
41-
VCS commands are ran inside a ``LocalEnvironment``.
41+
VCS commands are executed inside a ``BaseBuildEnvironment`` subclass.
4242
"""
4343

4444
supports_tags = False # Whether this VCS supports tags or not.

0 commit comments

Comments
 (0)