Browse code

perl: Automation script to autoupdate perl modules

This script automatically finds the latest versions of all the perl modules in the Photon distribution and updates their spec files accordingly.

Change-Id: Ifef2aa72b382b88491fd9fbca6c7e3d33e085f7c
Reviewed-on: http://photon-jenkins.eng.vmware.com:8082/5750
Reviewed-by: Sharath George
Tested-by: Sharath George

dweepadvani authored on 2018/09/21 02:36:51
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,174 @@
0
+#!/bin/sh
1
+PROG=$0                                   # The script name
2
+PERL_PACKAGE_LIST=""                      # List of spec directories of perl packages
3
+tmpJsonFile=$(mktemp /tmp/perl.XXXXXXXXX) # Temporary file to hold package json
4
+perlModuleName=""                         # Name of the current perl module
5
+srcUrl=""                                 # Source code URL
6
+latestVersion=""                          # Package version number
7
+SPECSDIR=SPECS                            # Parent directory of  all SPEC files
8
+SOURCESDIR="stage/SOURCES"                # Directory where sources are downloaded
9
+latestSrcFileName=""                      # Base name of source file
10
+srcSha1Sum=""                             # sha1sum of the sources
11
+changeMsgLine1=""                         # first line of the changelog message to record
12
+changeMsgLine2=""                         # second line of the changelog message to record
13
+
14
+function _help()
15
+{
16
+    echo "
17
+Usage: $PROG [--help|-h]
18
+
19
+Run the script from git workspace root of photon where .git directory exists.
20
+This script upgrades the Perl modules used in Photon to their latest versions.
21
+"
22
+    exit ${1:-0}
23
+}
24
+
25
+function _isRunningFromGitWorkspace()
26
+{
27
+    local folderName
28
+    local rc=0
29
+
30
+    for folderName in .git SPECS
31
+    do
32
+        if [ ! -d ".git" ]
33
+        then
34
+            rc=$?
35
+            echo "Expected directory $folderName does not exist." \
36
+                 "Please run this script from git workspace of Photon."
37
+            return $rc
38
+        fi
39
+    done
40
+    return $rc
41
+}
42
+################################################################################
43
+# Converts spec dir name of a Perl package to a module name
44
+#
45
+# Usage: _specDirToPackageName perl-DBIx-Simple
46
+# Sample output : DBIx::Simple
47
+################################################################################
48
+function _specDirToPackageName()
49
+{
50
+    echo "$1" | cut -d '-' -f 2- | sed -e 's/-/::/g'
51
+}
52
+
53
+
54
+################################################################################
55
+# Gets the Json of package information from metacpan.org
56
+#
57
+# Usage: _downloadJsonForPackage <package-name> ## example, "DBIx::Simple"
58
+# Sample output :
59
+# {
60
+#   "date" : "2017-12-08T22:54:24",
61
+#   "download_url" : "https://cpan.metacpan.org/authors/id/J/JU/JUERD/DBIx-Simple-1.37.tar.gz",
62
+#   "status" : "latest",
63
+#   "version" : "1.37"
64
+# }
65
+################################################################################
66
+function _downloadJsonForPackage()
67
+{
68
+    curl -s "http://fastapi.metacpan.org/v1/download_url/$1" > $tmpJsonFile
69
+}
70
+
71
+################################################################################
72
+# Downloads a source archive from provided url and store as target file name
73
+#
74
+# Usage: _downloadSourceFromUrl <source-archive-url> <target-download-path>
75
+################################################################################
76
+function _downloadSourceFromUrl()
77
+{
78
+    local fromUrl="$1"
79
+    local toFile="$2"
80
+    curl -s "$fromUrl" > "$toFile"
81
+}
82
+
83
+################################################################################
84
+# Update spec file with provided parameters
85
+#
86
+# Usage: _updateSpecFile <spec-file> <version> <url> <localSourceFile>
87
+#                        <changelog-firstline> <changelog-secondline>
88
+################################################################################
89
+function _updateSpecFile()
90
+{
91
+    local specFile="$1"
92
+    local latestVersion="$2"
93
+    local srcUrl="$3"
94
+    local localSrcArchive="$4"
95
+    local changeLogMsg1="$5"
96
+    local changeLogMsg2="$6"
97
+    local sha1sum=""
98
+    local existingVersion=""
99
+    local downloadUrl="$(echo $srcUrl | sed -E "s/$latestVersion/%{version}/g")"
100
+    downloadUrl="$(echo "$downloadUrl" | sed -E 's#/#\\/#g')"
101
+    _downloadSourceFromUrl "$srcUrl" "$localSrcArchive"
102
+    sha1sum="$(sha1sum $localSrcArchive | awk '{print $1}')"
103
+
104
+    existingVersion="$(awk '/^Version:/{print $2}' $specFile)"
105
+
106
+    if [ "$existingVersion" == "$latestVersion" ]
107
+    then
108
+        echo "$specFile is already at latest version $latestVersion. Skipping..."
109
+        return 0
110
+    fi
111
+    echo -n "Upgrading spec file $specFile to $latestVersion from $existingVersion..."
112
+
113
+    # Update %version
114
+    sed -i -e "s/^\(Version:[[:space:]]*\)[^[:space:]].*$/\1$latestVersion/" $specFile
115
+
116
+    # Update %release
117
+    sed -i -e "s/^\(Release:[[:space:]]*\)[^[:space:]]*%\(.*\)$/\11%\2/" $specFile
118
+    
119
+    # Update %Source/%Source0
120
+    sed -i -e "s/^\(Source[0]*:[[:space:]]*\).*$/\1$downloadUrl/" $specFile
121
+
122
+    # Update sha1sum
123
+    sed -i -e "s/^\(%define[[:space:]]\+sha1.*\)=.*$/\1=$sha1sum/" $specFile
124
+
125
+    # Update changelog
126
+    sed -i -e "/^%changelog/a\
127
+              $changeLogMsg1" $specFile
128
+    sed -i -e "/$changeLogMsg1/a\
129
+              $changeLogMsg2" $specFile
130
+    echo "Done"
131
+}
132
+
133
+if [ $# -gt 1 ]
134
+then
135
+    _help 1
136
+elif [ $# -eq 1 ]
137
+then
138
+    echo "$1" | grep -q -E -- '(^--help$)|(^-h$)' && _help 0
139
+    echo "Invalid option '$1'" 1>&2
140
+    _help 1
141
+fi
142
+# exit if not running from git workspace of Photon
143
+_isRunningFromGitWorkspace || _help $?
144
+PERL_PACKAGE_LIST="$(find SPECS/ -type d -name 'perl-*' -exec basename {} \;)"
145
+mkdir -p "$SOURCESDIR"
146
+for p in $PERL_PACKAGE_LIST
147
+do
148
+    perlModuleName="$(_specDirToPackageName $p)"
149
+    _downloadJsonForPackage "$perlModuleName"
150
+    while read line
151
+    do
152
+        if echo $line | grep -q '"download_url"'
153
+        then
154
+            srcUrl="$(echo \"$line\" | cut -d ':' -f 2- | sed -e 's/[,\" ]//g')";
155
+            continue
156
+        fi
157
+        if echo $line | grep -q '"version"'
158
+        then
159
+            latestVersion="$(echo \"$line\" | cut -d ':' -f 2- | sed -e 's/[,\" ]//g')";
160
+            continue
161
+        fi
162
+    done < $tmpJsonFile
163
+    latestSrcFileName="$(basename $srcUrl)"
164
+    changeMsgLine1="$(echo -e "*   $(date '+%a %b %d %Y') Dweep Advani <$(git config --get user.email)> $latestVersion-1\n")"
165
+    changeMsgLine2="$(echo -e "-   Update to version $latestVersion\n")"
166
+    _updateSpecFile "$SPECSDIR/$p/$p.spec" \
167
+                    "$latestVersion" \
168
+                    "$srcUrl" \
169
+                    "$SOURCESDIR/$latestSrcFileName" \
170
+                    "$changeMsgLine1" \
171
+                    "$changeMsgLine2"
172
+done
173
+rm -f $tmpJsonFile