gen_empty_partition.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env python
  2. #
  3. # generates an empty binary file
  4. #
  5. # This tool generates an empty binary file of the required size.
  6. #
  7. # Copyright 2018 Espressif Systems (Shanghai) PTE LTD
  8. #
  9. # Licensed under the Apache License, Version 2.0 (the "License");
  10. # you may not use this file except in compliance with the License.
  11. # You may obtain a copy of the License at
  12. #
  13. # http:#www.apache.org/licenses/LICENSE-2.0
  14. #
  15. # Unless required by applicable law or agreed to in writing, software
  16. # distributed under the License is distributed on an "AS IS" BASIS,
  17. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. # See the License for the specific language governing permissions and
  19. # limitations under the License.
  20. from __future__ import print_function, division
  21. import argparse
  22. import os
  23. import re
  24. import struct
  25. import sys
  26. import hashlib
  27. import binascii
  28. __version__ = '1.0'
  29. quiet = False
  30. def status(msg):
  31. """ Print status message to stderr """
  32. if not quiet:
  33. critical(msg)
  34. def critical(msg):
  35. """ Print critical message to stderr """
  36. if not quiet:
  37. sys.stderr.write(msg)
  38. sys.stderr.write('\n')
  39. def generate_blanked_file(size, output_path):
  40. output = ""
  41. for i in range(size):
  42. output += b"\xFF"
  43. try:
  44. stdout_binary = sys.stdout.buffer # Python 3
  45. except AttributeError:
  46. stdout_binary = sys.stdout
  47. with stdout_binary if output_path == '-' else open(output_path, 'wb') as f:
  48. f.write(output)
  49. def main():
  50. global quiet
  51. parser = argparse.ArgumentParser(description='Generates an empty binary file of the required size.')
  52. parser.add_argument('--quiet', '-q', help="Don't print status messages to stderr", action='store_true')
  53. parser.add_argument('--size', help='Size of generated the file', type=str, required=True)
  54. parser.add_argument('output', help='Path for binary file.', nargs='?', default='-')
  55. args = parser.parse_args()
  56. quiet = args.quiet
  57. size = int(args.size, 0)
  58. if size > 0 :
  59. generate_blanked_file(size, args.output)
  60. return 0
  61. class InputError(RuntimeError):
  62. def __init__(self, e):
  63. super(InputError, self).__init__(e)
  64. if __name__ == '__main__':
  65. try:
  66. r = main()
  67. sys.exit(r)
  68. except InputError as e:
  69. print(e, file=sys.stderr)
  70. sys.exit(2)