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