1 #!/usr/bin/perl 2 3 use strict; 4 use warnings; 5 use Getopt::Long qw(:config gnu_getopt no_auto_abbrev); 6 use rpm_spec; 7 use config; 8 9 my $rpm_target = "i586"; 10 our $opt_specdir; 11 my $spec_id = 0; 12 my @specs_to_build = (); 13 my $build_command; 14 15 sub usage (;$) { 16 my $retval = shift; 17 print << "EOF"; 18 build-gnome2 [options] [command] specs... 19 20 Options: 21 22 --dumprc 23 Print the current configuration in a format suitable 24 for an rc file. 25 26 --get-name 27 Print the self package or rpm name. Use with 'parse' 28 commad. 29 30 --get-child-names 31 Print the child package or rpm names. Use with 'parse' 32 command. 33 34 --get-sources 35 Print all source filenames in the spec file. Use with 36 the 'parse' command. 37 38 --get-sources-from-child=name 39 Print the source filenames beloging to the specified 40 rpm name. Use with the 'parse' command. 41 42 --get-patches 43 Print all patch filenames in the spec file. Use with 44 the 'parse' command. 45 46 --get-patches-from-child=name 47 Print the patch filenames beloging to the specified 48 rpm name. Use with the 'parse' command. 49 50 --get-builddir 51 Print the build dirname under %{_buildir}. Use with the 52 'parse' command. 53 54 --get-child-builddirs 55 Print all child build dirnames under %{_buildir}/parent. 56 Use with the 'parse' command. 57 58 --get-builddir-from-child=name 59 Print the all child build dirnames under %{_buildir}/parent. 60 belonging to the specified rpm name, Use with the 'parse' 61 command. 62 63 Commands: 64 65 uninstall-pkgs Remove all rpms defined in the spec files listed 66 on the command line. (runs rpm --erase --nodeps) 67 68 parse Parse the spec file. Currently effect spec file names 69 and source filenames only. 70 71 EOF 72 exit 0; 73 } 74 75 76 # --------- utility functions ---------------------------------------------- 77 my $os; 78 my $os_rel; 79 80 sub init () { 81 $os = `uname -s`; 82 chomp ($os); 83 $os = lc($os); 84 $os_rel = `uname -r`; 85 chomp ($os_rel); 86 if ($os eq 'sunos') { 87 if ($os_rel >= 5) { 88 $os = 'solaris'; 89 } 90 } 91 92 if ($os eq "linux") { 93 $opt_specdir = "/jds/packages/spec-files/base-specs"; 94 } else { 95 $opt_specdir = "/jds/packages/spec-files"; 96 } 97 } 98 99 my $defaults; 100 101 sub process_defaults () { 102 $defaults = config->new(); 103 $defaults->add ('get_name', 'n', 104 'get the self package or rpm name', 105 0); 106 $defaults->add ('get_child_names', 'n', 107 'get the child package or rpm names', 108 0); 109 $defaults->add ('get_source_names', 'n', 110 'get all source filenames', 111 0); 112 $defaults->add ('get_source_names_from_child', 's', 113 'get all source filenames beloging to the specified rpm name', 114 ''); 115 $defaults->add ('get_patch_names', 'n', 116 'get all patch filenames', 117 0); 118 $defaults->add ('get_patch_names_from_child', 's', 119 'get all patch filenames beloging to the specified rpm name', 120 ''); 121 $defaults->add ('get_builddir_name', 'n', 122 'get the build dirname under %{_buildir}', 123 0); 124 $defaults->add ('get_child_builddir_names', 'n', 125 'get all child build dirnames under %{_buildir}/parent', 126 0); 127 $defaults->add ('get_builddir_name_from_child', 's', 128 'get the build dirname under %{_buildir}/parent belonging to the specified rpm name,', 129 ''); 130 } 131 132 sub add_spec ($) { 133 my $spec_name = shift; 134 my $spec; 135 136 if (-f $spec_name) { 137 $spec = rpm_spec->new ($spec_name, $rpm_target); 138 } else { 139 if (not $spec_name =~ /\.spec$/) { 140 $spec_name = "${opt_specdir}/${spec_name}.spec"; 141 } 142 $spec = rpm_spec->new ("$spec_name", $rpm_target); 143 } 144 145 if (not defined ($spec)) { 146 printf ("error"); 147 } else { 148 my $this_spec_id = $spec_id ++; 149 $specs_to_build[$this_spec_id] = $spec; 150 } 151 } 152 153 sub process_args { 154 my $arg = shift; 155 156 if (not defined ($build_command)) { 157 if (($arg ne "parse")) { 158 printf ("unknown command: $arg\n"); 159 usage (1); 160 } 161 $build_command = $arg; 162 } else { 163 add_spec ($arg); 164 } 165 } 166 167 sub process_options { 168 GetOptions ( 169 'get-name' => sub { $defaults->set ('get_name', 1); }, 170 'get-child-names' => sub { $defaults->set ('get_child_names', 1); }, 171 'get-sources' => sub { $defaults->set ('get_source_names', 1); }, 172 'get-sources-from-child=s' => sub { shift; $defaults->set ('get_source_names_from_child', shift); }, 173 'get-patches' => sub { $defaults->set ('get_patch_names', 1); }, 174 'get-patches-from-child=s' => sub { shift; $defaults->set ('get_patch_names_from_child', shift); }, 175 'get-builddir' => sub { $defaults->set ('get_builddir_name', 1); }, 176 'get-child-builddirs' => sub { $defaults->set ('get_child_builddir_names', 1); }, 177 'get-builddir-from-child=s' => sub { shift; $defaults->set ('get_builddir_name_from_child', shift); }, 178 'help' => \&usage, 179 '<>' => \&process_args); 180 } 181 182 # --------- parse command -------------------------------------------------- 183 sub print_self_name ($) { 184 my $spec = shift; 185 my @sources; 186 187 if ($os eq "linux") { 188 @sources = $spec->get_param_array ('sources'); 189 printf ("%s\n", $spec->{name}); 190 } elsif ($os eq "solaris") { 191 # printf ("%s\n", $spec); 192 printf ("%s\n", $spec->get_name($spec)); 193 } 194 } 195 196 sub print_child_names ($) { 197 my $spec = shift; 198 my @sources; 199 200 if ($os eq "linux") { 201 return; 202 } elsif ($os eq "solaris") { 203 @sources = $spec->get_sources('sources'); 204 my @spec_names_used = keys %{$spec->{_specs_used}}; 205 206 foreach my $spec_name_used (@spec_names_used) { 207 my $spec_used = $spec->{_specs_used}->{$spec_name_used}; 208 printf ("%s\n", $$spec_used->get_name($$spec_used)); 209 # printf ("%s\n", $$spec_used->get_base_file_name()); 210 } 211 } 212 } 213 214 sub print_sources ($) { 215 my $spec = shift; 216 my @sources; 217 218 if ($os eq "linux") { 219 @sources = $spec->get_param_array ('sources'); 220 foreach my $src (@sources) { 221 if (not defined ($src)) { 222 next; 223 } 224 printf ("%s\n", $src); 225 } 226 } elsif ($os eq "solaris") { 227 @sources = $spec->get_sources('sources'); 228 foreach my $src (@sources) { 229 if (not defined ($src)) { 230 next; 231 } 232 printf ("%s\n", $src); 233 } 234 } 235 } 236 237 sub print_sources_from_child ($$) { 238 my $spec = shift; 239 my $user_rpm = shift; 240 my @sources; 241 242 if ($os eq "linux") { 243 return; 244 } elsif ($os eq "solaris") { 245 @sources = $spec->get_sources('sources'); 246 my @spec_names_used = keys %{$spec->{_specs_used}}; 247 foreach my $spec_name_used (@spec_names_used) { 248 my $spec_used = $spec->{_specs_used}->{$spec_name_used}; 249 250 if($user_rpm eq $$spec_used->get_name ($$spec_used)) { 251 my @sources_used = $$spec_used->get_sources (); 252 253 foreach my $src (@sources_used) { 254 printf ("%s\n", $src); 255 } 256 exit 0; 257 } 258 } 259 260 printf ("#### Not Found the rpm: %s\n", $user_rpm); 261 exit 1; 262 } 263 } 264 265 sub print_patches ($) { 266 my $spec = shift; 267 my @patches; 268 269 if ($os eq "linux") { 270 @patches = $spec->get_param_array ('patches'); 271 foreach my $patch (@patches) { 272 if (not defined ($patch)) { 273 next; 274 } 275 printf ("%s\n", $patch); 276 } 277 } elsif ($os eq "solaris") { 278 @patches = $spec->get_patches('patches'); 279 foreach my $patch (@patches) { 280 if (not defined ($patch )) { 281 next; 282 } 283 printf ("%s\n", $patch); 284 } 285 } 286 } 287 288 sub print_patches_from_child ($$) { 289 my $spec = shift; 290 my $user_rpm = shift; 291 my @sources; 292 293 if ($os eq "linux") { 294 return; 295 } elsif ($os eq "solaris") { 296 @sources = $spec->get_sources('sources'); 297 my @spec_names_used = keys %{$spec->{_specs_used}}; 298 foreach my $spec_name_used (@spec_names_used) { 299 my $spec_used = $spec->{_specs_used}->{$spec_name_used}; 300 301 if($user_rpm eq $$spec_used->get_name ($$spec_used)) { 302 my @patches_used = $$spec_used->get_patches (); 303 304 foreach my $patch (@patches_used) { 305 printf ("%s\n", $patch); 306 } 307 exit 0; 308 } 309 } 310 311 printf ("#### Not Found the rpm: %s\n", $user_rpm); 312 exit 1; 313 } 314 } 315 316 sub print_builddir ($) { 317 my $spec = shift; 318 my @sources; 319 320 if ($os eq "linux") { 321 @sources = $spec->get_param_array ('sources'); 322 my $bsdirname = $spec->get_def('_build_src_dir_name'); 323 324 if (not defined $bsdirname) { 325 $bsdirname = $spec->_deref ('%name-%version'); 326 } 327 printf ("%s\n", $bsdirname); 328 329 } elsif ($os eq "solaris") { 330 @sources = $spec->get_sources('sources'); 331 my $bsdirname = $spec->eval ('%{_build_src_dir_name}'); 332 333 if ($bsdirname eq '%{_build_src_dir_name}') { 334 $bsdirname = $spec->eval ('%name-%version'); 335 } 336 printf ("%s\n", $bsdirname); 337 338 } 339 } 340 341 sub print_child_builddirs ($) { 342 my $spec = shift; 343 my @sources; 344 345 if ($os eq "linux") { 346 return; 347 } elsif ($os eq "solaris") { 348 @sources = $spec->get_sources('sources'); 349 my @spec_names_used = keys %{$spec->{_specs_used}}; 350 351 foreach my $spec_name_used (@spec_names_used) { 352 my $spec_used = $spec->{_specs_used}->{$spec_name_used}; 353 my $bsdirname = $$spec_used->eval ('%{_build_src_dir_name}'); 354 355 if ($bsdirname ne '%{_build_src_dir_name}') { 356 printf ("%s\n", $bsdirname); 357 } 358 } 359 360 } 361 } 362 363 sub print_builddir_from_child ($$) { 364 my $spec = shift; 365 my $user_rpm = shift; 366 my @sources; 367 368 if ($os eq "linux") { 369 return; 370 } elsif ($os eq "solaris") { 371 @sources = $spec->get_sources('sources'); 372 my @spec_names_used = keys %{$spec->{_specs_used}}; 373 374 foreach my $spec_name_used (@spec_names_used) { 375 my $spec_used = $spec->{_specs_used}->{$spec_name_used}; 376 377 if($user_rpm eq $$spec_used->get_name ($$spec_used)) { 378 my $bsdirname = $$spec_used->eval ('%{_build_src_dir_name}'); 379 380 if ($bsdirname ne '%{_build_src_dir_name}') { 381 printf ("%s\n", $bsdirname); 382 } 383 exit 0; 384 } 385 } 386 387 printf ("#### Not Found the rpm: %s\n", $user_rpm); 388 exit 1; 389 } 390 } 391 392 sub get_misc () { 393 my $spec = shift; 394 my @sources; 395 396 if ($os eq "linux") { 397 @sources = $spec->get_param_array ('sources'); 398 foreach my $src (@sources) { 399 if (not defined ($src)) { 400 next; 401 } 402 # $1 Name:, 403 # $2 build dir name 404 # $3 source filename 405 printf ("Srcs: %s %s %s\n", $spec->{name}, $spec, $src); 406 } 407 } else { 408 @sources = $spec->get_sources('sources'); 409 foreach my $src (@sources) { 410 if (not defined ($src)) { 411 next; 412 } 413 # $1 Solaris Name:, 414 # $2 Solaris spec filename, 415 # $3 source filename 416 printf ("Srcs: %s %s %s\n", $spec, $spec->{_base_file_name}, $src); 417 } 418 419 my @spec_names_used = keys %{$spec->{_specs_used}}; 420 foreach my $spec_name_used (@spec_names_used) { 421 my $spec_used = $spec->{_specs_used}->{$spec_name_used}; 422 423 my @sources_used = $$spec_used->get_sources (); 424 foreach my $src (@sources_used) { 425 # $1 Solaris Name:, 426 # $2 Solaris spec filename, 427 # $3 Linux Name:, 428 # $4 Linux source filename 429 430 # I don't know but when used 'printf (' instead of 'printf(', got exec errors. 431 432 printf("Spec sources: %s %s %s %s\n", $spec, 433 $spec->{_base_file_name}, 434 $spec_name_used, 435 $src); 436 } 437 438 my @patches_used = $$spec_used->get_patches (); 439 foreach my $patch (@patches_used) { 440 # $1 Solaris Name:, 441 # $2 Solaris spec filename, 442 # $3 Linux Name:, 443 # $4 Linux patch filename 444 printf("Spec pths: %s %s %s %s\n", $spec, 445 $spec->{_base_file_name}, 446 $spec_name_used, 447 $patch); 448 } 449 } 450 } 451 } 452 453 sub get_parse_options ($) { 454 my $spec_id = shift; 455 my $spec = $specs_to_build[$spec_id]; 456 my @sources; 457 my $user_rpm_name; 458 459 if ($defaults->get ('get_name')) { 460 print_self_name ($spec); 461 exit (0); 462 } 463 464 if ($defaults->get ('get_child_names')) { 465 print_child_names ($spec); 466 exit (0); 467 } 468 469 if ($defaults->get ('get_source_names')) { 470 print_sources ($spec); 471 exit (0); 472 } 473 474 $user_rpm_name = $defaults->get ('get_source_names_from_child'); 475 if ($user_rpm_name ne "") { 476 print_sources_from_child ($spec, $user_rpm_name); 477 exit (0); 478 } 479 480 if ($defaults->get ('get_patch_names')) { 481 print_patches ($spec); 482 exit (0); 483 } 484 485 $user_rpm_name = $defaults->get ('get_patch_names_from_child'); 486 if ($user_rpm_name ne "") { 487 print_patches_from_child ($spec, $user_rpm_name); 488 exit (0); 489 } 490 491 if ($defaults->get ('get_builddir_name')) { 492 print_builddir ($spec); 493 exit (0); 494 } 495 496 if ($defaults->get ('get_child_builddir_names')) { 497 print_child_builddirs ($spec); 498 exit (0); 499 } 500 501 $user_rpm_name = $defaults->get ('get_builddir_name_from_child'); 502 if ($user_rpm_name ne "") { 503 print_builddir_from_child ($spec, $user_rpm_name); 504 exit (0); 505 } 506 507 else { 508 sub get_misc (); 509 exit (0); 510 } 511 512 } 513 514 sub do_build { 515 while(0){}; #dummy 516 } 517 518 sub do_parse { 519 for (my $i = 0; $i <=$#specs_to_build; $i++) { 520 get_parse_options ($i); 521 } 522 } 523 524 sub main { 525 process_defaults (); 526 process_options (); 527 528 if ($build_command eq "build") { 529 do_build; 530 } elsif ($build_command eq "parse") { 531 do_parse; 532 } 533 } 534 535 init; 536 main; 537