__init__.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. """
  2. Base class for Scrapy commands
  3. """
  4. import os
  5. from optparse import OptionGroup
  6. from twisted.python import failure
  7. from scrapy.utils.conf import arglist_to_dict
  8. from scrapy.exceptions import UsageError
  9. class ScrapyCommand(object):
  10. requires_project = False
  11. crawler_process = None
  12. # default settings to be used for this command instead of global defaults
  13. default_settings = {}
  14. exitcode = 0
  15. def __init__(self):
  16. self.settings = None # set in scrapy.cmdline
  17. def set_crawler(self, crawler):
  18. assert not hasattr(self, '_crawler'), "crawler already set"
  19. self._crawler = crawler
  20. def syntax(self):
  21. """
  22. Command syntax (preferably one-line). Do not include command name.
  23. """
  24. return ""
  25. def short_desc(self):
  26. """
  27. A short description of the command
  28. """
  29. return ""
  30. def long_desc(self):
  31. """A long description of the command. Return short description when not
  32. available. It cannot contain newlines, since contents will be formatted
  33. by optparser which removes newlines and wraps text.
  34. """
  35. return self.short_desc()
  36. def help(self):
  37. """An extensive help for the command. It will be shown when using the
  38. "help" command. It can contain newlines, since no post-formatting will
  39. be applied to its contents.
  40. """
  41. return self.long_desc()
  42. def add_options(self, parser):
  43. """
  44. Populate option parse with options available for this command
  45. """
  46. group = OptionGroup(parser, "Global Options")
  47. group.add_option("--logfile", metavar="FILE",
  48. help="log file. if omitted stderr will be used")
  49. group.add_option("-L", "--loglevel", metavar="LEVEL", default=None,
  50. help="log level (default: %s)" % self.settings['LOG_LEVEL'])
  51. group.add_option("--nolog", action="store_true",
  52. help="disable logging completely")
  53. group.add_option("--profile", metavar="FILE", default=None,
  54. help="write python cProfile stats to FILE")
  55. group.add_option("--pidfile", metavar="FILE",
  56. help="write process ID to FILE")
  57. group.add_option("-s", "--set", action="append", default=[], metavar="NAME=VALUE",
  58. help="set/override setting (may be repeated)")
  59. group.add_option("--pdb", action="store_true", help="enable pdb on failure")
  60. parser.add_option_group(group)
  61. def process_options(self, args, opts):
  62. try:
  63. self.settings.setdict(arglist_to_dict(opts.set),
  64. priority='cmdline')
  65. except ValueError:
  66. raise UsageError("Invalid -s value, use -s NAME=VALUE", print_help=False)
  67. if opts.logfile:
  68. self.settings.set('LOG_ENABLED', True, priority='cmdline')
  69. self.settings.set('LOG_FILE', opts.logfile, priority='cmdline')
  70. if opts.loglevel:
  71. self.settings.set('LOG_ENABLED', True, priority='cmdline')
  72. self.settings.set('LOG_LEVEL', opts.loglevel, priority='cmdline')
  73. if opts.nolog:
  74. self.settings.set('LOG_ENABLED', False, priority='cmdline')
  75. if opts.pidfile:
  76. with open(opts.pidfile, "w") as f:
  77. f.write(str(os.getpid()) + os.linesep)
  78. if opts.pdb:
  79. failure.startDebugMode()
  80. def run(self, args, opts):
  81. """
  82. Entry point for running commands
  83. """
  84. raise NotImplementedError