PKyoM +''env_replace.py#!/usr/bin/env python3 """ env-replace: un-expand environment variables for more compact output Usage: some-big-output-producing-command | env-replace """ from collections import OrderedDict import os import sys __version__ = '1.0.0' def _sort_by_length(key_value): """Sort key, value pairs to prioritize shortest substitution - prioritize long strings first to ensure the longest match is substituted - prioritize short variable names in case two keys have the same value """ key, value = key_value return (-len(value), len(key)) def main(): # create ordered dict, sorted by length replacements = OrderedDict() assignments = {} for key, value in sorted(os.environ.items(), key=_sort_by_length): if len(value) > len(key) and value not in replacements: # only include substitutions that would shorten output # and only include the *shortest* environment variable # in case of multiple variables with the same value envvar = '$' + key replacements[value] = envvar assignments[envvar] = key + '=' for line in sys.stdin: # perform replacements per line save_line = line for to_replace, envvar in replacements.items(): if line.startswith(assignments[envvar]): # avoid rewriting assignments, e.g. # the output of `env` itself continue line = line.replace(to_replace, envvar) sys.stdout.write(line) if __name__ == '__main__': main() PK!H+0,env_replace-1.0.0.dist-info/entry_points.txtN+I/N.,()J+-J-ILNl<..PKsoM''#env_replace-1.0.0.dist-info/LICENSEMIT License Copyright (c) 2018 Min RK Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!H>*RQ!env_replace-1.0.0.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,rzd&Y)r$[)T&UrPK!HN $env_replace-1.0.0.dist-info/METADATASMo@pX%7KET-I-p#k{b//#MxƱ 7Λy[0ZDҚel.4fݣSB|eg.i-nd8n0uw-5MR m=BeUK} hctF6s-Le&a~3:g)07ÑRP!C>4M7-g_K_5K(&z~sDqsrX'îA%夃Xe% (7anyV:&/壖70Iƌ:tkJ"@lE"iS6aT65-W- lj'eo08PjG[p"(2IUDo$:<2 FVR1p N:&(c{DHˏ&.FyH W4;#UkaTU'{p.MR/7Je&d_-vmb@^0? ZYGPK!H?*"env_replace-1.0.0.dist-info/RECORD}λr@>šE U* `p (]4}lt3妟R(}t~fI7ۛy; ɖ^&N'SVŏW`Bnl8'}[J6.'U~; #Θǯ6gFPV/-2R0wm f1b-^5埜aįڡqHP M3|Moi-z8wOQUFWZɄ@cySUAPƇ:4 a0ziEg?PKyoM +''env_replace.pyPK!H+0,Senv_replace-1.0.0.dist-info/entry_points.txtPKsoM''#env_replace-1.0.0.dist-info/LICENSEPK!H>*RQ!0 env_replace-1.0.0.dist-info/WHEELPK!HN $ env_replace-1.0.0.dist-info/METADATAPK!H?*"env_replace-1.0.0.dist-info/RECORDPK