PageRenderTime 21ms CodeModel.GetById 12ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/src/scripts/zotonic-installmodule

http://github.com/zotonic/zotonic
#! | 106 lines | 84 code | 22 blank | 0 comment | 0 complexity | 19858019d4d759816149a5d849517af6 MD5 | raw file
  1#!/usr/bin/env python
  2#
  3# Copyright 2011 Arjan Scherpenisse <arjan@scherpenisse.net>
  4#
  5# Licensed under the Apache License, Version 2.0 (the "License");
  6# you may not use this file except in compliance with the License.
  7# You may obtain a copy of the License at
  8# 
  9#	 http://www.apache.org/licenses/LICENSE-2.0
 10# 
 11# Unless required by applicable law or agreed to in writing, software
 12# distributed under the License is distributed on an "AS IS" BASIS,
 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14# See the License for the specific language governing permissions and
 15# limitations under the License.
 16
 17#
 18# Download and install a module from modules.zotonic.com
 19#
 20
 21import os
 22import sys
 23import httplib
 24import re
 25from urlparse import urlparse
 26
 27try:
 28    import json
 29except ImportError:
 30    import simplejson as json
 31
 32INSTALL_PATH = os.path.join(os.environ['ZOTONIC'], 'priv/modules')
 33BASE_MODULE_PATH = os.path.join(os.environ['ZOTONIC'], 'modules')
 34
 35
 36def install_module(modulename, moduleList):
 37    if not re.match("^mod_[a-z_]+$", modulename):
 38        # fixme - valid module name
 39        print "** Invalid module name:", modulename
 40        return False
 41    
 42    info  = [mod for mod in modules if mod['title'] == modulename]
 43    if not info:
 44        print "** Module not found:", modulename
 45        return False
 46    info = info[0]
 47
 48    clonepath = os.path.join(INSTALL_PATH, modulename)
 49    if os.path.exists(clonepath):
 50        print "** Target path already exists:", clonepath
 51        return False
 52
 53    if os.path.exists(os.path.join(BASE_MODULE_PATH, modulename)):
 54        print "** Module already exists in zotonic core:", clonepath
 55        return False
 56
 57    if not info["repository"] or not urlparse(info["repository"])[0]: # check on URL scheme
 58        # todo - check on valid URL
 59        print "** Module has no valid repository URL"
 60        return False
 61
 62    if info["scm"] not in ["hg", "git"]:
 63        print "** Module has unsupported/unknown SCM:", scmcmd
 64        return False
 65
 66    print "** Installing", modulename,"..."
 67
 68    cmd = "%s clone %s %s" % (info["scm"], info["repository"], clonepath)
 69    code, signal = divmod(os.system(cmd), 1<<8)
 70    if code != 0:
 71        return False
 72    else:
 73        print "**", modulename, "OK"
 74        return True
 75
 76
 77from optparse import OptionParser
 78parser = OptionParser()
 79parser.add_option("-s", "--site", help="Repository site", default='modules.zotonic.com')
 80
 81(options, to_install) = parser.parse_args()
 82
 83if not to_install:
 84    print "Specify at least one module"
 85    exit(1)
 86
 87print "Getting module index"
 88
 89connection = httplib.HTTPConnection(options.site)
 90url = "http://%s/api/zmr/repositories" % options.site
 91connection.request("GET", url, headers={}) 
 92response = connection.getresponse()
 93
 94try:
 95    modules = json.loads(response.read())
 96except:
 97    print "Parse error while getting module list from " + url
 98    exit(2)
 99
100ok = False
101for module in to_install:
102    ok = ok or install_module(module, modules)
103    
104if ok:
105    os.system("make -j4")
106    os.system("%s update" % os.path.join(os.environ["ZOTONIC_BIN"], "zotonic"))