Package.pm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. # Copyright © 2008 Raphaël Hertzog <hertzog@debian.org>
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 2 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License along
  11. # with this program; if not, write to the Free Software Foundation, Inc.,
  12. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  13. package Dpkg::Source::Package;
  14. use strict;
  15. use warnings;
  16. use Dpkg::Gettext;
  17. use Dpkg::ErrorHandling;
  18. use Dpkg::Control;
  19. use Dpkg::Checksums;
  20. use Dpkg::Version;
  21. use Dpkg::Compression;
  22. use Dpkg::Exit;
  23. use Dpkg::Path qw(check_files_are_the_same);
  24. use Dpkg::IPC;
  25. use Dpkg::Vendor qw(run_vendor_hook);
  26. use POSIX;
  27. use File::Basename;
  28. # Public variables
  29. our $diff_ignore_default_regexp = '
  30. # Ignore general backup files
  31. (?:^|/).*~$|
  32. # Ignore emacs recovery files
  33. (?:^|/)\.#.*$|
  34. # Ignore vi swap files
  35. (?:^|/)\..*\.sw.$|
  36. # Ignore baz-style junk files or directories
  37. (?:^|/),,.*(?:$|/.*$)|
  38. # File-names that should be ignored (never directories)
  39. (?:^|/)(?:DEADJOE|\.arch-inventory|\.(?:bzr|cvs|hg|git)ignore)$|
  40. # File or directory names that should be ignored
  41. (?:^|/)(?:CVS|RCS|\.deps|\{arch\}|\.arch-ids|\.svn|\.hg(?:tags)?|_darcs|\.git|
  42. \.shelf|_MTN|\.be|\.bzr(?:\.backup|tags)?)(?:$|/.*$)
  43. ';
  44. # Take out comments and newlines
  45. $diff_ignore_default_regexp =~ s/^#.*$//mg;
  46. $diff_ignore_default_regexp =~ s/\n//sg;
  47. no warnings 'qw';
  48. our @tar_ignore_default_pattern = qw(
  49. *.a
  50. *.la
  51. *.o
  52. *.so
  53. .*.sw?
  54. *~
  55. ,,*
  56. .[#~]*
  57. .arch-ids
  58. .arch-inventory
  59. .be
  60. .bzr
  61. .bzr.backup
  62. .bzr.tags
  63. .bzrignore
  64. .cvsignore
  65. .deps
  66. .git
  67. .gitignore
  68. .hg
  69. .hgignore
  70. .hgtags
  71. .shelf
  72. .svn
  73. CVS
  74. DEADJOE
  75. RCS
  76. _MTN
  77. _darcs
  78. {arch}
  79. );
  80. # Object methods
  81. sub new {
  82. my ($this, %args) = @_;
  83. my $class = ref($this) || $this;
  84. my $self = {
  85. 'fields' => Dpkg::Control->new(type => CTRL_PKG_SRC),
  86. 'options' => {},
  87. };
  88. bless $self, $class;
  89. if (exists $args{'options'}) {
  90. $self->{'options'} = $args{'options'};
  91. }
  92. if (exists $args{"filename"}) {
  93. $self->initialize($args{"filename"});
  94. $self->init_options();
  95. }
  96. return $self;
  97. }
  98. sub init_options {
  99. my ($self) = @_;
  100. # Use full ignore list by default
  101. # note: this function is not called by V1 packages
  102. $self->{'options'}{'diff_ignore_regexp'} ||= $diff_ignore_default_regexp;
  103. if (defined $self->{'options'}{'tar_ignore'}) {
  104. $self->{'options'}{'tar_ignore'} = [ @tar_ignore_default_pattern ]
  105. unless @{$self->{'options'}{'tar_ignore'}};
  106. } else {
  107. $self->{'options'}{'tar_ignore'} = [ @tar_ignore_default_pattern ];
  108. }
  109. # Skip debianization while specific to some formats has an impact
  110. # on code common to all formats
  111. $self->{'options'}{'skip_debianization'} ||= 0;
  112. }
  113. sub initialize {
  114. my ($self, $filename) = @_;
  115. my ($fn, $dir) = fileparse($filename);
  116. error(_g("%s is not the name of a file"), $filename) unless $fn;
  117. $self->{'basedir'} = $dir || "./";
  118. $self->{'filename'} = $fn;
  119. # Check if it contains a signature
  120. open(DSC, "<", $filename) || syserr(_g("cannot open %s"), $filename);
  121. $self->{'is_signed'} = 0;
  122. while (<DSC>) {
  123. next if /^\s*$/o;
  124. $self->{'is_signed'} = 1 if /^-----BEGIN PGP SIGNED MESSAGE-----$/o;
  125. last;
  126. }
  127. close(DSC);
  128. # Read the fields
  129. open(CDATA, "<", $filename) || syserr(_g("cannot open %s"), $filename);
  130. my $fields = Dpkg::Control->new(type => CTRL_PKG_SRC);
  131. $fields->parse_fh(\*CDATA, sprintf(_g("source control file %s"), $filename));
  132. close(CDATA);
  133. $self->{'fields'} = $fields;
  134. foreach my $f (qw(Source Format Version Files)) {
  135. unless (defined($fields->{$f})) {
  136. error(_g("missing critical source control field %s"), $f);
  137. }
  138. }
  139. $self->parse_files();
  140. $self->upgrade_object_type(0);
  141. }
  142. sub upgrade_object_type {
  143. my ($self, $update_format) = @_;
  144. $update_format = 1 unless defined $update_format;
  145. my $format = $self->{'fields'}{'Format'};
  146. if ($format =~ /^([\d\.]+)(?:\s+\((.*)\))?$/) {
  147. my ($version, $variant, $major, $minor) = ($1, $2, $1, undef);
  148. $major =~ s/\.[\d\.]+$//;
  149. my $module = "Dpkg::Source::Package::V$major";
  150. $module .= "::$variant" if defined $variant;
  151. eval "require $module; \$minor = \$${module}::CURRENT_MINOR_VERSION;";
  152. $minor = 0 unless defined $minor;
  153. if ($update_format) {
  154. $self->{'fields'}{'Format'} = "$major.$minor";
  155. $self->{'fields'}{'Format'} .= " ($variant)" if defined $variant;
  156. }
  157. if ($@) {
  158. error(_g("source package format `%s' is not supported (Perl module %s is required)"), $format, $module);
  159. }
  160. bless $self, $module;
  161. } else {
  162. error(_g("invalid Format field `%s'"), $format);
  163. }
  164. }
  165. sub get_filename {
  166. my ($self) = @_;
  167. return $self->{'basedir'} . $self->{'filename'};
  168. }
  169. sub get_files {
  170. my ($self) = @_;
  171. return keys %{$self->{'files'}};
  172. }
  173. sub parse_files {
  174. my ($self) = @_;
  175. my $rx_fname = qr/[0-9a-zA-Z][-+:.,=0-9a-zA-Z_~]+/;
  176. my $files = $self->{'fields'}{'Files'};
  177. foreach my $file (split(/\n/, $files)) {
  178. next if $file eq '';
  179. $file =~ m/^($check_regex{md5}) # checksum
  180. [ \t]+(\d+) # size
  181. [ \t]+($rx_fname) # filename
  182. $/x
  183. || error(_g("Files field contains bad line `%s'"), $file);
  184. if (exists $self->{'files'}{$3}) {
  185. error(_g("file `%s' listed twice in Files field"), $3);
  186. } else {
  187. $self->{'files'}{$3} = $2;
  188. }
  189. }
  190. }
  191. sub check_checksums {
  192. my ($self) = @_;
  193. my ($fields, %checksum, %size) = $self->{'fields'};
  194. my $has_md5 = 1;
  195. if (not exists $fields->{'Checksums-Md5'}) {
  196. $fields->{'Checksums-Md5'} = $fields->{'Files'};
  197. $has_md5 = 0;
  198. }
  199. # extract the checksums from the fields in two hashes
  200. readallchecksums($self->{'fields'}, \%checksum, \%size);
  201. delete $fields->{'Checksums-Md5'} unless $has_md5;
  202. # getchecksums verify the checksums if they are pre-filled
  203. foreach my $file ($self->get_files()) {
  204. getchecksums($self->{'basedir'} . $file, $checksum{$file},
  205. \$size{$file});
  206. }
  207. }
  208. sub get_basename {
  209. my ($self, $with_revision) = @_;
  210. my $f = $self->{'fields'};
  211. unless (exists $f->{'Source'} and exists $f->{'Version'}) {
  212. error(_g("source and version are required to compute the source basename"));
  213. }
  214. my $v = Dpkg::Version->new($f->{'Version'});
  215. my $basename = $f->{'Source'} . "_" . $v->version();
  216. if ($with_revision and $f->{'Version'} =~ /-/) {
  217. $basename .= "-" . $v->revision();
  218. }
  219. return $basename;
  220. }
  221. sub find_original_tarballs {
  222. my ($self, $ext) = @_;
  223. $ext ||= $comp_regex;
  224. my $basename = $self->get_basename();
  225. my @tar;
  226. foreach my $dir (".", $self->{'basedir'}, $self->{'options'}{'origtardir'}) {
  227. next unless defined($dir) and -d $dir;
  228. opendir(DIR, $dir) || syserr(_g("cannot opendir %s"), $dir);
  229. push @tar, map { "$dir/$_" }
  230. grep { /^\Q$basename\E\.orig(-[\w-]+)?\.tar\.$ext$/ }
  231. readdir(DIR);
  232. closedir(DIR);
  233. }
  234. return @tar;
  235. }
  236. sub is_signed {
  237. my $self = shift;
  238. return $self->{'is_signed'};
  239. }
  240. sub check_signature {
  241. my ($self) = @_;
  242. my $dsc = $self->get_filename();
  243. my @exec;
  244. if (-x '/usr/bin/gpgv') {
  245. push @exec, "gpgv";
  246. } elsif (-x '/usr/bin/gpg') {
  247. push @exec, "gpg", "--no-default-keyring", "-q", "--verify";
  248. }
  249. if (scalar(@exec)) {
  250. if (-r "$ENV{'HOME'}/.gnupg/trustedkeys.gpg") {
  251. push @exec, "--keyring", "$ENV{'HOME'}/.gnupg/trustedkeys.gpg";
  252. }
  253. foreach my $vendor_keyring (run_vendor_hook('keyrings')) {
  254. if (-r $vendor_keyring) {
  255. push @exec, "--keyring", $vendor_keyring;
  256. }
  257. }
  258. push @exec, $dsc;
  259. my ($stdout, $stderr);
  260. fork_and_exec('exec' => \@exec, wait_child => 1, nocheck => 1,
  261. to_string => \$stdout, error_to_string => \$stderr,
  262. timeout => 10);
  263. if (WIFEXITED($?)) {
  264. my $gpg_status = WEXITSTATUS($?);
  265. print STDERR "$stdout$stderr" if $gpg_status;
  266. if ($gpg_status == 1 or ($gpg_status &&
  267. $self->{'options'}{'require_valid_signature'}))
  268. {
  269. error(_g("failed to verify signature on %s"), $dsc);
  270. } elsif ($gpg_status) {
  271. warning(_g("failed to verify signature on %s"), $dsc);
  272. }
  273. } else {
  274. subprocerr("@exec");
  275. }
  276. } else {
  277. if ($self->{'options'}{'require_valid_signature'}) {
  278. error(_g("could not verify signature on %s since gpg isn't installed"), $dsc);
  279. } else {
  280. warning(_g("could not verify signature on %s since gpg isn't installed"), $dsc);
  281. }
  282. }
  283. }
  284. sub parse_cmdline_options {
  285. my ($self, @opts) = @_;
  286. foreach (@opts) {
  287. if (not $self->parse_cmdline_option($_)) {
  288. warning(_g("%s is not a valid option for %s"), $_, ref($self));
  289. }
  290. }
  291. }
  292. sub parse_cmdline_option {
  293. return 0;
  294. }
  295. sub extract {
  296. my $self = shift;
  297. my $newdirectory = $_[0];
  298. my ($ok, $error) = version_check($self->{'fields'}{'Version'});
  299. error($error) unless $ok;
  300. # Copy orig tarballs
  301. if ($self->{'options'}{'copy_orig_tarballs'}) {
  302. my $basename = $self->get_basename();
  303. my ($dirname, $destdir) = fileparse($newdirectory);
  304. $destdir ||= "./";
  305. foreach my $orig (grep { /^\Q$basename\E\.orig(-\w+)?\.tar\.$comp_regex$/ }
  306. $self->get_files())
  307. {
  308. my $src = File::Spec->catfile($self->{'basedir'}, $orig);
  309. my $dst = File::Spec->catfile($destdir, $orig);
  310. if (not check_files_are_the_same($src, $dst, 1)) {
  311. system('cp', '--', $src, $dst);
  312. subprocerr("cp $src to $dst") if $?;
  313. }
  314. }
  315. }
  316. # Try extract
  317. eval { $self->do_extract(@_) };
  318. if ($@) {
  319. &$_() foreach reverse @Dpkg::Exit::handlers;
  320. die $@;
  321. }
  322. # Store format if non-standard so that next build keeps the same format
  323. if ($self->{'fields'}{'Format'} ne "1.0" and
  324. not $self->{'options'}{'skip_debianization'})
  325. {
  326. my $srcdir = File::Spec->catdir($newdirectory, "debian", "source");
  327. my $format_file = File::Spec->catfile($srcdir, "format");
  328. mkdir($srcdir) unless -e $srcdir;
  329. open(FORMAT, ">", $format_file) || syserr(_g("cannot write %s"), $format_file);
  330. print FORMAT $self->{'fields'}{'Format'} . "\n";
  331. close(FORMAT);
  332. }
  333. # Make sure debian/rules is executable
  334. my $rules = File::Spec->catfile($newdirectory, "debian", "rules");
  335. my @s = lstat($rules);
  336. if (not scalar(@s)) {
  337. unless ($! == ENOENT) {
  338. syserr(_g("cannot stat %s"), $rules);
  339. }
  340. warning(_g("%s does not exist"), $rules)
  341. unless $self->{'options'}{'skip_debianization'};
  342. } elsif (-f _) {
  343. chmod($s[2] | 0111, $rules) ||
  344. syserr(_g("cannot make %s executable"), $rules);
  345. } else {
  346. warning(_g("%s is not a plain file"), $rules);
  347. }
  348. }
  349. sub do_extract {
  350. internerr("Dpkg::Source::Package doesn't know how to unpack a " .
  351. "source package. Use one of the subclasses.");
  352. }
  353. # Function used specifically during creation of a source package
  354. sub build {
  355. my $self = shift;
  356. eval { $self->do_build(@_) };
  357. if ($@) {
  358. &$_() foreach reverse @Dpkg::Exit::handlers;
  359. die $@;
  360. }
  361. }
  362. sub do_build {
  363. internerr("Dpkg::Source::Package doesn't know how to build a " .
  364. "source package. Use one of the subclasses.");
  365. }
  366. sub can_build {
  367. my ($self, $dir) = @_;
  368. return (0, "can_build() has not been overriden");
  369. }
  370. sub add_file {
  371. my ($self, $filename) = @_;
  372. my ($fn, $dir) = fileparse($filename);
  373. if (exists $self->{'files'}{$fn}) {
  374. internerr("tried to add file '%s' twice", $fn);
  375. }
  376. my (%sums, $size);
  377. getchecksums($filename, \%sums, \$size);
  378. $self->{'files'}{$fn} = $size;
  379. foreach my $alg (sort keys %sums) {
  380. $self->{'fields'}{"Checksums-$alg"} .= "\n$sums{$alg} $size $fn";
  381. }
  382. $self->{'fields'}{'Files'}.= "\n$sums{md5} $size $fn";
  383. }
  384. sub write_dsc {
  385. my ($self, %opts) = @_;
  386. my $fields = $self->{'fields'};
  387. foreach my $f (keys %{$opts{'override'}}) {
  388. $fields->{$f} = $opts{'override'}{$f};
  389. }
  390. unless($opts{'nocheck'}) {
  391. foreach my $f (qw(Source Version)) {
  392. unless (defined($fields->{$f})) {
  393. error(_g("missing information for critical output field %s"), $f);
  394. }
  395. }
  396. foreach my $f (qw(Maintainer Architecture Standards-Version)) {
  397. unless (defined($fields->{$f})) {
  398. warning(_g("missing information for output field %s"), $f);
  399. }
  400. }
  401. }
  402. foreach my $f (keys %{$opts{'remove'}}) {
  403. delete $fields->{$f};
  404. }
  405. my $filename = $opts{'filename'};
  406. unless (defined $filename) {
  407. $filename = $self->get_basename(1) . ".dsc";
  408. }
  409. open(DSC, ">", $filename) || syserr(_g("cannot write %s"), $filename);
  410. delete $fields->{'Checksums-Md5'}; # identical with Files field
  411. $fields->apply_substvars($opts{'substvars'});
  412. $fields->output(\*DSC);
  413. close(DSC);
  414. }
  415. # vim: set et sw=4 ts=8
  416. 1;