Package.pm 13 KB

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