-
Notifications
You must be signed in to change notification settings - Fork 0
/
compose.py
201 lines (143 loc) · 6.88 KB
/
compose.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import argparse
import logging
import re
import json
import sys
#indentation
try:
from textwrap import indent
except ImportError:
def indent(text: str, prefix: str):
predicate = lambda line: line.strip()
def prefixed_lines():
for line in text.splitlines(True):
yield (prefix + line if predicate(line) else line)
return ''.join(prefixed_lines())
#vapi headers
HEADER = '\n[CCode ({0})]\nnamespace {1} {{'
HEADER_OPTION = 'cheader_filename = "{0}", has_type_id = false, cprefix = "{1}", lower_case_cprefix = "{1}"'
DEPENDENCIES = ['posix']
IGNORED_FILES = []
#helper functions
def prepare_license(div = '', wrapped = True, filename = 'LICENSE'):
license_header = 'Mongo C Driver bindings for Vala\n'
with open('./{}'.format(filename), 'r') as f:
raw_license = f.read()
logging.debug('Reading LICENSE file')
if wrapped:
license_wrapped = indent("{}\n{}".format(license_header, raw_license), div)
return '/*\n{}\n*/'.format(license_wrapped)
return "{}\n{}".format(license_header, raw_license)
def find_partials(folder, suffix):
logging.debug('Searching for *{0} files in {1}'.format(suffix, folder))
partials = []
for fn in os.listdir('./{}'.format(folder)):
if fn.endswith(suffix) and fn not in IGNORED_FILES:
partials.append('./{0}/{1}'.format(folder, fn))
return partials
def prepare_partials(partials_path, div):
final_partials = []
for fn in partials_path:
with open(fn, 'r') as f:
fn_strip = fn
while fn_strip.startswith('./'):
fn_strip = fn_strip[2:]
FILE_HEADER = "\n/**\n * Partial section from {}\n */\n".format(fn_strip)
final_partials.append(indent(FILE_HEADER + f.read(), div))
logging.debug('Reading file {}'.format(fn))
return final_partials
def prepare_deps(deps: list):
return '\n'.join(deps)
def compose_vapi(onefile: bool, folder: str, div: str, out: str, deps: bool, filename = 'libmongoc-1.0', filename2 = 'libbson-1.0'):
logging.debug('Composing VAPI content from partials')
if not os.path.exists(out):
os.makedirs(out)
logging.debug("Created folder {}".format(out))
license_wrapped = prepare_license(div = div)
mongoc_wrapped = prepare_partials(find_partials(folder = './{}/libmongoc'.format(folder), suffix = '.vapi'), div = div)
bson_wrapped = prepare_partials(find_partials(folder = './{}/libbson'.format(folder), suffix = '.vapi'), div = div)
if not onefile:
mongoc_joined = ''.join(mongoc_wrapped)
bson_joined = ''.join(bson_wrapped)
mongoc_header = HEADER.format(HEADER_OPTION.format('mongoc.h','mongoc_'), 'Mongo')
bson_header = HEADER.format(HEADER_OPTION.format('bson.h','bson_'), 'Bson')
bson_re = re.compile(r"(?:\b)Bson(?!(\.[\d\w]*)|([\d\w]*\b\ [\(\{]))")
mongoc_formatted = '{0}\n{1}\n{2}\n}}\n'.format(
license_wrapped,
mongoc_header,
bson_re.sub("Bson.Bson", mongoc_joined)
)
bson_formatted = '{0}\n{1}\n{2}\n}}\n'.format(
license_wrapped,
bson_header,
re.sub(r"\bCollection\b", "Mongo.Collection", bson_joined)
)
with open('{0}/{1}.vapi'.format(out, filename), 'w') as f:
f.write(mongoc_formatted)
logging.debug('Writing on {0}/{1}.vapi'.format(out, filename))
with open('{0}/{1}.vapi'.format(out, filename2), 'w') as f:
f.write(bson_formatted)
logging.debug('Writing on {0}/{1}.vapi'.format(out, filename2))
else:
joined = ''.join(mongoc_wrapped + bson_wrapped)
formatted = '{0}\n{1}\n{2}\n}}\n'.format(
license_wrapped,
HEADER.format(HEADER_OPTION.format('mongoc.h,bson.h','mongoc_'), 'Mongo'),
joined
)
with open('{0}/{1}.vapi'.format(out, filename), 'w') as f:
f.write(formatted)
logging.debug('Writing on {0}/{1}.vapi'.format(out, filename))
if deps:
with open('{0}/{1}.deps'.format(out, filename), 'w') as f:
f.write(prepare_deps(DEPENDENCIES))
logging.debug('Writing on {0}/{1}.deps'.format(out, filename))
if not onefile:
with open('{0}/{1}.deps'.format(out, filename2), 'w') as f:
f.write(prepare_deps(DEPENDENCIES))
logging.debug('Writing on {0}/{1}.deps'.format(out, filename))
#arg parsing
parser = argparse.ArgumentParser(
prog = 'compose',
usage = '%(prog)s [options]',
description = 'Compose the partials and generate a VAPI file'
)
parser.add_argument('-d', '--deps', dest = 'deps', action = 'store_true', default = True, help = 'Specify if .deps file will be generated')
parser.add_argument('-o', '--out', dest = 'out', type = str, action = 'store', default = 'vapi', help = 'Specify output folder')
parser.add_argument('-f', '--folder', dest = 'folder', action = 'store', type = str, default = 'partials', help = 'Specify partials folder')
parser.add_argument('-v', '--verbose', dest = 'verbose', action = 'store_true', help = 'Activate verbose mode [Debug]')
parser.add_argument('-q', '--quiet', dest = 'quiet', action = 'store_true', help = 'Activate quiet mode [Error]')
parser.add_argument('-l', '--license', dest = 'license', action = 'store_true', help = 'Show license')
parser.add_argument('-i', '--indent', dest = 'indent', type = int, action = 'store', default = 4, help = 'Specify indentation, default 4 spaces')
parser.add_argument('--onefile', dest = 'onefile', action = 'store_true', help = 'Specify if the ouput will be one file')
args, _ = parser.parse_known_args()
def set_verbosity(verbose, quiet):
if verbose and not quiet:
logging.basicConfig(level=logging.DEBUG, format='%(message)s')
logging.debug("Verbose mode setted")
elif quiet and not verbose:
logging.basicConfig(level=logging.ERROR, format='%(message)s')
else:
logging.basicConfig(level=logging.INFO, format='%(message)s')
set_verbosity(args.verbose, args.quiet)
if args.license:
print(prepare_license(wrapped = False), '\n')
elif args.out and args.folder:
try:
kwargs = {"onefile": args.onefile, "folder": args.folder, "div": ' ' * args.indent, "out": args.out, "deps": args.deps}
logging.debug(json.dumps(kwargs, indent = ' ' * args.indent))
compose_vapi(**kwargs)
except:
logging.critical("An error occurred, please retry", exc_info = True if args.verbose else False)
sys.exit(1)
else:
print('Done. VAPI generated in ./{} [{}]'.format(args.out, 'onefile' if args.onefile else 'separated'))
finally:
logging.info('\nLicensed under MIT, see LICENSE or `make license`')
logging.info('Run `python3 compose.py -h` for usage options')
else:
parser.print_help(sys.stderr)
sys.exit(0)