forked from clibs/clib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccan2clib.py
119 lines (93 loc) · 3.16 KB
/
ccan2clib.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
import glob
import json
import os
import os.path
import subprocess
import sys
doc_sections = [
'summary',
'description',
# 'functions',
'example',
'author',
'maintainer',
'license',
'see_also',
]
doc_extract_bin = 'tools/doc_extract'
depends_bin = 'tools/ccan_depends'
def get_source_path(module):
return 'ccan/{module}'.format(module=module)
def get_dependencies(module):
args = [depends_bin, get_source_path(module)]
try:
dependencies = subprocess.check_output(args)
except subprocess.CalledProcessError:
return {}
else:
return {d.replace('ccan/', 'clibs/'): '*'
for d in dependencies.split('\n')
if d != ''}
def get_src(module):
src_path = get_source_path(module)
files = glob.glob('{src_path}/*.c'.format(src_path=src_path))
files.extend(glob.glob('{src_path}/*.h'.format(src_path=src_path)))
return files
def get_summary(module):
args = [doc_extract_bin, 'summary', "{src_path}/_info".format(src_path=get_source_path(module))]
try:
return subprocess.check_output(args).strip()
except subprocess.CalledProcessError:
return None
def ccan2repo(module, dst_path):
dst_path = dst_path + '/' + module
src_path = 'ccan/{module}'.format(module=module)
if not os.path.isfile('{src_path}/_info'.format(src_path=src_path)):
print "WARNING: {module} does not have a _info file".format(module=module)
return
try:
os.mkdir(dst_path)
except OSError:
pass
# Create package.json
package = {
"name": module,
"version": "master",
"repo": "rustyrussell/ccan",
"src": get_src(module),
}
summary = get_summary(module)
if summary:
package['description'] = summary
else:
print "WARNING: {module} does not have a summary".format(module=module)
dependencies = get_dependencies(module)
if dependencies:
package['dependencies'] = dependencies
packagejson = open(dst_path + "/package.json", "w")
packagejson.write(json.dumps(package, indent=2, sort_keys=True))
packagejson.close()
# Create source files
# for src_fname in get_src(module):
# dst_fname = src_fname.replace(src_path, module)
# open(dst_fname, 'w').write(open(src_fname, 'r').read())
# Create README.rst
readme = open(dst_path + "/README.rst", "w")
readme.write('This repository is a mirror of www.github.com/rustyrussell/ccan/tree/master/ccan/{module}\n\n'.format(module=module))
for section in doc_sections:
args = [doc_extract_bin, section, "{src_path}/_info".format(src_path=src_path)]
try:
text = subprocess.check_output(args).strip()
except subprocess.CalledProcessError:
continue
if text == '': continue
if section == 'example':
indent = ' ' * 4
text = ".. code-block:: c\n\n" + indent + ('\n' + indent).join(text.split('\n'))
readme.writelines([
section.title() + '\n',
len(section) * '-' + '\n',
text + '\n' * 2,
])
readme.close()
ccan2repo(sys.argv[1], sys.argv[2])