File: checksummer.rb

package info (click to toggle)
puppet-agent 8.10.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,404 kB
  • sloc: ruby: 286,820; sh: 492; xml: 116; makefile: 88; cs: 68
file content (62 lines) | stat: -rw-r--r-- 1,860 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
# frozen_string_literal: true

require_relative '../../../puppet/util/json'
require_relative '../../../puppet/module_tool/checksums'

module Puppet::ModuleTool
  module Applications
    class Checksummer < Application
      def initialize(path, options = {})
        @path = Pathname.new(path)
        super(options)
      end

      def run
        changes = []
        sums = Puppet::ModuleTool::Checksums.new(@path)
        checksums.each do |child_path, canonical_checksum|
          # Avoid checksumming the checksums.json file
          next if File.basename(child_path) == "checksums.json"

          path = @path + child_path
          unless path.exist? && canonical_checksum == sums.checksum(path)
            changes << child_path
          end
        end

        # Return an Array of strings representing file paths of files that have
        # been modified since this module was installed. All paths are relative
        # to the installed module directory. This return value is used by the
        # module_tool face changes action, and displayed on the console.
        #
        # Example return value:
        #
        #   [ "REVISION", "manifests/init.pp"]
        #
        changes
      end

      private

      def checksums
        if checksums_file.exist?
          Puppet::Util::Json.load(checksums_file.read)
        elsif metadata_file.exist?
          # Check metadata.json too; legacy modules store their checksums there.
          Puppet::Util::Json.load(metadata_file.read)['checksums'] or
            raise ArgumentError, _("No file containing checksums found.")
        else
          raise ArgumentError, _("No file containing checksums found.")
        end
      end

      def metadata_file
        @path + 'metadata.json'
      end

      def checksums_file
        @path + 'checksums.json'
      end
    end
  end
end