|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright (c) 2013, Michael Droettboom All rights reserved. |
| 4 | + |
| 5 | +# Redistribution and use in source and binary forms, with or without |
| 6 | +# modification, are permitted provided that the following conditions |
| 7 | +# are met: |
| 8 | + |
| 9 | +# 1. Redistributions of source code must retain the above copyright |
| 10 | +# notice, this list of conditions and the following disclaimer. |
| 11 | +# 2. Redistributions in binary form must reproduce the above copyright |
| 12 | +# notice, this list of conditions and the following disclaimer in |
| 13 | +# the documentation and/or other materials provided with the |
| 14 | +# distribution. |
| 15 | + |
| 16 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 19 | +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 20 | +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 | +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 22 | +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 23 | +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 24 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 26 | +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | +# POSSIBILITY OF SUCH DAMAGE. |
| 28 | + |
| 29 | +# The views and conclusions contained in the software and |
| 30 | +# documentation are those of the authors and should not be interpreted |
| 31 | +# as representing official policies, either expressed or implied, of |
| 32 | +# the FreeBSD Project. |
| 33 | + |
| 34 | +from __future__ import print_function, unicode_literals, absolute_import |
| 35 | + |
| 36 | +import glob |
| 37 | +import imp |
| 38 | +import os |
| 39 | +import re |
| 40 | +import sys |
| 41 | + |
| 42 | + |
| 43 | +def write_if_different(filename, data): |
| 44 | + """ Write `data` to `filename`, if the content of the file is different. |
| 45 | +
|
| 46 | + Parameters |
| 47 | + ---------- |
| 48 | + filename : str |
| 49 | + The file name to be written to. |
| 50 | + data : bytes |
| 51 | + The data to be written to `filename`. |
| 52 | + """ |
| 53 | + assert isinstance(data, bytes) |
| 54 | + |
| 55 | + if os.path.exists(filename): |
| 56 | + with open(filename, 'rb') as fd: |
| 57 | + original_data = fd.read() |
| 58 | + else: |
| 59 | + original_data = None |
| 60 | + |
| 61 | + if original_data != data: |
| 62 | + with open(filename, 'wb') as fd: |
| 63 | + fd.write(data) |
| 64 | + |
| 65 | + |
| 66 | +if sys.version_info[0] >= 3: |
| 67 | + from io import StringIO |
| 68 | + string_types = (str, bytes) |
| 69 | + |
| 70 | + def get_byte(x): |
| 71 | + return x |
| 72 | +else: |
| 73 | + from cStringIO import StringIO |
| 74 | + string_types = (str, unicode) |
| 75 | + |
| 76 | + def get_byte(x): |
| 77 | + return ord(x) |
| 78 | + |
| 79 | +def generate_c_docstrings(name, input_path, output_path): |
| 80 | + input_file_path = os.path.join(input_path, name + ".py") |
| 81 | + module = imp.load_source(name, input_file_path) |
| 82 | + docstrings = module.__dict__ |
| 83 | + |
| 84 | + keys = [ |
| 85 | + key for key in docstrings.keys() |
| 86 | + if not key.startswith('__') and type(key) in string_types and |
| 87 | + type(docstrings[key]) in string_types] |
| 88 | + keys.sort() |
| 89 | + docs = {} |
| 90 | + for key in keys: |
| 91 | + docs[key] = docstrings[key].encode('utf8').lstrip() + b'\0' |
| 92 | + |
| 93 | + h_file = StringIO() |
| 94 | + h_file.write("""/* |
| 95 | +DO NOT EDIT! |
| 96 | +
|
| 97 | +This file is autogenerated by setup.py. To edit its contents, edit |
| 98 | +{0}. |
| 99 | +*/ |
| 100 | +
|
| 101 | +#ifndef __DOC_{1}_H__ |
| 102 | +#define __DOC_{1}_H__ |
| 103 | +""".format(input_file_path, name.upper(), name)) |
| 104 | + for key in keys: |
| 105 | + val = docs[key] |
| 106 | + h_file.write('extern char doc_{0}[{1}];\n'.format(key, len(val))) |
| 107 | + h_file.write("\n#endif\n\n") |
| 108 | + |
| 109 | + write_if_different( |
| 110 | + os.path.join(output_path, name + ".h"), |
| 111 | + h_file.getvalue().encode('utf-8')) |
| 112 | + |
| 113 | + c_file = StringIO() |
| 114 | + c_file.write("""/* |
| 115 | +DO NOT EDIT! |
| 116 | +
|
| 117 | +This file is autogenerated by setup.py. To edit its contents, edit |
| 118 | +{0} |
| 119 | +
|
| 120 | +We use numeric array initializers rather than string literals here, |
| 121 | +because some C compilers, notably MSVC, do not support string literals |
| 122 | +greater than 256 characters. |
| 123 | +*/ |
| 124 | +
|
| 125 | +#include "{1}.h" |
| 126 | +
|
| 127 | +""".format(input_file_path, name)) |
| 128 | + |
| 129 | + for key in keys: |
| 130 | + val = docs[key] |
| 131 | + c_file.write('char doc_{0}[{1}] = {{\n'.format( |
| 132 | + key, len(val))) |
| 133 | + for i in range(0, len(val), 10): |
| 134 | + c_file.write(' ') |
| 135 | + for c in val[i:i+10]: |
| 136 | + c_file.write("0x{0:02x}, ".format(get_byte(c))) |
| 137 | + c_file.write('\n') |
| 138 | + c_file.write('};\n\n') |
| 139 | + |
| 140 | + write_if_different( |
| 141 | + os.path.join(output_path, name + ".c"), |
| 142 | + c_file.getvalue().encode('utf-8')) |
| 143 | + |
| 144 | + |
| 145 | +def convert_all_docstrings(input_dir, output_dir): |
| 146 | + sys.path.insert(0, input_dir) |
| 147 | + |
| 148 | + if not os.path.exists(output_dir): |
| 149 | + os.makedirs(output_dir) |
| 150 | + |
| 151 | + for path in glob.glob(os.path.join(input_dir, "*.py")): |
| 152 | + name = os.path.basename(path)[:-3] |
| 153 | + module = imp.load_source(name, path) |
| 154 | + generate_c_docstrings(name, input_dir, output_dir) |
0 commit comments