CAT-SOOP is a flexible, programmable learning management system based on the Python programming language. https://catsoop.mit.edu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

146 lines
4.8 KiB

  1. # This file is part of CAT-SOOP
  2. # Copyright (c) 2011-2020 by The CAT-SOOP Developers <catsoop-dev@mit.edu>
  3. #
  4. # This program is free software: you can redistribute it and/or modify it under
  5. # the terms of the GNU Affero General Public License as published by the Free
  6. # Software Foundation, either version 3 of the License, or (at your option) any
  7. # later version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but WITHOUT
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  12. # details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. import os
  17. import sys
  18. import argparse
  19. import pkg_resources
  20. # -----------------------------------------------------------------------------
  21. from catsoop import __codename__ as codename
  22. def command_line_interface(args=None, arglist=None):
  23. """
  24. Main catsoop command line entry point
  25. args, arglist are used for unit testing
  26. """
  27. raw_version = pkg_resources.require("catsoop")[0].version
  28. version = '%s ("%s")' % (raw_version, codename)
  29. if "dev" in version:
  30. gitfile = os.path.join(os.path.dirname(__file__), "dev.hash")
  31. if os.path.isfile(gitfile):
  32. with open(gitfile, "r") as f:
  33. try:
  34. vcs, hash_, date = f.read().split("|")
  35. version = '%s ("%s" Development Snapshot)\n%s revision: %s\n%s' % (
  36. raw_version,
  37. codename,
  38. vcs,
  39. hash_,
  40. date,
  41. )
  42. except:
  43. pass
  44. help_text = """
  45. Example commands:
  46. runserver : starts the CAT-SOOP webserver
  47. start : alias for runserver
  48. configure : generate CAT-SOOP configuration file using an interactive wizard
  49. logread : show the content of a given log
  50. logwrite : overwrite the content of a given log
  51. logedit : edit the content of a given log in a text editor
  52. """
  53. cmd_help = """A variety of commands are available, each with different arguments:
  54. runserver : starts the CAT-SOOP webserver
  55. start : alias for runserver
  56. configure : generate CAT-SOOP configuration file using an interactive wizard
  57. logread : show the content of a given log
  58. logwrite : overwrite the content of a given log
  59. logedit : edit the content of a given log in a text editor
  60. """
  61. parser = argparse.ArgumentParser(
  62. description=help_text, formatter_class=argparse.RawTextHelpFormatter
  63. )
  64. parser.add_argument("command", help=cmd_help)
  65. parser.add_argument("args", nargs="*", help="arguments for the given command")
  66. parser.add_argument(
  67. "-v", "--verbose", help="increase debug output verbosity", action="store_true"
  68. )
  69. parser.add_argument(
  70. "--quiet", help="decrease debug output verbosity", action="store_true"
  71. )
  72. parser.add_argument(
  73. "--log-level", type=int, help="force log level to that specified", default=None
  74. )
  75. default_config_location = os.environ.get(
  76. "XDG_CONFIG_HOME", os.path.expanduser(os.path.join("~", ".config"))
  77. )
  78. parser.add_argument("--version", action="version", version="catsoop v%s" % version)
  79. default_config_location = os.path.abspath(
  80. os.path.join(default_config_location, "catsoop", "config.py")
  81. )
  82. parser.add_argument(
  83. "-c",
  84. "--config-file",
  85. help="name of configuration file to use",
  86. default=default_config_location,
  87. )
  88. if not args:
  89. args = parser.parse_args(arglist)
  90. cfn = os.path.abspath(args.config_file)
  91. if args.verbose:
  92. os.environ["CATSOOP_DEBUG_LEVEL"] = "1"
  93. if args.quiet:
  94. os.environ["CATSOOP_DEBUG_LEVEL"] = "20"
  95. if args.log_level:
  96. os.environ["CATSOOP_DEBUG_LEVEL"] = str(args.log_level)
  97. print(
  98. "Forcing catsoop debug log level to %s" % os.environ["CATSOOP_DEBUG_LEVEL"]
  99. )
  100. if args.command == "configure":
  101. from .scripts import configure
  102. configure.main()
  103. elif args.command in {"runserver", "start"}:
  104. from .scripts import start_catsoop
  105. print("cfn=%s" % cfn)
  106. start_catsoop.startup_catsoop(cfn)
  107. elif args.command == "logread":
  108. from .scripts import log_scripts
  109. log_scripts.log_read(args.args)
  110. elif args.command == "logwrite":
  111. from .scripts import log_scripts
  112. log_scripts.log_write(args.args)
  113. elif args.command == "logedit":
  114. from .scripts import log_scripts
  115. log_scripts.log_edit(args.args)
  116. else:
  117. print("Unknown command %s" % args.command)
  118. sys.exit(-1)