Package.pm 13 KB

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