From cb2d3bceada187525f182c9ea7c7d4f5e93fefce Mon Sep 17 00:00:00 2001 From: Don Armstrong Date: Mon, 12 Mar 2012 22:29:45 -0700 Subject: [PATCH] add new_bug and overwrite file --- Debbugs/Common.pm | 24 ++++++++++++++++++- Debbugs/Status.pm | 59 ++++++++++++++++++++++++++++++++++++++++++++++- scripts/process | 24 ++++--------------- 3 files changed, 86 insertions(+), 21 deletions(-) diff --git a/Debbugs/Common.pm b/Debbugs/Common.pm index ae3c844..915fa85 100644 --- a/Debbugs/Common.pm +++ b/Debbugs/Common.pm @@ -39,7 +39,7 @@ BEGIN{ @EXPORT = (); %EXPORT_TAGS = (util => [qw(getbugcomponent getbuglocation getlocationpath get_hashname), - qw(appendfile buglog getparsedaddrs getmaintainers), + qw(appendfile overwritefile buglog getparsedaddrs getmaintainers), qw(bug_status), qw(getmaintainers_reverse), qw(getpseudodesc), @@ -218,6 +218,28 @@ sub appendfile { close $fh or die "Unable to close $file: $!"; } +=head2 overwritefile + + ovewritefile($file,'data','to','append'); + +Opens file.new, writes data to it, then moves file.new to file. + +=cut + +sub overwritefile { + my ($file,@data) = @_; + my $fh = IO::File->new("${file}.new",'w') or + die "Unable top open ${file}.new for writing: $!"; + print {$fh} @data or die "Unable to write to ${file}.new: $!"; + close $fh or die "Unable to close ${file}.new: $!"; + rename("${file}.new",$file) or + die "Unable to rename ${file}.new to $file: $!"; +} + + + + + =head2 getparsedaddrs my $address = getparsedaddrs($address); diff --git a/Debbugs/Status.pm b/Debbugs/Status.pm index bdac755..e70643f 100644 --- a/Debbugs/Status.pm +++ b/Debbugs/Status.pm @@ -44,6 +44,7 @@ use Debbugs::Packages qw(makesourceversions make_source_versions getversions get use Debbugs::Versions; use Debbugs::Versions::Dpkg; use POSIX qw(ceil); +use File::Copy qw(copy); use Storable qw(dclone); use List::Util qw(min max); @@ -62,6 +63,7 @@ BEGIN{ qw(lock_read_all_merged_bugs), ], write => [qw(writebug makestatus unlockwritebug)], + new => [qw(new_bug)], versions => [qw(addfoundversions addfixedversions), qw(removefoundversions removefixedversions) ], @@ -69,7 +71,7 @@ BEGIN{ fields => [qw(%fields)], ); @EXPORT_OK = (); - Exporter::export_ok_tags(qw(status read write versions hook fields)); + Exporter::export_ok_tags(keys %EXPORT_TAGS); $EXPORT_TAGS{all} = [@EXPORT_OK]; } @@ -517,6 +519,61 @@ sub lock_read_all_merged_bugs { return ($locks,@data); } +=head2 new_bug + + my $new_bug_num = new_bug(copy => $data->{bug_num}); + +Creates a new bug and returns the new bug number upon success. + +Dies upon failures. + +=cut + +sub new_bug { + my %param = + validate_with(params => \@_, + spec => {copy => {type => SCALAR, + regex => qr/^\d+/, + optional => 1, + }, + }, + ); + filelock("nextnumber.lock"); + my $nn_fh = IO::File->new("nextnumber",'r') or + die "Unable to open nextnuber for reading: $!"; + local $\; + my $nn = <$nn_fh>; + ($nn) = $nn =~ m/^(\d+)\n$/ or die "Bad format of nextnumber; is not exactly ".'^\d+\n$'; + close $nn_fh; + overwritefile("nextnumber", + ($nn+1)."\n"); + my $nn_hash = get_hashname($nn); + use IO::File; + my $t_fh = IO::File->new("/home/don/temp.txt",'a') or die "Unable to open ~don/temp.txt for writing: $!"; + use Data::Dumper; + print {$t_fh} Dumper({%param,nn => $nn, nn_hash => $nn_hash, nextnumber => qx(cat nextnumber)}); + close $t_fh; + if ($param{copy}) { + my $c_hash = get_hashname($param{copy}); + for my $file (qw(log status summary report)) { + copy("db-h/$c_hash/$param{copy}.$file", + "db-h/$nn_hash/${nn}.$file") + } + } + else { + for my $file (qw(log status summary report)) { + overwritefile("db-h/$nn_hash/${nn}.$file", + ""); + } + } + + # this probably needs to be munged to do something more elegant +# &bughook('new', $clone, $data); + + return($nn); +} + + my @v1fieldorder = qw(originator date subject msgid package keywords done forwarded mergedwith severity); diff --git a/scripts/process b/scripts/process index 4f7d15a..cab73b3 100755 --- a/scripts/process +++ b/scripts/process @@ -16,8 +16,8 @@ use Debbugs::MIME qw(decode_rfc1522 create_mime_message getmailbody); use Debbugs::Mail qw(send_mail_message encode_headers get_addresses); use Debbugs::Packages qw(getpkgsrc binary_to_source); use Debbugs::User qw(read_usertags write_usertags); -use Debbugs::Common qw(:lock get_hashname package_maintainer); -use Debbugs::Status qw(writebug isstrongseverity lockreadbugmerge lockreadbug read_bug splitpackages :versions); +use Debbugs::Common qw(:lock get_hashname package_maintainer overwritefile); +use Debbugs::Status qw(writebug isstrongseverity lockreadbugmerge lockreadbug new_bug read_bug splitpackages :versions); use Debbugs::CGI qw(html_escape bug_url); @@ -591,14 +591,8 @@ if ($ref<0) { # new bug report if (defined($pheader{forwarded})) { $data->{forwarded} = $pheader{forwarded}; } - &filelock("nextnumber.lock"); - open(N,"nextnumber") || die "nextnumber: read: $!"; - my $nextnumber=; $nextnumber =~ s/\n$// || die "nextnumber bad format"; - $ref= $nextnumber+0; $nextnumber += 1; $newref=1; - &overwrite('nextnumber', "$nextnumber\n"); - &unfilelock; + $ref = new_bug(); my $hash = get_hashname($ref); - &overwrite("db-h/$hash/$ref.log",''); $data->{originator} = $replyto; $data->{date} = $intdate; $data->{subject} = $subject; @@ -632,8 +626,8 @@ if ($ref<0) { # new bug report ); } } - &overwrite("db-h/$hash/$ref.report", - join("\n",@msg)."\n"); + overwritefile("db-h/$hash/$ref.report", + map {"$_\n"} @msg); } &checkmaintainers; @@ -901,14 +895,6 @@ if (not exists $header{'x-debbugs-no-ack'} and &appendlog; &finish; -sub overwrite { - my ($f,$v) = @_; - open(NEW,">$f.new") || die "$f.new: create: $!"; - print(NEW "$v") || die "$f.new: write: $!"; - close(NEW) || die "$f.new: close: $!"; - rename("$f.new","$f") || die "rename $f.new to $f: $!"; -} - sub appendlog { my $hash = get_hashname($ref); if (!open(AP,">>db-h/$hash/$ref.log")) { -- 2.39.2