annotate extra/soundsoftware/convert-external-repos.rb @ 1452:d6b9fd02bb89 feature_36_js_refactoring

Deprecated develoment branch.
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Fri, 11 Oct 2013 17:01:24 +0100
parents 102056ec2de9
children b61a51fb42b9
rev   line source
chris@217 1 #!/usr/bin/env ruby
chris@217 2
chris@217 3 # == Synopsis
chris@217 4 #
chris@241 5 # convert-external-repos: Update local Mercurial mirrors of external repos,
chris@241 6 # by running an external command for each project requiring an update.
chris@217 7 #
chris@217 8 # == Usage
chris@217 9 #
chris@217 10 # convert-external-repos [OPTIONS...] -s [DIR] -r [HOST]
chris@217 11 #
chris@217 12 # == Arguments (mandatory)
chris@217 13 #
chris@241 14 # -s, --scm-dir=DIR use DIR as base directory for repositories
chris@217 15 # -r, --redmine-host=HOST assume Redmine is hosted on HOST. Examples:
chris@217 16 # -r redmine.example.net
chris@217 17 # -r http://redmine.example.net
chris@217 18 # -r https://example.net/redmine
chris@217 19 # -k, --key=KEY use KEY as the Redmine API key
chris@241 20 # -c, --command=COMMAND use this command to update each external
chris@241 21 # repository: command is called with the name
chris@241 22 # of the project, the path to its repo, and
chris@241 23 # its external repo url as its three args
chris@217 24 #
chris@217 25 # == Options
chris@217 26 #
chris@217 27 # --http-user=USER User for HTTP Basic authentication with Redmine WS
chris@217 28 # --http-pass=PASSWORD Password for Basic authentication with Redmine WS
chris@217 29 # -t, --test only show what should be done
chris@217 30 # -h, --help show help and exit
chris@217 31 # -v, --verbose verbose
chris@217 32 # -V, --version print version and exit
chris@217 33 # -q, --quiet no log
chris@217 34
chris@217 35
chris@217 36 require 'getoptlong'
chris@217 37 require 'rdoc/usage'
chris@217 38 require 'find'
chris@217 39 require 'etc'
chris@217 40
chris@217 41 Version = "1.0"
chris@217 42
chris@217 43 opts = GetoptLong.new(
chris@241 44 ['--scm-dir', '-s', GetoptLong::REQUIRED_ARGUMENT],
chris@217 45 ['--redmine-host', '-r', GetoptLong::REQUIRED_ARGUMENT],
chris@217 46 ['--key', '-k', GetoptLong::REQUIRED_ARGUMENT],
chris@217 47 ['--http-user', GetoptLong::REQUIRED_ARGUMENT],
chris@217 48 ['--http-pass', GetoptLong::REQUIRED_ARGUMENT],
chris@241 49 ['--command' , '-c', GetoptLong::REQUIRED_ARGUMENT],
chris@217 50 ['--test', '-t', GetoptLong::NO_ARGUMENT],
chris@217 51 ['--verbose', '-v', GetoptLong::NO_ARGUMENT],
chris@217 52 ['--version', '-V', GetoptLong::NO_ARGUMENT],
chris@217 53 ['--help' , '-h', GetoptLong::NO_ARGUMENT],
chris@217 54 ['--quiet' , '-q', GetoptLong::NO_ARGUMENT]
chris@217 55 )
chris@217 56
chris@217 57 $verbose = 0
chris@217 58 $quiet = false
chris@217 59 $redmine_host = ''
chris@217 60 $repos_base = ''
chris@217 61 $http_user = ''
chris@217 62 $http_pass = ''
chris@217 63 $test = false
chris@217 64
chris@437 65 $mirrordir = '/var/mirror'
chris@437 66
chris@217 67 def log(text, options={})
chris@217 68 level = options[:level] || 0
chris@217 69 puts text unless $quiet or level > $verbose
chris@217 70 exit 1 if options[:exit]
chris@217 71 end
chris@217 72
chris@217 73 def system_or_raise(command)
chris@217 74 raise "\"#{command}\" failed" unless system command
chris@217 75 end
chris@217 76
chris@217 77 begin
chris@217 78 opts.each do |opt, arg|
chris@217 79 case opt
chris@241 80 when '--scm-dir'; $repos_base = arg.dup
chris@217 81 when '--redmine-host'; $redmine_host = arg.dup
chris@217 82 when '--key'; $api_key = arg.dup
chris@217 83 when '--http-user'; $http_user = arg.dup
chris@217 84 when '--http-pass'; $http_pass = arg.dup
chris@241 85 when '--command'; $command = arg.dup
chris@217 86 when '--verbose'; $verbose += 1
chris@217 87 when '--test'; $test = true
chris@217 88 when '--version'; puts Version; exit
chris@217 89 when '--help'; RDoc::usage
chris@217 90 when '--quiet'; $quiet = true
chris@217 91 end
chris@217 92 end
chris@217 93 rescue
chris@217 94 exit 1
chris@217 95 end
chris@217 96
chris@217 97 if $test
chris@217 98 log("running in test mode")
chris@217 99 end
chris@217 100
chris@241 101 if ($redmine_host.empty? or $repos_base.empty? or $command.empty?)
chris@217 102 RDoc::usage
chris@217 103 end
chris@217 104
chris@217 105 unless File.directory?($repos_base)
chris@217 106 log("directory '#{$repos_base}' doesn't exist", :exit => true)
chris@217 107 end
chris@217 108
chris@217 109 begin
chris@217 110 require 'active_resource'
chris@217 111 rescue LoadError
chris@217 112 log("This script requires activeresource.\nRun 'gem install activeresource' to install it.", :exit => true)
chris@217 113 end
chris@217 114
chris@217 115 class Project < ActiveResource::Base
chris@217 116 self.headers["User-agent"] = "SoundSoftware external repository converter/#{Version}"
chris@217 117 end
chris@217 118
chris@217 119 log("querying Redmine for projects...", :level => 1);
chris@217 120
chris@217 121 $redmine_host.gsub!(/^/, "http://") unless $redmine_host.match("^https?://")
chris@217 122 $redmine_host.gsub!(/\/$/, '')
chris@217 123
chris@217 124 Project.site = "#{$redmine_host}/sys";
chris@217 125 Project.user = $http_user;
chris@217 126 Project.password = $http_pass;
chris@217 127
chris@217 128 begin
chris@217 129 # Get all active projects that have the Repository module enabled
chris@217 130 projects = Project.find(:all, :params => {:key => $api_key})
chris@217 131 rescue => e
chris@217 132 log("Unable to connect to #{Project.site}: #{e}", :exit => true)
chris@217 133 end
chris@217 134
chris@217 135 if projects.nil?
chris@217 136 log('no project found, perhaps you forgot to "Enable WS for repository management"', :exit => true)
chris@217 137 end
chris@217 138
chris@217 139 log("retrieved #{projects.size} projects", :level => 1)
chris@217 140
chris@217 141 projects.each do |project|
chris@217 142 log("treating project #{project.name}", :level => 1)
chris@217 143
chris@217 144 if project.identifier.empty?
chris@217 145 log("\tno identifier for project #{project.name}")
chris@217 146 next
chris@217 147 elsif not project.identifier.match(/^[a-z0-9\-]+$/)
chris@217 148 log("\tinvalid identifier for project #{project.name} : #{project.identifier}");
chris@217 149 next
chris@217 150 end
chris@217 151
chris@217 152 if !project.respond_to?(:repository) or !project.repository.is_external?
chris@217 153 log("\tproject #{project.identifier} does not use an external repository");
chris@217 154 next
chris@217 155 end
chris@217 156
chris@217 157 external_url = project.repository.external_url;
chris@217 158 log("\tproject #{project.identifier} has external repository url #{external_url}");
chris@217 159
chris@217 160 if !external_url.match(/^[a-z][a-z+]{0,8}[a-z]:\/\//)
chris@217 161 log("\tthis doesn't look like a plausible url to me, skipping")
chris@217 162 next
chris@217 163 end
chris@217 164
chris@217 165 repos_path = File.join($repos_base, project.identifier).gsub(File::SEPARATOR, File::ALT_SEPARATOR || File::SEPARATOR)
chris@217 166
chris@241 167 unless File.directory?(repos_path)
chris@241 168 log("\tproject repo directory '#{repos_path}' doesn't exist")
chris@217 169 next
chris@217 170 end
chris@217 171
chris@241 172 system($command, project.identifier, repos_path, external_url)
chris@437 173
chris@437 174 $cache_clearance_file = File.join($mirrordir, project.identifier, 'url_changed')
chris@437 175 if File.file?($cache_clearance_file)
chris@437 176 log("\tproject repo url has changed, requesting cache clearance")
chris@437 177 if project.post(:repository_cache, :key => $api_key)
chris@437 178 File.delete($cache_clearance_file)
chris@437 179 end
chris@437 180 end
chris@217 181
chris@217 182 end
chris@217 183