Source code for libka.ka_wrap_and_sort

#!/usr/bin/python3
# kate: space-indent on; indent-width 4; replace-tabs on; indent-mode python; remove-trailing-space modified;
# vim: expandtab ts=4

############################################################################
#   Copyright © 2015-2016 José Manuel Santamaría Lema <panfaust@gmail.com> #
#                                                                          #
#   This program is free software; you can redistribute it and/or modify   #
#   it under the terms of the GNU General Public License as published by   #
#   the Free Software Foundation; either version 2 of the License, or      #
#   (at your option) any later version.                                    #
############################################################################

# module to sort the fields of a control file, wrap-and-sort wrapper

import subprocess
from libka.control_edit import *

# Finds field in field order ignoring case
# (for KDE Frameworks some packages have a "Multi-arch: same" and that's apparently legit)
[docs] def field_order_index(field,field_order): i = 0 for f in field_order: if field.lower() == f.lower(): return i i += 1 return i
# Sort paragraph @p using @field_order which is a list with fields names ordered by the # intended order
[docs] def sort_paragraph_fields(p,field_order): processed_p = p.copy() sorted_fields = sorted(p,key=lambda x:field_order_index(x,field_order)) for field in sorted_fields: del processed_p[field] processed_p[field] = p[field] return processed_p
# Function to sort the fields from a source debian/control paragraph
[docs] def sort_source_fields(src_pkg): #This list gives the order than the paragraph fields should follow field_order_source = ["Source", "Section","Priority", "Maintainer","XSBC-Original-Maintainer","Uploaders", "Build-Depends", "Build-Depends-Indep", "Build-Conflicts", "Build-Conflicts-Indep", "Built-Using", "Standards-Version", "Homepage", "Vcs-Browser", "Vcs-Arch", "Vcs-Bzr", "Vcs-Cvs", "Vcs-Darcs", "Vcs-Git", "Vcs-Hg", "Vcs-Mtn", "Vcs-Svn" ] return sort_paragraph_fields(src_pkg,field_order_source)
#Function to sort the fields from a binary package debian/control paragraph
[docs] def sort_binary_package_fields(bin_pkg): #This list gives the order than the paragraph fields should follow field_order_bin = ["Package", "X-Debian-ABI","X-CMake-Target", "Architecture", "Multi-Arch", "Section", "Priority", "Essential", "Depends", "Recommends", "Suggests", "Enhances", "Pre-Depends", "Breaks", "Conflicts", "Provides", "Replaces", "Description", "Homepage", "Built-Using", "Package-Type"] return sort_paragraph_fields(bin_pkg,field_order_bin)
# Function to sort all the debian/control fields
[docs] def sort_control_fields(src_pkg, bin_pkg_list, bin_pkg_map): src_pkg = sort_source_fields(src_pkg) for key in bin_pkg_map: bin_pkg_map[key] = sort_binary_package_fields(bin_pkg_map[key]) return src_pkg, bin_pkg_list, bin_pkg_map
[docs] def ka_wrap_and_sort(): #Sanitize the control file sanitize_control_file('./debian/control') #Run wrap-and-sort subprocess.check_call(["wrap-and-sort","-bt"]) #Open the control file src_pkg, bin_pkg_list, bin_pkg_map = parse_control('./debian/control'); #Sort the control fields src_pkg, bin_pkg_list, bin_pkg_map = sort_control_fields(src_pkg, bin_pkg_list, bin_pkg_map) #Dump the contents of the control file dump_control('./debian/control',src_pkg, bin_pkg_list, bin_pkg_map)