/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. # 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. os.system(cmd)
  57. print "**", modulename, "OK"
  58. return True
  59. from optparse import OptionParser
  60. parser = OptionParser()
  61. parser.add_option("-s", "--site", help="Repository site", default='modules.zotonic.com')
  62. (options, to_install) = parser.parse_args()
  63. if not to_install:
  64. print "Specify at least one module"
  65. exit(1)
  66. print "Getting module index"
  67. connection = httplib.HTTPConnection(options.site)
  68. url = "http://%s/api/zmr/repositories" % options.site
  69. connection.request("GET", url, headers={})
  70. response = connection.getresponse()
  71. try:
  72. modules = json.loads(response.read())
  73. except:
  74. print "Parse error while getting module list from " + url
  75. exit(2)
  76. ok = False
  77. for module in to_install:
  78. ok = ok or install_module(module, modules)
  79. if ok:
  80. os.system("make -j4")
  81. os.system("%s update" % os.path.join(os.environ["ZOTONIC_BIN"], "zotonic"))