Zen Cart Logo
Forums / Code Collaboration / Litlle ZCUpgradeParser script

Litlle ZCUpgradeParser script

Views: 1,361

Results 1 to 2 of 2
12 Feb 2016, 3:11 PM
#1
hubert avatar

hubert

Zen Follower

Join Date:
Mar 2005
Posts:
229
Plugin Contributions:
0

Litlle ZCUpgradeParser script

If you're like me, i.e. you didn't touch your website for a long time and you plan to upgrade, you didn't take notes of what modules you installed or even you don't understand the notes you've taken (it happens), then perhaps this little
quick and dirty script will help you.

It's meant to review differences between ZC distributions, old, new, and your website and to parse files in new folders.

Fell free to test, comment and modify

Here is the bash script

#!/bin/bash
#
# This script is intend to be used during a Zen-Cart upgrade
# It's quick and dirty so modifiy it at your convenience
#
# arguments are previous version, next version, current site
# Even if ZC team recommends to upgrade by building a brand new test site,
# if you're like me and don't remember the modules you installed or modifications you made
# comparing and sorting files could help you to find your way.
#

#Check the three parameters which must be directories : previous version dir, next version dir, running version dir
CheckParam () {

if [ $# -eq 0 ]; then
    echo "No arguments supplied, I'm waiting for ./PreviousVersionDir ./NextVersionDir ./WebSiteDir"
    echo -e "\n"
    echo "Using these arguments I'll give you detailed reports on modified or added files between the versions"
    echo "You'll get 7 file lists:"
    echo "-SiteAdded.txt : added to your website (somehow module files)"
    echo "-SiteMods.txt : customized files of your website"
    echo "-SiteRemvd.txt : missing from your website"
    echo "-DistNew.txt : new files in new ZC"
    echo "-DistMods.txt : files modified in new ZC"
    echo "-DistRemvd.txt : files that are no longer in the new ZC"
    echo "-FilesToReview.txt : where (DistNew+DistMods) overlaps with (SiteMods+SiteAdded)"
    echo -e "\n"
    echo "I can even sort the files in directories for you"
    exit 0
fi
if [ $# != 3 ]; then
	echo "not enough arguments"
	echo "I'm waiting for ./PreviousVersionDir ./NextVersionDir ./WebSiteDir"
	exit 0
fi
if [ ! -d "$1" ]; then
  echo "$1 wrong parameter, must be previous version directory"
  exit 0
fi
if [ ! -d "$2" ]; then
  echo "$2 wrong parameter, must be new version directory"
  exit 0
fi
if [ ! -d "$3" ]; then
  echo "$3 wrong parameter, must be current site directory"
  exit 0
fi
####Attention, version check relies on paypal, could not always be right in the future.
PrevVer=$(find $1 -name 'paypal.php' -exec cat {} \; | grep 'codeVersion ='| cut -c27-31)
NxtVer=$(find $2 -name 'paypal.php' -exec cat {} \; | grep 'codeVersion ='| cut -c27-31)
WebVer=$(find $3 -name 'paypal.php' -exec cat {} \; | grep 'codeVersion ='| cut -c27-31)

PFstCar=$(echo $PrevVer |cut -d '.' -f 1)
PMidCar=$(echo $PrevVer |cut -d '.' -f 2)
PLstCar=$(echo $PrevVer |cut -d '.' -f 3)
NFstCar=$(echo $NxtVer |cut -d '.' -f 1)
NMidCar=$(echo $NxtVer |cut -d '.' -f 2)
NLstCar=$(echo $NxtVer |cut -d '.' -f 3)

PrvGTNxt=false
if [ $PLstCar -lt $NLstCar ]; then
	PrvGTNxt=true
elif [ $PMidCar -lt $NMidCar ]; then
		PrvGTNxt=true
elif [ $PFstCar -lt $NFstCar ]; then
		PrvGTNxt=true
fi
if [ $PrvGTNxt == false ]; then
	echo "Version $NxtVer has to be greater than $PrevVer"
	exit 0
fi
if [ $PrevVer != $WebVer ]; then
	echo "Website version $WebVer can t be compared to $PrevVer distribution. Sorry"
	exit 0
fi

}
RemoveOld () {
if [ -f $1 ]; then
rm $1
fi
}

# ctrl_c() {
#         echo "**  CTRL-C pressed, tidying created files and folders"
#         
# }


main () {
CheckParam $1 $2 $3
RemoveOld ./DistRemvd.txt
RemoveOld ./DistNew.txt
RemoveOld ./DistMods.txt
RemoveOld ./AddedOrUpdated.txt
RemoveOld ./SiteAdded.txt
RemoveOld ./AddOrModOnSite.txt
RemoveOld ./SiteRemvd.txt
RemoveOld ./SiteMods.txt

echo "Type the exact name of your admin directory, followed by [ENTER]:"
read AdminDir


#Rename admin dirs in brand new distributions to match your admin
#Verify if entered name is right
if [ ! -d "$3/$AdminDir" ]; then
	echo "I don't see this directory in your website, exiting"
	exit 0
fi
if [ -d "$1/admin" ] || [ ! -d "$1/$AdminDir" ]; then
	mv $1/admin $1/"$AdminDir"
	elif  [ -d "$1/admin" ]; then
	echo "There is no directory named admin in $1, check please"
	exit 0
fi
if [ -d "$2/admin" ] || [ ! -d "$2/$AdminDir" ]; then
	mv $2/admin $2/"$AdminDir"
	elif  [ -d "$1/admin" ]; then
	echo "There is no directory named admin in $2, check please"
	exit 0
fi

#Compare previous distribution with website
echo "Comparing your website to distribution..."
if [ -f ./.CurrentMods.txt ]; then
	read -rsp "Result file already exists, Do you want to keep it (y/n) ?" -n1 KeepIt
	echo -e "\n"
	if [ $KeepIt == "n" ] || [ $KeepIt == "N" ]; then
		RemoveOld ./.CurrentMods.txt
		diff --brief -r $1 $3 -x .DS_Store -x '*.jpg' -x '*.jpeg' -x '*.gif' -x '*.png' -x '*.log' -x 'sess_*' -x '$AdminDir/backups/*' > .CurrentMods.txt
	fi
else
	diff --brief -r $1 $3 -x .DS_Store -x '*.jpg' -x '*.jpeg' -x '*.gif' -x '*.png' -x '*.log' -x 'sess_*' -x '$AdminDir/backups/*' > .CurrentMods.txt

fi
#Compare previous distribution with next version distribution
echo "Comparing distributions..."
if [ -f ./.NewZCFiles.txt ]; then
	read -rsp "Result file already exists, Do you want to keep it (y/n) ?" -n1 KeepIt
	echo -e "\n"
	if [ $KeepIt == "n" ] || [ $KeepIt == "N" ]; then
		RemoveOld ./.NewZCFiles.txt
		diff --brief -r $1 $2 -x .DS_Store -x "$3/images/*" -x "$3/cache/*" -x '*.log' -x 'sess_*' > .NewZCFiles.txt
	fi
else
	diff --brief -r $1 $2 -x .DS_Store -x "$3/images/*" -x "$3/cache/*" -x '*.log' -x 'sess_*' > .NewZCFiles.txt
fi

read -rsp "Do you want to classify files in new directories (y/n) ?" -n1 MoveFile
echo -e "\n"
if [ $MoveFile == "y" ] || [ $MoveFile == "Y" ]; then
	MoveFile=true
else
	MoveFile=false
fi

#make patch from previous $1 to new version $2 by sorting files
cat .NewZCFiles.txt | while read i; do
#Parse lines 
	case "$i" in 
		"Only in $1"*) 
			# move to FilesToRemove
			last=`echo $i | sed 's/: /\//' | cut -d ' ' -f 3` #get only path 
			file=${last#$1} #remove folder name from path
			if [ -f $last ]; then
			file=${file#./} #remove dot from path
				echo $last >> DistRemvd.txt
				filename=$(basename "$last")
				path=$(dirname "$file")
				if [ $MoveFile == true ]; then
					if [ ! -d "./DistRemvd/$path" ]; then
						mkdir -p "./DistRemvd"$path
					fi
					cp $last "./DistRemvd/$file"
				fi
			fi
			;;
		"Only in $2"*)
			# move to DistNewFiles
			last=`echo $i | sed 's/: /\//' | cut -d ' ' -f 3` 
			file=${last#$2}
			if [ -f $last ]; then
				echo $last >> DistNew.txt
				file=${file#./}
				echo $file >> AddedOrUpdated.txt
				filename=$(basename "$last")
				path=$(dirname "$file")
				if [ $MoveFile == true ]; then
					if [ ! -d "./DistNewFiles/$path" ]; then
						mkdir -p "./DistNewFiles"$path
					fi
					cp $last "./DistNewFiles/$file"
				fi
			fi
			;;
		"Files $1"*)
			# move to ModifiedFiles
			last=`echo $i  | cut -d' ' -f 4` #get second path in the line which is the new version one
			file=${last#$2}
			if [ -f $last ]; then
				echo $last >> DistMods.txt
				file=${file#./}
				echo $file >> AddedOrUpdated.txt
				filename=$(basename "$last")
				path=$(dirname "$file")
				if [ $MoveFile == true ]; then
					if [ ! -d "./DistMods/$path" ]; then
					mkdir -p "./DistMods"$path
					fi
					cp $last "./DistMods/$file"
				fi
			fi
			;;
	esac
done

echo "####################_"
echo "In the new version :"
Nbr=`cat DistMods.txt |wc -l`
echo $Nbr " files have been modified in $2"
Nbr=`cat DistNew.txt |wc -l`
echo $Nbr " files have been added to $2"
Nbr=`cat DistRemvd.txt |wc -l`
echo $Nbr " files have been removed from $2"
echo "####################_"
#end of distributions comparison



#make list of modified or added files in running version
cat .CurrentMods.txt | while read i; do
		if [[ $i == "Only in $3"* ]]; then
			# move to SiteAdded
			last=`echo $i | sed 's/: /\//' | cut -d ' ' -f 3`
			if [ -f $last ]; then
				file=${last#$3}
				echo $last >> SiteAdded.txt
				file=${file#./}
				echo $file >> AddOrModOnSite.txt
				#filename=$(basename "$last")
				path=$(dirname "$file")
				if [ $MoveFile == true ]; then
					if [ ! -d "./SiteAdded/$path" ]; then
						mkdir -p "./SiteAdded"$path
					fi
					cp $last "./SiteAdded$file"
				fi
			elif [ -d $last ]; then
				echo $last >> SiteAdded.txt
			fi
		fi	
		if [[ $i == "Only in $1"* ]]; then
			# move to SiteRemvd
			last=`echo $i | sed 's/: /\//' | cut -d ' ' -f 3`
			if [ -f $last ]; then
				file=${last#$1}
				echo $last >> SiteRemvd.txt
				file=${file#./}
				#filename=$(basename "$last")
				path=$(dirname "$file")
				if [ $MoveFile == true ]; then
					if [ ! -d "./SiteRemvd/$path" ]; then
						mkdir -p "./SiteRemvd"$path
					fi
					cp $last "./SiteRemvd$file"
				fi
			elif [ -d $last ]; then
				echo $last >> SiteRemvd.txt
			fi
		fi
		if [[ $i == "Files $1"* ]]; then
			# move to SiteMods
			last=`echo $i  | cut -d' ' -f 4`
			if [ -f $last ]; then
				file=${last#$3}
				echo $last >> SiteMods.txt
				file=${file#./}
				echo $file >> AddOrModOnSite.txt
				filename=$(basename "$last")
				path=$(dirname "$file")
				if [ $MoveFile == true ]; then
					if [ ! -d "./SiteMods/$path" ]; then
					mkdir -p "./SiteMods"$path
					fi
					cp $last "./SiteMods$file"
				fi
			elif [ -d $last ]; then
				echo $last >> SiteMods.txt
			fi
		fi
done
RemoveOld ./.CurrentMods.txt
RemoveOld ./.NewZCFiles.txt


echo "In currently running version"
Nbr=`cat SiteAdded.txt |wc -l`
echo $Nbr " file(s) have been added in $3"
Nbr=`cat SiteMods.txt |wc -l`
echo $Nbr " file(s) have been modified in $3"
Nbr=`cat SiteRemvd.txt |wc -l`
echo $Nbr " file(s) are not in $3"
echo "####################_"
echo -e "\n"

echo "Following files are both been added or updated in your website and new version :"
#AWK will review each line of one file to lines of the other and tell if there's a match
echo "----"
awk ' BEGIN { FS="" }                   
	(NR==FNR) { ll1[FNR]=$0; nl1=FNR; }   
 	(NR!=FNR) { ss2[$0]++; }             
 	END {
		for (ll=1; ll<=nl1; ll++) if ((ll1[ll] in ss2)) print ll1[ll]
 	}'  AddOrModOnSite.txt AddedOrUpdated.txt > FilesToReview.txt
 	cat FilesToReview.txt
echo "----"

RemoveOld ./AddOrModOnSite.txt
RemoveOld ./AddedOrUpdated.txt
#set -x
#trap read debug

# move files to review from patch dir (DistMods or DistNew)to new dir 
# this can only be done if you said yes to classify files
if [ $MoveFile == true ]; then
	if [ ! -d ./DistToRev ]; then 
	mkdir ./DistToRev
	fi
	if [ ! -d ./WebToRev ]; then
	mkdir ./WebToRev
	fi
	 cat FilesToReview.txt | while read i; do
		if [ -f "./DistMods$i" ]; then
			File="./DistMods$i"
		elif [ -f "./DistNewFiles$i" ]; then
			File="./DistNewFiles$i"
		else 
			echo "file is not there, exiting"
			exit 0
		fi
		FileDir=$(dirname $i)
		if [ ! -d ./DistToRev$FileDir ]; then 
			mkdir -p "./DistToRev"$FileDir
			mkdir -p "./WebToRev"$FileDir
		fi
		mv $File ./DistToRev$FileDir
		cp $3$i ./WebToRev$FileDir
	 done
fi
echo -e "\n####################_"
Nbr=$(cat FilesToReview.txt |wc -l)
echo $Nbr " files have been moved from new distribution patch (DistMods or DistNewFiles )to DistToRev directory"
echo "For convenience the same files from your website have been copied to WebToRev directory"
echo -e "\n"
# trap ctrl_c INT
# trap 'echo $0 is ending..." ; exit 3 ' 15
# trap 'echo "$0 other signal" ; exit $? ' SIGINT 
# trap 'echo "$0 other signal" ; exit $? ' SIGHUP
# trap 'echo "$0 other signal" ; exit $? ' SIGTERM
}
main $1 $2 $3
exit 0

As it's a bash script you' have to be on Unix to run it.

$sudo nano ZCUpgrParser.sh

then copy/paste the script
CTRL-x to exit, and answer yes when it asks to save.
then change permission

sudo chmod 755 ZCUpgrParser.sh

To run the script asks for three directories.

$./ZCUpgrParser.sh ./OldDistribution ./NewDistribution ./Website

For example, in my case it's

$./ZCUpgrParser.sh ./ZC-150-Full ./ZC-154-Full ./public_html

Hope this helps

Hubert

12 Feb 2016, 4:30 PM
#2
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Litlle ZCUpgradeParser script

Two things of comment. There's a file that identifies the version of ZC (assuming all other files aren't updated outside of it) which may be more beneficial to use as the version check. There's one in includes (includes/version.php). (PROJECT_VERSION_MAJOR and PROJECT_VERSION_MINOR, apply)

While it obviously took some time to write, would have suggested possibly a comparison tool like listed in this FAQ: https://www.zen-cart.com/content.php?179-useful-tools-for-working-with-zen-cart that is already written and might offer other/additional functionality not covered by the above.