| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
import sys |
| 25 |
import argparse |
| 26 |
import os |
| 27 |
import os.path |
| 28 |
from datetime import datetime as dt |
| 29 |
|
| 30 |
|
| 31 |
outfile_name = "multibin_" + ((dt.now()).strftime("%H_%M_%S")) + ".melf" |
| 32 |
|
| 33 |
outfile_path = os.getcwd() |
| 34 |
|
| 35 |
|
| 36 |
def check_outfile_path(dir_path): |
| 37 |
try: |
| 38 |
return os.makedirs(dir_path, exist_ok=True) |
| 39 |
except Exception as e: |
| 40 |
if type(e) == TypeError: |
| 41 |
|
| 42 |
pass |
| 43 |
else: |
| 44 |
print(e) |
| 45 |
sys.exit("invalid path provided for output file \n absolute path required") |
| 46 |
|
| 47 |
if not os.path.isdir(dir_path): |
| 48 |
try: |
| 49 |
|
| 50 |
return os.makedirs(dir_path) |
| 51 |
except Exception as e: |
| 52 |
print(e) |
| 53 |
sys.exit("invalid path provided for output file \n absolute path required") |
| 54 |
|
| 55 |
|
| 56 |
def process_out_file_arg(outfile_arg): |
| 57 |
global outfile_name |
| 58 |
global outfile_path |
| 59 |
|
| 60 |
if outfile_arg == os.path.join(outfile_path, outfile_name): |
| 61 |
return |
| 62 |
|
| 63 |
out_path, file_name = os.path.split(outfile_arg) |
| 64 |
|
| 65 |
if out_path and file_name and os.path.splitext(file_name)[1]: |
| 66 |
outfile_name = file_name |
| 67 |
check_outfile_path(out_path) |
| 68 |
outfile_path = os.path.abspath(out_path) |
| 69 |
elif str(outfile_arg).endswith(os.path.sep): |
| 70 |
check_outfile_path(outfile_arg) |
| 71 |
outfile_path = os.path.abspath(out_path) |
| 72 |
elif os.path.split(outfile_arg)[1] and os.path.splitext(os.path.split(outfile_arg)[1])[1]: |
| 73 |
outfile_name = file_name |
| 74 |
else: |
| 75 |
sys.exit("invalid output path or output file. use --help for help menu") |
| 76 |
|
| 77 |
|
| 78 |
def get_abs_path_input_files(in_files): |
| 79 |
abs_path_list = [] |
| 80 |
for infile in in_files: |
| 81 |
if not os.path.isfile(infile): |
| 82 |
sys.exit("-f OR --in_files argument error. {} is missing. use --help for help menu".format(infile)) |
| 83 |
abs_path_list.append(os.path.abspath(infile)) |
| 84 |
return abs_path_list |
| 85 |
|
| 86 |
|
| 87 |
def make_concatenated_binary(input_file_list): |
| 88 |
infiles_abs_path_list = get_abs_path_input_files(input_file_list) |
| 89 |
out_file = os.path.join(outfile_path, outfile_name) |
| 90 |
if os.path.exists(out_file): |
| 91 |
os.remove(out_file) |
| 92 |
output_bin = open(out_file, mode='ab') |
| 93 |
for file in infiles_abs_path_list: |
| 94 |
in_file = open(file, mode='rb') |
| 95 |
output_bin.write(in_file.read()) |
| 96 |
in_file.close() |
| 97 |
output_bin.close() |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
def main(): |
| 102 |
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter) |
| 103 |
|
| 104 |
|
| 105 |
parser.add_argument('-f', '--in_files', help=r'comma separated absolute paths of files to be concatenated' |
| 106 |
r'\neg: "-f C:\Temp\myelf1.elf,C:\Temp\myelf2.elf"', |
| 107 |
type=lambda s: [str(item).strip() for item in s.split(',')], |
| 108 |
required=True) |
| 109 |
|
| 110 |
|
| 111 |
parser.add_argument('-o', '--out_file', help=r'* give file name OR absolute path OR file name with absolute path *' |
| 112 |
r'\neg: "-o C:\Temp\\" OR "-o C:\Temp\myelf.melf" OR "-o myelf.melf"' |
| 113 |
r'\n(if only giving output path - terminate by path separator)', |
| 114 |
type=str, |
| 115 |
nargs='?', |
| 116 |
default=os.path.join(outfile_path, outfile_name)) |
| 117 |
args = parser.parse_args() |
| 118 |
|
| 119 |
|
| 120 |
if len(args.in_files) < 2: |
| 121 |
sys.exit("minimum 2 input files are required for concatenating") |
| 122 |
|
| 123 |
|
| 124 |
process_out_file_arg(args.out_file) |
| 125 |
|
| 126 |
|
| 127 |
make_concatenated_binary(args.in_files) |
| 128 |
|
| 129 |
print("\n\n *** Created concatenated binary: " + outfile_name + " at " + outfile_path + os.path.sep + " ***") |
| 130 |
|
| 131 |
|
| 132 |
if __name__ == "__main__": |
| 133 |
main() |