File indexing completed on 2024-04-14 05:02:33

0001 # frozen_string_literal: true
0002 #
0003 # Copyright (C) 2016 Harald Sitter <sitter@kde.org>
0004 #
0005 # This library is free software; you can redistribute it and/or
0006 # modify it under the terms of the GNU Lesser General Public
0007 # License as published by the Free Software Foundation; either
0008 # version 2.1 of the License, or (at your option) version 3, or any
0009 # later version accepted by the membership of KDE e.V. (or its
0010 # successor approved by the membership of KDE e.V.), which shall
0011 # act as a proxy defined in Section 6 of version 3 of the license.
0012 #
0013 # This library is distributed in the hope that it will be useful,
0014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0016 # Lesser General Public License for more details.
0017 #
0018 # You should have received a copy of the GNU Lesser General Public
0019 # License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0020 
0021 require_relative 'deb822'
0022 
0023 module Debian
0024   # debian/control parser
0025   class Control < Deb822
0026     attr_reader :source
0027     attr_reader :binaries
0028 
0029     # FIXME: deprecate invocation without path
0030     def initialize(directory = Dir.pwd)
0031       @source = nil
0032       @binaries = nil
0033       @directory = directory
0034     end
0035 
0036     def parse!
0037       lines = File.new("#{@directory}/debian/control").readlines
0038 
0039       # Source Paragraph
0040       @source = parse_paragraph(lines, source_fields)
0041 
0042       # Binary Paragraphs
0043       @binaries = []
0044       until lines.empty?
0045         data = parse_paragraph(lines, binary_fields)
0046         @binaries << data if data
0047       end
0048 
0049       # TODO: Strip custom fields and add a Control::flags_for(entry) method.
0050     end
0051 
0052     def dump
0053       output = ''
0054 
0055       # Source Paragraph
0056       output += dump_paragraph(@source, source_fields)
0057       return output unless @binaries
0058 
0059       # Binary Paragraphs
0060       output += "\n"
0061       @binaries.each do |b|
0062         output += dump_paragraph(b, binary_fields)
0063       end
0064 
0065       output + "\n"
0066     end
0067 
0068     private
0069 
0070     def source_fields
0071       @source_fields ||= {}.tap do |fields|
0072         fields[:mandatory] = %w(source maintainer)
0073         fields[:relationship] = %w(
0074           build-depends
0075           build-depends-indep
0076           build-conflicts
0077           build-conflicts-indep
0078         )
0079         fields[:foldable] = ['uploaders'] + fields[:relationship]
0080       end
0081     end
0082 
0083     def binary_fields
0084       @binary_fields ||= {}.tap do |fields|
0085         fields[:mandatory] = %w(
0086           package
0087           architecture
0088           description
0089         )
0090         fields[:multiline] = ['description']
0091         fields[:relationship] = %w(
0092           depends
0093           recommends
0094           suggests
0095           enhances
0096           pre-depends
0097           breaks
0098           replaces
0099           conflicts
0100           provides
0101         )
0102         fields[:foldable] = fields[:relationship]
0103       end
0104     end
0105   end
0106 end
0107 
0108 # FIXME: deprecate
0109 class DebianControl < Debian::Control; end