| Giorgi Lekveishvili | 285ab62 | 2023-11-22 13:50:45 +0400 | [diff] [blame^] | 1 | # Copyright (C) 2018 The Android Open Source Project |
| 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 | import random |
| 16 | import string |
| 17 | |
| 18 | |
| 19 | def check_if_ancestor_image_is_inherited(image, ancestor): |
| 20 | """Helper function that looks for a given ancestor image in the layers of a |
| 21 | provided image. It can be used to check, whether an image uses the expected |
| 22 | FROM-statement |
| 23 | |
| 24 | Arguments: |
| 25 | image {docker.images.Image} -- Docker image object to be checked |
| 26 | ancestor {str} -- Complete name of the expected ancestor image |
| 27 | |
| 28 | Returns: |
| 29 | boolean -- True, if ancestor is inherited by image |
| 30 | """ |
| 31 | |
| 32 | contains_tag = False |
| 33 | for layer in image.history(): |
| 34 | contains_tag = layer["Tags"] is not None and ancestor in layer["Tags"] |
| 35 | if contains_tag: |
| 36 | break |
| 37 | return contains_tag |
| 38 | |
| 39 | |
| 40 | def create_random_string(length=8): |
| 41 | return "".join([random.choice(string.ascii_letters) for n in range(length)]).lower() |