PageRenderTime 14ms CodeModel.GetById 9ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/src/scripts/zotonic-installmodule

https://code.google.com/p/zotonic/
#! | 103 lines | 81 code | 22 blank | 0 comment | 0 complexity | 0d8e858fed7920d8c6eb707d17e8c023 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    os.system(cmd)
 70    print "**", modulename, "OK"
 71    return True
 72
 73
 74from optparse import OptionParser
 75parser = OptionParser()
 76parser.add_option("-s", "--site", help="Repository site", default='modules.zotonic.com')
 77
 78(options, to_install) = parser.parse_args()
 79
 80if not to_install:
 81    print "Specify at least one module"
 82    exit(1)
 83
 84print "Getting module index"
 85
 86connection = httplib.HTTPConnection(options.site)
 87url = "http://%s/api/zmr/repositories" % options.site
 88connection.request("GET", url, headers={}) 
 89response = connection.getresponse()
 90
 91try:
 92    modules = json.loads(response.read())
 93except:
 94    print "Parse error while getting module list from " + url
 95    exit(2)
 96
 97ok = False
 98for module in to_install:
 99    ok = ok or install_module(module, modules)
100    
101if ok:
102    os.system("make -j4")
103    os.system("%s update" % os.path.join(os.environ["ZOTONIC_BIN"], "zotonic"))