-
Notifications
You must be signed in to change notification settings - Fork 259
/
Copy pathpatch_translated_code.py
executable file
·135 lines (124 loc) · 4.05 KB
/
patch_translated_code.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from common import Config
from plumbum.cmd import echo, perl
from plumbum import local
from typing import Iterable, Tuple
import argparse
import os
config = Config()
C2RUST_DIR = config.ROOT_DIR
LIBXML2_REPO = os.path.join(C2RUST_DIR, "examples/libxml2/repo")
RUST_ROOT_DIR = os.path.join(LIBXML2_REPO, "rust")
# TODO(kkysen) shouldn't need `extern crate`s
PATCHES = {
"examples/xmllint.rs": {
"replace_all": [
("extern crate libc;", "extern crate libc;\nextern crate libxml2_rs;"),
],
},
"examples/runtest.rs": {
"replace_all": [
("extern crate libc;", "extern crate libc;\nextern crate libxml2_rs;"),
],
},
"examples/testC14N.rs": {
"replace_all": [
("extern crate libc;", "extern crate libc;\nextern crate libxml2_rs;"),
],
},
"examples/testHTML.rs": {
"replace_all": [
("extern crate libc;", "extern crate libc;\nextern crate libxml2_rs;"),
],
},
"examples/testReader.rs": {
"replace_all": [
("extern crate libc;", "extern crate libc;\nextern crate libxml2_rs;"),
],
},
"examples/testRelax.rs": {
"replace_all": [
("extern crate libc;", "extern crate libc;\nextern crate libxml2_rs;"),
],
},
"examples/testThreads.rs": {
"replace_all": [
("extern crate libc;", "extern crate libc;\nextern crate libxml2_rs;"),
],
},
"examples/testapi.rs": {
"replace_all": [
("extern crate libc;", "extern crate libc;\nextern crate libxml2_rs;"),
],
},
"examples/testchar.rs": {
"replace_all": [
("extern crate libc;", "extern crate libc;\nextern crate libxml2_rs;"),
],
},
"examples/testlimits.rs": {
"replace_all": [
("extern crate libc;", "extern crate libc;\nextern crate libxml2_rs;"),
],
},
"examples/testrecurse.rs": {
"replace_all": [
("extern crate libc;", "extern crate libc;\nextern crate libxml2_rs;"),
],
},
"examples/testSAX.rs": {
"replace_all": [
("extern crate libc;", "extern crate libc;\nextern crate libxml2_rs;"),
],
},
"examples/testURI.rs": {
"replace_all": [
("extern crate libc;", "extern crate libc;\nextern crate libxml2_rs;"),
],
},
"examples/testAutomata.rs": {
"replace_all": [
("extern crate libc;", "extern crate libc;\nextern crate libxml2_rs;"),
],
},
"examples/testdict.rs": {
"replace_all": [
("extern crate libc;", "extern crate libc;\nextern crate libxml2_rs;"),
],
},
"examples/testModule.rs": {
"replace_all": [
("extern crate libc;", "extern crate libc;\nextern crate libxml2_rs;"),
],
},
"examples/testRegexp.rs": {
"replace_all": [
("extern crate libc;", "extern crate libc;\nextern crate libxml2_rs;"),
],
},
"examples/testSchemas.rs": {
"replace_all": [
("extern crate libc;", "extern crate libc;\nextern crate libxml2_rs;"),
],
},
"examples/testXPath.rs": {
"replace_all": [
("extern crate libc;", "extern crate libc;\nextern crate libxml2_rs;"),
],
},
}
# TODO: Better error handling
def replace_all(file_path: str, replacements: Iterable[Tuple[str, str]]) -> None:
perl_args = ["-000", "-pi"]
for replace_from, replace_to in replacements:
perl_args.append("-e")
perl_args.append("s/{}/{}/g;".format(replace_from, replace_to))
perl_args.append(file_path)
retcode, stdout, stderr = perl[perl_args].run()
assert retcode != 1, "Failed to apply patch {}/replace_all:\n{}".format(file_name, stderr)
if __name__ == "__main__":
for file_name, patch_config in PATCHES.items():
file_path = os.path.join(RUST_ROOT_DIR, file_name)
if "replace_all" in patch_config:
replace_all(file_path, patch_config["replace_all"])