MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / fix

Function fix

tools/python-3.11.9-amd64/Tools/scripts/pathfix.py:119–185  ·  view source on GitHub ↗
(filename)

Source from the content-addressed store, hash-verified

117
118
119def fix(filename):
120## dbg('fix(%r)\n' % (filename,))
121 try:
122 f = open(filename, 'rb')
123 except IOError as msg:
124 err('%s: cannot open: %r\n' % (filename, msg))
125 return 1
126 with f:
127 line = f.readline()
128 fixed = fixline(line)
129 if line == fixed:
130 rep(filename+': no change\n')
131 return
132 head, tail = os.path.split(filename)
133 tempname = os.path.join(head, '@' + tail)
134 try:
135 g = open(tempname, 'wb')
136 except IOError as msg:
137 err('%s: cannot create: %r\n' % (tempname, msg))
138 return 1
139 with g:
140 rep(filename + ': updating\n')
141 g.write(fixed)
142 BUFSIZE = 8*1024
143 while 1:
144 buf = f.read(BUFSIZE)
145 if not buf: break
146 g.write(buf)
147
148 # Finishing touch -- move files
149
150 mtime = None
151 atime = None
152 # First copy the file's mode to the temp file
153 try:
154 statbuf = os.stat(filename)
155 mtime = statbuf.st_mtime
156 atime = statbuf.st_atime
157 os.chmod(tempname, statbuf[ST_MODE] & 0o7777)
158 except OSError as msg:
159 err('%s: warning: chmod failed (%r)\n' % (tempname, msg))
160 # Then make a backup of the original file as filename~
161 if create_backup:
162 try:
163 os.rename(filename, filename + '~')
164 except OSError as msg:
165 err('%s: warning: backup failed (%r)\n' % (filename, msg))
166 else:
167 try:
168 os.remove(filename)
169 except OSError as msg:
170 err('%s: warning: removing failed (%r)\n' % (filename, msg))
171 # Now move the temp file to the original file
172 try:
173 os.rename(tempname, filename)
174 except OSError as msg:
175 err('%s: rename failed (%r)\n' % (filename, msg))
176 return 1

Callers 2

mainFunction · 0.70
recursedownFunction · 0.70

Calls 12

utimeMethod · 0.80
fixlineFunction · 0.70
openFunction · 0.50
readlineMethod · 0.45
splitMethod · 0.45
joinMethod · 0.45
writeMethod · 0.45
readMethod · 0.45
statMethod · 0.45
chmodMethod · 0.45
renameMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected