setup-transifex-config 2.9 KB

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