rbm 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use File::Basename;
  4. use lib dirname($0) . '/lib';
  5. use RBM;
  6. use YAML::XS;
  7. use Getopt::Long;
  8. #use Data::Dump qw/dd/;
  9. my %actions = (
  10. projects => {
  11. run => \&print_projects,
  12. descr => 'Print projects list',
  13. },
  14. fetch => {
  15. run => \&fetch,
  16. descr => 'Fetch commits from remote git repository',
  17. },
  18. tar => {
  19. run => \&tar,
  20. descr => 'Create source tarball',
  21. },
  22. rpmspec => {
  23. run => \&rpmspec,
  24. descr => 'Create rpm spec file',
  25. },
  26. rpm => {
  27. run => sub { build_script('rpm', @_) },
  28. descr => 'Build an rpm package',
  29. },
  30. srpm => {
  31. run => sub { build_script('srpm', @_) },
  32. descr => 'Create source rpm file',
  33. },
  34. 'deb-src' => {
  35. run => sub { build_script('deb_src', @_) },
  36. descr => 'Create debian source package',
  37. },
  38. 'deb' => {
  39. run => sub { build_script('deb', @_) },
  40. descr => 'Create debian package',
  41. },
  42. build => {
  43. run => sub { build_script('build', @_) },
  44. descr => 'Build project with a custom build script',
  45. },
  46. pkg => {
  47. run => \&pkg,
  48. descr => 'Build a package',
  49. },
  50. publish => {
  51. run => \&publish,
  52. descr => 'Publish a package',
  53. },
  54. showconf => {
  55. run => \&show_conf,
  56. descr => 'Show configuration',
  57. },
  58. usage => {
  59. run => \&usage,
  60. descr => 'Show usage information for an action',
  61. no_config => 1,
  62. },
  63. '--help' => {
  64. run => \&usage,
  65. no_config => 1,
  66. },
  67. );
  68. sub usage {
  69. if ($_[1] && $actions{$_[1]} && $actions{$_[1]}->{descr}) {
  70. system('man', "rbm-$_[1]");
  71. } else {
  72. print STDERR "$0 <action> [options]\n";
  73. print STDERR "$0 usage [action]\n\n";
  74. print STDERR "Available actions:\n";
  75. my @actions = grep { $actions{$_}->{descr} } keys %actions;
  76. print STDERR map { " - $_ : $actions{$_}->{descr}\n" } @actions;
  77. print STDERR "\nSee '$0 usage <action>' for usage informations\n";
  78. }
  79. exit 0;
  80. }
  81. sub usageexit {
  82. my $cmd = shift;
  83. print STDERR "Incorrect argument(s).\n";
  84. print STDERR "See '$0 usage $cmd' for usage informations\n";
  85. exit 1;
  86. }
  87. sub set_options {
  88. my @options = qw(distribution=s version=s tag-gpg-id=s@ commit-gpg-id=s@
  89. projects-dir=s git-clone-dir=s git-hash=s output-dir=s
  90. compress_tar=s pkg-rel=s timestamp=i fetch!
  91. gpg-keyring=s gpg-keyring-dir=s gpg-args=s gpg-bin=s
  92. sysconf-file=s debsign-keyid=s use-pbuilder!
  93. step=s target=s@ publish-src-dir=s debug! hg-clone-dir=s
  94. hg-hash=s localconf-file=s build-log=s);
  95. my %val;
  96. Getopt::Long::GetOptionsFromArray(\@_, \%val, @options) || exit 1;
  97. foreach my $k (keys %val) {
  98. if ($k eq 'step') {
  99. $RBM::config->{step} = $val{$k};
  100. next;
  101. }
  102. my $l = $k;
  103. $l =~ s/-/_/g;
  104. $RBM::config->{run}{$l} = $val{$k};
  105. }
  106. RBM::load_system_config(@_);
  107. RBM::load_local_config(@_);
  108. return $RBM::config->{run}{args} = \@_;
  109. }
  110. sub show_conf {
  111. shift;
  112. my $args = set_options(@_);
  113. if (@$args == 0) {
  114. print YAML::XS::Dump($RBM::config);
  115. return;
  116. }
  117. my $project = shift @$args;
  118. RBM::valid_project($project);
  119. my $r = @$args ? RBM::project_config($project,
  120. @$args == 1 ? $args->[0] : \@$args)
  121. : $RBM::config->{projects}{$project};
  122. RBM::exit_error "Undefined" unless defined $r;
  123. print ref $r ? YAML::XS::Dump($r) : "$r\n";
  124. }
  125. sub fetch {
  126. shift;
  127. $RBM::config->{run}{fetch} = 1;
  128. my $args = set_options(@_);
  129. my @l = @$args ? @$args : (RBM::projectslist());
  130. foreach my $project (@l) {
  131. RBM::valid_project($project);
  132. if (RBM::project_config($project, 'git_url')) {
  133. print "Fetching commits for $project\n";
  134. RBM::git_clone_fetch_chdir($project);
  135. } elsif (RBM::project_config($project, 'hg_url')) {
  136. print "Fetching commits for $project\n";
  137. RBM::hg_clone_fetch_chdir($project);
  138. } else {
  139. print "Skipping $project\n";
  140. }
  141. }
  142. }
  143. sub tar {
  144. usageexit($_[0]) unless @_ >= 2;
  145. shift;
  146. my $args = set_options(@_);
  147. usageexit('tar') unless @$args == 1;
  148. RBM::maketar($args->[0]);
  149. }
  150. sub rpmspec {
  151. usageexit($_[0]) unless @_ >= 2;
  152. shift;
  153. my $args = set_options(@_);
  154. usageexit('rpmspec') unless @$args == 1;
  155. RBM::rpmspec($args->[0]);
  156. }
  157. sub print_projects {
  158. usageexit($_[0]) unless @_ == 1;
  159. print join("\n", RBM::projectslist()), "\n";
  160. }
  161. sub build_script {
  162. my $script_name = shift;
  163. usageexit($_[0]) unless @_ >= 2;
  164. my $cmd = shift;
  165. my $args = set_options(@_);
  166. usageexit($cmd) unless @$args == 1;
  167. $script_name = $RBM::config->{step} if $RBM::config->{step} ne 'rbm_init';
  168. RBM::build_pkg($args->[0], { pkg_type => $script_name });
  169. }
  170. sub pkg {
  171. usageexit($_[0]) unless @_ >= 2;
  172. my $cmd = shift;
  173. my $args = set_options(@_);
  174. usageexit($cmd) unless @$args == 1;
  175. RBM::build_pkg($args->[0]);
  176. }
  177. sub publish {
  178. usageexit($_[0]) unless @_ >= 2;
  179. my $cmd = shift;
  180. my $args = set_options(@_);
  181. usageexit($cmd) unless @$args == 1;
  182. RBM::publish($args->[0]);
  183. }
  184. if (@ARGV == 0 || !$actions{$ARGV[0]}) {
  185. usage();
  186. exit 1;
  187. }
  188. usage('usage', $ARGV[0]) if grep { $_ eq '--help' } @ARGV[1..(@ARGV - 1)];
  189. RBM::load_config unless $actions{$ARGV[0]}->{no_config};
  190. RBM::set_default_env unless $actions{$ARGV[0]}->{no_config};
  191. $actions{$ARGV[0]}->{run}->(@ARGV);
  192. # vim: expandtab sw=4