setup-transifex-config 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. :codeauthor: Pedro Algarvio (pedro@algarvio.me)
  5. setup-transifex-config
  6. ~~~~~~~~~~~~~~~~~~~~~~
  7. Setup the Transifex client configuration file
  8. """
  9. import getpass
  10. # Import python libs
  11. import os
  12. import sys
  13. import ConfigParser
  14. HOST = "https://www.transifex.com"
  15. RCFILE = os.path.abspath(
  16. os.environ.get("TRANSIFEX_RC", os.path.expanduser("~/.transifexrc"))
  17. )
  18. def main():
  19. """
  20. Run the setup code
  21. """
  22. print(
  23. "This script will setup a Transifex client configuration file, or, "
  24. "if it already exists, make some minimal checks to see if it's "
  25. "properly configured\n"
  26. )
  27. if not os.path.exists(RCFILE):
  28. while True:
  29. username = os.environ.get("TRANSIFEX_USER", None)
  30. if username is not None:
  31. break
  32. try:
  33. username = raw_input("What is your username on Transifex.com? ")
  34. if username:
  35. break
  36. except KeyboardInterrupt:
  37. print
  38. sys.exit(1)
  39. while True:
  40. password = os.environ.get("TRANSIFEX_PASS", None)
  41. if password is not None:
  42. break
  43. try:
  44. password = getpass.getpass("What is your password on Transifex.com? ")
  45. if password:
  46. break
  47. except KeyboardInterrupt:
  48. print
  49. sys.exit(1)
  50. config = ConfigParser.SafeConfigParser()
  51. config.add_section(HOST)
  52. config.set(HOST, "token", "")
  53. config.set(HOST, "hostname", HOST)
  54. config.set(HOST, "username", username)
  55. config.set(HOST, "password", password)
  56. config.write(open(RCFILE, "w"))
  57. print("username and password stored in '{0}'".format(RCFILE))
  58. os.chmod(RCFILE, 0600)
  59. print("Secured the permissions on '{0}' to 0600".format(RCFILE))
  60. sys.exit(0)
  61. # The file exists, let's see if it's properly configured
  62. config = ConfigParser.SafeConfigParser()
  63. config.read([RCFILE])
  64. if not config.has_section(HOST):
  65. print(
  66. "'~/.transifexrc' is not properly configured, it's missing "
  67. "the {0} section".format(HOST)
  68. )
  69. for setting in ("username", "password", "hostname", "token"):
  70. if not config.has_option(HOST, setting):
  71. print(
  72. "'~/.transifexrc' is not properly configured, it's "
  73. "missing the {0} option".format(setting)
  74. )
  75. sys.exit(1)
  76. if setting == "token":
  77. # Token should be left empty
  78. continue
  79. if not config.get(HOST, setting):
  80. print(
  81. "'~/.transifexrc' is not properly configured, it's "
  82. "missing a value for the {0} option".format(setting)
  83. )
  84. sys.exit(1)
  85. if __name__ == "__main__":
  86. main()