~sircmpwn/builds.sr.ht

builds.sr.ht/buildsrht/manifest.py -rw-r--r-- 5.5 KiB
5054bcbbHaowen Liu crontab: remove hirsute 15 days ago
                                                                                
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from enum import Enum
import os
import re
import subprocess
import uuid
import yaml

class TriggerAction(Enum):
    email = 'email'
    webhook = 'webhook'

class TriggerCondition(Enum):
    success = 'success'
    failure = 'failure'
    always = 'always'

class Trigger:
    def __init__(self, yml):
        if not isinstance(yml, dict):
            raise Exception("Expected trigger to be a dict")
        self.action = TriggerAction(yml["action"])
        self.condition = TriggerCondition(yml["condition"])
        self.attrs = {
            key: yml[key] for key in yml.keys()
                if key not in ["action", "condition"]
        }

    def to_dict(self):
        attrs = self.attrs
        attrs.update({
            "action": self.action.value,
            "condition": self.condition.value,
        })
        return attrs

class Task:
    def __init__(self, yml):
        if not isinstance(yml, dict) or len(yml) != 1:
            raise Exception("Expected task to be a string: string")
        for key in yml:
            if not isinstance(key, str) or not isinstance(yml[key], str):
                raise Exception("Expected task to be a string: string")
            self.name = key
            self.script = yml[key].strip()
        if not re.match(r"^[a-z0-9_-]+$", self.name) or len(self.name) > 128:
            raise Exception(f"Task name '{self.name}' is invalid (must be all lowercase letters, " +
                "numbers, underscores, and dashes, and <=128 characters)")

    def __repr__(self):
        return "<Task {}>".format(self.name)

class Manifest:
    def __init__(self, yml):
        self.yaml = yml
        image = self.yaml.get("image")
        arch = self.yaml.get("arch")
        packages = self.yaml.get("packages")
        repos = self.yaml.get("repositories")
        sources = self.yaml.get("sources")
        env = self.yaml.get("environment")
        secrets = self.yaml.get("secrets")
        shell = self.yaml.get("shell")
        artifacts = self.yaml.get("artifacts")
        oauth = self.yaml.get("oauth")
        if not image:
            raise Exception("Missing image in manifest")
        if not isinstance(image, str):
            raise Exception("Expected image to be a string")
        if packages:
            if not isinstance(packages, list) or not all([isinstance(p, str) for p in packages]):
                raise Exception("Expected packages to be a string array")
        if repos:
            if not isinstance(repos, dict):
                raise Exception("Expected repositories to be a dict")
            for repo in repos:
                if not isinstance(repos[repo], str):
                    raise Exception("Expected url for repository {}".format(repo))
        if sources:
            if not isinstance(sources, list) or not all([isinstance(s, str) for s in sources]):
                raise Exception("Expected sources to be a string array")
        if env:
            if not isinstance(env, dict):
                raise Exception("Expected environment to be a dictionary")
        if secrets:
            if not isinstance(secrets, list) or not all([isinstance(s, str) for s in secrets]):
                raise Exception("Expected secrets to be a UUID array")
            # Will throw exception on invalid UUIDs as well
            secrets = list(map(uuid.UUID, secrets))
        if shell is not None and not isinstance(shell, bool):
            raise Exception("Expected shell to be a boolean")
        if artifacts is not None and (
                not isinstance(artifacts, list) or
                any(not isinstance(p, str) or p == "" for p in artifacts) or
                len(set(os.path.basename(p) for p in artifacts)) != len(artifacts)):
            raise Exception("Expected artifacts to be a list of unique file paths")
        if oauth is not None and not isinstance(oauth, str):
            raise Exception("Expected oauth to be a string")
        self.image = image
        self.arch = arch
        self.packages = packages
        self.repos = repos
        self.sources = sources
        self.environment = env
        self.secrets = secrets
        self.shell = shell
        self.artifacts = artifacts
        self.oauth = oauth
        tasks = self.yaml.get("tasks")
        if not tasks or not isinstance(tasks, list):
            if (tasks is None or tasks == []) and not self.shell:
                raise Exception("Attempted to create manifest with no tasks")
            else:
                tasks = []
        self.tasks = [Task(t) for t in tasks]
        for task in self.tasks:
            if len([t for t in self.tasks if t.name == task.name]) != 1:
                raise Exception(f"Duplicate task '{task.name}'")
        triggers = self.yaml.get("triggers")
        self.triggers = [Trigger(t) for t in triggers] if triggers else []

    def __repr__(self):
        return "<Manifest {}, {} tasks>".format(self.image, len(self.tasks))
    
    def to_dict(self):
        return {
            "image": self.image,
            "arch": self.arch,
            "packages": self.packages,
            "repositories": self.repos,
            "sources": self.sources,
            "environment": self.environment,
            "secrets": [str(s) for s in self.secrets] if self.secrets else None,
            "tasks": [{ t.name: t.script } for t in self.tasks],
            "triggers": [t.to_dict() for t in self.triggers] if any(self.triggers) else None,
            "shell": self.shell,
            "artifacts": self.artifacts,
            "oauth": self.oauth,
        }

    def to_yaml(self):
        return yaml.dump(self.to_dict())