-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate_block_from_file.py
More file actions
executable file
·182 lines (146 loc) · 5.45 KB
/
update_block_from_file.py
File metadata and controls
executable file
·182 lines (146 loc) · 5.45 KB
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
#!/usr/bin/env python3
#encoding=utf-8
from os import listdir, remove, rename
from os.path import join, exists, isfile, islink, splitext
from pathlib import Path
import argparse
def update_block_between_files(first_path,second_path, begin_string, resume_string):
output_debug_message = False
#output_debug_message = True
output_filepath = first_path + ".tmp"
less_file = open(first_path, 'r')
more_file = open(second_path, 'r')
output_file = open(output_filepath, 'w')
left_part_Second_Begin = begin_string
left_part_Second_Begin_length = len(left_part_Second_Begin)
left_part_First_Resume = resume_string
left_part_First_Resume_length = len(left_part_First_Resume)
# first part use less.
is_less_file_seeked_begin = False
x_line = less_file.readline()
while x_line:
if left_part_Second_Begin == x_line[:left_part_Second_Begin_length]:
is_less_file_seeked_begin = True
break
output_file.write(x_line)
x_line = less_file.readline()
if output_debug_message:
print("first part interrupt:", is_less_file_seeked_begin)
# second part use more.
is_more_file_seeked_begin = False
x_line = more_file.readline()
while x_line:
if left_part_Second_Begin == x_line[:left_part_Second_Begin_length]:
is_more_file_seeked_begin = True
if left_part_First_Resume_length > 0:
if left_part_First_Resume == x_line[:left_part_First_Resume_length]:
# more part, end here.
break
is_our_line = False
if is_less_file_seeked_begin and is_more_file_seeked_begin:
#print("both file seeked.")
is_our_line = True
if is_our_line:
output_file.write(x_line)
x_line = more_file.readline()
if output_debug_message:
print("second part interrupt:", is_more_file_seeked_begin)
# third part use less.
is_our_line = False
if left_part_First_Resume_length > 0:
if is_less_file_seeked_begin and is_more_file_seeked_begin:
x_line = less_file.readline()
while x_line:
if not is_our_line:
if left_part_First_Resume == x_line[:left_part_First_Resume_length]:
is_our_line = True
if is_our_line:
output_file.write(x_line)
x_line = less_file.readline()
if output_debug_message:
print("third part interrupt:", is_our_line)
less_file.close()
more_file.close()
output_file.close()
remove(first_path)
rename(output_filepath, first_path)
def try_stat(path):
ret = False
from os import stat
try:
stat(path)
ret = True
except OSError as e:
pass
return ret
def copy_out(args):
second_path, first_path = args.second, args.first
begin_string, resume_string = args.begin, args.resume
# start to scan files.
print("first file:", first_path)
print("second file:", second_path)
print("begin_string:", begin_string)
print("resume_string:", resume_string)
file_count = 1
update_file_count = 0
is_pass_all_check = True
if is_pass_all_check:
# isfile will be False, if symlink
# under symlink, all is false @_@;
# please don't use under symlink.
if False:
print("exists(first_path)", exists(first_path))
print("isfile(first_path)", isfile(first_path))
print("islink(first_path)", islink(first_path))
print("is_symlink(first_path)", Path(first_path).is_symlink())
print('try_stat()',try_stat(first_path))
if not exists(first_path):
is_pass_all_check = False
print("Error: first path not exists:", first_path)
else:
if not isfile(first_path):
is_pass_all_check = False
print("Error: first file not exists:", first_path)
if is_pass_all_check:
# under symlink, all is false @_@;
# please don't use under symlink.
if not exists(second_path):
is_pass_all_check = False
print("Error: second path not exists:", second_path)
else:
if not isfile(second_path):
is_pass_all_check = False
print("Error: second file not exists:", second_path)
if is_pass_all_check:
if len(begin_string) == 0:
is_pass_all_check = False
print("Error: begin string is empty!")
if is_pass_all_check:
update_block_between_files(first_path,second_path, begin_string, resume_string)
update_file_count += 1
print("check file count:",file_count)
print("update file count:",update_file_count)
def cli():
parser = argparse.ArgumentParser(
description="Copy lost kerning info in glyphs to output folder")
parser.add_argument("--first",
help="first file path, will be updated.",
required=True,
type=str)
parser.add_argument("--second",
help="second file path, content weill be copied.",
required=True,
type=str)
parser.add_argument("--begin",
help="second file begin string",
required=True,
type=str)
parser.add_argument("--resume",
help="first file resume string",
default="",
required=False,
type=str)
args = parser.parse_args()
copy_out(args)
if __name__ == "__main__":
cli()