| 1 |
# Copyright 2009-present MongoDB, Inc. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
from collections import OrderedDict as OD |
| 16 |
from textwrap import dedent |
| 17 |
from typing import Iterable |
| 18 |
|
| 19 |
from evergreen_config_generator import ConfigObject |
| 20 |
|
| 21 |
from . import MutableValueMapping, Value, ValueMapping, ValueOrderedDict |
| 22 |
|
| 23 |
|
| 24 |
def func(func_name: str, **kwargs: Value) -> MutableValueMapping: |
| 25 |
od: MutableValueMapping = OD([('func', func_name)]) |
| 26 |
if kwargs: |
| 27 |
od['vars'] = OD(sorted(kwargs.items())) |
| 28 |
|
| 29 |
return od |
| 30 |
|
| 31 |
|
| 32 |
def s3_put(remote_file: str, project_path: bool = True, **kwargs: Value) -> ValueMapping: |
| 33 |
if project_path: |
| 34 |
remote_file = '${project}/' + remote_file |
| 35 |
|
| 36 |
return ValueOrderedDict( |
| 37 |
[ |
| 38 |
('command', 's3.put'), |
| 39 |
( |
| 40 |
'params', |
| 41 |
ValueOrderedDict( |
| 42 |
( |
| 43 |
('aws_key', '${aws_key}'), |
| 44 |
('aws_secret', '${aws_secret}'), |
| 45 |
('remote_file', remote_file), |
| 46 |
('bucket', 'mciuploads'), |
| 47 |
('permissions', 'public-read'), |
| 48 |
*kwargs.items(), |
| 49 |
) |
| 50 |
), |
| 51 |
), |
| 52 |
] |
| 53 |
) |
| 54 |
|
| 55 |
|
| 56 |
def strip_lines(s: str) -> str: |
| 57 |
return '\n'.join(line for line in s.split('\n') if line.strip()) |
| 58 |
|
| 59 |
|
| 60 |
def shell_exec( |
| 61 |
script: str, |
| 62 |
test: bool = True, |
| 63 |
errexit: bool = True, |
| 64 |
xtrace: bool = False, |
| 65 |
silent: bool = False, |
| 66 |
continue_on_err: bool = False, |
| 67 |
working_dir: str | None = None, |
| 68 |
background: bool = False, |
| 69 |
add_expansions_to_env: bool = False, |
| 70 |
redirect_standard_error_to_output: bool = False, |
| 71 |
include_expansions_in_env: Iterable[str] = (), |
| 72 |
) -> ValueMapping: |
| 73 |
dedented = '' |
| 74 |
if errexit: |
| 75 |
dedented += 'set -o errexit\n' |
| 76 |
|
| 77 |
if xtrace: |
| 78 |
dedented += 'set -o xtrace\n' |
| 79 |
|
| 80 |
dedented += dedent(strip_lines(script)) |
| 81 |
command = ValueOrderedDict([('command', 'shell.exec')]) |
| 82 |
if test: |
| 83 |
command['type'] = 'test' |
| 84 |
|
| 85 |
command['params'] = OD() |
| 86 |
if silent: |
| 87 |
command['params']['silent'] = True |
| 88 |
|
| 89 |
if working_dir is not None: |
| 90 |
command['params']['working_dir'] = working_dir |
| 91 |
|
| 92 |
if continue_on_err: |
| 93 |
command['params']['continue_on_err'] = True |
| 94 |
|
| 95 |
if background: |
| 96 |
command['params']['background'] = True |
| 97 |
|
| 98 |
if add_expansions_to_env: |
| 99 |
command['params']['add_expansions_to_env'] = True |
| 100 |
|
| 101 |
if redirect_standard_error_to_output: |
| 102 |
command['params']['redirect_standard_error_to_output'] = True |
| 103 |
|
| 104 |
if include_expansions_in_env: |
| 105 |
command['params']['include_expansions_in_env'] = list(include_expansions_in_env) |
| 106 |
|
| 107 |
command['params']['shell'] = 'bash' |
| 108 |
command['params']['script'] = dedented |
| 109 |
return command |
| 110 |
|
| 111 |
|
| 112 |
def targz_pack(target: str, source_dir: str, *include: str) -> ValueMapping: |
| 113 |
return OD( |
| 114 |
[ |
| 115 |
('command', 'archive.targz_pack'), |
| 116 |
('params', OD([('target', target), ('source_dir', source_dir), ('include', list(include))])), |
| 117 |
] |
| 118 |
) |
| 119 |
|
| 120 |
|
| 121 |
class Function(ConfigObject): |
| 122 |
def __init__(self, *commands: Value): |
| 123 |
super(Function, self).__init__() |
| 124 |
self.commands = commands |
| 125 |
|
| 126 |
def to_dict(self) -> Value: |
| 127 |
return list(self.commands) |