gen_empty_partition.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. from __future__ import unicode_literals
  22. import argparse
  23. import sys
  24. __version__ = '1.0'
  25. quiet = False
  26. def generate_blanked_file(size, output_path):
  27. output = b"\xFF" * size
  28. try:
  29. stdout_binary = sys.stdout.buffer # Python 3
  30. except AttributeError:
  31. stdout_binary = sys.stdout
  32. with stdout_binary if output_path == '-' else open(output_path, 'wb') as f:
  33. f.write(output)
  34. def main():
  35. parser = argparse.ArgumentParser(description='Generates an empty binary file of the required size.')
  36. parser.add_argument('size', help='Size of generated the file', type=str)
  37. parser.add_argument('output', help='Path for binary file.', nargs='?', default='-')
  38. args = parser.parse_args()
  39. size = int(args.size, 0)
  40. if size > 0:
  41. generate_blanked_file(size, args.output)
  42. return 0
  43. class InputError(RuntimeError):
  44. def __init__(self, e):
  45. super(InputError, self).__init__(e)
  46. if __name__ == '__main__':
  47. try:
  48. r = main()
  49. sys.exit(r)
  50. except InputError as e:
  51. print(e, file=sys.stderr)
  52. sys.exit(2)