/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. # Download and install a module from modules.zotonic.com
  18. #
  19. import os
  20. import sys
  21. import httplib
  22. import re
  23. from urlparse import urlparse
  24. try:
  25. import json
  26. except ImportError:
  27. import simplejson as json
  28. INSTALL_PATH = os.path.join(os.environ['ZOTONIC'], 'priv/modules')
  29. BASE_MODULE_PATH = os.path.join(os.environ['ZOTONIC'], 'modules')
  30. def install_module(modulename, moduleList):
  31. if not re.match("^mod_[a-z_]+$", modulename):
  32. # fixme - valid module name
  33. print "** Invalid module name:", modulename
  34. return False
  35. info = [mod for mod in modules if mod['title'] == modulename]
  36. if not info:
  37. print "** Module not found:", modulename
  38. return False
  39. info = info[0]
  40. clonepath = os.path.join(INSTALL_PATH, modulename)
  41. if os.path.exists(clonepath):
  42. print "** Target path already exists:", clonepath
  43. return False
  44. if os.path.exists(os.path.join(BASE_MODULE_PATH, modulename)):
  45. print "** Module already exists in zotonic core:", clonepath
  46. return False
  47. if not info["repository"] or not urlparse(info["repository"])[0]: # check on URL scheme
  48. # todo - check on valid URL
  49. print "** Module has no valid repository URL"
  50. return False
  51. if info["scm"] not in ["hg", "git"]:
  52. print "** Module has unsupported/unknown SCM:", scmcmd
  53. return False
  54. print "** Installing", modulename,"..."
  55. cmd = "%s clone %s %s" % (info["scm"], info["repository"], clonepath)
  56. code, signal = divmod(os.system(cmd), 1<<8)
  57. if code != 0:
  58. return False
  59. else:
  60. print "**", modulename, "OK"
  61. return True
  62. from optparse import OptionParser
  63. parser = OptionParser()
  64. parser.add_option("-s", "--site", help="Repository site", default='modules.zotonic.com')
  65. (options, to_install) = parser.parse_args()
  66. if not to_install:
  67. print "Specify at least one module"
  68. exit(1)
  69. print "Getting module index"
  70. connection = httplib.HTTPConnection(options.site)
  71. url = "http://%s/api/zmr/repositories" % options.site
  72. connection.request("GET", url, headers={})
  73. response = connection.getresponse()
  74. try:
  75. modules = json.loads(response.read())
  76. except:
  77. print "Parse error while getting module list from " + url
  78. exit(2)
  79. ok = False
  80. for module in to_install:
  81. ok = ok or install_module(module, modules)
  82. if ok:
  83. os.system("make -j4")
  84. os.system("%s update" % os.path.join(os.environ["ZOTONIC_BIN"], "zotonic"))