cms-cli 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. def usage():
  23. print("Usage: %s [OPTIONS] [ACTION]" % sys.argv[0])
  24. print("")
  25. print("Options:")
  26. print(" -d|--db PATH Path to database. Default: ./db")
  27. print(" -w|--www PATH Path to the static data. Default: ./www-data")
  28. print(" -i|--images SUBPATH Path to images. Default: /images")
  29. print(" -D|--domain DOMAIN The domain name. Default: example.com")
  30. print(" -C|--cython Import Cython modules")
  31. print(" -P|--profile LEVEL Enable profiling")
  32. print(" -L|--loop LOOPS Run LOOPS number of loops. For profiling.")
  33. print("")
  34. print("Actions:")
  35. print(" GET <path> Do a GET request on 'path'")
  36. print(" POST <path> Do a POST request on 'path'")
  37. def main():
  38. action = None
  39. path = "/"
  40. opt_db = "./db"
  41. opt_www = "./www-data"
  42. opt_images = "/images"
  43. opt_domain = "example.com"
  44. opt_cython = False
  45. opt_profile = 0
  46. opt_loop = 1
  47. try:
  48. (opts, args) = getopt.getopt(sys.argv[1:],
  49. "hd:w:i:D:CP:L:",
  50. [ "help", "db=", "www=", "images=", "domain=", "cython",
  51. "profile=", "loop=", ])
  52. except (getopt.GetoptError) as e:
  53. usage()
  54. return 1
  55. for (o, v) in opts:
  56. if o in ("-h", "--help"):
  57. usage()
  58. return 0
  59. if o in ("-d", "--db"):
  60. opt_db = v
  61. if o in ("-w", "--www"):
  62. opt_www = v
  63. if o in ("-i", "--images"):
  64. opt_images = v
  65. if o in ("-D", "--domain"):
  66. opt_domain = v
  67. if o in ("-C", "--cython"):
  68. opt_cython = True
  69. if o in ("-P", "--profile"):
  70. try:
  71. opt_profile = int(v)
  72. except ValueError:
  73. print("Invalid -P|--profile value")
  74. return 1
  75. if o in ("-L", "--loop"):
  76. try:
  77. opt_loop = int(v)
  78. if opt_loop < 1:
  79. raise ValueError
  80. except ValueError:
  81. print("Invalid -L|--loop value")
  82. return 1
  83. if len(args) >= 1:
  84. action = args[0]
  85. if len(args) >= 2:
  86. path = args[1]
  87. if not action:
  88. print("No action specified")
  89. return 1
  90. retval = 0
  91. cms = prof = None
  92. if opt_cython:
  93. from cms_cython import CMS, CMSException
  94. from cms_cython.profiler import Profiler
  95. else:
  96. from cms import CMS, CMSException
  97. from cms.profiler import Profiler
  98. try:
  99. if opt_profile >= 1:
  100. prof = Profiler()
  101. if opt_profile >= 2:
  102. prof.start()
  103. cms = CMS(dbPath=opt_db,
  104. wwwPath=opt_www,
  105. imagesDir=opt_images,
  106. domain=opt_domain,
  107. debug=True)
  108. if opt_profile == 1:
  109. prof.start()
  110. for _ in range(opt_loop):
  111. if action.upper() == "GET":
  112. data, mime = cms.get(path)
  113. elif action.upper() == "POST":
  114. data, mime = cms.post(path)
  115. else:
  116. print("Invalid action")
  117. return 1
  118. cms.shutdown()
  119. if opt_profile >= 1:
  120. prof.stop()
  121. except (CMSException) as e:
  122. if cms:
  123. data, mime, headers = cms.getErrorPage(e)
  124. else:
  125. data, mime = "CMSException", "application/xhtml+xml"
  126. retval = 1
  127. if "html" in mime:
  128. result = data + b"\n"
  129. else:
  130. result = data
  131. sys.stdout.buffer.write(result)
  132. sys.stdout.buffer.flush()
  133. if prof:
  134. print(prof.getResult(), file=sys.stderr)
  135. return retval
  136. if __name__ == "__main__":
  137. sys.exit(main())