Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/smshex_r.../proc/self/root/bin
File: pathfix.py
#! /usr/libexec/platform-python
[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
ispythonprog = re.compile(r'^[a-zA-Z0-9_]+\.py$')
[91] Fix | Delete
[92] Fix | Delete
[93] Fix | Delete
def ispython(name):
[94] Fix | Delete
return bool(ispythonprog.match(name))
[95] Fix | Delete
[96] Fix | Delete
[97] Fix | Delete
def recursedown(dirname):
[98] Fix | Delete
dbg('recursedown(%r)\n' % (dirname,))
[99] Fix | Delete
bad = 0
[100] Fix | Delete
try:
[101] Fix | Delete
names = os.listdir(dirname)
[102] Fix | Delete
except OSError as msg:
[103] Fix | Delete
err('%s: cannot list directory: %r\n' % (dirname, msg))
[104] Fix | Delete
return 1
[105] Fix | Delete
names.sort()
[106] Fix | Delete
subdirs = []
[107] Fix | Delete
for name in names:
[108] Fix | Delete
if name in (os.curdir, os.pardir): continue
[109] Fix | Delete
fullname = os.path.join(dirname, name)
[110] Fix | Delete
if os.path.islink(fullname): pass
[111] Fix | Delete
elif os.path.isdir(fullname):
[112] Fix | Delete
subdirs.append(fullname)
[113] Fix | Delete
elif ispython(name):
[114] Fix | Delete
if fix(fullname): bad = 1
[115] Fix | Delete
for fullname in subdirs:
[116] Fix | Delete
if recursedown(fullname): bad = 1
[117] Fix | Delete
return bad
[118] Fix | Delete
[119] Fix | Delete
[120] Fix | Delete
def fix(filename):
[121] Fix | Delete
## dbg('fix(%r)\n' % (filename,))
[122] Fix | Delete
try:
[123] Fix | Delete
f = open(filename, 'rb')
[124] Fix | Delete
except IOError as msg:
[125] Fix | Delete
err('%s: cannot open: %r\n' % (filename, msg))
[126] Fix | Delete
return 1
[127] Fix | Delete
line = f.readline()
[128] Fix | Delete
fixed = fixline(line)
[129] Fix | Delete
if line == fixed:
[130] Fix | Delete
rep(filename+': no change\n')
[131] Fix | Delete
f.close()
[132] Fix | Delete
return
[133] Fix | Delete
head, tail = os.path.split(filename)
[134] Fix | Delete
tempname = os.path.join(head, '@' + tail)
[135] Fix | Delete
try:
[136] Fix | Delete
g = open(tempname, 'wb')
[137] Fix | Delete
except IOError as msg:
[138] Fix | Delete
f.close()
[139] Fix | Delete
err('%s: cannot create: %r\n' % (tempname, msg))
[140] Fix | Delete
return 1
[141] Fix | Delete
rep(filename + ': updating\n')
[142] Fix | Delete
g.write(fixed)
[143] Fix | Delete
BUFSIZE = 8*1024
[144] Fix | Delete
while 1:
[145] Fix | Delete
buf = f.read(BUFSIZE)
[146] Fix | Delete
if not buf: break
[147] Fix | Delete
g.write(buf)
[148] Fix | Delete
g.close()
[149] Fix | Delete
f.close()
[150] Fix | Delete
[151] Fix | Delete
# Finishing touch -- move files
[152] Fix | Delete
[153] Fix | Delete
mtime = None
[154] Fix | Delete
atime = None
[155] Fix | Delete
# First copy the file's mode to the temp file
[156] Fix | Delete
try:
[157] Fix | Delete
statbuf = os.stat(filename)
[158] Fix | Delete
mtime = statbuf.st_mtime
[159] Fix | Delete
atime = statbuf.st_atime
[160] Fix | Delete
os.chmod(tempname, statbuf[ST_MODE] & 0o7777)
[161] Fix | Delete
except OSError as msg:
[162] Fix | Delete
err('%s: warning: chmod failed (%r)\n' % (tempname, msg))
[163] Fix | Delete
# Then make a backup of the original file as filename~
[164] Fix | Delete
if create_backup:
[165] Fix | Delete
try:
[166] Fix | Delete
os.rename(filename, filename + '~')
[167] Fix | Delete
except OSError as msg:
[168] Fix | Delete
err('%s: warning: backup failed (%r)\n' % (filename, msg))
[169] Fix | Delete
else:
[170] Fix | Delete
try:
[171] Fix | Delete
os.remove(filename)
[172] Fix | Delete
except OSError as msg:
[173] Fix | Delete
err('%s: warning: removing failed (%r)\n' % (filename, msg))
[174] Fix | Delete
# Now move the temp file to the original file
[175] Fix | Delete
try:
[176] Fix | Delete
os.rename(tempname, filename)
[177] Fix | Delete
except OSError as msg:
[178] Fix | Delete
err('%s: rename failed (%r)\n' % (filename, msg))
[179] Fix | Delete
return 1
[180] Fix | Delete
if preserve_timestamps:
[181] Fix | Delete
if atime and mtime:
[182] Fix | Delete
try:
[183] Fix | Delete
os.utime(filename, (atime, mtime))
[184] Fix | Delete
except OSError as msg:
[185] Fix | Delete
err('%s: reset of timestamp failed (%r)\n' % (filename, msg))
[186] Fix | Delete
return 1
[187] Fix | Delete
# Return success
[188] Fix | Delete
return 0
[189] Fix | Delete
[190] Fix | Delete
[191] Fix | Delete
def parse_shebang(shebangline):
[192] Fix | Delete
shebangline = shebangline.rstrip(b'\n')
[193] Fix | Delete
start = shebangline.find(b' -')
[194] Fix | Delete
if start == -1:
[195] Fix | Delete
return b''
[196] Fix | Delete
return shebangline[start:]
[197] Fix | Delete
[198] Fix | Delete
[199] Fix | Delete
def populate_flags(shebangline):
[200] Fix | Delete
old_flags = b''
[201] Fix | Delete
if keep_flags:
[202] Fix | Delete
old_flags = parse_shebang(shebangline)
[203] Fix | Delete
if old_flags:
[204] Fix | Delete
old_flags = old_flags[2:]
[205] Fix | Delete
if not (old_flags or add_flags):
[206] Fix | Delete
return b''
[207] Fix | Delete
# On Linux, the entire string following the interpreter name
[208] Fix | Delete
# is passed as a single argument to the interpreter.
[209] Fix | Delete
# e.g. "#! /usr/bin/python3 -W Error -s" runs "/usr/bin/python3 "-W Error -s"
[210] Fix | Delete
# so shebang should have single '-' where flags are given and
[211] Fix | Delete
# flag might need argument for that reasons adding new flags is
[212] Fix | Delete
# between '-' and original flags
[213] Fix | Delete
# e.g. #! /usr/bin/python3 -sW Error
[214] Fix | Delete
return b' -' + add_flags + old_flags
[215] Fix | Delete
[216] Fix | Delete
[217] Fix | Delete
def fixline(line):
[218] Fix | Delete
if not line.startswith(b'#!'):
[219] Fix | Delete
return line
[220] Fix | Delete
[221] Fix | Delete
if b"python" not in line:
[222] Fix | Delete
return line
[223] Fix | Delete
[224] Fix | Delete
flags = populate_flags(line)
[225] Fix | Delete
return b'#! ' + new_interpreter + flags + b'\n'
[226] Fix | Delete
[227] Fix | Delete
[228] Fix | Delete
if __name__ == '__main__':
[229] Fix | Delete
main()
[230] Fix | Delete
[231] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function