Package.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. 'checksums' => Dpkg::Checksums->new(),
  91. };
  92. bless $self, $class;
  93. if (exists $args{'options'}) {
  94. $self->{'options'} = $args{'options'};
  95. }
  96. if (exists $args{"filename"}) {
  97. $self->initialize($args{"filename"});
  98. $self->init_options();
  99. }
  100. return $self;
  101. }
  102. sub init_options {
  103. my ($self) = @_;
  104. # Use full ignore list by default
  105. # note: this function is not called by V1 packages
  106. $self->{'options'}{'diff_ignore_regexp'} ||= $diff_ignore_default_regexp;
  107. if (defined $self->{'options'}{'tar_ignore'}) {
  108. $self->{'options'}{'tar_ignore'} = [ @tar_ignore_default_pattern ]
  109. unless @{$self->{'options'}{'tar_ignore'}};
  110. } else {
  111. $self->{'options'}{'tar_ignore'} = [ @tar_ignore_default_pattern ];
  112. }
  113. # Skip debianization while specific to some formats has an impact
  114. # on code common to all formats
  115. $self->{'options'}{'skip_debianization'} ||= 0;
  116. }
  117. sub initialize {
  118. my ($self, $filename) = @_;
  119. my ($fn, $dir) = fileparse($filename);
  120. error(_g("%s is not the name of a file"), $filename) unless $fn;
  121. $self->{'basedir'} = $dir || "./";
  122. $self->{'filename'} = $fn;
  123. # Check if it contains a signature
  124. open(DSC, "<", $filename) || syserr(_g("cannot open %s"), $filename);
  125. $self->{'is_signed'} = 0;
  126. while (<DSC>) {
  127. next if /^\s*$/o;
  128. $self->{'is_signed'} = 1 if /^-----BEGIN PGP SIGNED MESSAGE-----$/o;
  129. last;
  130. }
  131. close(DSC);
  132. # Read the fields
  133. my $fields = Dpkg::Control->new(type => CTRL_PKG_SRC);
  134. $fields->load($filename);
  135. $self->{'fields'} = $fields;
  136. foreach my $f (qw(Source Format Version Files)) {
  137. unless (defined($fields->{$f})) {
  138. error(_g("missing critical source control field %s"), $f);
  139. }
  140. }
  141. $self->{'checksums'}->add_from_control($fields, use_files_for_md5 => 1);
  142. $self->upgrade_object_type(0);
  143. }
  144. sub upgrade_object_type {
  145. my ($self, $update_format) = @_;
  146. $update_format = 1 unless defined $update_format;
  147. my $format = $self->{'fields'}{'Format'};
  148. if ($format =~ /^([\d\.]+)(?:\s+\((.*)\))?$/) {
  149. my ($version, $variant, $major, $minor) = ($1, $2, $1, undef);
  150. $major =~ s/\.[\d\.]+$//;
  151. my $module = "Dpkg::Source::Package::V$major";
  152. $module .= "::$variant" if defined $variant;
  153. eval "require $module; \$minor = \$${module}::CURRENT_MINOR_VERSION;";
  154. $minor = 0 unless defined $minor;
  155. if ($update_format) {
  156. $self->{'fields'}{'Format'} = "$major.$minor";
  157. $self->{'fields'}{'Format'} .= " ($variant)" if defined $variant;
  158. }
  159. if ($@) {
  160. error(_g("source package format `%s' is not supported (Perl module %s is required)"), $format, $module);
  161. }
  162. bless $self, $module;
  163. } else {
  164. error(_g("invalid Format field `%s'"), $format);
  165. }
  166. }
  167. sub get_filename {
  168. my ($self) = @_;
  169. return $self->{'basedir'} . $self->{'filename'};
  170. }
  171. sub get_files {
  172. my ($self) = @_;
  173. return $self->{'checksums'}->get_files();
  174. }
  175. sub check_checksums {
  176. my ($self) = @_;
  177. my $checksums = $self->{'checksums'};
  178. # add_from_file verify the checksums if they are already existing
  179. foreach my $file ($checksums->get_files()) {
  180. $checksums->add_from_file($self->{'basedir'} . $file, key => $file);
  181. }
  182. }
  183. sub get_basename {
  184. my ($self, $with_revision) = @_;
  185. my $f = $self->{'fields'};
  186. unless (exists $f->{'Source'} and exists $f->{'Version'}) {
  187. error(_g("source and version are required to compute the source basename"));
  188. }
  189. my $v = Dpkg::Version->new($f->{'Version'});
  190. my $basename = $f->{'Source'} . "_" . $v->version();
  191. if ($with_revision and $f->{'Version'} =~ /-/) {
  192. $basename .= "-" . $v->revision();
  193. }
  194. return $basename;
  195. }
  196. sub find_original_tarballs {
  197. my ($self, %opts) = @_;
  198. $opts{extension} = $compression_re_file_ext unless exists $opts{extension};
  199. $opts{include_main} = 1 unless exists $opts{include_main};
  200. $opts{include_supplementary} = 1 unless exists $opts{include_supplementary};
  201. my $basename = $self->get_basename();
  202. my @tar;
  203. foreach my $dir (".", $self->{'basedir'}, $self->{'options'}{'origtardir'}) {
  204. next unless defined($dir) and -d $dir;
  205. opendir(DIR, $dir) || syserr(_g("cannot opendir %s"), $dir);
  206. push @tar, map { "$dir/$_" } grep {
  207. ($opts{include_main} and
  208. /^\Q$basename\E\.orig\.tar\.$opts{extension}$/) or
  209. ($opts{include_supplementary} and
  210. /^\Q$basename\E\.orig-[\w-]+\.tar\.$opts{extension}$/)
  211. } readdir(DIR);
  212. closedir(DIR);
  213. }
  214. return @tar;
  215. }
  216. sub is_signed {
  217. my $self = shift;
  218. return $self->{'is_signed'};
  219. }
  220. sub check_signature {
  221. my ($self) = @_;
  222. my $dsc = $self->get_filename();
  223. my @exec;
  224. if (-x '/usr/bin/gpgv') {
  225. push @exec, "gpgv";
  226. } elsif (-x '/usr/bin/gpg') {
  227. push @exec, "gpg", "--no-default-keyring", "-q", "--verify";
  228. }
  229. if (scalar(@exec)) {
  230. if (-r "$ENV{'HOME'}/.gnupg/trustedkeys.gpg") {
  231. push @exec, "--keyring", "$ENV{'HOME'}/.gnupg/trustedkeys.gpg";
  232. }
  233. foreach my $vendor_keyring (run_vendor_hook('keyrings')) {
  234. if (-r $vendor_keyring) {
  235. push @exec, "--keyring", $vendor_keyring;
  236. }
  237. }
  238. push @exec, $dsc;
  239. my ($stdout, $stderr);
  240. spawn('exec' => \@exec, wait_child => 1, nocheck => 1,
  241. to_string => \$stdout, error_to_string => \$stderr,
  242. timeout => 10);
  243. if (WIFEXITED($?)) {
  244. my $gpg_status = WEXITSTATUS($?);
  245. print STDERR "$stdout$stderr" if $gpg_status;
  246. if ($gpg_status == 1 or ($gpg_status &&
  247. $self->{'options'}{'require_valid_signature'}))
  248. {
  249. error(_g("failed to verify signature on %s"), $dsc);
  250. } elsif ($gpg_status) {
  251. warning(_g("failed to verify signature on %s"), $dsc);
  252. }
  253. } else {
  254. subprocerr("@exec");
  255. }
  256. } else {
  257. if ($self->{'options'}{'require_valid_signature'}) {
  258. error(_g("could not verify signature on %s since gpg isn't installed"), $dsc);
  259. } else {
  260. warning(_g("could not verify signature on %s since gpg isn't installed"), $dsc);
  261. }
  262. }
  263. }
  264. sub parse_cmdline_options {
  265. my ($self, @opts) = @_;
  266. foreach (@opts) {
  267. if (not $self->parse_cmdline_option($_)) {
  268. warning(_g("%s is not a valid option for %s"), $_, ref($self));
  269. }
  270. }
  271. }
  272. sub parse_cmdline_option {
  273. return 0;
  274. }
  275. sub extract {
  276. my $self = shift;
  277. my $newdirectory = $_[0];
  278. my ($ok, $error) = version_check($self->{'fields'}{'Version'});
  279. error($error) unless $ok;
  280. # Copy orig tarballs
  281. if ($self->{'options'}{'copy_orig_tarballs'}) {
  282. my $basename = $self->get_basename();
  283. my ($dirname, $destdir) = fileparse($newdirectory);
  284. $destdir ||= "./";
  285. my $ext = $compression_re_file_ext;
  286. foreach my $orig (grep { /^\Q$basename\E\.orig(-\w+)?\.tar\.$ext$/ }
  287. $self->get_files())
  288. {
  289. my $src = File::Spec->catfile($self->{'basedir'}, $orig);
  290. my $dst = File::Spec->catfile($destdir, $orig);
  291. if (not check_files_are_the_same($src, $dst, 1)) {
  292. system('cp', '--', $src, $dst);
  293. subprocerr("cp $src to $dst") if $?;
  294. }
  295. }
  296. }
  297. # Try extract
  298. eval { $self->do_extract(@_) };
  299. if ($@) {
  300. &$_() foreach reverse @Dpkg::Exit::handlers;
  301. die $@;
  302. }
  303. # Store format if non-standard so that next build keeps the same format
  304. if ($self->{'fields'}{'Format'} ne "1.0" and
  305. not $self->{'options'}{'skip_debianization'})
  306. {
  307. my $srcdir = File::Spec->catdir($newdirectory, "debian", "source");
  308. my $format_file = File::Spec->catfile($srcdir, "format");
  309. mkdir($srcdir) unless -e $srcdir;
  310. open(FORMAT, ">", $format_file) || syserr(_g("cannot write %s"), $format_file);
  311. print FORMAT $self->{'fields'}{'Format'} . "\n";
  312. close(FORMAT);
  313. }
  314. # Make sure debian/rules is executable
  315. my $rules = File::Spec->catfile($newdirectory, "debian", "rules");
  316. my @s = lstat($rules);
  317. if (not scalar(@s)) {
  318. unless ($! == ENOENT) {
  319. syserr(_g("cannot stat %s"), $rules);
  320. }
  321. warning(_g("%s does not exist"), $rules)
  322. unless $self->{'options'}{'skip_debianization'};
  323. } elsif (-f _) {
  324. chmod($s[2] | 0111, $rules) ||
  325. syserr(_g("cannot make %s executable"), $rules);
  326. } else {
  327. warning(_g("%s is not a plain file"), $rules);
  328. }
  329. }
  330. sub do_extract {
  331. internerr("Dpkg::Source::Package doesn't know how to unpack a " .
  332. "source package. Use one of the subclasses.");
  333. }
  334. # Function used specifically during creation of a source package
  335. sub build {
  336. my $self = shift;
  337. eval { $self->do_build(@_) };
  338. if ($@) {
  339. &$_() foreach reverse @Dpkg::Exit::handlers;
  340. die $@;
  341. }
  342. }
  343. sub do_build {
  344. internerr("Dpkg::Source::Package doesn't know how to build a " .
  345. "source package. Use one of the subclasses.");
  346. }
  347. sub can_build {
  348. my ($self, $dir) = @_;
  349. return (0, "can_build() has not been overriden");
  350. }
  351. sub add_file {
  352. my ($self, $filename) = @_;
  353. my ($fn, $dir) = fileparse($filename);
  354. if ($self->{'checksums'}->has_file($fn)) {
  355. internerr("tried to add file '%s' twice", $fn);
  356. }
  357. $self->{'checksums'}->add_from_file($filename, key => $fn);
  358. $self->{'checksums'}->export_to_control($self->{'fields'},
  359. use_files_for_md5 => 1);
  360. }
  361. sub write_dsc {
  362. my ($self, %opts) = @_;
  363. my $fields = $self->{'fields'};
  364. foreach my $f (keys %{$opts{'override'}}) {
  365. $fields->{$f} = $opts{'override'}{$f};
  366. }
  367. unless($opts{'nocheck'}) {
  368. foreach my $f (qw(Source Version)) {
  369. unless (defined($fields->{$f})) {
  370. error(_g("missing information for critical output field %s"), $f);
  371. }
  372. }
  373. foreach my $f (qw(Maintainer Architecture Standards-Version)) {
  374. unless (defined($fields->{$f})) {
  375. warning(_g("missing information for output field %s"), $f);
  376. }
  377. }
  378. }
  379. foreach my $f (keys %{$opts{'remove'}}) {
  380. delete $fields->{$f};
  381. }
  382. my $filename = $opts{'filename'};
  383. unless (defined $filename) {
  384. $filename = $self->get_basename(1) . ".dsc";
  385. }
  386. open(DSC, ">", $filename) || syserr(_g("cannot write %s"), $filename);
  387. $fields->apply_substvars($opts{'substvars'});
  388. $fields->output(\*DSC);
  389. close(DSC);
  390. }
  391. # vim: set et sw=4 ts=8
  392. 1;