Class: FPM::Package::FreeBSD

Inherits:
FPM::Package show all
Defined in:
lib/fpm/package/freebsd.rb

Constant Summary collapse

SCRIPT_MAP =
{
  :before_install     => "pre-install",
  :after_install      => "post-install",
  :before_remove      => "pre-deinstall",
  :after_remove       => "post-deinstall",
}

Instance Attribute Summary

Attributes inherited from FPM::Package

#attributes, #attrs, #category, #config_files, #conflicts, #dependencies, #description, #directories, #epoch, #iteration, #license, #maintainer, #name, #provides, #replaces, #scripts, #url, #vendor, #version

Instance Method Summary collapse

Methods inherited from FPM::Package

apply_options, #build_path, #cleanup, #cleanup_build, #cleanup_staging, #convert, #converted_from, default_attributes, #edit_file, #files, inherited, #initialize, #input, option, #script, #staging_path, #type, type, types

Methods included from Util

#ar_cmd, #ar_cmd_deterministic?, #copied_entries, #copy_entry, #copy_metadata, #default_shell, #erbnew, #execmd, #expand_pessimistic_constraints, #logger, #program_exists?, #program_in_path?, #safesystem, #safesystemout, #tar_cmd, #tar_cmd_supports_sort_names_and_set_mtime?

Constructor Details

This class inherits a constructor from FPM::Package

Instance Method Details

#add_path(tar, tar_path, path) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/fpm/package/freebsd.rb', line 117

def add_path(tar, tar_path, path)
  stat = File.lstat(path)
  if stat.directory?
    tar.mkdir(tar_path, stat.mode)
  elsif stat.symlink?
    tar.add_symlink(tar_path, File.readlink(path), stat.mode)
  else
    tar.add_file_simple(tar_path, stat.mode, stat.size) do |io|
      File.open(path) do |fd|
        chunk = nil
        size = 0
        while chunk = fd.read(16384) do
          size += io.write(chunk)
        end
        if size != stat.size
          raise "Failed to add #{path} to the archive; expected to " +
                "write #{stat.size} bytes, only wrote #{size}"
        end
      end
    end # tar.tar.add_file_simple
  end
end

#architectureObject

Handle architecture naming conversion: <osname>:<osversion>:<arch>:<wordsize>



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/fpm/package/freebsd.rb', line 94

def architecture
  osname    = %x{uname -s}.chomp
  osversion = %x{uname -r}.chomp.split('.').first

  # Essentially because no testing on other platforms
  arch = 'x86'

  wordsize = case @architecture
  when nil, 'native'
    %x{getconf LONG_BIT}.chomp # 'native' is current arch
  when 'arm64'
    '64'
  when 'amd64'
    '64'
  when 'i386'
    '32'
  else
    %x{getconf LONG_BIT}.chomp # default to native, the current arch
  end

  return [osname, osversion, arch, wordsize].join(':')
end

#output(output_path) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fpm/package/freebsd.rb', line 19

def output(output_path)
  output_check(output_path)

  # Build the packaging metadata files.
  checksums = {}
  self.files.each do |f|
    path = staging_path(f)
    if File.symlink?(path)
      checksums[f] = "-"
    elsif File.file?(path)
      checksums[f] = Digest::SHA256.file(path).hexdigest
    end
  end

  pkg_origin = attributes[:freebsd_origin]
  if pkg_origin == "fpm/<name>"  # fill in default
    pkg_origin = "fpm/#{name}"
  end

  # Follow similar rules to these used in ``to_s_fullversion`` method.
  # FIXME: maybe epoch should also be introduced somehow ("#{version},#{epoch})?
  #        should it go to pkgdata["version"] or to another place?
  # https://www.freebsd.org/doc/en/books/porters-handbook/makefile-naming.html
  pkg_version = (iteration and (iteration.to_i > 0)) ?  "#{version}-#{iteration}" : "#{version}"

  pkgdata = {
    "arch" => architecture,
    "name" => name,
    "version" => pkg_version,
    "comment" => description,
    "desc" => description,
    "origin" => pkg_origin,
    "maintainer" => maintainer,
    "www" => url,
    # prefix is required, but it doesn't seem to matter
    "prefix" => "/",
  }

  # Write +COMPACT_MANIFEST, without the "files" section.
  File.open(staging_path("+COMPACT_MANIFEST"), "w+") do |file|
    file.write(pkgdata.to_json + "\n")
  end

  # Populate files + checksums, then write +MANIFEST.
  pkgdata["files"] = {}
  checksums.each do |f, shasum|
    # pkg expands % URL-style escapes, so make sure to escape % as %25
    pkgdata["files"]["/" + f.gsub("%", "%25")] = shasum
  end

  # Populate scripts
  pkgdata["scripts"] = {}
  scripts.each do |name, data|
    pkgdata["scripts"][SCRIPT_MAP[name]] = data
  end

  File.open(staging_path("+MANIFEST"), "w+") do |file|
    file.write(pkgdata.to_json + "\n")
  end

  file_list = File.new(build_path("file_list"), "w")
  files.each do |i|
    file_list.puts(i)
  end
  file_list.close

  # Create the .txz package archive from the files in staging_path.
  # We use --files-from here to keep the tar entries from having `./` as the prefix.
  # This is done as a best effor to mimic what FreeBSD packages do, having everything at the top-level as
  # file names, like "+MANIFEST" instead of "./+MANIFEST"
  safesystem("tar", "-Jcf", output_path, "-C", staging_path, "--files-from", build_path("file_list"), "--transform", 's|^\([^+]\)|/\1|')
end

#to_s(format = nil) ⇒ Object



148
149
150
# File 'lib/fpm/package/freebsd.rb', line 148

def to_s(format=nil)
  return super(format.nil? ? "NAME-FULLVERSION.EXTENSION" : format)
end

#to_s_extensionObject

def add_path



140
# File 'lib/fpm/package/freebsd.rb', line 140

def to_s_extension; "txz"; end

#to_s_fullversionObject



142
143
144
145
146
# File 'lib/fpm/package/freebsd.rb', line 142

def to_s_fullversion()
  # iteration (PORTREVISION on FreeBSD) shall be appended only(?) if non-zero.
  # https://www.freebsd.org/doc/en/books/porters-handbook/makefile-naming.html
  (iteration and (iteration.to_i > 0)) ?  "#{version}_#{iteration}" : "#{version}"
end