Class: Xezat::Command::Validate

Inherits:
Object
  • Object
show all
Includes:
Xezat
Defined in:
lib/xezat/command/validate.rb,
lib/xezat/command/validate/config.rb,
lib/xezat/command/validate/license.rb,
lib/xezat/command/validate/pkgconfig.rb

Constant Summary

Constants included from Xezat

Xezat::CONFIG_FILE, DATA_DIR, REPOSITORY_DIR, ROOT_DIR, TEMPLATE_DIR, VERSION

Instance Method Summary collapse

Methods included from Xezat

#config, #packages, #print_yaml, #variables

Constructor Details

#initialize(options, cygport) ⇒ Validate

Returns a new instance of Validate.



17
18
19
20
# File 'lib/xezat/command/validate.rb', line 17

def initialize(options, cygport)
  @options = options
  @cygport = cygport
end

Instance Method Details

#executeObject



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
# File 'lib/xezat/command/validate.rb', line 22

def execute
  Xezat.logger.debug('Start validating')
  vars = variables(@cygport)
  pkgs = packages

  gcc_version = Cygversion.new(pkgs[:'gcc-core'].gsub(/^gcc-core-/, '')).version.split('.')[0]

  Xezat.logger.debug('  Validate .cygport')
  validate_cygport(@cygport)

  Xezat.logger.debug('  Validate category')
  validate_category(vars[:CATEGORY])

  Xezat.logger.debug('  Validate homepage')
  validate_homepage(vars[:HOMEPAGE])

  Xezat.logger.debug('  Validate licenses')
  validate_license(vars)

  Xezat.logger.debug('  Validate BUILD_REQUIRES')
  validate_build_requires(vars[:BUILD_REQUIRES], pkgs)

  Xezat.logger.debug('  Validate *.pc')
  validate_pkgconfig(vars, gcc_version)

  Xezat.logger.debug('  Validate *-config')
  validate_config(vars, gcc_version)

  Xezat.logger.debug('End validating')
end

#validate_build_requires(build_requires, pkgs) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/xezat/command/validate.rb', line 78

def validate_build_requires(build_requires, pkgs)
  return if build_requires.nil?

  build_requires.split.each do |build_require|
    build_require_pkg = pkgs[build_require.to_sym]
    if build_require_pkg.nil?
      Xezat.logger.error("    #{build_require} not found")
    else
      Xezat.logger.debug("    #{build_require_pkg}")
    end
  end
end

#validate_category(category) ⇒ Object



59
60
61
62
# File 'lib/xezat/command/validate.rb', line 59

def validate_category(category)
  categories_file = File.expand_path(File.join(DATA_DIR, 'categories.yaml'))
  Xezat.logger.error("    Category is invalid : #{category}") unless YAML.safe_load(File.open(categories_file), [Symbol]).include?(category.downcase)
end

#validate_config(variables, gcc_version) ⇒ Object



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
# File 'lib/xezat/command/validate/config.rb', line 9

def validate_config(variables, gcc_version)
  configs = Dir.glob(File.join(variables[:D], '/usr/bin/*-config'))
  configs.each do |config|
    basename = File.basename(config)
    Xezat.logger.debug("    #{basename} found")

    result, _, status = Open3.capture3("#{config} --cflags")
    if status.success?
      Xezat.logger.debug("      cflags = #{result.strip}")
    else
      Xezat.logger.warn('       cflags not supported')
    end

    result, _, status = Open3.capture3("#{config} --cxxflags")
    if status.success?
      Xezat.logger.debug("      cxxflags = #{result.strip}")
    else
      Xezat.logger.warn('       cxxflags not supported')
    end

    result, _, status = Open3.capture3("#{config} --libs")
    if status.success?
      Xezat.logger.debug("      libs = #{result.strip}")
      validate_libs(variables, result.strip, gcc_version)
    else
      Xezat.logger.warn('       libs not supported')
    end
  end
end

#validate_cygport(cygport) ⇒ Object



53
54
55
56
57
# File 'lib/xezat/command/validate.rb', line 53

def validate_cygport(cygport)
  original_string = File.read(cygport)
  stripped_string = original_string.gsub(/^\xEF\xBB\xBF/, '')
  Xezat.logger.error('    .cygport contains BOM') unless original_string == stripped_string
end

#validate_homepage(homepage) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/xezat/command/validate.rb', line 64

def validate_homepage(homepage)
  response = Net::HTTP.get_response(URI.parse(homepage))
  code = response.code
  if code == '200'
    Xezat.logger.debug("    code = #{code}")
  else
    Xezat.logger.error("    code = #{code}")
  end
rescue OpenSSL::SSL::SSLError => e
  raise e unless @options[:ignore]

  Xezat.logger.error('    Ignore SSLError')
end

#validate_libs(variables, libs, gcc_version) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/xezat/command/validate.rb', line 91

def validate_libs(variables, libs, gcc_version)
  lib_dirs = [File.join(variables[:D], '/usr/lib'), '/usr/lib', '/usr/lib/w32api', "/usr/lib/gcc/x86_64-pc-cygwin/#{gcc_version}"]
  libs.split do |option|
    if option.start_with?('-l')
      lib_name = option[2, 255] # Assume file length limit
      found = false
      lib_dirs.each do |dir|
        archive_path = File.join(dir, "lib#{lib_name}.dll.a")
        if File.exist?(archive_path)
          Xezat.logger.debug("        #{lib_name} -> #{archive_path.gsub(variables[:D], '$D')}")
          found = true
          break
        end
        static_path = File.join(dir, "lib#{lib_name}.a")
        next unless File.exist?(static_path)

        Xezat.logger.debug("        #{lib_name} -> #{static_path}")
        found = true
        break
      end
      Xezat.logger.error("        #{lib_name} not found") unless found
    end
  end
end

#validate_license(vars) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/xezat/command/validate/license.rb', line 9

def validate_license(vars)
  license = vars[:LICENSE]
  if license.nil? || license.empty?
    Xezat.logger.warn('     LICENSE is not defined')
  elsif Spdx.valid?(license)
    Xezat.logger.debug("    LICENSE = #{license}")
  else
    Xezat.logger.error("    LICENSE = #{license} (invalid)")
  end

  license_uri = vars[:LICENSE_URI]
  if license_uri.nil? || license_uri.empty?
    Xezat.logger.warn('     LICENSE_URI is not defined')
  elsif File.exist?(File.join(vars[:S], license_uri))
    Xezat.logger.debug("    LICENSE_URI = #{license_uri}")
  else
    Xezat.logger.error("    LICENSE_URI = #{license_uri} (not found)")
  end
end

#validate_pkgconfig(variables, gcc_version) ⇒ Object



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
# File 'lib/xezat/command/validate/pkgconfig.rb', line 9

def validate_pkgconfig(variables, gcc_version)
  pkgconfig_path = File.join(variables[:D], 'usr', 'lib', 'pkgconfig')
  PKGConfig.add_path(pkgconfig_path)
  Dir.glob('*.pc', 0, base: pkgconfig_path).each do |pc|
    Xezat.logger.debug("    #{pc} found")
    basename = File.basename(pc, '.pc')

    modversion = PKGConfig.modversion(basename)
    Xezat.logger.debug("      modversion = #{modversion}")
    pv = variables[:PV][0].gsub(/\+.+$/, '')
    Xezat.logger.error("        modversion differs from $PN = #{pv}") unless modversion == pv

    prefix = PKGConfig.variable(basename, 'prefix')
    if prefix.nil? || prefix.empty? || prefix.eql?('/usr')
      Xezat.logger.debug("      prefix = #{prefix}")
    else
      Xezat.logger.warn("       prefix = #{prefix} (not standard)")
    end

    Xezat.logger.debug("      cflags = #{PKGConfig.cflags(basename)}")

    libs = PKGConfig.libs(basename)
    Xezat.logger.debug("      libs = #{libs}")
    validate_libs(variables, libs, gcc_version)
  end
end