Package.pm 13 KB

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