From 6912410eacfc6fbaecc761e95bb9092d52a1d826 Mon Sep 17 00:00:00 2001
From: Giorgos Korfiatis <gkorf@grnet.gr>
Date: Wed, 9 Sep 2015 12:42:58 +0300
Subject: [PATCH] Convert installation scripts to Python

---
 cacert_cp.py | 14 ++++++++++++
 cacert_cp.sh | 24 --------------------
 configure.py | 17 ++++++++++++++
 configure.sh | 34 ----------------------------
 get_nwjs.py  | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 get_nwjs.sh  | 56 -----------------------------------------------
 6 files changed, 93 insertions(+), 114 deletions(-)
 create mode 100644 cacert_cp.py
 delete mode 100755 cacert_cp.sh
 create mode 100644 configure.py
 delete mode 100755 configure.sh
 create mode 100644 get_nwjs.py
 delete mode 100755 get_nwjs.sh

diff --git a/cacert_cp.py b/cacert_cp.py
new file mode 100644
index 0000000..da73515
--- /dev/null
+++ b/cacert_cp.py
@@ -0,0 +1,14 @@
+import os
+import shutil
+
+def main():
+    os.chdir(os.path.dirname(os.path.realpath(__file__)))
+    os.system("pip install certifi")
+
+    print "Copying certifi's cacert.pem"
+    import certifi
+    shutil.copy2(certifi.where(), 'agkyra/resources/cacert.pem')
+
+
+if __name__ == '__main__':
+    main()
diff --git a/cacert_cp.sh b/cacert_cp.sh
deleted file mode 100755
index d50914f..0000000
--- a/cacert_cp.sh
+++ /dev/null
@@ -1,24 +0,0 @@
-#!/usr/bin/env bash
-# Copyright (C) 2015 GRNET S.A.
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-CURPWD=$(pwd)
-cd "$(dirname "$0")"
-ROOTDIR=$(pwd)
-
-pip install certifi
-
-echo "Copying certifi's cacert.pem"
-python -c "import certifi; import shutil; shutil.copy2(certifi.where(), 'agkyra/resources/cacert.pem')"
diff --git a/configure.py b/configure.py
new file mode 100644
index 0000000..f6e70a4
--- /dev/null
+++ b/configure.py
@@ -0,0 +1,17 @@
+import os
+
+def main():
+    os.chdir(os.path.dirname(os.path.realpath(__file__)))
+    # this is needed for mock
+    os.system("pip install --upgrade setuptools")
+
+    import get_nwjs
+    get_nwjs.main()
+    import cacert_cp
+    cacert_cp.main()
+
+    print "Now run 'python setup.py install'."
+
+
+if __name__ == '__main__':
+    main()
diff --git a/configure.sh b/configure.sh
deleted file mode 100755
index ee7a23b..0000000
--- a/configure.sh
+++ /dev/null
@@ -1,34 +0,0 @@
-#!/bin/bash
-# Copyright (C) 2015 GRNET S.A.
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-CURPWD=$(pwd)
-cd "$(dirname "$0")"
-ROOTPATH=$(pwd)
-
-# this is needed for mock
-pip install --upgrade setuptools
-
-./get_nwjs.sh $1
-if [ $? -ne 0 ]; then
-    exit 1
-fi
-
-./cacert_cp.sh
-if [ $? -ne 0 ]; then
-    exit 1
-fi
-
-echo "Now run 'python setup.py install'."
diff --git a/get_nwjs.py b/get_nwjs.py
new file mode 100644
index 0000000..8cb258e
--- /dev/null
+++ b/get_nwjs.py
@@ -0,0 +1,62 @@
+import os
+import sys
+import shutil
+import urllib
+
+VERSION="v0.12.3"
+
+nwjsfile = {
+    "win64": "nwjs-%s-win-x64.zip",
+    "osx64": "nwjs-%s-osx-x64.zip",
+    "linux64": "nwjs-%s-linux-x64.tar.gz",
+    "win32": "nwjs-%s-win-ia32.zip",
+    "osx32" : "nwjs-%s-osx-ia32.zip",
+    "linux32": "nwjs-%s-linux-ia32.tar.gz",
+}
+
+def main():
+    os.chdir(os.path.dirname(os.path.realpath(__file__)))
+
+    if len(sys.argv) < 2:
+        print "Select one of: %s" % " ".join(nwjsfile.keys())
+        exit(1)
+
+    osarg = sys.argv[1]
+    filename = nwjsfile[osarg] % VERSION
+    url = "http://dl.nwjs.io/%s/%s" % (VERSION, filename)
+
+    print "Will first download nwjs."
+    target = "agkyra/resources/nwjs"
+    if os.path.isdir(target):
+        print "Warning: cleaning up %s." % target
+        shutil.rmtree(target)
+    elif os.path.exists(target):
+        print "%s exists and is not a dir; aborting." % target
+        exit(1)
+
+    print "Retrieving %s" % url
+    urllib.urlretrieve(url, filename)
+
+    print "Extracting %s" % filename
+    if osarg.startswith("linux"):
+        toplevel = filename.strip('.tar.gz')
+        os.system("tar xzf %s" % filename)
+        print "Renaming %s to %s" % (toplevel, target)
+        os.rename(toplevel, target)
+    else:
+        toplevel = filename.strip('.zip')
+        if osarg.startswith('osx'):
+            os.system("unzip %s" % filename)
+        else:  # Windows has no unzip command
+            import zipfile
+            with zipfile.ZipFile(filename, "r") as z:
+                z.extractall('.')
+        print "Renaming %s to %s" % (toplevel, target)
+        os.rename(toplevel, target)
+
+    print "Deleting %s" % filename
+    os.unlink(filename)
+
+
+if __name__ == "__main__":
+    main()
diff --git a/get_nwjs.sh b/get_nwjs.sh
deleted file mode 100755
index 094aefa..0000000
--- a/get_nwjs.sh
+++ /dev/null
@@ -1,56 +0,0 @@
-#!/usr/bin/env bash
-# Copyright (C) 2015 GRNET S.A.
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-VERSION="v0.12.3"
-
-CURPWD=$(pwd)
-cd "$(dirname "$0")"
-ROOTDIR=$(pwd)
-
-declare -A nwjsfile
-nwjsfile[win64]="nwjs-${VERSION}-win-x64.zip"
-nwjsfile[osx64]="nwjs-${VERSION}-osx-x64.zip"
-nwjsfile[linux64]="nwjs-${VERSION}-linux-x64.tar.gz"
-nwjsfile[win32]="nwjs-${VERSION}-win-ia32.zip"
-nwjsfile[osx32]="nwjs-${VERSION}-osx-ia32.zip"
-nwjsfile[linux32]="nwjs-${VERSION}-linux-ia32.tar.gz"
-
-if [[ -z $1 ]]; then
-    echo "Select one of:" ${!nwjsfile[@]}
-    exit 1
-fi
-
-os=$1
-file=${nwjsfile[$os]}
-url="http://dl.nwjs.io/${VERSION}/"$file
-
-echo "Will first download nwjs."
-AGKPATH=agkyra
-NWJSPATH=$AGKPATH/resources/nwjs
-if [ -d $NWJSPATH ]; then
-    echo "Warning: cleaning up $NWJSPATH."
-    rm -r $NWJSPATH
-fi
-
-mkdir $NWJSPATH
-wget $url
-if [[ "$os" =~ ^(linux64|linux32)$ ]]; then
-    tar xzf $file --strip-components 1 -C $NWJSPATH
-else
-    unzip -d tmpnwjs $file && mv tmpnwjs/*/* $NWJSPATH && rm -r tmpnwjs
-fi
-
-rm $file
-- 
GitLab