Mercurial > hg > soundsoftware-site
comparison extra/svn/reposman.rb @ 1115:433d4f72a19b redmine-2.2
Update to Redmine SVN revision 11137 on 2.2-stable branch
author | Chris Cannam |
---|---|
date | Mon, 07 Jan 2013 12:01:42 +0000 |
parents | cbb26bc654de |
children | bb32da3bea34 |
comparison
equal
deleted
inserted
replaced
929:5f33065ddc4b | 1115:433d4f72a19b |
---|---|
1 #!/usr/bin/env ruby | 1 #!/usr/bin/env ruby |
2 | 2 |
3 # == Synopsis | 3 require 'optparse' |
4 # | |
5 # reposman: manages your repositories with Redmine | |
6 # | |
7 # == Usage | |
8 # | |
9 # reposman [OPTIONS...] -s [DIR] -r [HOST] | |
10 # | |
11 # Examples: | |
12 # reposman --svn-dir=/var/svn --redmine-host=redmine.example.net --scm subversion | |
13 # reposman -s /var/git -r redmine.example.net -u http://svn.example.net --scm git | |
14 # | |
15 # == Arguments (mandatory) | |
16 # | |
17 # -s, --svn-dir=DIR use DIR as base directory for svn repositories | |
18 # -r, --redmine-host=HOST assume Redmine is hosted on HOST. Examples: | |
19 # -r redmine.example.net | |
20 # -r http://redmine.example.net | |
21 # -r https://example.net/redmine | |
22 # -k, --key=KEY use KEY as the Redmine API key (you can use the | |
23 # --key-file option as an alternative) | |
24 # | |
25 # == Options | |
26 # | |
27 # -o, --owner=OWNER owner of the repository. using the rails login | |
28 # allow user to browse the repository within | |
29 # Redmine even for private project. If you want to | |
30 # share repositories through Redmine.pm, you need | |
31 # to use the apache owner. | |
32 # -g, --group=GROUP group of the repository. (default: root) | |
33 # --scm=SCM the kind of SCM repository you want to create (and | |
34 # register) in Redmine (default: Subversion). | |
35 # reposman is able to create Git and Subversion | |
36 # repositories. For all other kind, you must specify | |
37 # a --command option | |
38 # -u, --url=URL the base url Redmine will use to access your | |
39 # repositories. This option is used to automatically | |
40 # register the repositories in Redmine. The project | |
41 # identifier will be appended to this url. Examples: | |
42 # -u https://example.net/svn | |
43 # -u file:///var/svn/ | |
44 # if this option isn't set, reposman won't register | |
45 # the repositories in Redmine | |
46 # -c, --command=COMMAND use this command instead of "svnadmin create" to | |
47 # create a repository. This option can be used to | |
48 # create repositories other than subversion and git | |
49 # kind. | |
50 # This command override the default creation for git | |
51 # and subversion. | |
52 # -f, --force force repository creation even if the project | |
53 # repository is already declared in Redmine | |
54 # --key-file=PATH path to a file that contains the Redmine API key | |
55 # (use this option instead of --key if you don't | |
56 # the key to appear in the command line) | |
57 # -t, --test only show what should be done | |
58 # -h, --help show help and exit | |
59 # -v, --verbose verbose | |
60 # -V, --version print version and exit | |
61 # -q, --quiet no log | |
62 # | |
63 # == References | |
64 # | |
65 # You can find more information on the redmine's wiki : http://www.redmine.org/wiki/redmine/HowTos | |
66 | |
67 | |
68 require 'getoptlong' | |
69 require 'rdoc/usage' | |
70 require 'find' | 4 require 'find' |
71 require 'etc' | 5 require 'etc' |
72 | 6 require 'rubygems' |
73 Version = "1.3" | 7 |
8 Version = "1.4" | |
74 SUPPORTED_SCM = %w( Subversion Darcs Mercurial Bazaar Git Filesystem ) | 9 SUPPORTED_SCM = %w( Subversion Darcs Mercurial Bazaar Git Filesystem ) |
75 | |
76 opts = GetoptLong.new( | |
77 ['--svn-dir', '-s', GetoptLong::REQUIRED_ARGUMENT], | |
78 ['--redmine-host', '-r', GetoptLong::REQUIRED_ARGUMENT], | |
79 ['--key', '-k', GetoptLong::REQUIRED_ARGUMENT], | |
80 ['--key-file', GetoptLong::REQUIRED_ARGUMENT], | |
81 ['--owner', '-o', GetoptLong::REQUIRED_ARGUMENT], | |
82 ['--group', '-g', GetoptLong::REQUIRED_ARGUMENT], | |
83 ['--url', '-u', GetoptLong::REQUIRED_ARGUMENT], | |
84 ['--command' , '-c', GetoptLong::REQUIRED_ARGUMENT], | |
85 ['--scm', GetoptLong::REQUIRED_ARGUMENT], | |
86 ['--test', '-t', GetoptLong::NO_ARGUMENT], | |
87 ['--force', '-f', GetoptLong::NO_ARGUMENT], | |
88 ['--verbose', '-v', GetoptLong::NO_ARGUMENT], | |
89 ['--version', '-V', GetoptLong::NO_ARGUMENT], | |
90 ['--help' , '-h', GetoptLong::NO_ARGUMENT], | |
91 ['--quiet' , '-q', GetoptLong::NO_ARGUMENT] | |
92 ) | |
93 | 10 |
94 $verbose = 0 | 11 $verbose = 0 |
95 $quiet = false | 12 $quiet = false |
96 $redmine_host = '' | 13 $redmine_host = '' |
97 $repos_base = '' | 14 $repos_base = '' |
131 end | 48 end |
132 end | 49 end |
133 | 50 |
134 end | 51 end |
135 | 52 |
136 begin | 53 def read_key_from_file(filename) |
137 opts.each do |opt, arg| | 54 begin |
138 case opt | 55 $api_key = File.read(filename).strip |
139 when '--svn-dir'; $repos_base = arg.dup | 56 rescue Exception => e |
140 when '--redmine-host'; $redmine_host = arg.dup | 57 $stderr.puts "Unable to read the key from #{filename}: #{e.message}" |
141 when '--key'; $api_key = arg.dup | 58 exit 1 |
142 when '--key-file' | 59 end |
143 begin | 60 end |
144 $api_key = File.read(arg).strip | 61 |
145 rescue Exception => e | 62 def set_scm(scm) |
146 $stderr.puts "Unable to read the key from #{arg}: #{e.message}" | 63 $scm = scm.capitalize |
147 exit 1 | 64 unless SUPPORTED_SCM.include?($scm) |
148 end | 65 log("Invalid SCM: #{$scm}\nValid SCM are: #{SUPPORTED_SCM.join(', ')}", :exit => true) |
149 when '--owner'; $svn_owner = arg.dup; $use_groupid = false; | 66 end |
150 when '--group'; $svn_group = arg.dup; $use_groupid = false; | 67 end |
151 when '--url'; $svn_url = arg.dup | 68 |
152 when '--scm'; $scm = arg.dup.capitalize; log("Invalid SCM: #{$scm}", :exit => true) unless SUPPORTED_SCM.include?($scm) | 69 optparse = OptionParser.new do |opts| |
153 when '--command'; $command = arg.dup | 70 opts.banner = "Usage: reposman.rb [OPTIONS...] -s [DIR] -r [HOST] -k [KEY]" |
154 when '--verbose'; $verbose += 1 | 71 opts.separator("") |
155 when '--test'; $test = true | 72 opts.separator("Manages your repositories with Redmine.") |
156 when '--force'; $force = true | 73 opts.separator("") |
157 when '--version'; puts Version; exit | 74 opts.separator("Required arguments:") |
158 when '--help'; RDoc::usage | 75 opts.on("-s", "--svn-dir DIR", "use DIR as base directory for svn repositories") {|v| $repos_base = v} |
159 when '--quiet'; $quiet = true | 76 opts.on("-r", "--redmine-host HOST","assume Redmine is hosted on HOST. Examples:", |
160 end | 77 " -r redmine.example.net", |
161 end | 78 " -r http://redmine.example.net", |
162 rescue | 79 " -r https://redmine.example.net") {|v| $redmine_host = v} |
163 exit 1 | 80 opts.on("-k", "--key KEY", "use KEY as the Redmine API key", |
164 end | 81 "(you can use --key-file option as an alternative)") {|v| $api_key = v} |
82 opts.separator("") | |
83 opts.separator("Options:") | |
84 opts.on("-o", "--owner OWNER", "owner of the repository. using the rails login", | |
85 "allows users to browse the repository within", | |
86 "Redmine even for private projects. If you want to", | |
87 "share repositories through Redmine.pm, you need", | |
88 "to use the apache owner.") {|v| $svn_owner = v; $use_groupid = false} | |
89 opts.on("-g", "--group GROUP", "group of the repository (default: root)") {|v| $svn_group = v; $use_groupid = false} | |
90 opts.on("-u", "--url URL", "the base url Redmine will use to access your", | |
91 "repositories. This option is used to register", | |
92 "the repositories in Redmine automatically. The", | |
93 "project identifier will be appended to this url.", | |
94 "Examples:", | |
95 " -u https://example.net/svn", | |
96 " -u file:///var/svn/", | |
97 "if this option isn't set, reposman won't register", | |
98 "the repositories in Redmine") {|v| $svn_url = v} | |
99 opts.on( "--scm SCM", "the kind of SCM repository you want to create", | |
100 "(and register) in Redmine (default: Subversion).", | |
101 "reposman is able to create Git and Subversion", | |
102 "repositories.", | |
103 "For all other kind, you must specify a --command", | |
104 "option") {|v| set_scm(v)} | |
105 opts.on("-c", "--command COMMAND", "use this command instead of `svnadmin create` to", | |
106 "create a repository. This option can be used to", | |
107 "create repositories other than subversion and git", | |
108 "kind.", | |
109 "This command override the default creation for", | |
110 "git and subversion.") {|v| $command = v} | |
111 opts.on( "--key-file FILE", "path to a file that contains the Redmine API key", | |
112 "(use this option instead of --key if you don't", | |
113 "want the key to appear in the command line)") {|v| read_key_from_file(v)} | |
114 opts.on("-t", "--test", "only show what should be done") {$test = true} | |
115 opts.on("-f", "--force", "force repository creation even if the project", "repository is already declared in Redmine") {$force = true} | |
116 opts.on("-v", "--verbose", "verbose") {$verbose += 1} | |
117 opts.on("-V", "--version", "show version and exit") {puts Version; exit} | |
118 opts.on("-h", "--help", "show help and exit") {puts opts; exit 1} | |
119 opts.on("-q", "--quiet", "no log") {$quiet = true} | |
120 opts.separator("") | |
121 opts.separator("Examples:") | |
122 opts.separator(" reposman.rb --svn-dir=/var/svn --redmine-host=redmine.host") | |
123 opts.separator(" reposman.rb -s /var/git -r redmine.host -u http://git.host --scm git") | |
124 opts.separator("") | |
125 opts.separator("You can find more information on the redmine's wiki:\nhttp://www.redmine.org/projects/redmine/wiki/HowTos") | |
126 | |
127 opts.summary_width = 25 | |
128 end | |
129 optparse.parse! | |
165 | 130 |
166 if $test | 131 if $test |
167 log("running in test mode") | 132 log("running in test mode") |
168 end | 133 end |
169 | 134 |
177 end | 142 end |
178 | 143 |
179 $svn_url += "/" if $svn_url and not $svn_url.match(/\/$/) | 144 $svn_url += "/" if $svn_url and not $svn_url.match(/\/$/) |
180 | 145 |
181 if ($redmine_host.empty? or $repos_base.empty?) | 146 if ($redmine_host.empty? or $repos_base.empty?) |
182 RDoc::usage | 147 puts "Some arguments are missing. Use reposman.rb --help for getting help." |
148 exit 1 | |
183 end | 149 end |
184 | 150 |
185 unless File.directory?($repos_base) | 151 unless File.directory?($repos_base) |
186 log("directory '#{$repos_base}' doesn't exists", :exit => true) | 152 log("directory '#{$repos_base}' doesn't exists", :exit => true) |
187 end | 153 end |
251 log("treating project #{project.name}", :level => 1) | 217 log("treating project #{project.name}", :level => 1) |
252 | 218 |
253 if project.identifier.empty? | 219 if project.identifier.empty? |
254 log("\tno identifier for project #{project.name}") | 220 log("\tno identifier for project #{project.name}") |
255 next | 221 next |
256 elsif not project.identifier.match(/^[a-z0-9\-]+$/) | 222 elsif not project.identifier.match(/^[a-z0-9\-_]+$/) |
257 log("\tinvalid identifier for project #{project.name} : #{project.identifier}"); | 223 log("\tinvalid identifier for project #{project.name} : #{project.identifier}"); |
258 next; | 224 next; |
259 end | 225 end |
260 | 226 |
261 repos_path = File.join($repos_base, project.identifier).gsub(File::SEPARATOR, File::ALT_SEPARATOR || File::SEPARATOR) | 227 repos_path = File.join($repos_base, project.identifier).gsub(File::SEPARATOR, File::ALT_SEPARATOR || File::SEPARATOR) |