# Copyright Kevin Deldycke <kevin@deldycke.com> and contributors.## 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.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."""Introspection utilities to produce comparison matrixes between managers."""from__future__importannotationsfrompathlibimportPathfromclick_extra.docs_updateimportreplace_contentfromtabulateimporttabulatefrom.baseimportOperationsfrom.platformsimportPLATFORM_GROUPSfrom.poolimportpool
[docs]defoperation_matrix()->str:"""Inspect manager and print a matrix of their current implementation."""# Build up the column titles.headers=["Package manager","Min. version",]# Footnotes are used to details the OSes covered by each platform group.footnotes=[]forgroupinPLATFORM_GROUPS:header_title=group.name# Add footnote for groups with more than one platform.iflen(group.platforms)>1:footnote_tag=f"[^{group.id}]"header_title+=footnote_tagplatforms_string=", ".join(sorted((p.nameforpingroup.platforms),key=str.casefold),)footnotes.append(f"{footnote_tag}: {group.name}: {platforms_string}.")headers.append(header_title)headers.extend(f"`{op.name}`"foropinOperations)table=[]formid,minsorted(pool.items()):line=[f"[`{mid}`]({m.homepage_url})"+(""ifnotm.deprecatedelsef" [β οΈ]({m.deprecation_url})"),f"{m.requirement}",]forgroupinPLATFORM_GROUPS:line.append(group.iconifgroup.issubset(m.platforms)andgroup.iconelse"",)foropinOperations:line.append("β"ifm.implements(op)else"")table.append(line)# Set each column alignment.alignments=["left","left"]alignments.extend(["center"]*len(PLATFORM_GROUPS))alignments.extend(["center"]*len(Operations))rendered_table=tabulate(table,headers=headers,tablefmt="github",colalign=alignments,disable_numparse=True,)# Manually produce Markdown alignment hints. This has been proposed upstream at:# https://github.com/astanin/python-tabulate/pull/261# https://github.com/astanin/python-tabulate/issues/53separators=[]forcol_index,headerinenumerate(headers):cells=[line[col_index]forlineintable]+[header]max_len=max(len(c)forcincells)align=alignments[col_index]ifalign=="center":sep=f":{'-'*(max_len-2)}:"elifalign=="right":sep=f"{'-'*(max_len-1)}:"else:sep="-"*max_lenseparators.append(sep)header_separator=f"| {' | '.join(separators)} |"lines=rendered_table.splitlines()lines[1]=header_separatoroutput="\n".join(lines)iffootnotes:output+="\n\n"output+="\n\n".join(footnotes)returnoutput
[docs]defupdate_readme()->None:"""Update `readme.md` at the root of the project with the implementation table for each manager we support."""replace_content(Path(__file__).parent.parent.joinpath("readme.md"),"<!-- operation-matrix-start -->\n\n","\n\n<!-- operation-matrix-end -->",operation_matrix(),)