| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  | #!/usr/bin/ruby | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # == Synopsis | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2007-10-21 16:26:14 +00:00
										 |  |  | # reposman: manages your svn repositories with Redmine | 
					
						
							| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # == Usage | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2008-09-13 16:31:11 +00:00
										 |  |  | #    reposman [OPTIONS...] -s [DIR] -r [HOST] | 
					
						
							|  |  |  | #      | 
					
						
							|  |  |  | #  Examples: | 
					
						
							|  |  |  | #    reposman --svn-dir=/var/svn --redmine-host=redmine.example.net | 
					
						
							|  |  |  | #    reposman -s /var/svn -r redmine.example.net -u http://svn.example.net | 
					
						
							| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # == Arguments (mandatory) | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2008-09-13 16:31:11 +00:00
										 |  |  | #   -s, --svn-dir=DIR         use DIR as base directory for svn repositories | 
					
						
							|  |  |  | #   -r, --redmine-host=HOST   assume Redmine is hosted on HOST. Examples: | 
					
						
							|  |  |  | #                             -r redmine.example.net | 
					
						
							|  |  |  | #                             -r http://redmine.example.net | 
					
						
							|  |  |  | #                             -r https://example.net/redmine | 
					
						
							| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # == Options | 
					
						
							| 
									
										
										
										
											2007-10-21 16:26:14 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2008-09-13 16:31:11 +00:00
										 |  |  | #   -o, --owner=OWNER         owner of the repository. using the rails login | 
					
						
							|  |  |  | #                             allow user to browse the repository within | 
					
						
							|  |  |  | #                             Redmine even for private project | 
					
						
							|  |  |  | #   -u, --url=URL             the base url Redmine will use to access your | 
					
						
							|  |  |  | #                             repositories. This option is used to automatically | 
					
						
							|  |  |  | #                             register the repositories in Redmine. The project | 
					
						
							|  |  |  | #                             identifier will be appended to this url. Examples: | 
					
						
							|  |  |  | #                             -u https://example.net/svn | 
					
						
							|  |  |  | #                             -u file:///var/svn/ | 
					
						
							|  |  |  | #                             if this option isn't set, reposman won't register | 
					
						
							|  |  |  | #                             the repositories in Redmine | 
					
						
							|  |  |  | #   -c, --command=COMMAND     use this command instead of "svnadmin create" to | 
					
						
							|  |  |  | #                             create a repository. This option can be used to | 
					
						
							|  |  |  | #                             create non-subversion repositories | 
					
						
							|  |  |  | #       --scm                 SCM vendor used to register the repository in | 
					
						
							|  |  |  | #                             Redmine (default: Subversion). Can be one of the | 
					
						
							|  |  |  | #                             other supported SCM: Bazaar, Darcs, Filesystem, | 
					
						
							|  |  |  | #                             Git, Mercurial (case sensitive). | 
					
						
							|  |  |  | #                             This option should be used when both options --url | 
					
						
							|  |  |  | #                             and --command are used. | 
					
						
							|  |  |  | #   -f, --force               force repository creation even if the project | 
					
						
							|  |  |  | #                             repository is already declared in Redmine | 
					
						
							|  |  |  | #   -t, --test                only show what should be done | 
					
						
							|  |  |  | #   -h, --help                show help and exit | 
					
						
							|  |  |  | #   -v, --verbose             verbose | 
					
						
							|  |  |  | #   -V, --version             print version and exit | 
					
						
							|  |  |  | #   -q, --quiet               no log | 
					
						
							| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | require 'getoptlong' | 
					
						
							|  |  |  | require 'rdoc/usage' | 
					
						
							|  |  |  | require 'soap/wsdlDriver' | 
					
						
							|  |  |  | require 'find' | 
					
						
							|  |  |  | require 'etc' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-13 16:31:11 +00:00
										 |  |  | Version = "1.1" | 
					
						
							|  |  |  | SUPPORTED_SCM = %w( Subversion Darcs Mercurial Bazaar Git Filesystem ) | 
					
						
							| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | opts = GetoptLong.new( | 
					
						
							|  |  |  |                       ['--svn-dir',      '-s', GetoptLong::REQUIRED_ARGUMENT], | 
					
						
							|  |  |  |                       ['--redmine-host', '-r', GetoptLong::REQUIRED_ARGUMENT], | 
					
						
							| 
									
										
										
										
											2007-10-21 16:26:14 +00:00
										 |  |  |                       ['--owner',        '-o', GetoptLong::REQUIRED_ARGUMENT], | 
					
						
							|  |  |  |                       ['--url',          '-u', GetoptLong::REQUIRED_ARGUMENT], | 
					
						
							| 
									
										
										
										
											2008-09-13 16:31:11 +00:00
										 |  |  |                       ['--command' ,     '-c', GetoptLong::REQUIRED_ARGUMENT], | 
					
						
							|  |  |  |                       ['--scm',                GetoptLong::REQUIRED_ARGUMENT], | 
					
						
							| 
									
										
										
										
											2007-11-18 18:51:48 +00:00
										 |  |  |                       ['--test',         '-t', GetoptLong::NO_ARGUMENT], | 
					
						
							| 
									
										
										
										
											2008-09-13 16:31:11 +00:00
										 |  |  |                       ['--force',        '-f', GetoptLong::NO_ARGUMENT], | 
					
						
							| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  |                       ['--verbose',      '-v', GetoptLong::NO_ARGUMENT], | 
					
						
							|  |  |  |                       ['--version',      '-V', GetoptLong::NO_ARGUMENT], | 
					
						
							|  |  |  |                       ['--help'   ,      '-h', GetoptLong::NO_ARGUMENT], | 
					
						
							|  |  |  |                       ['--quiet'  ,      '-q', GetoptLong::NO_ARGUMENT] | 
					
						
							|  |  |  |                       ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $verbose      = 0
 | 
					
						
							|  |  |  | $quiet        = false | 
					
						
							|  |  |  | $redmine_host = '' | 
					
						
							|  |  |  | $repos_base   = '' | 
					
						
							| 
									
										
										
										
											2007-10-21 16:26:14 +00:00
										 |  |  | $svn_owner    = 'root' | 
					
						
							| 
									
										
										
										
											2007-12-15 12:23:39 +00:00
										 |  |  | $use_groupid  = true | 
					
						
							| 
									
										
										
										
											2007-10-21 16:26:14 +00:00
										 |  |  | $svn_url      = false | 
					
						
							| 
									
										
										
										
											2007-11-18 18:51:48 +00:00
										 |  |  | $test         = false | 
					
						
							| 
									
										
										
										
											2008-09-13 16:31:11 +00:00
										 |  |  | $command      = "svnadmin create" | 
					
						
							|  |  |  | $force        = false | 
					
						
							|  |  |  | $scm          = 'Subversion' | 
					
						
							| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def log(text,level=0, exit=false) | 
					
						
							| 
									
										
										
										
											2008-09-14 19:03:46 +00:00
										 |  |  |   puts text unless $quiet or level > $verbose | 
					
						
							| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  |   exit 1 if exit | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | begin | 
					
						
							|  |  |  |   opts.each do |opt, arg| | 
					
						
							|  |  |  |     case opt | 
					
						
							| 
									
										
										
										
											2007-10-21 16:26:14 +00:00
										 |  |  |     when '--svn-dir';        $repos_base   = arg.dup | 
					
						
							| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  |     when '--redmine-host';   $redmine_host = arg.dup | 
					
						
							| 
									
										
										
										
											2007-12-15 12:23:39 +00:00
										 |  |  |     when '--owner';          $svn_owner    = arg.dup; $use_groupid = false; | 
					
						
							| 
									
										
										
										
											2007-10-21 16:26:14 +00:00
										 |  |  |     when '--url';            $svn_url      = arg.dup | 
					
						
							| 
									
										
										
										
											2008-09-13 16:31:11 +00:00
										 |  |  |     when '--scm';            $scm          = arg.dup; log("Invalid SCM: #{$scm}", 0, true) unless SUPPORTED_SCM.include?($scm) | 
					
						
							|  |  |  |     when '--command';        $command =      arg.dup | 
					
						
							| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  |     when '--verbose';        $verbose += 1
 | 
					
						
							| 
									
										
										
										
											2007-11-18 18:51:48 +00:00
										 |  |  |     when '--test';           $test = true | 
					
						
							| 
									
										
										
										
											2008-09-13 16:31:11 +00:00
										 |  |  |     when '--force';          $force = true | 
					
						
							| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  |     when '--version';        puts Version; exit | 
					
						
							|  |  |  |     when '--help';           RDoc::usage | 
					
						
							|  |  |  |     when '--quiet';          $quiet = true | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | rescue | 
					
						
							|  |  |  |   exit 1
 | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-18 18:51:48 +00:00
										 |  |  | if $test | 
					
						
							|  |  |  |   log("running in test mode") | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-13 16:31:11 +00:00
										 |  |  | # Make sure command is overridden if SCM vendor is not Subversion | 
					
						
							|  |  |  | if $scm != 'Subversion' && $command == 'svnadmin create' | 
					
						
							|  |  |  |   log("Please use --command option to specify how to create a #{$scm} repository.", 0, true) | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-21 16:26:14 +00:00
										 |  |  | $svn_url += "/" if $svn_url and not $svn_url.match(/\/$/) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  | if ($redmine_host.empty? or $repos_base.empty?) | 
					
						
							|  |  |  |   RDoc::usage | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | unless File.directory?($repos_base) | 
					
						
							|  |  |  |   log("directory '#{$repos_base}' doesn't exists", 0, true) | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-21 16:26:14 +00:00
										 |  |  | log("querying Redmine for projects...", 1); | 
					
						
							| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | $redmine_host.gsub!(/^/, "http://") unless $redmine_host.match("^https?://") | 
					
						
							|  |  |  | $redmine_host.gsub!(/\/$/, '') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | wsdl_url = "#{$redmine_host}/sys/service.wsdl"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | begin | 
					
						
							|  |  |  |   soap = SOAP::WSDLDriverFactory.new(wsdl_url).create_rpc_driver | 
					
						
							|  |  |  | rescue => e | 
					
						
							|  |  |  |   log("Unable to connect to #{wsdl_url} : #{e}", 0, true) | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-13 16:31:11 +00:00
										 |  |  | projects = soap.ProjectsWithRepositoryEnabled | 
					
						
							| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if projects.nil? | 
					
						
							|  |  |  |   log('no project found, perhaps you forgot to "Enable WS for repository management"', 0, true) | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | log("retrieved #{projects.size} projects", 1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-21 16:26:14 +00:00
										 |  |  | def set_owner_and_rights(project, repos_path, &block) | 
					
						
							|  |  |  |   if RUBY_PLATFORM =~ /mswin/ | 
					
						
							|  |  |  |     yield if block_given? | 
					
						
							|  |  |  |   else | 
					
						
							| 
									
										
										
										
											2007-12-15 12:23:39 +00:00
										 |  |  |     uid, gid = Etc.getpwnam($svn_owner).uid, ($use_groupid ? Etc.getgrnam(project.identifier).gid : 0) | 
					
						
							| 
									
										
										
										
											2007-11-18 18:51:48 +00:00
										 |  |  |     right = project.is_public ? 0775 : 0770
 | 
					
						
							| 
									
										
										
										
											2007-10-21 16:26:14 +00:00
										 |  |  |     yield if block_given? | 
					
						
							|  |  |  |     Find.find(repos_path) do |f| | 
					
						
							|  |  |  |       File.chmod right, f | 
					
						
							|  |  |  |       File.chown uid, gid, f | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def other_read_right?(file) | 
					
						
							|  |  |  |   (File.stat(file).mode & 0007).zero? ? false : true | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def owner_name(file) | 
					
						
							|  |  |  |   RUBY_PLATFORM =~ /mswin/ ? | 
					
						
							|  |  |  |     $svn_owner : | 
					
						
							|  |  |  |     Etc.getpwuid( File.stat(file).uid ).name   | 
					
						
							|  |  |  | end | 
					
						
							| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-21 16:26:14 +00:00
										 |  |  | projects.each do |project| | 
					
						
							|  |  |  |   log("treating project #{project.name}", 1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if project.identifier.empty? | 
					
						
							|  |  |  |     log("\tno identifier for project #{project.name}") | 
					
						
							| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  |     next | 
					
						
							| 
									
										
										
										
											2007-10-21 16:26:14 +00:00
										 |  |  |   elsif not project.identifier.match(/^[a-z0-9\-]+$/) | 
					
						
							|  |  |  |     log("\tinvalid identifier for project #{project.name} : #{project.identifier}"); | 
					
						
							| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  |     next; | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-21 16:26:14 +00:00
										 |  |  |   repos_path = $repos_base + "/" + project.identifier | 
					
						
							| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if File.directory?(repos_path) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-21 16:26:14 +00:00
										 |  |  |     # we must verify that repository has the good owner and the good | 
					
						
							|  |  |  |     # rights before leaving | 
					
						
							|  |  |  |     other_read = other_read_right?(repos_path) | 
					
						
							|  |  |  |     owner      = owner_name(repos_path) | 
					
						
							|  |  |  |     next if project.is_public == other_read and owner == $svn_owner | 
					
						
							| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-18 18:51:48 +00:00
										 |  |  |     if $test | 
					
						
							|  |  |  |       log("\tchange mode on #{repos_path}") | 
					
						
							|  |  |  |       next | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  |     begin | 
					
						
							| 
									
										
										
										
											2007-10-21 16:26:14 +00:00
										 |  |  |       set_owner_and_rights(project, repos_path) | 
					
						
							| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  |     rescue Errno::EPERM => e | 
					
						
							|  |  |  |       log("\tunable to change mode on #{repos_path} : #{e}\n") | 
					
						
							|  |  |  |       next | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     log("\tmode change on #{repos_path}"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   else | 
					
						
							| 
									
										
										
										
											2008-09-13 16:31:11 +00:00
										 |  |  |     # if repository is already declared in redmine, we don't create | 
					
						
							|  |  |  |     # unless user use -f with reposman | 
					
						
							|  |  |  |     if $force == false and not project.repository.nil? | 
					
						
							|  |  |  |       log("\trepository for project #{project.identifier} already exists in Redmine", 1) | 
					
						
							|  |  |  |       next | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-18 18:51:48 +00:00
										 |  |  |     project.is_public ? File.umask(0002) : File.umask(0007) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if $test | 
					
						
							|  |  |  |       log("\tcreate repository #{repos_path}") | 
					
						
							|  |  |  |       log("\trepository #{repos_path} registered in Redmine with url #{$svn_url}#{project.identifier}") if $svn_url; | 
					
						
							|  |  |  |       next | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     begin | 
					
						
							| 
									
										
										
										
											2007-10-21 16:26:14 +00:00
										 |  |  |       set_owner_and_rights(project, repos_path) do | 
					
						
							| 
									
										
										
										
											2008-09-13 16:31:11 +00:00
										 |  |  |         command = "#{$command} #{repos_path}" | 
					
						
							|  |  |  |         raise "#{command} failed" unless system( command  ) | 
					
						
							| 
									
										
										
										
											2007-10-21 16:26:14 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  |     rescue => e | 
					
						
							|  |  |  |       log("\tunable to create #{repos_path} : #{e}\n") | 
					
						
							|  |  |  |       next | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-21 16:26:14 +00:00
										 |  |  |     if $svn_url | 
					
						
							| 
									
										
										
										
											2008-09-13 16:31:11 +00:00
										 |  |  |       ret = soap.RepositoryCreated project.identifier, $scm, "#{$svn_url}#{project.identifier}" | 
					
						
							| 
									
										
										
										
											2007-10-21 16:26:14 +00:00
										 |  |  |       if ret > 0
 | 
					
						
							|  |  |  |         log("\trepository #{repos_path} registered in Redmine with url #{$svn_url}#{project.identifier}"); | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         log("\trepository #{repos_path} not registered in Redmine. Look in your log to find why."); | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-14 17:27:04 +00:00
										 |  |  |     log("\trepository #{repos_path} created"); | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 |