Package.pm 14 KB

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