Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../opt/alt/python31.../bin
File: pathfix.py
#! /opt/alt/python310/bin/python3.10
[0] Fix | Delete
[1] Fix | Delete
# Change the #! line (shebang) occurring in Python scripts. The new interpreter
[2] Fix | Delete
# pathname must be given with a -i option.
[3] Fix | Delete
#
[4] Fix | Delete
# Command line arguments are files or directories to be processed.
[5] Fix | Delete
# Directories are searched recursively for files whose name looks
[6] Fix | Delete
# like a python module.
[7] Fix | Delete
# Symbolic links are always ignored (except as explicit directory
[8] Fix | Delete
# arguments).
[9] Fix | Delete
# The original file is kept as a back-up (with a "~" attached to its name),
[10] Fix | Delete
# -n flag can be used to disable this.
[11] Fix | Delete
[12] Fix | Delete
# Sometimes you may find shebangs with flags such as `#! /usr/bin/env python -si`.
[13] Fix | Delete
# Normally, pathfix overwrites the entire line, including the flags.
[14] Fix | Delete
# To change interpreter and keep flags from the original shebang line, use -k.
[15] Fix | Delete
# If you want to keep flags and add to them one single literal flag, use option -a.
[16] Fix | Delete
[17] Fix | Delete
[18] Fix | Delete
# Undoubtedly you can do this using find and sed or perl, but this is
[19] Fix | Delete
# a nice example of Python code that recurses down a directory tree
[20] Fix | Delete
# and uses regular expressions. Also note several subtleties like
[21] Fix | Delete
# preserving the file's mode and avoiding to even write a temp file
[22] Fix | Delete
# when no changes are needed for a file.
[23] Fix | Delete
#
[24] Fix | Delete
# NB: by changing only the function fixfile() you can turn this
[25] Fix | Delete
# into a program for a different change to Python programs...
[26] Fix | Delete
[27] Fix | Delete
import sys
[28] Fix | Delete
import re
[29] Fix | Delete
import os
[30] Fix | Delete
from stat import *
[31] Fix | Delete
import getopt
[32] Fix | Delete
[33] Fix | Delete
err = sys.stderr.write
[34] Fix | Delete
dbg = err
[35] Fix | Delete
rep = sys.stdout.write
[36] Fix | Delete
[37] Fix | Delete
new_interpreter = None
[38] Fix | Delete
preserve_timestamps = False
[39] Fix | Delete
create_backup = True
[40] Fix | Delete
keep_flags = False
[41] Fix | Delete
add_flags = b''
[42] Fix | Delete
[43] Fix | Delete
[44] Fix | Delete
def main():
[45] Fix | Delete
global new_interpreter
[46] Fix | Delete
global preserve_timestamps
[47] Fix | Delete
global create_backup
[48] Fix | Delete
global keep_flags
[49] Fix | Delete
global add_flags
[50] Fix | Delete
[51] Fix | Delete
usage = ('usage: %s -i /interpreter -p -n -k -a file-or-directory ...\n' %
[52] Fix | Delete
sys.argv[0])
[53] Fix | Delete
try:
[54] Fix | Delete
opts, args = getopt.getopt(sys.argv[1:], 'i:a:kpn')
[55] Fix | Delete
except getopt.error as msg:
[56] Fix | Delete
err(str(msg) + '\n')
[57] Fix | Delete
err(usage)
[58] Fix | Delete
sys.exit(2)
[59] Fix | Delete
for o, a in opts:
[60] Fix | Delete
if o == '-i':
[61] Fix | Delete
new_interpreter = a.encode()
[62] Fix | Delete
if o == '-p':
[63] Fix | Delete
preserve_timestamps = True
[64] Fix | Delete
if o == '-n':
[65] Fix | Delete
create_backup = False
[66] Fix | Delete
if o == '-k':
[67] Fix | Delete
keep_flags = True
[68] Fix | Delete
if o == '-a':
[69] Fix | Delete
add_flags = a.encode()
[70] Fix | Delete
if b' ' in add_flags:
[71] Fix | Delete
err("-a option doesn't support whitespaces")
[72] Fix | Delete
sys.exit(2)
[73] Fix | Delete
if not new_interpreter or not new_interpreter.startswith(b'/') or \
[74] Fix | Delete
not args:
[75] Fix | Delete
err('-i option or file-or-directory missing\n')
[76] Fix | Delete
err(usage)
[77] Fix | Delete
sys.exit(2)
[78] Fix | Delete
bad = 0
[79] Fix | Delete
for arg in args:
[80] Fix | Delete
if os.path.isdir(arg):
[81] Fix | Delete
if recursedown(arg): bad = 1
[82] Fix | Delete
elif os.path.islink(arg):
[83] Fix | Delete
err(arg + ': will not process symbolic links\n')
[84] Fix | Delete
bad = 1
[85] Fix | Delete
else:
[86] Fix | Delete
if fix(arg): bad = 1
[87] Fix | Delete
sys.exit(bad)
[88] Fix | Delete
[89] Fix | Delete
[90] Fix | Delete
def ispython(name):
[91] Fix | Delete
return name.endswith('.py')
[92] Fix | Delete
[93] Fix | Delete
[94] Fix | Delete
def recursedown(dirname):
[95] Fix | Delete
dbg('recursedown(%r)\n' % (dirname,))
[96] Fix | Delete
bad = 0
[97] Fix | Delete
try:
[98] Fix | Delete
names = os.listdir(dirname)
[99] Fix | Delete
except OSError as msg:
[100] Fix | Delete
err('%s: cannot list directory: %r\n' % (dirname, msg))
[101] Fix | Delete
return 1
[102] Fix | Delete
names.sort()
[103] Fix | Delete
subdirs = []
[104] Fix | Delete
for name in names:
[105] Fix | Delete
if name in (os.curdir, os.pardir): continue
[106] Fix | Delete
fullname = os.path.join(dirname, name)
[107] Fix | Delete
if os.path.islink(fullname): pass
[108] Fix | Delete
elif os.path.isdir(fullname):
[109] Fix | Delete
subdirs.append(fullname)
[110] Fix | Delete
elif ispython(name):
[111] Fix | Delete
if fix(fullname): bad = 1
[112] Fix | Delete
for fullname in subdirs:
[113] Fix | Delete
if recursedown(fullname): bad = 1
[114] Fix | Delete
return bad
[115] Fix | Delete
[116] Fix | Delete
[117] Fix | Delete
def fix(filename):
[118] Fix | Delete
## dbg('fix(%r)\n' % (filename,))
[119] Fix | Delete
try:
[120] Fix | Delete
f = open(filename, 'rb')
[121] Fix | Delete
except IOError as msg:
[122] Fix | Delete
err('%s: cannot open: %r\n' % (filename, msg))
[123] Fix | Delete
return 1
[124] Fix | Delete
with f:
[125] Fix | Delete
line = f.readline()
[126] Fix | Delete
fixed = fixline(line)
[127] Fix | Delete
if line == fixed:
[128] Fix | Delete
rep(filename+': no change\n')
[129] Fix | Delete
return
[130] Fix | Delete
head, tail = os.path.split(filename)
[131] Fix | Delete
tempname = os.path.join(head, '@' + tail)
[132] Fix | Delete
try:
[133] Fix | Delete
g = open(tempname, 'wb')
[134] Fix | Delete
except IOError as msg:
[135] Fix | Delete
err('%s: cannot create: %r\n' % (tempname, msg))
[136] Fix | Delete
return 1
[137] Fix | Delete
with g:
[138] Fix | Delete
rep(filename + ': updating\n')
[139] Fix | Delete
g.write(fixed)
[140] Fix | Delete
BUFSIZE = 8*1024
[141] Fix | Delete
while 1:
[142] Fix | Delete
buf = f.read(BUFSIZE)
[143] Fix | Delete
if not buf: break
[144] Fix | Delete
g.write(buf)
[145] Fix | Delete
[146] Fix | Delete
# Finishing touch -- move files
[147] Fix | Delete
[148] Fix | Delete
mtime = None
[149] Fix | Delete
atime = None
[150] Fix | Delete
# First copy the file's mode to the temp file
[151] Fix | Delete
try:
[152] Fix | Delete
statbuf = os.stat(filename)
[153] Fix | Delete
mtime = statbuf.st_mtime
[154] Fix | Delete
atime = statbuf.st_atime
[155] Fix | Delete
os.chmod(tempname, statbuf[ST_MODE] & 0o7777)
[156] Fix | Delete
except OSError as msg:
[157] Fix | Delete
err('%s: warning: chmod failed (%r)\n' % (tempname, msg))
[158] Fix | Delete
# Then make a backup of the original file as filename~
[159] Fix | Delete
if create_backup:
[160] Fix | Delete
try:
[161] Fix | Delete
os.rename(filename, filename + '~')
[162] Fix | Delete
except OSError as msg:
[163] Fix | Delete
err('%s: warning: backup failed (%r)\n' % (filename, msg))
[164] Fix | Delete
else:
[165] Fix | Delete
try:
[166] Fix | Delete
os.remove(filename)
[167] Fix | Delete
except OSError as msg:
[168] Fix | Delete
err('%s: warning: removing failed (%r)\n' % (filename, msg))
[169] Fix | Delete
# Now move the temp file to the original file
[170] Fix | Delete
try:
[171] Fix | Delete
os.rename(tempname, filename)
[172] Fix | Delete
except OSError as msg:
[173] Fix | Delete
err('%s: rename failed (%r)\n' % (filename, msg))
[174] Fix | Delete
return 1
[175] Fix | Delete
if preserve_timestamps:
[176] Fix | Delete
if atime and mtime:
[177] Fix | Delete
try:
[178] Fix | Delete
os.utime(filename, (atime, mtime))
[179] Fix | Delete
except OSError as msg:
[180] Fix | Delete
err('%s: reset of timestamp failed (%r)\n' % (filename, msg))
[181] Fix | Delete
return 1
[182] Fix | Delete
# Return success
[183] Fix | Delete
return 0
[184] Fix | Delete
[185] Fix | Delete
[186] Fix | Delete
def parse_shebang(shebangline):
[187] Fix | Delete
shebangline = shebangline.rstrip(b'\n')
[188] Fix | Delete
start = shebangline.find(b' -')
[189] Fix | Delete
if start == -1:
[190] Fix | Delete
return b''
[191] Fix | Delete
return shebangline[start:]
[192] Fix | Delete
[193] Fix | Delete
[194] Fix | Delete
def populate_flags(shebangline):
[195] Fix | Delete
old_flags = b''
[196] Fix | Delete
if keep_flags:
[197] Fix | Delete
old_flags = parse_shebang(shebangline)
[198] Fix | Delete
if old_flags:
[199] Fix | Delete
old_flags = old_flags[2:]
[200] Fix | Delete
if not (old_flags or add_flags):
[201] Fix | Delete
return b''
[202] Fix | Delete
# On Linux, the entire string following the interpreter name
[203] Fix | Delete
# is passed as a single argument to the interpreter.
[204] Fix | Delete
# e.g. "#! /usr/bin/python3 -W Error -s" runs "/usr/bin/python3 "-W Error -s"
[205] Fix | Delete
# so shebang should have single '-' where flags are given and
[206] Fix | Delete
# flag might need argument for that reasons adding new flags is
[207] Fix | Delete
# between '-' and original flags
[208] Fix | Delete
# e.g. #! /usr/bin/python3 -sW Error
[209] Fix | Delete
return b' -' + add_flags + old_flags
[210] Fix | Delete
[211] Fix | Delete
[212] Fix | Delete
def fixline(line):
[213] Fix | Delete
if not line.startswith(b'#!'):
[214] Fix | Delete
return line
[215] Fix | Delete
[216] Fix | Delete
if b"python" not in line:
[217] Fix | Delete
return line
[218] Fix | Delete
[219] Fix | Delete
flags = populate_flags(line)
[220] Fix | Delete
return b'#! ' + new_interpreter + flags + b'\n'
[221] Fix | Delete
[222] Fix | Delete
[223] Fix | Delete
if __name__ == '__main__':
[224] Fix | Delete
main()
[225] Fix | Delete
[226] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function