Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../usr/share/Modules/bin
File: createmodule.py
#!/usr/libexec/platform-python
[0] Fix | Delete
#
[1] Fix | Delete
# createmodule.py - Takes the name of a environment init script and
[2] Fix | Delete
# produces a modulefile that duplicates the changes made by the init script
[3] Fix | Delete
#
[4] Fix | Delete
# Copyright (C) 2012 by Orion E. Poplawski <orion@cora.nwra.com>
[5] Fix | Delete
#
[6] Fix | Delete
# This program is free software: you can redistribute it and/or modify
[7] Fix | Delete
# it under the terms of the GNU General Public License as published by
[8] Fix | Delete
# the Free Software Foundation, either version 2 of the License, or
[9] Fix | Delete
# (at your option) any later version.
[10] Fix | Delete
[11] Fix | Delete
# This program is distributed in the hope that it will be useful,
[12] Fix | Delete
# but WITHOUT ANY WARRANTY; without even the implied warranty of
[13] Fix | Delete
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
[14] Fix | Delete
# GNU General Public License for more details.
[15] Fix | Delete
[16] Fix | Delete
# You should have received a copy of the GNU General Public License
[17] Fix | Delete
# along with this program. If not, see <http://www.gnu.org/licenses/>.
[18] Fix | Delete
from __future__ import print_function
[19] Fix | Delete
[20] Fix | Delete
from optparse import OptionParser
[21] Fix | Delete
import os,sys,re
[22] Fix | Delete
from subprocess import *
[23] Fix | Delete
import platform
[24] Fix | Delete
[25] Fix | Delete
# Handle options
[26] Fix | Delete
usage = "Usage: %prog [-p prefix] <initscript> [args]"
[27] Fix | Delete
parser = OptionParser()
[28] Fix | Delete
parser.set_usage(usage)
[29] Fix | Delete
parser.add_option('-p', '--prefix', action='store', type='string', dest='prefix', help='Specify path prefix')
[30] Fix | Delete
parser.add_option('--noprefix', action='store_true', dest='noprefix', default=False, help='Do not generate a prefix')
[31] Fix | Delete
(options, args) = parser.parse_args()
[32] Fix | Delete
[33] Fix | Delete
# Need a script name
[34] Fix | Delete
if not args:
[35] Fix | Delete
parser.print_usage()
[36] Fix | Delete
exit(1)
[37] Fix | Delete
[38] Fix | Delete
# Determine if running environment is based on cmd.exe or not
[39] Fix | Delete
def iscmdshell():
[40] Fix | Delete
return True if platform.system() == 'Windows' else False
[41] Fix | Delete
[42] Fix | Delete
# Return environment after a command
[43] Fix | Delete
def getenv(cmd = ':'):
[44] Fix | Delete
env = {}
[45] Fix | Delete
if iscmdshell():
[46] Fix | Delete
# ':' command not supported by cmd.exe
[47] Fix | Delete
cmd = (cmd if cmd != ':' else '@echo off') + " >nul & set"
[48] Fix | Delete
else:
[49] Fix | Delete
cmd = cmd + " >/dev/null;env"
[50] Fix | Delete
p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE, universal_newlines=True)
[51] Fix | Delete
(stdout, stderr) = p.communicate()
[52] Fix | Delete
if p.returncode != 0:
[53] Fix | Delete
print("ERROR: Could not execute initscript:")
[54] Fix | Delete
print("%s returned exit code %d" % (cmd, p.returncode))
[55] Fix | Delete
print(stderr)
[56] Fix | Delete
exit(1)
[57] Fix | Delete
if stderr != '':
[58] Fix | Delete
print("WARNING: initscript sent the following to stderr:")
[59] Fix | Delete
print(stderr)
[60] Fix | Delete
# Parse the output key=value pairs
[61] Fix | Delete
skip = False
[62] Fix | Delete
for line in stdout.splitlines():
[63] Fix | Delete
if skip:
[64] Fix | Delete
if line == '}':
[65] Fix | Delete
skip = False
[66] Fix | Delete
continue
[67] Fix | Delete
elif iscmdshell() and line.find('=') == -1:
[68] Fix | Delete
continue
[69] Fix | Delete
try:
[70] Fix | Delete
(var,value) = line.split('=',1)
[71] Fix | Delete
except ValueError:
[72] Fix | Delete
print("ERROR: Could not parse output line:")
[73] Fix | Delete
print(line)
[74] Fix | Delete
exit(1)
[75] Fix | Delete
# Exported functions - not handled
[76] Fix | Delete
if value.find('() {') == 0:
[77] Fix | Delete
skip = True
[78] Fix | Delete
else:
[79] Fix | Delete
env[var] = value
[80] Fix | Delete
return env
[81] Fix | Delete
[82] Fix | Delete
#Record initial environment
[83] Fix | Delete
env1=getenv()
[84] Fix | Delete
[85] Fix | Delete
#Record environment after sourcing the initscript
[86] Fix | Delete
if iscmdshell():
[87] Fix | Delete
if len(args)>1:
[88] Fix | Delete
env2=getenv('"' + args[0] + '" ' + " ".join(args[1:]))
[89] Fix | Delete
else:
[90] Fix | Delete
env2=getenv('"' + args[0] + '"')
[91] Fix | Delete
else:
[92] Fix | Delete
env2=getenv(". " + " ".join(args))
[93] Fix | Delete
[94] Fix | Delete
# Initialize our variables for storing modifications
[95] Fix | Delete
chdir = None
[96] Fix | Delete
appendpath = {}
[97] Fix | Delete
prependpath = {}
[98] Fix | Delete
unhandled = {}
[99] Fix | Delete
setenv = {}
[100] Fix | Delete
unsetenv = []
[101] Fix | Delete
pathnames = []
[102] Fix | Delete
[103] Fix | Delete
# Function to nomalize all paths in a list of paths and remove duplicate items
[104] Fix | Delete
def normpaths(paths):
[105] Fix | Delete
newpaths = []
[106] Fix | Delete
for path in paths:
[107] Fix | Delete
normpath = os.path.normpath(path)
[108] Fix | Delete
if normpath not in newpaths and normpath != '.':
[109] Fix | Delete
newpaths.append(os.path.normpath(path))
[110] Fix | Delete
return newpaths
[111] Fix | Delete
[112] Fix | Delete
# Start with existing keys and look for changes
[113] Fix | Delete
for key in env1.keys():
[114] Fix | Delete
# Test for delete
[115] Fix | Delete
if key not in env2:
[116] Fix | Delete
unsetenv.append(key)
[117] Fix | Delete
continue
[118] Fix | Delete
# No change
[119] Fix | Delete
if env1[key] == env2[key]:
[120] Fix | Delete
del env2[key]
[121] Fix | Delete
continue
[122] Fix | Delete
#Working directory change
[123] Fix | Delete
if key == 'PWD':
[124] Fix | Delete
chdir=os.path.normpath(env2[key])
[125] Fix | Delete
pathnames.append(chdir)
[126] Fix | Delete
del env2[key]
[127] Fix | Delete
continue
[128] Fix | Delete
# Determine modifcations to beginning and end of the string
[129] Fix | Delete
try:
[130] Fix | Delete
(prepend,append) = env2[key].split(env1[key])
[131] Fix | Delete
except ValueError:
[132] Fix | Delete
continue
[133] Fix | Delete
if prepend:
[134] Fix | Delete
presep = prepend[-1:]
[135] Fix | Delete
prependpaths = prepend.strip(presep).split(presep)
[136] Fix | Delete
# LICENSE variables often include paths outside install directory
[137] Fix | Delete
if 'LICENSE' not in key:
[138] Fix | Delete
pathnames += prependpaths
[139] Fix | Delete
if presep not in prependpath:
[140] Fix | Delete
prependpath[presep] = {}
[141] Fix | Delete
newpath = presep.join(normpaths(prependpaths))
[142] Fix | Delete
if newpath:
[143] Fix | Delete
prependpath[presep][key] = newpath
[144] Fix | Delete
else:
[145] Fix | Delete
unhandled[key] = env2[key]
[146] Fix | Delete
if append:
[147] Fix | Delete
appsep = append[0:1]
[148] Fix | Delete
appendpaths = append.strip(appsep).split(appsep)
[149] Fix | Delete
# LICENSE variables often include paths outside install directory
[150] Fix | Delete
if 'LICENSE' not in key:
[151] Fix | Delete
pathnames += appendpaths
[152] Fix | Delete
if appsep not in appendpath:
[153] Fix | Delete
appendpath[appsep] = {}
[154] Fix | Delete
newpath = appsep.join(normpaths(appendpaths))
[155] Fix | Delete
if newpath:
[156] Fix | Delete
appendpath[appsep][key] = newpath
[157] Fix | Delete
else:
[158] Fix | Delete
unhandled[key] = env2[key]
[159] Fix | Delete
del env2[key]
[160] Fix | Delete
[161] Fix | Delete
# We're left with new keys in env2
[162] Fix | Delete
for key in env2.keys():
[163] Fix | Delete
# Use prepend-path for new paths
[164] Fix | Delete
if (re.search('(DIRS|FILES|PATH)$',key)) or (':' in env2[key]):
[165] Fix | Delete
prependpaths = env2[key].strip(':').split(':')
[166] Fix | Delete
# MANPATH can have system defaults added it it wasn't previously set
[167] Fix | Delete
# LICENSE variables often include paths outside install directory
[168] Fix | Delete
if key != 'MANPATH' and 'LICENSE' not in key:
[169] Fix | Delete
pathnames += prependpaths
[170] Fix | Delete
if ':' not in prependpath:
[171] Fix | Delete
prependpath[':'] = {}
[172] Fix | Delete
prependpath[':'][key] = ':'.join(normpaths(prependpaths))
[173] Fix | Delete
continue
[174] Fix | Delete
# Set new variables
[175] Fix | Delete
setenv[key] = os.path.normpath(env2[key])
[176] Fix | Delete
if 'LICENSE' not in key:
[177] Fix | Delete
pathnames.append(setenv[key])
[178] Fix | Delete
[179] Fix | Delete
# Report unhandled keys
[180] Fix | Delete
for key in unhandled.keys():
[181] Fix | Delete
print("Unhandled change of", key, file=sys.stderr)
[182] Fix | Delete
print("Before <%s>" % env1[key], file=sys.stderr)
[183] Fix | Delete
print("After <%s>" % unhandled[key], file=sys.stderr)
[184] Fix | Delete
for sepkey in appendpath.keys():
[185] Fix | Delete
appendpath[sepkey].pop(key, None)
[186] Fix | Delete
for sepkey in prependpath.keys():
[187] Fix | Delete
prependpath[sepkey].pop(key, None)
[188] Fix | Delete
[189] Fix | Delete
# Determine a prefix
[190] Fix | Delete
prefix=None
[191] Fix | Delete
if options.prefix:
[192] Fix | Delete
prefix = options.prefix
[193] Fix | Delete
elif not options.noprefix:
[194] Fix | Delete
prefix = os.path.commonprefix(pathnames).rstrip('/')
[195] Fix | Delete
if prefix == '':
[196] Fix | Delete
prefix = None
[197] Fix | Delete
[198] Fix | Delete
# Print out the modulefile
[199] Fix | Delete
print("#%Module 1.0")
[200] Fix | Delete
[201] Fix | Delete
# Prefix
[202] Fix | Delete
if prefix is not None:
[203] Fix | Delete
print("\nset prefix " + prefix + "\n")
[204] Fix | Delete
[205] Fix | Delete
# Chdir
[206] Fix | Delete
if chdir is not None:
[207] Fix | Delete
print("chdir\t" + chdir)
[208] Fix | Delete
[209] Fix | Delete
# Function to format output line with tabs and substituting prefix
[210] Fix | Delete
def formatline(item, key, value=None):
[211] Fix | Delete
print(item, end=' ')
[212] Fix | Delete
print("\t"*(2-(len(item)+1)//8), end=' ')
[213] Fix | Delete
print(key, end=' ')
[214] Fix | Delete
if value is not None:
[215] Fix | Delete
print("\t"*(3-(len(key)+1)//8), end=' ')
[216] Fix | Delete
if prefix is not None:
[217] Fix | Delete
# Prefer usage of regular expression to perform a none
[218] Fix | Delete
# case-sensitive substitution (cygwin vs cmd.exe)
[219] Fix | Delete
if iscmdshell():
[220] Fix | Delete
print(re.sub('(?i)' + re.escape(prefix), '$prefix', value))
[221] Fix | Delete
else:
[222] Fix | Delete
print(value.replace(prefix,'$prefix'))
[223] Fix | Delete
else:
[224] Fix | Delete
print(value)
[225] Fix | Delete
[226] Fix | Delete
# Paths first, grouped by variable name
[227] Fix | Delete
for sepkey in prependpath.keys():
[228] Fix | Delete
pathkeys = list(prependpath[sepkey].keys())
[229] Fix | Delete
pathkeys.sort()
[230] Fix | Delete
for key in pathkeys:
[231] Fix | Delete
if sepkey == ":":
[232] Fix | Delete
formatline("prepend-path",key,prependpath[sepkey][key])
[233] Fix | Delete
else:
[234] Fix | Delete
formatline("prepend-path --delim %s" % sepkey,key,prependpath[sepkey][key])
[235] Fix | Delete
[236] Fix | Delete
for sepkey in appendpath.keys():
[237] Fix | Delete
pathkeys = list(appendpath[sepkey].keys())
[238] Fix | Delete
pathkeys.sort()
[239] Fix | Delete
for key in pathkeys:
[240] Fix | Delete
if sepkey == ":":
[241] Fix | Delete
formatline("append-path",key,appendpath[sepkey][key])
[242] Fix | Delete
else:
[243] Fix | Delete
formatline("append-path --delim %s" % sepkey,key,appendpath[sepkey][key])
[244] Fix | Delete
[245] Fix | Delete
# Setenv
[246] Fix | Delete
setenvkeys = list(setenv.keys())
[247] Fix | Delete
setenvkeys.sort()
[248] Fix | Delete
if setenvkeys:
[249] Fix | Delete
print()
[250] Fix | Delete
for key in setenvkeys:
[251] Fix | Delete
formatline("setenv",key,setenv[key])
[252] Fix | Delete
[253] Fix | Delete
# Unsetenv
[254] Fix | Delete
unsetenv.sort()
[255] Fix | Delete
if unsetenv:
[256] Fix | Delete
print()
[257] Fix | Delete
for key in unsetenv:
[258] Fix | Delete
formatline("unsetenv",key)
[259] Fix | Delete
[260] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function