File: recipe.py

package info (click to toggle)
python-spython 0.3.13-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 736 kB
  • sloc: python: 3,299; sh: 61; makefile: 28
file content (90 lines) | stat: -rw-r--r-- 2,605 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Copyright (C) 2017-2022 Vanessa Sochat.

# This Source Code Form is subject to the terms of the
# Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
# with this file, You can obtain one at http://mozilla.org/MPL/2.0/.


class Recipe:
    """
    a recipe includes an environment, labels, runscript or command,
    and install sequence. This object is interacted with by a Parser
    (intended to popualte the recipe with content) and a Writer (intended
    to write a recipe to file). The parsers and writers are located in
    parsers.py, and writers.py, respectively. The user is also free to use
    the recipe class to build recipes.

    Parameters
    ==========
    recipe: the original recipe file, parsed by the subclass either
            DockerParser or SingularityParser
    layer: the count of the layer, for human readability

    """

    def __init__(self, recipe=None, layer=1):
        self.cmd = None
        self.comments = []
        self.entrypoint = None
        self.environ = []
        self.files = []
        self.layer_files = {}
        self.install = []
        self.labels = []
        self.ports = []
        self.test = None
        self.volumes = []
        self.workdir = None
        self.layer = layer
        self.fromHeader = None

        self.source = recipe

    def __str__(self):
        """show the user the recipe object, along with the type. E.g.,

        [spython-recipe][source:Singularity]
        [spython-recipe][source:Dockerfile]

        """
        base = "[spython-recipe]"
        if self.source:
            base = "%s[source:%s]" % (base, self.source)
        return base

    def json(self):
        """return a dictionary version of the recipe, intended to be parsed
        or printed as json.

        Returns: a dictionary of attributes including cmd, comments,
                 entrypoint, environ, files, install, labels, ports,
                 test, volumes, and workdir, organized by layer for
                 multistage builds.
        """
        attributes = [
            "cmd",
            "comments",
            "entrypoint",
            "environ",
            "files",
            "fromHeader",
            "layer_files",
            "install",
            "labels",
            "ports",
            "test",
            "volumes",
            "workdir",
        ]

        result = {}

        for attrib in attributes:
            value = getattr(self, attrib)
            if value:
                result[attrib] = value

        return result

    def __repr__(self):
        return self.__str__()