-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake.py
executable file
·128 lines (112 loc) · 4.4 KB
/
make.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
#!/usr/bin/env python
import argparse
import subprocess
import logging
import os
import sys
from copy import copy
class Crate(object):
def __init__(self, path, namespace, template=None):
self.path = os.path.normcase(path)
self.namespace = namespace
self.template = template
# NOTE: The order of crates incorporates cross-crate dependencies.
# All crates that are dependencies of a crate in the list should precede
# that crate.
crates = [
Crate(path='crates/glib-sys',
namespace='GLib-2.0',
template='glib-sys.tmpl'),
Crate(path='crates/gobject-sys',
namespace='GObject-2.0',
template='gobject-sys.tmpl'),
Crate(path='crates/gio-sys',
namespace='Gio-2.0',
template='gio-sys.tmpl'),
]
python = sys.executable or 'python'
project_root = os.path.abspath(os.path.dirname(__file__))
tools_dir = os.path.join(project_root, 'tools')
def _filter_crates(paths):
if len(paths) == 0:
return copy(crates)
else:
filter_set = set([os.path.normcase(os.path.normpath(path))
for path in paths])
result = []
for crate in crates:
if crate.path in filter_set:
result.append(crate)
filter_set.remove(crate.path)
if len(filter_set) != 0:
if len(filter_set) == 1:
msg = "unknown crate path: '{}'".format(filter_set.pop())
else:
msg = ("unknown crate paths: "
+ ", ".join(["'{}'".format(p) for p in filter_set]))
raise ValueError(msg)
return result
def _run(args, **kwargs):
# TODO: escape the arguments for logging with shlex.quote(),
# when we live in the future and use Python 3.
if 'cwd' in kwargs:
logging.debug('Running (in %s) %s', kwargs['cwd'], ' '.join(args))
else:
logging.debug('Running %s', ' '.join(args))
kwargs['cwd'] = project_root
subprocess.check_call(args, **kwargs)
def install_tools(args):
if os.getenv('GI_RUST_NO_UPDATE_TOOLS'):
return
gen_srcdir = os.path.join(project_root, 'grust-gen')
pythonpath = list(sys.path)
del pythonpath[0]
pythonpath.insert(0, tools_dir)
tools_env = os.environ.copy()
tools_env['PYTHONPATH'] = os.pathsep.join(pythonpath)
cmd_args = [python, 'setup.py']
if not args.verbose:
cmd_args.append('--quiet')
cmd_args.extend(['develop', '--install-dir', tools_dir])
_run(cmd_args, env=tools_env, cwd=gen_srcdir)
def generate(args):
install_tools(args)
grust_gen = os.path.join(tools_dir, 'grust-gen')
for crate in _filter_crates(args.crate_paths):
cmd_args = [grust_gen, '--sys', '--include-dir', 'gir']
if crate.template:
cmd_args.extend(['--template',
os.path.join(crate.path, crate.template)])
cmd_args.extend(['--output', os.path.join(crate.path, 'lib.rs')])
cmd_args.append(os.path.join('gir', crate.namespace + '.gir'))
_run(cmd_args)
def _add_crate_paths_argument(parser):
parser.add_argument('crate_paths', metavar='PATH', nargs='*',
help='''Paths of crate submodules to process,
relative to the project root.
By default, all crates are processed.''')
def _get_arg_parser():
parser = argparse.ArgumentParser(
description='''
Performs build tasks for the crates in this project.''')
parser.add_argument('--verbose', action='store_true',
help='produce verbose output')
subparsers = parser.add_subparsers(title='subcommands',
help='one of the available commands')
subp_generate = subparsers.add_parser(
'generate',
description='''
Generate code for the crates in the project,
using the GIR files from submodule 'gir'.''',
help='generate code for the crates')
subp_generate.set_defaults(command_func=generate)
_add_crate_paths_argument(subp_generate)
return parser
if __name__ == '__main__':
arg_parser = _get_arg_parser()
args = arg_parser.parse_args()
logging.basicConfig(
level=logging.DEBUG if args.verbose else logging.INFO,
format='%(module)s:%(levelname)s: %(message)s')
command_func = args.command_func
command_func(args)