uploadrelease.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/usr/bin/python -u
  2. '''
  3. ADOdb release upload script
  4. '''
  5. from distutils.version import LooseVersion
  6. import getopt
  7. import glob
  8. import os
  9. from os import path
  10. import re
  11. import subprocess
  12. import sys
  13. # Directories and files to exclude from release tarballs
  14. sf_files = "frs.sourceforge.net:/home/frs/project/adodb/"
  15. rsync_cmd = "rsync -vP --rsh ssh {opt} {src} {usr}@{dst}"
  16. # Command-line options
  17. options = "hn"
  18. long_options = ["help", "dry-run"]
  19. def usage():
  20. print '''Usage: %s [options] username [release_path]
  21. This script will upload the files in the given directory (or the
  22. current one if unspecified) to Sourceforge.
  23. Parameters:
  24. username Sourceforge user account
  25. release_path Location of the release files to upload
  26. (see buildrelease.py)
  27. Options:
  28. -h | --help Show this usage message
  29. -n | --dry-run Do not upload the files
  30. ''' % (
  31. path.basename(__file__)
  32. )
  33. #end usage()
  34. def call_rsync(usr, opt, src, dst):
  35. ''' Calls rsync to upload files with given parameters
  36. usr = ssh username
  37. opt = options
  38. src = source directory
  39. dst = target directory
  40. '''
  41. global dry_run
  42. command = rsync_cmd.format(usr=usr, opt=opt, src=src, dst=dst)
  43. if dry_run:
  44. print command
  45. else:
  46. subprocess.call(command, shell=True)
  47. def get_release_version():
  48. ''' Get the version number from the zip file to upload
  49. '''
  50. try:
  51. zipfile = glob.glob('adodb-*.zip')[0]
  52. except IndexError:
  53. print "ERROR: release zip file not found in '%s'" % release_path
  54. sys.exit(1)
  55. try:
  56. version = re.search(
  57. "^adodb-([\d]+\.[\d]+\.[\d]+)\.zip$",
  58. zipfile
  59. ).group(1)
  60. except AttributeError:
  61. print "ERROR: unable to extract version number from '%s'" % zipfile
  62. print " Only 3 groups of digits separated by periods are allowed"
  63. sys.exit(1)
  64. return version
  65. def sourceforge_target_dir(version):
  66. ''' Returns the sourceforge target directory
  67. Base directory as defined in sf_files global variable, plus
  68. - if version >= 5.21: adodb-X.Y
  69. - for older versions: adodb-XYZ-for-php5
  70. '''
  71. # Keep only X.Y (discard patch number)
  72. short_version = version.rsplit('.', 1)[0]
  73. directory = 'adodb-php5-only/'
  74. if LooseVersion(version) >= LooseVersion('5.21'):
  75. directory += "adodb-" + short_version
  76. else:
  77. directory += "adodb-{}-for-php5".format(short_version.replace('.', ''))
  78. return directory
  79. def process_command_line():
  80. ''' Retrieve command-line options and set global variables accordingly
  81. '''
  82. global upload_files, upload_doc, dry_run, username, release_path
  83. # Get command-line options
  84. try:
  85. opts, args = getopt.gnu_getopt(sys.argv[1:], options, long_options)
  86. except getopt.GetoptError, err:
  87. print str(err)
  88. usage()
  89. sys.exit(2)
  90. if len(args) < 1:
  91. usage()
  92. print "ERROR: please specify the Sourceforge user and release_path"
  93. sys.exit(1)
  94. # Default values for flags
  95. dry_run = False
  96. for opt, val in opts:
  97. if opt in ("-h", "--help"):
  98. usage()
  99. sys.exit(0)
  100. elif opt in ("-n", "--dry-run"):
  101. dry_run = True
  102. # Mandatory parameters
  103. username = args[0]
  104. # Change to release directory, current if not specified
  105. try:
  106. release_path = args[1]
  107. os.chdir(release_path)
  108. except IndexError:
  109. release_path = os.getcwd()
  110. def upload_release_files():
  111. ''' Upload release files from source directory to SourceForge
  112. '''
  113. version = get_release_version()
  114. target = sf_files + sourceforge_target_dir(version)
  115. print
  116. print "Uploading release files..."
  117. print " Source:", release_path
  118. print " Target: " + target
  119. print
  120. call_rsync(
  121. username,
  122. "",
  123. path.join(release_path, "*"),
  124. target
  125. )
  126. def main():
  127. process_command_line()
  128. # Start upload process
  129. print "ADOdb release upload script"
  130. upload_release_files()
  131. #end main()
  132. if __name__ == "__main__":
  133. main()