This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathcombine_docfiles.py
executable file
·55 lines (45 loc) · 1.69 KB
/
combine_docfiles.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
#!/usr/bin/env python3
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
''' Combine file from:
* script argument 1
with content of file from:
* script argument 2
using the beginning of line separators
hardcoded using regexes in this file:
We exclude any text using the separate
regex specified here
'''
import os
import re
import sys
insert_separator_regex = r'(.*?\[\^\]\:\ \(autogen_docs_start\))(.*?)(\n\[\^\]\:\ \(autogen_docs_end\).*?$)' # noqa: E501
exclude_separator_regex = r'(.*?)Copyright 20\d\d Google LLC.*?limitations under the License.(.*?)$' # noqa: E501
if len(sys.argv) != 3:
sys.exit(1)
if not os.path.isfile(sys.argv[1]):
sys.exit(0)
input = open(sys.argv[1], "r").read()
replace_content = open(sys.argv[2], "r").read()
# Exclude the specified content from the replacement content
groups = re.match(
exclude_separator_regex,
replace_content,
re.DOTALL
).groups(0)
replace_content = groups[0] + groups[1]
# Find where to put the replacement content, overwrite the input file
groups = re.match(insert_separator_regex, input, re.DOTALL).groups(0)
output = groups[0] + replace_content + groups[2] + "\n"
open(sys.argv[1], "w").write(output)