File: functions.py

package info (click to toggle)
mongo-c-driver 1.23.1-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 41,364 kB
  • sloc: ansic: 176,588; javascript: 7,954; python: 4,498; sh: 136; makefile: 32; xml: 10; cpp: 2
file content (116 lines) | stat: -rw-r--r-- 3,172 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
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
# Copyright 2018-present MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from collections import OrderedDict as OD
from textwrap import dedent

from evergreen_config_generator import ConfigObject


def func(func_name, **kwargs):
    od = OD([('func', func_name)])
    if kwargs:
        od['vars'] = OD(sorted(kwargs.items()))

    return od


def bootstrap(VERSION='latest', TOPOLOGY=None, **kwargs):
    if TOPOLOGY:
        return func('bootstrap mongo-orchestration',
                    VERSION=VERSION,
                    TOPOLOGY=TOPOLOGY,
                    **kwargs)

    return func('bootstrap mongo-orchestration',
                VERSION=VERSION,
                **kwargs)


def run_tests(URI=None, **kwargs):
    if URI:
        return func('run tests', URI=URI, **kwargs)

    return func('run tests', **kwargs)


def s3_put(remote_file, project_path=True, **kwargs):
    if project_path:
        remote_file = '${project}/' + remote_file

    od = OD([
        ('command', 's3.put'),
        ('params', OD([
            ('aws_key', '${aws_key}'),
            ('aws_secret', '${aws_secret}'),
            ('remote_file', remote_file),
            ('bucket', 'mciuploads'),
            ('permissions', 'public-read')]))])

    od['params'].update(kwargs)
    return od


def strip_lines(s):
    return '\n'.join(line for line in s.split('\n') if line.strip())


def shell_exec(script, test=True, errexit=True, xtrace=False, silent=False,
               continue_on_err=False, working_dir=None, background=False):
    dedented = ''
    if errexit:
        dedented += 'set -o errexit\n'

    if xtrace:
        dedented += 'set -o xtrace\n'

    dedented += dedent(strip_lines(script))
    command = OD([('command', 'shell.exec')])
    if test:
        command['type'] = 'test'

    command['params'] = OD()
    if silent:
        command['params']['silent'] = True

    if working_dir is not None:
        command['params']['working_dir'] = working_dir

    if continue_on_err:
        command['params']['continue_on_err'] = True

    if background:
        command['params']['background'] = True

    command['params']['shell'] = 'bash'
    command['params']['script'] = dedented
    return command


def targz_pack(target, source_dir, *include):
    return OD([
        ('command', 'archive.targz_pack'),
        ('params', OD([
            ('target', target),
            ('source_dir', source_dir),
            ('include', list(include))]))])


class Function(ConfigObject):
    def __init__(self, *commands):
        super(Function, self).__init__()
        self.commands = commands

    def to_dict(self):
        return list(self.commands)