|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +""" |
| 4 | +This script sanitises directory and file names for GitHub Actions artifacts. |
| 5 | +Example error from the upload-artifact action if you have an invalid path: |
| 6 | +
|
| 7 | + Error: The path for one of the files in artifact is not valid: |
| 8 | + /tempest-artifacts.2024-08-29T18:18+00:00/docker.log. Contains the following |
| 9 | + character: Colon : |
| 10 | + |
| 11 | + Invalid characters include: Double quote ", Colon :, Less than <, Greater than |
| 12 | + >, Vertical bar |, Asterisk *, Question mark ?, Carriage return \r, Line feed |
| 13 | + \n |
| 14 | + |
| 15 | + The following characters are not allowed in files that are uploaded due to |
| 16 | + limitations with certain file systems such as NTFS. To maintain file system |
| 17 | + agnostic behavior, these characters are intentionally not allowed to prevent |
| 18 | + potential problems with downloads on different file systems. |
| 19 | +""" |
| 20 | + |
| 21 | +import os |
| 22 | +import sys |
| 23 | +import typing as t |
| 24 | + |
| 25 | + |
| 26 | +def main() -> None: |
| 27 | + if len(sys.argv) != 2: |
| 28 | + usage() |
| 29 | + sys.exit(1) |
| 30 | + |
| 31 | + sanitise(sys.argv[1]) |
| 32 | + |
| 33 | + |
| 34 | +def usage() -> None: |
| 35 | + print(f"Usage: {sys.argv[0]} <path>") |
| 36 | + |
| 37 | + |
| 38 | +def sanitise(path: str) -> None: |
| 39 | + # Recursively walk a directory, sanitising subdirectories and files as we go. |
| 40 | + # Walk bottom-up to avoid directory renames breaking subsequent paths. |
| 41 | + table = translation_table() |
| 42 | + for dirpath, dirnames, filenames in os.walk(path, topdown=False, followlinks=False): |
| 43 | + for filename in filenames: |
| 44 | + sanitise_file_or_dir(filename, table, dirpath) |
| 45 | + for dirname in dirnames: |
| 46 | + sanitise_file_or_dir(dirname, table, dirpath) |
| 47 | + |
| 48 | + |
| 49 | +def translation_table() -> t.Dict: |
| 50 | + # Return a translation table that translates all disallowed characters to a dash. |
| 51 | + disallowed = "\":<>|*?\r\n" |
| 52 | + return str.maketrans(disallowed, "-" * len(disallowed)) |
| 53 | + |
| 54 | + |
| 55 | +def sanitise_file_or_dir(path: str, table: t.Dict, dirpath: str) -> None: |
| 56 | + # Sanitise a single file or directory. |
| 57 | + sanitised = path.translate(table) |
| 58 | + if path != sanitised: |
| 59 | + print(f"Sanitising {path} as {sanitised} in {dirpath}") |
| 60 | + path = os.path.join(dirpath, path) |
| 61 | + dirpath = os.path.join(dirpath, sanitised) |
| 62 | + os.rename(path, dirpath) |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + main() |
0 commit comments