Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Tools/scripts/bin2hex.py
Views: 1798
#!/usr/bin/env python312# Copyright (c) 2008,2010,2011,2012,2013 Alexander Belchenko3# All rights reserved.4#5# Redistribution and use in source and binary forms,6# with or without modification, are permitted provided7# that the following conditions are met:8#9# * Redistributions of source code must retain10# the above copyright notice, this list of conditions11# and the following disclaimer.12# * Redistributions in binary form must reproduce13# the above copyright notice, this list of conditions14# and the following disclaimer in the documentation15# and/or other materials provided with the distribution.16# * Neither the name of the author nor the names17# of its contributors may be used to endorse18# or promote products derived from this software19# without specific prior written permission.20#21# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS22# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,23# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY24# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.25# IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE26# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,27# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,28# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,29# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED30# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,31# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)32# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,33# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.3435'''Intel HEX file format bin2hex convertor utility.'''3637VERSION = '1.5'3839if __name__ == '__main__':40import getopt41import os42import sys4344from intelhex import bin2hex4546usage = '''Bin2Hex convertor utility.47Usage:48python bin2hex.py [options] INFILE [OUTFILE]4950Arguments:51INFILE name of bin file for processing.52Use '-' for reading from stdin.5354OUTFILE name of output file. If omitted then output55will be writing to stdout.5657Options:58-h, --help this help message.59-v, --version version info.60--offset=N offset for loading bin file (default: 0).61'''6263offset = 06465try:66opts, args = getopt.getopt(sys.argv[1:], "hv",67["help", "version", "offset="])6869for o, a in opts:70if o in ("-h", "--help"):71print(usage)72sys.exit(0)73elif o in ("-v", "--version"):74print(VERSION)75sys.exit(0)76elif o in ("--offset"):77base = 1078if a[:2].lower() == '0x':79base = 1680try:81offset = int(a, base)82except:83raise getopt.GetoptError('Bad offset value')8485if not args:86raise getopt.GetoptError('Input file is not specified')8788if len(args) > 2:89raise getopt.GetoptError('Too many arguments')9091except getopt.GetoptError as msg:92txt = 'ERROR: '+str(msg) # that's required to get not-so-dumb result from 2to3 tool93print(txt)94print(usage)95sys.exit(2)9697def force_stream_binary(stream):98"""Force binary mode for stream on Windows."""99if os.name == 'nt':100f_fileno = getattr(stream, 'fileno', None)101if f_fileno:102fileno = f_fileno()103if fileno >= 0:104import msvcrt105msvcrt.setmode(fileno, os.O_BINARY)106107fin = args[0]108if fin == '-':109# read from stdin110fin = sys.stdin111force_stream_binary(fin)112elif not os.path.isfile(fin):113txt = "ERROR: File not found: %s" % fin # that's required to get not-so-dumb result from 2to3 tool114print(txt)115sys.exit(1)116117if len(args) == 2:118fout = args[1]119else:120# write to stdout121fout = sys.stdout122force_stream_binary(fout)123124sys.exit(bin2hex(fin, fout, offset))125126127