tx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from optparse import OptionParser, OptionValueError
  4. import os
  5. import sys
  6. from txclib import utils
  7. from txclib import get_version
  8. from txclib.log import set_log_level, logger
  9. reload(sys) # WTF? Otherwise setdefaultencoding doesn't work
  10. # This block ensures that ^C interrupts are handled quietly.
  11. try:
  12. import signal
  13. def exithandler(signum,frame):
  14. signal.signal(signal.SIGINT, signal.SIG_IGN)
  15. signal.signal(signal.SIGTERM, signal.SIG_IGN)
  16. sys.exit(1)
  17. signal.signal(signal.SIGINT, exithandler)
  18. signal.signal(signal.SIGTERM, exithandler)
  19. if hasattr(signal, 'SIGPIPE'):
  20. signal.signal(signal.SIGPIPE, signal.SIG_DFL)
  21. except KeyboardInterrupt:
  22. sys.exit(1)
  23. # When we open file with f = codecs.open we specifi FROM what encoding to read
  24. # This sets the encoding for the strings which are created with f.read()
  25. sys.setdefaultencoding('utf-8')
  26. def main(argv):
  27. """
  28. Here we parse the flags (short, long) and we instantiate the classes.
  29. """
  30. usage = "usage: %prog [options] command [cmd_options]"
  31. description = "This is the Transifex command line client which"\
  32. " allows you to manage your translations locally and sync"\
  33. " them with the master Transifex server.\nIf you'd like to"\
  34. " check the available commands issue `%prog help` or if you"\
  35. " just want help with a specific command issue `%prog help"\
  36. " command`"
  37. parser = OptionParser(
  38. usage=usage, version=get_version(), description=description
  39. )
  40. parser.disable_interspersed_args()
  41. parser.add_option(
  42. "-d", "--debug", action="store_true", dest="debug",
  43. default=False, help=("enable debug messages")
  44. )
  45. parser.add_option(
  46. "-q", "--quiet", action="store_true", dest="quiet",
  47. default=False, help="don't print status messages to stdout"
  48. )
  49. parser.add_option(
  50. "-r", "--root", action="store", dest="root_dir", type="string",
  51. default=None, help="change root directory (default is cwd)"
  52. )
  53. parser.add_option(
  54. "--traceback", action="store_true", dest="trace", default=False,
  55. help="print full traceback on exceptions"
  56. )
  57. parser.add_option(
  58. "--disable-colors", action="store_true", dest="color_disable",
  59. default=(os.name == 'nt' or not sys.stdout.isatty()),
  60. help="disable colors in the output of commands"
  61. )
  62. (options, args) = parser.parse_args()
  63. if len(args) < 1:
  64. parser.error("No command was given")
  65. utils.DISABLE_COLORS = options.color_disable
  66. # set log level
  67. if options.quiet:
  68. set_log_level('WARNING')
  69. elif options.debug:
  70. set_log_level('DEBUG')
  71. # find .tx
  72. path_to_tx = options.root_dir or utils.find_dot_tx()
  73. cmd = args[0]
  74. try:
  75. utils.exec_command(cmd, args[1:], path_to_tx)
  76. except utils.UnknownCommandError:
  77. logger.error("tx: Command %s not found" % cmd)
  78. except SystemExit:
  79. sys.exit()
  80. except:
  81. import traceback
  82. if options.trace:
  83. traceback.print_exc()
  84. else:
  85. formatted_lines = traceback.format_exc().splitlines()
  86. logger.error(formatted_lines[-1])
  87. sys.exit(1)
  88. # Run baby :) ... run
  89. if __name__ == "__main__":
  90. # sys.argv[0] is the name of the script that we’re running.
  91. main(sys.argv[1:])