Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/alt/python31.../bin
File: pathfix.py
#! /opt/alt/python312/bin/python3.12
[0] Fix | Delete
[1] Fix | Delete
import sys
[2] Fix | Delete
import os
[3] Fix | Delete
from stat import *
[4] Fix | Delete
import getopt
[5] Fix | Delete
[6] Fix | Delete
err = sys.stderr.write
[7] Fix | Delete
dbg = err
[8] Fix | Delete
rep = sys.stdout.write
[9] Fix | Delete
[10] Fix | Delete
new_interpreter = None
[11] Fix | Delete
preserve_timestamps = False
[12] Fix | Delete
create_backup = True
[13] Fix | Delete
keep_flags = False
[14] Fix | Delete
add_flags = b''
[15] Fix | Delete
[16] Fix | Delete
[17] Fix | Delete
def main():
[18] Fix | Delete
global new_interpreter
[19] Fix | Delete
global preserve_timestamps
[20] Fix | Delete
global create_backup
[21] Fix | Delete
global keep_flags
[22] Fix | Delete
global add_flags
[23] Fix | Delete
[24] Fix | Delete
usage = ('usage: %s -i /interpreter -p -n -k -a file-or-directory ...\n' %
[25] Fix | Delete
sys.argv[0])
[26] Fix | Delete
try:
[27] Fix | Delete
opts, args = getopt.getopt(sys.argv[1:], 'i:a:kpn')
[28] Fix | Delete
except getopt.error as msg:
[29] Fix | Delete
err(str(msg) + '\n')
[30] Fix | Delete
err(usage)
[31] Fix | Delete
sys.exit(2)
[32] Fix | Delete
for o, a in opts:
[33] Fix | Delete
if o == '-i':
[34] Fix | Delete
new_interpreter = a.encode()
[35] Fix | Delete
if o == '-p':
[36] Fix | Delete
preserve_timestamps = True
[37] Fix | Delete
if o == '-n':
[38] Fix | Delete
create_backup = False
[39] Fix | Delete
if o == '-k':
[40] Fix | Delete
keep_flags = True
[41] Fix | Delete
if o == '-a':
[42] Fix | Delete
add_flags = a.encode()
[43] Fix | Delete
if b' ' in add_flags:
[44] Fix | Delete
err("-a option doesn't support whitespaces")
[45] Fix | Delete
sys.exit(2)
[46] Fix | Delete
if not new_interpreter or not new_interpreter.startswith(b'/') or \
[47] Fix | Delete
not args:
[48] Fix | Delete
err('-i option or file-or-directory missing\n')
[49] Fix | Delete
err(usage)
[50] Fix | Delete
sys.exit(2)
[51] Fix | Delete
bad = 0
[52] Fix | Delete
for arg in args:
[53] Fix | Delete
if os.path.isdir(arg):
[54] Fix | Delete
if recursedown(arg): bad = 1
[55] Fix | Delete
elif os.path.islink(arg):
[56] Fix | Delete
err(arg + ': will not process symbolic links\n')
[57] Fix | Delete
bad = 1
[58] Fix | Delete
else:
[59] Fix | Delete
if fix(arg): bad = 1
[60] Fix | Delete
sys.exit(bad)
[61] Fix | Delete
[62] Fix | Delete
[63] Fix | Delete
def ispython(name):
[64] Fix | Delete
return name.endswith('.py')
[65] Fix | Delete
[66] Fix | Delete
[67] Fix | Delete
def recursedown(dirname):
[68] Fix | Delete
dbg('recursedown(%r)\n' % (dirname,))
[69] Fix | Delete
bad = 0
[70] Fix | Delete
try:
[71] Fix | Delete
names = os.listdir(dirname)
[72] Fix | Delete
except OSError as msg:
[73] Fix | Delete
err('%s: cannot list directory: %r\n' % (dirname, msg))
[74] Fix | Delete
return 1
[75] Fix | Delete
names.sort()
[76] Fix | Delete
subdirs = []
[77] Fix | Delete
for name in names:
[78] Fix | Delete
if name in (os.curdir, os.pardir): continue
[79] Fix | Delete
fullname = os.path.join(dirname, name)
[80] Fix | Delete
if os.path.islink(fullname): pass
[81] Fix | Delete
elif os.path.isdir(fullname):
[82] Fix | Delete
subdirs.append(fullname)
[83] Fix | Delete
elif ispython(name):
[84] Fix | Delete
if fix(fullname): bad = 1
[85] Fix | Delete
for fullname in subdirs:
[86] Fix | Delete
if recursedown(fullname): bad = 1
[87] Fix | Delete
return bad
[88] Fix | Delete
[89] Fix | Delete
[90] Fix | Delete
def fix(filename):
[91] Fix | Delete
## dbg('fix(%r)\n' % (filename,))
[92] Fix | Delete
try:
[93] Fix | Delete
f = open(filename, 'rb')
[94] Fix | Delete
except IOError as msg:
[95] Fix | Delete
err('%s: cannot open: %r\n' % (filename, msg))
[96] Fix | Delete
return 1
[97] Fix | Delete
with f:
[98] Fix | Delete
line = f.readline()
[99] Fix | Delete
fixed = fixline(line)
[100] Fix | Delete
if line == fixed:
[101] Fix | Delete
rep(filename+': no change\n')
[102] Fix | Delete
return
[103] Fix | Delete
head, tail = os.path.split(filename)
[104] Fix | Delete
tempname = os.path.join(head, '@' + tail)
[105] Fix | Delete
try:
[106] Fix | Delete
g = open(tempname, 'wb')
[107] Fix | Delete
except IOError as msg:
[108] Fix | Delete
err('%s: cannot create: %r\n' % (tempname, msg))
[109] Fix | Delete
return 1
[110] Fix | Delete
with g:
[111] Fix | Delete
rep(filename + ': updating\n')
[112] Fix | Delete
g.write(fixed)
[113] Fix | Delete
BUFSIZE = 8*1024
[114] Fix | Delete
while 1:
[115] Fix | Delete
buf = f.read(BUFSIZE)
[116] Fix | Delete
if not buf: break
[117] Fix | Delete
g.write(buf)
[118] Fix | Delete
[119] Fix | Delete
# Finishing touch -- move files
[120] Fix | Delete
[121] Fix | Delete
mtime = None
[122] Fix | Delete
atime = None
[123] Fix | Delete
# First copy the file's mode to the temp file
[124] Fix | Delete
try:
[125] Fix | Delete
statbuf = os.stat(filename)
[126] Fix | Delete
mtime = statbuf.st_mtime
[127] Fix | Delete
atime = statbuf.st_atime
[128] Fix | Delete
os.chmod(tempname, statbuf[ST_MODE] & 0o7777)
[129] Fix | Delete
except OSError as msg:
[130] Fix | Delete
err('%s: warning: chmod failed (%r)\n' % (tempname, msg))
[131] Fix | Delete
# Then make a backup of the original file as filename~
[132] Fix | Delete
if create_backup:
[133] Fix | Delete
try:
[134] Fix | Delete
os.rename(filename, filename + '~')
[135] Fix | Delete
except OSError as msg:
[136] Fix | Delete
err('%s: warning: backup failed (%r)\n' % (filename, msg))
[137] Fix | Delete
else:
[138] Fix | Delete
try:
[139] Fix | Delete
os.remove(filename)
[140] Fix | Delete
except OSError as msg:
[141] Fix | Delete
err('%s: warning: removing failed (%r)\n' % (filename, msg))
[142] Fix | Delete
# Now move the temp file to the original file
[143] Fix | Delete
try:
[144] Fix | Delete
os.rename(tempname, filename)
[145] Fix | Delete
except OSError as msg:
[146] Fix | Delete
err('%s: rename failed (%r)\n' % (filename, msg))
[147] Fix | Delete
return 1
[148] Fix | Delete
if preserve_timestamps:
[149] Fix | Delete
if atime and mtime:
[150] Fix | Delete
try:
[151] Fix | Delete
os.utime(filename, (atime, mtime))
[152] Fix | Delete
except OSError as msg:
[153] Fix | Delete
err('%s: reset of timestamp failed (%r)\n' % (filename, msg))
[154] Fix | Delete
return 1
[155] Fix | Delete
# Return success
[156] Fix | Delete
return 0
[157] Fix | Delete
[158] Fix | Delete
[159] Fix | Delete
def parse_shebang(shebangline):
[160] Fix | Delete
shebangline = shebangline.rstrip(b'\n')
[161] Fix | Delete
start = shebangline.find(b' -')
[162] Fix | Delete
if start == -1:
[163] Fix | Delete
return b''
[164] Fix | Delete
return shebangline[start:]
[165] Fix | Delete
[166] Fix | Delete
[167] Fix | Delete
def populate_flags(shebangline):
[168] Fix | Delete
old_flags = b''
[169] Fix | Delete
if keep_flags:
[170] Fix | Delete
old_flags = parse_shebang(shebangline)
[171] Fix | Delete
if old_flags:
[172] Fix | Delete
old_flags = old_flags[2:]
[173] Fix | Delete
if not (old_flags or add_flags):
[174] Fix | Delete
return b''
[175] Fix | Delete
# On Linux, the entire string following the interpreter name
[176] Fix | Delete
# is passed as a single argument to the interpreter.
[177] Fix | Delete
# e.g. "#! /usr/bin/python3 -W Error -s" runs "/usr/bin/python3 "-W Error -s"
[178] Fix | Delete
# so shebang should have single '-' where flags are given and
[179] Fix | Delete
# flag might need argument for that reasons adding new flags is
[180] Fix | Delete
# between '-' and original flags
[181] Fix | Delete
# e.g. #! /usr/bin/python3 -sW Error
[182] Fix | Delete
return b' -' + add_flags + old_flags
[183] Fix | Delete
[184] Fix | Delete
[185] Fix | Delete
def fixline(line):
[186] Fix | Delete
if not line.startswith(b'#!'):
[187] Fix | Delete
return line
[188] Fix | Delete
[189] Fix | Delete
if b"python" not in line:
[190] Fix | Delete
return line
[191] Fix | Delete
[192] Fix | Delete
flags = populate_flags(line)
[193] Fix | Delete
return b'#! ' + new_interpreter + flags + b'\n'
[194] Fix | Delete
[195] Fix | Delete
[196] Fix | Delete
if __name__ == '__main__':
[197] Fix | Delete
main()
[198] Fix | Delete
[199] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function