PageRenderTime 52ms CodeModel.GetById 20ms app.highlight 8ms RepoModel.GetById 12ms app.codeStats 0ms

/googlecode/googlecode_distutils_upload.py

http://showslow.googlecode.com/
Python | 117 lines | 97 code | 1 blank | 19 comment | 0 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
 10'''distutils command class for uploading to Google Code
 11
 12Add this command to your setup.py script for automatic uploading of
 13source and Windows binary distributions.  For example:
 14
 15try:
 16  from googlecode_distutils_upload import upload
 17except ImportError:
 18  class upload(distutils.core.Command):
 19    user_options = []
 20    def __init__(self, *args, **kwargs):
 21      sys.stderr.write("""\
 22error: Install this module in site-packages to upload:
 23 http://support.googlecode.com/svn/trunk/scripts/googlecode_distutils_upload.py
 24""")
 25      sys.exit(3)
 26
 27setup(...,
 28      cmdclass={'upload': upload},
 29      )
 30'''
 31
 32
 33
 34import distutils
 35import distutils.command.bdist_wininst
 36import os
 37import sys
 38
 39import googlecode_upload
 40
 41
 42SOURCE_LABELS = ['Type-Source']
 43WINDOWS_LABELS = ['OpSys-Windows', 'Type-Installer']
 44
 45
 46class upload(distutils.core.Command):
 47  description = 'upload source or Windows distribution to Google Code'
 48  user_options = [('src', None,
 49                   'upload source distribution'),
 50                  ('windows', None,
 51                   'upload Windows distribution'),
 52                  ('dist-dir=', 'd',
 53                   'directory to find distribution archive in'
 54                   ' [default: dist]'),
 55                  ('config-dir=', None,
 56                   'read svn auth data from DIR'
 57                   ' ("none" means not to use svn auth data)'),
 58                  ('user=', 'u',
 59                   'Google Code username'),
 60                  ]
 61  boolean_options = ['src', 'windows']
 62
 63  def initialize_options(self):
 64    self.src = False
 65    self.windows = False
 66    self.dist_dir = None
 67    self.config_dir = None
 68    self.user = None
 69
 70  def finalize_options(self):
 71    # Validate src and windows options.
 72    if (not self.src and not self.windows) or (self.src and self.windows):
 73      sys.stderr.write('error: Use exactly one of --src or --windows\n')
 74      sys.exit(2)
 75
 76    # Get dist-dir default from sdist or bdist_wininst.
 77    if self.src:
 78      self.set_undefined_options('sdist', ('dist_dir', 'dist_dir'))
 79    else:
 80      self.set_undefined_options('bdist_wininst', ('dist_dir', 'dist_dir'))
 81
 82    # Do nothing for config-dir and user; upload_find_auth does the
 83    # right thing when they're None.
 84
 85  def run(self):
 86    name = self.distribution.get_name()
 87    version = self.distribution.get_version()
 88
 89    if self.src:
 90      # TODO(epg): sdist is more flexible with formats...
 91      fn = os.path.join(self.dist_dir, self.distribution.get_fullname())
 92      if sys.platform == 'win32':
 93        fn += '.zip'
 94      else:
 95        fn += '.tar.gz'
 96      summary = ' '.join([name, version, 'source distribution'])
 97      labels = SOURCE_LABELS
 98    else:
 99      # Get filename from bdist_wininst.
100      bd = distutils.command.bdist_wininst.bdist_wininst(self.distribution)
101      bd.initialize_options()
102      bd.dist_dir = self.dist_dir
103      bd.finalize_options()
104      fn = bd.get_installer_filename(self.distribution.get_fullname())
105      summary = ' '.join([name, version, 'for Windows'])
106      labels = WINDOWS_LABELS
107
108    (status, reason,
109     file_url) = googlecode_upload.upload_find_auth(fn, name, summary,
110                                                    labels, self.config_dir,
111                                                    self.user)
112
113    if file_url is None:
114      sys.stderr.write('error: %s (%d)\n' % (reason, status))
115      sys.exit(2)
116
117    sys.stdout.write('Uploaded %s\n' % (file_url,))