rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | #!/usr/bin/python3 |
| 2 | |
| 3 | import os |
| 4 | import subprocess |
| 5 | from argparse import ArgumentParser, RawTextHelpFormatter |
| 6 | |
| 7 | class bcolors: |
| 8 | HEADER = '\033[95m' |
| 9 | OKBLUE = '\033[94m' |
| 10 | OKGREEN = '\033[92m' |
| 11 | WARNING = '\033[93m' |
| 12 | FAIL = '\033[91m' |
| 13 | ENDC = '\033[0m' |
| 14 | BOLD = '\033[1m' |
| 15 | UNDERLINE = '\033[4m' |
| 16 | |
| 17 | def parse_arg(): |
| 18 | arg_parser = ArgumentParser(description = "compress img with MD, DSP", |
| 19 | formatter_class = RawTextHelpFormatter) |
| 20 | |
| 21 | arg_parser.add_argument("img_folder", help = "Folder of BACH image") |
| 22 | arg_parser.print_help() |
| 23 | print() |
| 24 | return arg_parser.parse_args() |
| 25 | |
| 26 | def check_req(keyword, content): |
| 27 | if not keyword in content: |
| 28 | print(bcolors.FAIL + keyword + " doesn't exist" + bcolors.ENDC) |
| 29 | print() |
| 30 | return 1 |
| 31 | return 0 |
| 32 | |
| 33 | def check_bundle(content): |
| 34 | checksum = 0 |
| 35 | checksum = checksum | check_req("modem.img", content) |
| 36 | checksum = checksum | check_req(".EDB", content) |
| 37 | checksum = checksum | check_req("dsp.bin", content) |
| 38 | assert checksum == 0, bcolors.FAIL + "\n Please add them into image folder\n You can copy them from BACH official release" + bcolors.ENDC + "\n" |
| 39 | |
| 40 | def tar_files(source): |
| 41 | cmd = "tar jcvf source.tar.bz2" + source |
| 42 | print(cmd) |
| 43 | subprocess.call(cmd, shell=True) |
| 44 | print(bcolors.OKGREEN + "compressed file is in " + os.getcwd() + bcolors.ENDC) |
| 45 | |
| 46 | if __name__ == '__main__': |
| 47 | console_args = parse_arg() |
| 48 | directory = "./" + console_args.img_folder |
| 49 | if not os.path.isdir(directory): |
| 50 | print(console_args.img_folder + " doesn't exist") |
| 51 | exit(1) |
| 52 | |
| 53 | compress_files = " " |
| 54 | for filename in os.listdir(directory): |
| 55 | if not os.path.isdir(directory + "/" + filename): |
| 56 | compress_files += filename + " " |
| 57 | |
| 58 | check_bundle(compress_files) |
| 59 | os.chdir(directory) |
| 60 | tar_files(compress_files) |
| 61 | # TODO: show final tar file location |
| 62 | |