Updated to v3 - symlink support

This commit is contained in:
Jiri Vanek 2017-09-26 18:05:30 +02:00
parent d22671186c
commit cbfcea3a32
3 changed files with 122 additions and 81 deletions

View File

@ -6,9 +6,9 @@
Name: copy-jdk-configs Name: copy-jdk-configs
# hash relevant to version tag # hash relevant to version tag
%global htag 3f9d6c4448f867a95fb166416a41c45c7e795c10 %global htag c85eebe7ab9979810e5e1ee2a48eaf3dfa873b36
Version: 2.3 Version: 3.0
Release: 2%{?dist} Release: 1%{?dist}
Summary: JDKs configuration files copier Summary: JDKs configuration files copier
License: BSD License: BSD

View File

@ -159,6 +159,12 @@ if (debug) then
print(debug); print(debug);
end end
local function debugOneLinePrint(string)
if (debug) then
print(string)
end;
end
--trasnform substitute names to lua patterns --trasnform substitute names to lua patterns
local name = string.gsub(string.gsub(origname, "%-", "%%-"), "%.", "%%.") local name = string.gsub(string.gsub(origname, "%-", "%%-"), "%.", "%%.")
@ -166,6 +172,11 @@ local javaver = string.gsub(origjavaver, "%.", "%%.")
local jvms = { } local jvms = { }
function getPath(str,sep)
sep=sep or '/'
return str:match("(.*"..sep..")")
end
function splitToTable(source, pattern) function splitToTable(source, pattern)
local i1 = string.gmatch(source, pattern) local i1 = string.gmatch(source, pattern)
local l1 = {} local l1 = {}
@ -186,33 +197,44 @@ function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1")) return (s:gsub("^%s*(.-)%s*$", "%1"))
end end
local function dirWithParents(path)
local s = ""
local dirs = splitToTable(path, "[^/]+")
for i,d in pairs(dirs) do
if (i == #dirs) then
break
end
s = s.."/"..d
local stat2 = posix.stat(s, "type");
if (stat2 == nil) then
debugOneLinePrint(s.." does not exists, creating")
if (not dry) then
posix.mkdir(s)
end
else
debugOneLinePrint(s.." exists,not creating")
end
end
end
debugOneLinePrint("started")
if (debug) then
print("started")
end;
foundJvms = posix.dir(jvmdir); foundJvms = posix.dir(jvmdir);
if (foundJvms == nil) then if (foundJvms == nil) then
if (debug) then debugOneLinePrint("no, or nothing in "..jvmdir.." exit")
print("no, or nothing in "..jvmdir.." exit")
end;
return return
end end
if (debug) then debugOneLinePrint("found "..#foundJvms.."jvms")
print("found "..#foundJvms.."jvms")
end;
for i,p in pairs(foundJvms) do for i,p in pairs(foundJvms) do
-- regex similar to %{_jvmdir}/%{name}-%{javaver}*%{_arch} bash command -- regex similar to %{_jvmdir}/%{name}-%{javaver}*%{_arch} bash command
if (string.find(p, name.."%-"..javaver..".*"..arch) ~= nil ) then if (string.find(p, name.."%-"..javaver..".*"..arch) ~= nil ) then
if (debug) then debugOneLinePrint("matched: "..p)
print("matched: "..p)
end;
if (currentjvm == p) then if (currentjvm == p) then
if (debug) then debugOneLinePrint("this jdk is already installed. exiting lua script")
print("this jdk is already installed. exiting lua script")
end;
return return
end ; end ;
if (string.match(p, ".*-debug$")) then if (string.match(p, ".*-debug$")) then
@ -221,22 +243,16 @@ for i,p in pairs(foundJvms) do
table.insert(jvms, p) table.insert(jvms, p)
end end
else else
if (debug) then debugOneLinePrint("NOT matched: "..p)
print("NOT matched: "..p)
end;
end end
end end
if (#jvms <=0) then if (#jvms <=0) then
if (debug) then debugOneLinePrint("no matching jdk in "..jvmdir.." exit")
print("no matching jdk in "..jvmdir.." exit")
end;
return return
end; end;
if (debug) then debugOneLinePrint("matched "..#jvms.." jdk in "..jvmdir)
print("matched "..#jvms.." jdk in "..jvmdir)
end;
--full names are like java-1.7.0-openjdk-1.7.0.60-2.4.5.1.fc20.x86_64 --full names are like java-1.7.0-openjdk-1.7.0.60-2.4.5.1.fc20.x86_64
table.sort(jvms , function(a,b) table.sort(jvms , function(a,b)
@ -272,9 +288,7 @@ latestjvm = jvms[#jvms]
if ( temp ~= nil ) then if ( temp ~= nil ) then
src=jvmdir.."/"..latestjvm src=jvmdir.."/"..latestjvm
if (debug) then debugOneLinePrint("temp declared as "..temp.." saving used dir of "..src)
print("temp declared as "..temp.." saving used dir of "..src)
end
file = io.open (temp, "w") file = io.open (temp, "w")
file:write(src) file:write(src)
file:close() file:close()
@ -286,59 +300,44 @@ local readlinkOutput=os.tmpname()
for i,file in pairs(caredFiles) do for i,file in pairs(caredFiles) do
local SOURCE=jvmdir.."/"..latestjvm.."/"..file local SOURCE=jvmdir.."/"..latestjvm.."/"..file
local DEST=jvmDestdir.."/"..currentjvm.."/"..file local DEST=jvmDestdir.."/"..currentjvm.."/"..file
if (debug) then debugOneLinePrint("going to copy "..SOURCE)
print("going to copy "..SOURCE) debugOneLinePrint("to "..DEST)
print("to "..DEST)
end;
local stat1 = posix.stat(SOURCE, "type"); local stat1 = posix.stat(SOURCE, "type");
if (stat1 ~= nil) then if (stat1 ~= nil) then
if (debug) then debugOneLinePrint(SOURCE.." exists")
print(SOURCE.." exists") dirWithParents(DEST)
end;
local s = ""
local dirs = splitToTable(DEST, "[^/]+")
for i,d in pairs(dirs) do
if (i == #dirs) then
break
end
s = s.."/"..d
local stat2 = posix.stat(s, "type");
if (stat2 == nil) then
if (debug) then
print(s.." does not exists, creating")
end;
if (not dry) then
posix.mkdir(s)
end
else
if (debug) then
print(s.." exists,not creating")
end;
end
end
-- Copy with -a to keep everything intact -- Copy with -a to keep everything intact
local exe = "cp".." -ar "..SOURCE.." "..DEST local exe = "cp".." -ar "..SOURCE.." "..DEST
local linkExe = "readlink".." -f "..SOURCE.." > "..readlinkOutput local linkExe = "readlink".." -f "..SOURCE.." > "..readlinkOutput
if (debug) then debugOneLinePrint("executing "..linkExe)
print("executing "..linkExe) os.remove(readlinkOutput)
end;
os.execute(linkExe) os.execute(linkExe)
local link=trim(slurp(readlinkOutput)) local link=trim(slurp(readlinkOutput))
if (debug) then debugOneLinePrint(" ...link is "..link)
print(" ...link is "..link)
end
if (not ((link) == (SOURCE))) then if (not ((link) == (SOURCE))) then
print("WARNING link "..link.." where file "..SOURCE.."expected!") debugOneLinePrint("WARNING link "..link.." where file "..SOURCE.." expected!")
debugOneLinePrint("Will try to copy link target rather then link itself!")
--replacing any NVRA by future NVRA (still execting to have NVRA for any multiple-installable targets
-- lua stubbornly consider dash as inteval. Replacing by dot to match X-Y more correct as X.Y rather then not at all
local linkDest=string.gsub(link, latestjvm:gsub("-", "."), currentjvm)
debugOneLinePrint("attempting to copy "..link.." to "..linkDest)
if (link == linkDest) then
debugOneLinePrint("Those are identical files! Nothing to do!")
else
local exe2 = "cp".." -ar "..link.." "..linkDest
dirWithParents(linkDest)
debugOneLinePrint("executing "..exe2)
if (not dry) then
os.execute(exe2)
end end
if (debug) then end
print("executing "..exe) else
end; debugOneLinePrint("executing "..exe)
if (not dry) then if (not dry) then
os.execute(exe) os.execute(exe)
end end
end
else else
if (debug) then debugOneLinePrint(SOURCE.." does not exists")
print(SOURCE.." does not exists")
end;
end end
end end

View File

@ -58,9 +58,51 @@ if [ ! -d "$source" ] ; then
exit 33 exit 33
fi fi
listLinks(){
find $1 -type l -print0 | xargs -0 ls -ld | sed "s;.* $1;$1;" | sed "s; \+;_;g"
}
createListOfLinksTargetsDirectories(){
pushd $source >/dev/null 2>&1
local links=`listLinks $1`
for x in $links ; do
local ffileCandidate=$(echo $x | sed "s/.*_->_//") ;
# ignoring relative paths as they may lead who know where later
# there can be simlink relative to position, so push is not catching all
if [ "$ffileCandidate" != "${ffileCandidate#/}" ] ; then
if [ -d $ffileCandidate ] ; then
# should we accept the links to directories themselves?
echo $ffileCandidate
else
dirname $ffileCandidate
fi
fi
done | sort | uniq
popd >/dev/null 2>&1
}
sourceLinks=`listLinks $source`
targetLinks=`listLinks $target`
sourceLinksDirsTarget=`createListOfLinksTargetsDirectories $source`
targetLinksDirsTarget=`createListOfLinksTargetsDirectories $target`
debug "source: $source" debug "source: $source"
debug "target: $target" debug "target: $target"
debug "sourceLinks:
$sourceLinks"
debug "targetLinks:
$targetLinks"
debug "sourceLinksDirsTarget:
$sourceLinksDirsTarget"
debug "targetLinksDirsTarget:
$targetLinksDirsTarget"
sourceSearchPath="$source $sourceLinksDirsTarget"
targetSearchPath="$target $targetLinksDirsTarget"
work(){ work(){
if [ "X$1" == "Xrpmnew" -o "X$1" == "Xrpmorig" ] ; then if [ "X$1" == "Xrpmnew" -o "X$1" == "Xrpmorig" ] ; then
debug "Working with $1 (1)" debug "Working with $1 (1)"
@ -69,7 +111,7 @@ work(){
return 1 return 1
fi fi
local files=`find $target | grep "\\.$1$"` local files=`find $targetSearchPath | grep "\\.$1$"`
for file in $files ; do for file in $files ; do
local sf1=`echo $file | sed "s/\\.$1$//"` local sf1=`echo $file | sed "s/\\.$1$//"`
local sf2=`echo $sf1 | sed "s/$targetName/$srcName/"` local sf2=`echo $sf1 | sed "s/$targetName/$srcName/"`
@ -110,7 +152,7 @@ work rpmorig
debug "Working with rpmorig (2)" debug "Working with rpmorig (2)"
# simply moving old rpmsaves to new dir # simply moving old rpmsaves to new dir
# fix for config (replace) leftovers # fix for config (replace) leftovers
files=`find $source | grep "\\.rpmorig$"` files=`find $sourceSearchPath | grep "\\.rpmorig$"`
for file in $files ; do for file in $files ; do
rpmsaveTarget=`echo $file | sed "s/$srcName/$targetName/"` rpmsaveTarget=`echo $file | sed "s/$srcName/$targetName/"`
debug "relocating $file to $rpmsaveTarget" debug "relocating $file to $rpmsaveTarget"
@ -122,7 +164,7 @@ files=`find $source | grep "\\.rpmorig$"`
done done
debug "Working with rpmsave (1)" debug "Working with rpmsave (1)"
files=`find $source | grep "\\.rpmsave$"` files=`find $sourceSearchPath | grep "\\.rpmsave$"`
for file in $files ; do for file in $files ; do
rpmsaveTarget=`echo $file | sed "s/$srcName/$targetName/"` rpmsaveTarget=`echo $file | sed "s/$srcName/$targetName/"`
debug "relocating $file to $rpmsaveTarget" debug "relocating $file to $rpmsaveTarget"
@ -149,11 +191,11 @@ done
debug "cleaning legacy leftowers" debug "cleaning legacy leftowers"
if [ "x$debug" == "xtrue" ] ; then if [ "x$debug" == "xtrue" ] ; then
find $source -empty -type d -delete find $sourceSearchPath -empty -type d -delete
rmdir $rma $source rmdir $rma $sourceSearchPath
else else
find $source -empty -type d -delete 2>/dev/null >/dev/null find $sourceSearchPath -empty -type d -delete 2>/dev/null >/dev/null
rmdir $rma $source 2>/dev/null >/dev/null rmdir $rma $sourceSearchPath 2>/dev/null >/dev/null
fi fi
# and remove placeholders # and remove placeholders