Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/env python
2"""LICENSE
3Copyright 2017 Hermann Krumrey <hermann@krumreyh.com>
5This file is part of ci-scripts.
7ci-scripts is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
12ci-scripts is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
17You should have received a copy of the GNU General Public License
18along with ci-scripts. If not, see <http://www.gnu.org/licenses/>.
19LICENSE"""
21from typing import List
22from base64 import b64decode
23from colorama import Fore, Style
24from subprocess import check_output, CalledProcessError, STDOUT
27def process_call(command: List[str], ignore_error: bool = False) -> str:
28 """
29 Prints a command and executes it.
30 If the exit code is not 0, the program will crash.
31 :param command: The command to run
32 :param ignore_error: Ignores any errors during a process call if True
33 :return: The output of the command call, stripped of whitespace
34 """
35 print(Fore.CYAN + " ".join(command) + Style.RESET_ALL)
37 try:
38 output = check_output(command, stderr=STDOUT).decode()
39 except CalledProcessError as e:
40 if ignore_error:
41 output = "Error ignored during subprocess call"
42 else:
43 print(Fore.RED + e.stdout.decode() + Style.RESET_ALL)
44 raise e
46 print(Fore.MAGENTA + output + Style.RESET_ALL)
47 return output.strip()
50def decode_base64_string_to_file(content: str, dest: str):
51 """
52 Decodes a base64-encoded string and writes the result to a file
53 :param content: The string to decode
54 :param dest: The destination file path
55 :return: None
56 """
57 with open(dest, "wb") as f:
58 f.write(b64decode(bytes(content, "utf-8")))