cms-cli 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python
  2. #
  3. # simple WSGI/Python based CMS script
  4. # commandline interface
  5. #
  6. # Copyright (C) 2012 Michael Buesch <m@bues.ch>
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. import sys
  21. import getopt
  22. from cms import *
  23. def usage():
  24. print("Usage: %s [OPTIONS] [ACTION]" % sys.argv[0])
  25. print("")
  26. print("Options:")
  27. print(" -d|--db PATH Path to database. Default: ./db")
  28. print(" -w|--www PATH Path to the static data. Default: ./www-data")
  29. print(" -i|--images SUBPATH Path to images. Default: /images")
  30. print(" -D|--domain DOMAIN The domain name. Default: example.com")
  31. print("")
  32. print("Actions:")
  33. print(" GET <path> Do a GET request on 'path'")
  34. def main():
  35. action = None
  36. path = "/"
  37. opt_db = "./db"
  38. opt_www = "./www-data"
  39. opt_images = "/images"
  40. opt_domain = "example.com"
  41. try:
  42. (opts, args) = getopt.getopt(sys.argv[1:],
  43. "hd:w:i:D:",
  44. [ "help", "db=", "www=", "images=", "domain=", ])
  45. except (getopt.GetoptError), e:
  46. usage()
  47. return 1
  48. for (o, v) in opts:
  49. if o in ("-h", "--help"):
  50. usage()
  51. return 0
  52. if o in ("-d", "--db"):
  53. opt_db = v
  54. if o in ("-w", "--www"):
  55. opt_www = v
  56. if o in ("-i", "--images"):
  57. opt_images = v
  58. if o in ("-D", "--domain"):
  59. opt_domain = v
  60. if len(args) >= 1:
  61. action = args[0]
  62. if len(args) >= 2:
  63. path = args[1]
  64. if not action:
  65. print("No action specified")
  66. return 1
  67. cms = None
  68. try:
  69. cms = CMS(dbPath=opt_db,
  70. wwwPath=opt_www,
  71. imagesDir=opt_images,
  72. domain=opt_domain)
  73. if action.upper() == "GET":
  74. data, mime = cms.get(path)
  75. else:
  76. assert(0)
  77. cms.shutdown()
  78. except (CMSException), e:
  79. if cms:
  80. data, mime = cms.getErrorPage(e)
  81. else:
  82. data, mime = "CMSException", "text/html"
  83. if mime == "text/html":
  84. result = data + "\n"
  85. else:
  86. result = data
  87. sys.stdout.write(result)
  88. sys.stdout.flush()
  89. return 0
  90. if __name__ == "__main__":
  91. sys.exit(main())