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