/googlecode/googlecode_distutils_upload.py

http://showslow.googlecode.com/ · Python · 117 lines · 70 code · 18 blank · 29 comment · 11 complexity · 2e1b4b23745e0ae31ed005a9d3e71d16 MD5 · raw file

  1. # Copyright 2010 Google Inc. All Rights Reserved.
  2. #
  3. # Licensed under the terms of the Apache Software License 2.0:
  4. # http://www.apache.org/licenses/LICENSE-2.0
  5. #
  6. # Questions, comments, feature requests and patches are most welcome.
  7. # Please direct all of these to the Google Code users group:
  8. # http://groups.google.com/group/google-code-hosting
  9. '''distutils command class for uploading to Google Code
  10. Add this command to your setup.py script for automatic uploading of
  11. source and Windows binary distributions. For example:
  12. try:
  13. from googlecode_distutils_upload import upload
  14. except ImportError:
  15. class upload(distutils.core.Command):
  16. user_options = []
  17. def __init__(self, *args, **kwargs):
  18. sys.stderr.write("""\
  19. error: Install this module in site-packages to upload:
  20. http://support.googlecode.com/svn/trunk/scripts/googlecode_distutils_upload.py
  21. """)
  22. sys.exit(3)
  23. setup(...,
  24. cmdclass={'upload': upload},
  25. )
  26. '''
  27. import distutils
  28. import distutils.command.bdist_wininst
  29. import os
  30. import sys
  31. import googlecode_upload
  32. SOURCE_LABELS = ['Type-Source']
  33. WINDOWS_LABELS = ['OpSys-Windows', 'Type-Installer']
  34. class upload(distutils.core.Command):
  35. description = 'upload source or Windows distribution to Google Code'
  36. user_options = [('src', None,
  37. 'upload source distribution'),
  38. ('windows', None,
  39. 'upload Windows distribution'),
  40. ('dist-dir=', 'd',
  41. 'directory to find distribution archive in'
  42. ' [default: dist]'),
  43. ('config-dir=', None,
  44. 'read svn auth data from DIR'
  45. ' ("none" means not to use svn auth data)'),
  46. ('user=', 'u',
  47. 'Google Code username'),
  48. ]
  49. boolean_options = ['src', 'windows']
  50. def initialize_options(self):
  51. self.src = False
  52. self.windows = False
  53. self.dist_dir = None
  54. self.config_dir = None
  55. self.user = None
  56. def finalize_options(self):
  57. # Validate src and windows options.
  58. if (not self.src and not self.windows) or (self.src and self.windows):
  59. sys.stderr.write('error: Use exactly one of --src or --windows\n')
  60. sys.exit(2)
  61. # Get dist-dir default from sdist or bdist_wininst.
  62. if self.src:
  63. self.set_undefined_options('sdist', ('dist_dir', 'dist_dir'))
  64. else:
  65. self.set_undefined_options('bdist_wininst', ('dist_dir', 'dist_dir'))
  66. # Do nothing for config-dir and user; upload_find_auth does the
  67. # right thing when they're None.
  68. def run(self):
  69. name = self.distribution.get_name()
  70. version = self.distribution.get_version()
  71. if self.src:
  72. # TODO(epg): sdist is more flexible with formats...
  73. fn = os.path.join(self.dist_dir, self.distribution.get_fullname())
  74. if sys.platform == 'win32':
  75. fn += '.zip'
  76. else:
  77. fn += '.tar.gz'
  78. summary = ' '.join([name, version, 'source distribution'])
  79. labels = SOURCE_LABELS
  80. else:
  81. # Get filename from bdist_wininst.
  82. bd = distutils.command.bdist_wininst.bdist_wininst(self.distribution)
  83. bd.initialize_options()
  84. bd.dist_dir = self.dist_dir
  85. bd.finalize_options()
  86. fn = bd.get_installer_filename(self.distribution.get_fullname())
  87. summary = ' '.join([name, version, 'for Windows'])
  88. labels = WINDOWS_LABELS
  89. (status, reason,
  90. file_url) = googlecode_upload.upload_find_auth(fn, name, summary,
  91. labels, self.config_dir,
  92. self.user)
  93. if file_url is None:
  94. sys.stderr.write('error: %s (%d)\n' % (reason, status))
  95. sys.exit(2)
  96. sys.stdout.write('Uploaded %s\n' % (file_url,))