incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject [15/17] git commit: [#6692] Converted project_export to bash script
Date Thu, 31 Oct 2013 22:05:10 GMT
[#6692] Converted project_export to bash script

Signed-off-by: Cory Johns <cjohns@slashdotmedia.com>


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/4d649c0f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/4d649c0f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/4d649c0f

Branch: refs/heads/master
Commit: 4d649c0fd1d775f3367aa1c1ddb87c0ee629e3dd
Parents: 610e960
Author: Cory Johns <cjohns@slashdotmedia.com>
Authored: Thu Oct 31 20:42:45 2013 +0000
Committer: Cory Johns <cjohns@slashdotmedia.com>
Committed: Thu Oct 31 20:42:45 2013 +0000

----------------------------------------------------------------------
 scripts/project_export    | 94 ++++++++++++++++++++++++++++++++++++++++++
 scripts/project_export.py | 46 ---------------------
 2 files changed, 94 insertions(+), 46 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/4d649c0f/scripts/project_export
----------------------------------------------------------------------
diff --git a/scripts/project_export b/scripts/project_export
new file mode 100755
index 0000000..d15d02e
--- /dev/null
+++ b/scripts/project_export
@@ -0,0 +1,94 @@
+#!/bin/bash
+
+ACCESS_TOKEN=''
+USER_NAME=''
+PROJECT=''
+TOOLS=''
+HOST='sourceforge.net'
+OUTPUT=.
+
+MAX_MINUTES=10
+HEADERS=/tmp/project_export_response_headers
+BODY=/tmp/project_export_response
+
+function error() {
+    echo "$1"
+    exit 1
+}
+
+while getopts "ht:u:o:H:c:" opt; do
+    case $opt in
+        h)
+            echo "Usage: $0 [OPTION]... PROJECT TOOLS..."
+            echo "Schedule an export of PROJECT and retrieve the file when ready"
+            echo
+            echo "  -c CONFIG_FILE    Load config (optionally including PROJECT and TOOLs)
from file"
+            echo "  -H HOST           Host to use (default sourceforge.net)"
+            echo "  -o OUTPUT         Directory and/or filename for downloaded export file"
+            echo "  -t ACCESS_TOKEN   Access (bearer) token for the API"
+            echo "  -u USER_NAME      Username associated with access token (to download
file)"
+            echo "  -h                Show this help message"
+            echo
+            echo "The TOOLS should be one or more tool mount-point names, separated by spaces"
+            ;;
+        t)
+            C_ACCESS_TOKEN="$OPTARG"
+            ;;
+        u)
+            C_USER_NAME="$OPTARG"
+            ;;
+        H)
+            C_HOST="$OPTARG"
+            ;;
+        o)
+            C_OUTPUT="$OPTARG"
+            ;;
+        c)
+            source "$OPTARG"
+            ;;
+        *)
+            error "Invalid option: -$opt"
+    esac
+done
+# command-line overrides config
+[ -n "$C_ACCESS_TOKEN" ] && ACCESS_TOKEN="$C_ACCESS_TOKEN"
+[ -n "$C_USER_NAME" ] && USER_NAME="$C_USER_NAME"
+[ -n "$C_HOST" ] && HOST="$C_HOST"
+[ -n "$C_OUTPUT" ] && OUTPUT="$C_OUTPUT"
+shift $(($OPTIND - 1))
+if [[ $# -ge 1 ]]; then
+    PROJECT="$1"
+    shift
+fi
+[ $# -ge 1 ] && TOOLS="$@"
+# fall back to prompt
+[ -z "$ACCESS_TOKEN" ] && read -p "Access (bearer) token: " ACCESS_TOKEN
+[ -z "$USER_NAME" ] && read -p "Username: " USER_NAME
+[ -z "$PROJECT" ] && read -p "Project: " PROJECT
+[ -z "$TOOLS" ] && read -p "Tools: " TOOLS
+
+URL="https://$HOST/rest/p/$PROJECT/admin/export"
+
+function json() {
+    python -c "import sys, json;  print json.load(sys.stdin)['$1']"
+}
+
+curl -s -D $HEADERS -o $BODY --data "access_token=$ACCESS_TOKEN&tools=$TOOLS" "$URL"
+head -n1 $HEADERS | grep 400 && error "Invalid or missing tool"
+head -n1 $HEADERS | grep 503 && error "Export already in progress"
+head -n1 $HEADERS | grep -v 200 && error "Error: $(head -n1 $HEADERS)"
+
+filename=`cat $BODY | json filename`
+
+minutes=0
+while true; do
+    curl -s -D $HEADERS -o $BODY "${URL}_status?access_token=$ACCESS_TOKEN"
+    head -n1 $HEADERS | grep -v 200 && error "Error: $(head -n1 $HEADERS)"
+    status=`cat $BODY | json status`
+    [ "$status" == "ready" ] && break
+    sleep 60
+    let minutes+=1
+    [ "$minutes" -ge "$MAX_MINUTES" ] && error "Timeout waiting for export"
+done
+
+scp $USER_NAME@web.$HOST:/home/project-exports/$PROJECT/$filename $OUTPUT

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/4d649c0f/scripts/project_export.py
----------------------------------------------------------------------
diff --git a/scripts/project_export.py b/scripts/project_export.py
deleted file mode 100755
index a1946cc..0000000
--- a/scripts/project_export.py
+++ /dev/null
@@ -1,46 +0,0 @@
-#!/bin/env python
-
-import os
-import argparse
-import requests
-
-def get_opts():
-    parser = argparse.ArgumentParser(description='Initiate and download a project export
using the API')
-    parser.add_argument('project', help='Project shortname')
-    parser.add_argument('tools', nargs='+', help='Tool mount-points to export')
-    parser.add_argument('-H', '--host', default='sourceforge.net', help='Host name')
-    opts = parser.parse_args()
-    opts.url = 'https://{0}/rest/p/{1}/admin/'.format(opts.host, opts.project)
-    return opts
-
-opts = get_opts()
-access_token = raw_input('Access (bearer) token: ')
-username = raw_input('Username: ')
-
-r = requests.post(opts.url+'export', params={
-        'access_token': access_token,
-        'tools': opts.tools,
-    })
-assert r.status_code != 400, 'Invalid or missing tool mount-point'
-assert r.status_code != 503, 'Export already in progress'
-assert r.status_code == 200, 'Error [{0}]:\n{1}'.format(r.status_code, r.text)
-
-filename = r.json()['filename']
-
-print "Waiting for {0} to be ready...".format(filename)
-while True:
-    r = requests.get(opts.url+'export_status', params={'access_token': access_token})
-    assert r.status_code == 200, 'Error [{0}]:\n{1}'.format(r.status_code, r.text)
-    if r.json()['status'] == 'ready':
-        break
-
-print "Copying {0}...".format(filename)
-os.execv('/bin/env', [
-        'scp',
-        '{username}@web.sourceforge.net:/home/project-exports/{project}/{filename}'.format(
-            username=username,
-            project=opts.project,
-            filename=filename
-        ),
-        '.',
-    ])


Mime
View raw message