Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (c) 2016 Cisco and/or its affiliates. |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at: |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 16 | import os, pprint |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 17 | from os import removedirs |
| 18 | |
| 19 | |
| 20 | def underscore_to_camelcase(name): |
| 21 | name = name.title().replace("_", "") |
| 22 | return name[0].lower() + name[1:] |
| 23 | |
| 24 | |
| 25 | def underscore_to_camelcase_upper(name): |
| 26 | name = name.title().replace("_", "") |
| 27 | return name[0].upper() + name[1:] |
| 28 | |
| 29 | |
| 30 | def remove_folder(folder): |
| 31 | """ Remove folder with all its files """ |
| 32 | for root, dirs, files in os.walk(folder, topdown=False): |
| 33 | for name in files: |
| 34 | os.remove(os.path.join(root, name)) |
| 35 | removedirs(folder) |
| 36 | |
| 37 | |
Marek Gradzki | 8f3ad25 | 2017-12-07 16:49:26 +0100 | [diff] [blame] | 38 | reply_suffixes = ("reply", "details") |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 39 | |
| 40 | |
| 41 | def is_reply(name): |
| 42 | return name.lower().endswith(reply_suffixes) |
| 43 | |
| 44 | |
| 45 | def is_details(name): |
Marek Gradzki | 8f3ad25 | 2017-12-07 16:49:26 +0100 | [diff] [blame] | 46 | return name.lower().endswith(reply_suffixes[1]) |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 47 | |
Maros Marsalek | a53b0e2 | 2016-09-16 16:17:58 +0200 | [diff] [blame] | 48 | |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame] | 49 | def is_retval_field(name): |
| 50 | return name == 'retval' |
| 51 | |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 52 | dump_suffix = "dump" |
| 53 | |
| 54 | |
| 55 | def is_dump(name): |
| 56 | return name.lower().endswith(dump_suffix) |
| 57 | |
| 58 | |
| 59 | def get_reply_suffix(name): |
| 60 | for reply_suffix in reply_suffixes: |
| 61 | if name.lower().endswith(reply_suffix): |
Marek Gradzki | 8f3ad25 | 2017-12-07 16:49:26 +0100 | [diff] [blame] | 62 | return reply_suffix |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 63 | |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 64 | # Mapping according to: |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 65 | # http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 66 | # |
| 67 | # Unsigned types are converted to signed java types that have the same size. |
| 68 | # It is the API user responsibility to interpret them correctly. |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 69 | jni_2_java_type_mapping = {'u8': 'byte', |
| 70 | 'u8[]': 'byte[]', |
| 71 | 'i8': 'byte', |
| 72 | 'i8[]': 'byte[]', |
| 73 | 'u16': 'short', |
| 74 | 'u16[]': 'short[]', |
| 75 | 'i16': 'short', |
| 76 | 'i16[]': 'short[]', |
| 77 | 'u32': 'int', |
| 78 | 'u32[]': 'int[]', |
| 79 | 'i32': 'int', |
| 80 | 'i32[]': 'int[]', |
| 81 | 'u64': 'long', |
| 82 | 'u64[]': 'long[]', |
| 83 | 'i64': 'long', |
| 84 | 'i64[]': 'long[]', |
| 85 | 'f64': 'double', |
| 86 | 'f64[]': 'double[]' |
| 87 | } |
| 88 | |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 89 | vpp_2_jni_type_mapping = {'u8': 'jbyte', |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 90 | 'u8[]': 'jbyteArray', |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 91 | 'i8': 'jbyte', |
Marek Gradzki | 8f3ad25 | 2017-12-07 16:49:26 +0100 | [diff] [blame] | 92 | 'i8[]': 'jbyteArray', |
Jan Srnicek | 2e95f5a | 2016-07-06 13:19:12 +0200 | [diff] [blame] | 93 | 'u16': 'jshort', |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 94 | 'u16[]': 'jshortArray', |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 95 | 'i16': 'jshort', |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 96 | 'i16[]': 'jshortArray', |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 97 | 'u32': 'jint', |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 98 | 'u32[]': 'jintArray', |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 99 | 'i32': 'jint', |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 100 | 'i32[]': 'jintArray', |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 101 | 'u64': 'jlong', |
Maros Marsalek | 275b146 | 2016-11-07 11:37:49 +0100 | [diff] [blame] | 102 | 'u64[]': 'jlongArray', |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 103 | 'i64': 'jlong', |
Maros Marsalek | 275b146 | 2016-11-07 11:37:49 +0100 | [diff] [blame] | 104 | 'i64[]': 'jlongArray', |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 105 | 'f64': 'jdouble', |
| 106 | 'f64[]': 'jdoubleArray' |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 109 | # https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/types.html#type_signatures |
| 110 | jni_2_signature_mapping = {'u8': 'B', |
| 111 | 'u8[]': '[B', |
| 112 | 'i8': 'B', |
| 113 | 'i8[]': '[B', |
| 114 | 'u16': 'S', |
| 115 | 'u16[]': '[S', |
| 116 | 'i16': 'S', |
| 117 | 'i16[]': '[S', |
| 118 | 'u32': 'I', |
| 119 | 'u32[]': '[I', |
| 120 | 'i32': 'I', |
| 121 | 'i32[]': '[I', |
| 122 | 'u64': 'J', |
| 123 | 'u64[]': '[J', |
| 124 | 'i64': 'J', |
| 125 | 'i64[]': '[J', |
| 126 | 'f64': 'D', |
| 127 | 'f64[]': '[D' |
| 128 | } |
| 129 | |
| 130 | # https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html#Get_type_Field_routines |
| 131 | jni_field_accessors = {'u8': 'ByteField', |
| 132 | 'u8[]': 'ObjectField', |
| 133 | 'i8': 'ByteField', |
| 134 | 'i8[]': 'ObjectField', |
| 135 | 'u16': 'ShortField', |
| 136 | 'u16[]': 'ObjectField', |
| 137 | 'i16': 'ShortField', |
| 138 | 'i16[]': 'ObjectField', |
| 139 | 'u32': 'IntField', |
| 140 | 'u32[]': 'ObjectField', |
| 141 | 'i32': 'IntField', |
| 142 | 'i32[]': 'ObjectField', |
| 143 | 'u64': 'LongField', |
| 144 | 'u64[]': 'ObjectField', |
| 145 | 'i64': 'LongField', |
| 146 | 'i64[]': 'ObjectField', |
| 147 | 'f64': 'DoubleField', |
| 148 | 'f64[]': 'ObjectField' |
| 149 | } |
| 150 | |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 151 | |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 152 | def is_notification(name): |
Marek Gradzki | acf33e4 | 2017-12-07 17:17:35 +0100 | [diff] [blame] | 153 | """ Returns true if the structure is a notification """ |
| 154 | # FIXME no convention in the naming of events (notifications) in vpe.api |
| 155 | notifications_message_suffixes = ("event", "counters") |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 156 | |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 157 | return name.lower().endswith(notifications_message_suffixes) |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 158 | |
| 159 | |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 160 | def remove_reply_suffix(camel_case_name_with_suffix): |
| 161 | return remove_suffix(camel_case_name_with_suffix, get_reply_suffix(camel_case_name_with_suffix)) |
| 162 | |
| 163 | |
| 164 | def remove_suffix(camel_case_name_with_suffix, suffix): |
Matej Perina | ac1a728 | 2017-09-11 10:11:51 +0200 | [diff] [blame] | 165 | if not suffix: |
| 166 | return camel_case_name_with_suffix |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 167 | suffix_length = len(suffix) |
| 168 | return camel_case_name_with_suffix[:-suffix_length] if suffix_length != 0 else camel_case_name_with_suffix |
| 169 | |
| 170 | |
| 171 | def is_control_ping(camel_case_name_with_suffix): |
Marek Gradzki | 8f3ad25 | 2017-12-07 16:49:26 +0100 | [diff] [blame] | 172 | return camel_case_name_with_suffix.lower().startswith("controlping") |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 173 | |
Maros Marsalek | a53b0e2 | 2016-09-16 16:17:58 +0200 | [diff] [blame] | 174 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 175 | def api_message_to_javadoc(api_message): |
| 176 | """ Converts vpe.api message description to javadoc """ |
| 177 | str = pprint.pformat(api_message, indent=4, width=120, depth=None) |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 178 | return " * " + str.replace("\n", "\n * ") |
| 179 | |
| 180 | |
Maros Marsalek | a53b0e2 | 2016-09-16 16:17:58 +0200 | [diff] [blame] | 181 | def is_array(java_type_as_string): |
| 182 | return java_type_as_string.endswith("[]") |