cms-cli 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python3
  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. print(" POST <path> Do a POST request on 'path'")
  35. def main():
  36. action = None
  37. path = "/"
  38. opt_db = "./db"
  39. opt_www = "./www-data"
  40. opt_images = "/images"
  41. opt_domain = "example.com"
  42. try:
  43. (opts, args) = getopt.getopt(sys.argv[1:],
  44. "hd:w:i:D:",
  45. [ "help", "db=", "www=", "images=", "domain=", ])
  46. except (getopt.GetoptError) as e:
  47. usage()
  48. return 1
  49. for (o, v) in opts:
  50. if o in ("-h", "--help"):
  51. usage()
  52. return 0
  53. if o in ("-d", "--db"):
  54. opt_db = v
  55. if o in ("-w", "--www"):
  56. opt_www = v
  57. if o in ("-i", "--images"):
  58. opt_images = v
  59. if o in ("-D", "--domain"):
  60. opt_domain = v
  61. if len(args) >= 1:
  62. action = args[0]
  63. if len(args) >= 2:
  64. path = args[1]
  65. if not action:
  66. print("No action specified")
  67. return 1
  68. cms = None
  69. try:
  70. cms = CMS(dbPath=opt_db,
  71. wwwPath=opt_www,
  72. imagesDir=opt_images,
  73. domain=opt_domain)
  74. if action.upper() == "GET":
  75. data, mime = cms.get(path)
  76. elif action.upper() == "POST":
  77. data, mime = cms.post(path)
  78. else:
  79. print("Invalid action")
  80. return 1
  81. cms.shutdown()
  82. except (CMSException) as e:
  83. if cms:
  84. data, mime, headers = cms.getErrorPage(e)
  85. else:
  86. data, mime = "CMSException", "text/html"
  87. if mime.startswith("text/html"):
  88. result = data + b"\n"
  89. else:
  90. result = data
  91. sys.stdout.buffer.write(result)
  92. sys.stdout.buffer.flush()
  93. return 0
  94. if __name__ == "__main__":
  95. sys.exit(main())