Skip to content

Commit 1735925

Browse files
committed
Use enum to standardise phase
1 parent 0345a86 commit 1735925

File tree

5 files changed

+55
-25
lines changed

5 files changed

+55
-25
lines changed

repo2docker/__main__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
from .app import Repo2Docker
66
from .engine import BuildError, ImageLoadError
77
from . import __version__
8-
from .utils import validate_and_generate_port_mapping, is_valid_docker_image_name
8+
from .utils import (
9+
validate_and_generate_port_mapping,
10+
is_valid_docker_image_name,
11+
R2dState,
12+
)
913

1014

1115
def validate_image_name(image_name):
@@ -302,7 +306,7 @@ def make_r2d(argv=None):
302306
r2d.log.error(
303307
'Cannot mount "{}" in editable mode '
304308
"as it is not a directory".format(args.repo),
305-
extra=dict(phase="failed"),
309+
extra=dict(phase=R2dState.FAILED),
306310
)
307311
sys.exit(1)
308312

repo2docker/app.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
)
3939
from . import contentproviders
4040
from .engine import BuildError, ContainerEngineException, ImageLoadError
41-
from .utils import ByteSpecification, chdir
41+
from .utils import ByteSpecification, chdir, R2dState
4242

4343

4444
class Repo2Docker(Application):
@@ -465,7 +465,7 @@ def fetch(self, url, ref, checkout_path):
465465
for log_line in picked_content_provider.fetch(
466466
spec, checkout_path, yield_output=self.json_logs
467467
):
468-
self.log.info(log_line, extra=dict(phase="fetching"))
468+
self.log.info(log_line, extra=dict(phase=R2dState.FETCHING))
469469

470470
if not self.output_image_spec:
471471
image_spec = "r2d" + self.repo
@@ -491,7 +491,7 @@ def json_excepthook(self, etype, evalue, traceback):
491491
"Error during build: %s",
492492
evalue,
493493
exc_info=(etype, evalue, traceback),
494-
extra=dict(phase="failed"),
494+
extra=dict(phase=R2dState.FAILED),
495495
)
496496

497497
def initialize(self, *args, **kwargs):
@@ -532,7 +532,7 @@ def push_image(self):
532532
last_emit_time = time.time()
533533
for chunk in client.push(self.output_image_spec):
534534
if client.string_output:
535-
self.log.info(chunk, extra=dict(phase="pushing"))
535+
self.log.info(chunk, extra=dict(phase=R2dState.PUSHING))
536536
continue
537537
# else this is Docker output
538538

@@ -546,7 +546,7 @@ def push_image(self):
546546
self.log.warning("Not a JSON progress line: %r", line)
547547
continue
548548
if "error" in progress:
549-
self.log.error(progress["error"], extra=dict(phase="failed"))
549+
self.log.error(progress["error"], extra=dict(phase=R2dState.FAILED))
550550
raise ImageLoadError(progress["error"])
551551
if "id" not in progress:
552552
continue
@@ -561,13 +561,15 @@ def push_image(self):
561561
self.log.info(
562562
"Pushing image\n",
563563
extra=dict(
564-
progress=progress_layers, layers=layers, phase="pushing"
564+
progress=progress_layers,
565+
layers=layers,
566+
phase=R2dState.PUSHING,
565567
),
566568
)
567569
last_emit_time = time.time()
568570
self.log.info(
569571
"Successfully pushed {}".format(self.output_image_spec),
570-
extra=dict(phase="pushing"),
572+
extra=dict(phase=R2dState.PUSHING),
571573
)
572574

573575
def run_image(self):
@@ -659,24 +661,27 @@ def wait_for_container(self, container):
659661
for line in container.logs(stream=True, timestamps=True):
660662
line = line.decode("utf-8")
661663
last_timestamp, line = line.split(" ", maxsplit=1)
662-
self.log.info(line, extra=dict(phase="running"))
664+
self.log.info(line, extra=dict(phase=R2dState.RUNNING))
663665

664666
finally:
665667
container.reload()
666668
if container.status == "running":
667-
self.log.info("Stopping container...\n", extra=dict(phase="running"))
669+
self.log.info(
670+
"Stopping container...\n", extra=dict(phase=R2dState.RUNNING)
671+
)
668672
container.kill()
669673
exit_code = container.exitcode
670674

671675
container.wait()
672676

673677
self.log.info(
674-
"Container finished running.\n".upper(), extra=dict(phase="running")
678+
"Container finished running.\n".upper(),
679+
extra=dict(phase=R2dState.RUNNING),
675680
)
676681
# are there more logs? Let's send them back too
677682
late_logs = container.logs(since=last_timestamp).decode("utf-8")
678683
for line in late_logs.split("\n"):
679-
self.log.debug(line + "\n", extra=dict(phase="running"))
684+
self.log.debug(line + "\n", extra=dict(phase=R2dState.RUNNING))
680685

681686
container.remove()
682687
if exit_code:
@@ -751,7 +756,7 @@ def build(self):
751756
self.log.error(
752757
"Subdirectory %s does not exist",
753758
self.subdir,
754-
extra=dict(phase="failure"),
759+
extra=dict(phase=R2dState.FAILED),
755760
)
756761
raise FileNotFoundError("Could not find {}".format(checkout_path))
757762

@@ -786,7 +791,7 @@ def build(self):
786791
else:
787792
self.log.debug(
788793
picked_buildpack.render(build_args),
789-
extra=dict(phase="building"),
794+
extra=dict(phase=R2dState.BUILDING),
790795
)
791796
if self.user_id == 0:
792797
raise ValueError(
@@ -796,7 +801,7 @@ def build(self):
796801
self.log.info(
797802
"Using %s builder\n",
798803
bp.__class__.__name__,
799-
extra=dict(phase="building"),
804+
extra=dict(phase=R2dState.BUILDING),
800805
)
801806

802807
for l in picked_buildpack.build(
@@ -808,19 +813,24 @@ def build(self):
808813
self.extra_build_kwargs,
809814
):
810815
if docker_client.string_output:
811-
self.log.info(l, extra=dict(phase="building"))
816+
self.log.info(l, extra=dict(phase=R2dState.BUILDING))
812817
# else this is Docker output
813818
elif "stream" in l:
814-
self.log.info(l["stream"], extra=dict(phase="building"))
819+
self.log.info(
820+
l["stream"], extra=dict(phase=R2dState.BUILDING)
821+
)
815822
elif "error" in l:
816-
self.log.info(l["error"], extra=dict(phase="failure"))
823+
self.log.info(l["error"], extra=dict(phase=R2dState.FAILED))
817824
raise BuildError(l["error"])
818825
elif "status" in l:
819826
self.log.info(
820-
"Fetching base image...\r", extra=dict(phase="building")
827+
"Fetching base image...\r",
828+
extra=dict(phase=R2dState.BUILDING),
821829
)
822830
else:
823-
self.log.info(json.dumps(l), extra=dict(phase="building"))
831+
self.log.info(
832+
json.dumps(l), extra=dict(phase=R2dState.BUILDING)
833+
)
824834

825835
finally:
826836
# Cleanup checkout if necessary

repo2docker/contentproviders/git.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import subprocess
22

33
from .base import ContentProvider, ContentProviderException
4-
from ..utils import execute_cmd, check_ref
4+
from ..utils import execute_cmd, check_ref, R2dState
55

66

77
class Git(ContentProvider):
@@ -44,7 +44,7 @@ def fetch(self, spec, output_dir, yield_output=False):
4444
hash = check_ref(ref, output_dir)
4545
if hash is None:
4646
self.log.error(
47-
"Failed to check out ref %s", ref, extra=dict(phase="failed")
47+
"Failed to check out ref %s", ref, extra=dict(phase=R2dState.FAILED)
4848
)
4949
if ref == "master":
5050
msg = (

repo2docker/contentproviders/mercurial.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import subprocess
22

33
from .base import ContentProvider, ContentProviderException
4-
from ..utils import execute_cmd
4+
from ..utils import execute_cmd, R2dState
55

66
args_enabling_topic = ["--config", "extensions.topic="]
77

@@ -62,7 +62,7 @@ def fetch(self, spec, output_dir, yield_output=False):
6262
yield line
6363
except subprocess.CalledProcessError:
6464
self.log.error(
65-
"Failed to update to ref %s", ref, extra=dict(phase="failed")
65+
"Failed to update to ref %s", ref, extra=dict(phase=R2dState.FAILED)
6666
)
6767
raise ValueError("Failed to update to ref {}".format(ref))
6868

repo2docker/utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from contextlib import contextmanager
2+
from enum import Enum
23
from functools import partial
34
import os
45
import re
@@ -10,6 +11,21 @@
1011
from traitlets import Integer, TraitError
1112

1213

14+
class R2dState(Enum):
15+
"""
16+
The current state of repo2docker
17+
"""
18+
19+
FETCHING = "fetching"
20+
BUILDING = "building"
21+
PUSHING = "pushing"
22+
RUNNING = "running"
23+
FAILED = "failed"
24+
25+
def __str__(self):
26+
return self.value
27+
28+
1329
def execute_cmd(cmd, capture=False, **kwargs):
1430
"""
1531
Call given command, yielding output line by line if capture=True.

0 commit comments

Comments
 (0)