Advertisement
s243a

build_cannonical (3)

Dec 14th, 2019
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.74 KB | None | 0 0
  1. #!/bin/ash
  2.  
  3.  
  4. [ -z "$alias_file" ] && alias_file="$(realpath ~/cat_test_alias.in)"
  5.  
  6. PKG_FIX_CATEGORIES_in_REPO_DB=true
  7. PKG_use_ALIASES_in_DB_CAT_Lookup=true
  8.  
  9. CATEGORIES_INIT_BLOCK='FILENAME==CATFILE {parse_cats($0)
  10.                     next} #https://www3.physnet.uni-hamburg.de/physnet/Tru64-Unix/HTML/APS32DTE/WKXXXXXX.HTM'
  11. [ -z "$PKG_FIX_ALIASES" ] && PKG_FIX_ALIASES=true    
  12.  
  13. function cat_macro_fn(){
  14.     if [ "$PKG_FIX_ALIASES" = true ]; then
  15.       echo "$1"
  16.     else
  17.       echo ""
  18.     fi
  19. }
  20. function alias_macro_fn(){
  21.     if [ "$PKG_use_ALIASES_in_DB_CAT_Lookup" = true ]; then
  22.       echo "$1"
  23.     else
  24.       echo ""
  25.     fi
  26. }
  27. function comment_macro(){
  28.       echo ""
  29. }
  30.  
  31. ALIAS_FNS="$(awk -v  ALIASFILE="$alias_file" '
  32. function add_get_canonical_hdr(){
  33.  print "function get_canonical_name(s,v){                  "
  34.  print "  if (s in CANONICAL_ARY){                       "
  35.  print "   return CANONICAL_ARY[s]                       "
  36.  print "  } else {                                       "
  37.  print "    switch(s \"-\" v){                                   "
  38. }
  39. function add_get_canonical_footer(){
  40.  print "    }                                            "
  41.  print "  }                                              "
  42.  print "  return s                                       "
  43.  print "}                                                "
  44. }
  45. function add_canonical_case(s,name){
  46.  
  47.  #TODO: Do this untill there is no astrixs left to replace.
  48.  s=gensub(/^(.*[^.*])(\*)(.*)$/,"\\1.*\\3","G",s) # replace "*" with ".* "
  49.  s=gensub(/^(.*[^\\+])(\+)(.*)$/,"\\1\\\\+\\3","G",s) # replace "+" with "\+"
  50.  print "      case /" s "/:                              "
  51.  print "        return \"" name "\"                      "
  52. }
  53. #function write_CANONICAL_ARY_init_hdr(){
  54. #  print "function init_CANONICAL_ARY(){                            "
  55. #}
  56. function write__init_CANONICAL_ARY(){
  57.  print "function init_CANONICAL_ARY(){                            "
  58.  for (p in CANONICAL_ARY){
  59.    print "  CANONICAL_ARY[\"" p "\"]=\"" CANONICAL_ARY[p] "\""
  60.  }
  61.  print "}"
  62. }
  63.  
  64.  
  65. #This writes the start of the function get_canonical_name()
  66. #this function will return a single name for all equivalent alias values.
  67. BEGIN { FS = ","; add_get_canonical_hdr()}
  68.  
  69. #If wild cards are used in an alias get_canonical_name() uses regX case statments to do the match.
  70. #Otherwise get_cannonical_name() looks up the value in an associative array (AKA dictonary or hashmap)
  71. {
  72.  canonical_NAME=$1
  73.  sub(/\*/,"",canonical_NAME) #This isn'"'"'t necissary but makes the key slightly nicer looking
  74.   for(i=NF;i>=1;i--){
  75.     if(index($i,"*")>0){
  76.       add_canonical_case($i,canonical_NAME) #alias has a wild card so wirite case statement to match alias
  77.     }
  78.     else{
  79.       CANONICAL_ARY[$i]=canonical_NAME #alias has no wild card so add to lookup array)
  80.     }  
  81.   }
  82. }
  83. END {add_get_canonical_footer()
  84.      write__init_CANONICAL_ARY()
  85.     } ' "$alias_file")"
  86.    
  87. #echo "$ALIAS_FNS"  
  88.  
  89.  
  90. CATEGORY_FNS=\
  91. 'function cat_fallback(s){
  92.   if (length(s)>0){
  93.     switch(s){
  94.         case /vcs.*/:
  95.           return "Utility;development"
  96.           break
  97.         case /doc.*/:
  98.           return "Help"
  99.           break
  100.         case /admin.*/:
  101.           return "Setup"
  102.           break
  103.         case /games.*/:
  104.           return "Fun"
  105.           break
  106.         case /.{7,}/:
  107.           return "science"
  108.           break
  109.        default:
  110.           return "BuildingBlock"
  111.           break
  112.  
  113.     }
  114.   }
  115.   else
  116.   {
  117.       return "BuildingBlock"
  118.   }
  119. }
  120. function get_category(s1,v1,c1,     out,s2,sc1,sc2){
  121.   if ( s1 in pkg_categories){
  122.     return pkg_categories[s1]
  123.   } '"
  124. $(alias_macro_fn \
  125. '  sc1=get_canonical_name(s1,v1)
  126.   if (sc1 in pkg_categories_canonical){
  127.       return pkg_categories_canonical[sc1]
  128.   }
  129.   s2=gensub(/^(.+[^0-9])([0-9]*)$/,"\\1","g",s1)
  130.   if ( s2 in pkg_categories){
  131.     return pkg_categories[s2]
  132.   }
  133.   sc2=get_canonical_name(s2,v1)
  134.   if (sc2 in pkg_categories_canonical){
  135.       return pkg_categories_canonica[sc2]
  136.   }')""$(comment_macro "****End: alias_macro_fn()"****)"'  
  137.   #TODO: consider also trying to look up category based on the full name.
  138.   return cat_fallback(c1)
  139. }
  140. function parse_cats(s){
  141.   i=index(s,"=")
  142.   if (i>0){
  143.     #catagory=( package1 package2 ... packageN)  
  144.     category=substr(s,0,i-1)
  145.     sub(/PKGCAT_/,"",category) #e.g. category=PKGCAT_Utility => category=Utility
  146.     sub(/_/,";",category) #e.g. category=Filesystem_filemanager => category=Filesystem;filemanager
  147.    
  148.     #e.g catagory=( package1 package2 ... packageN) => package1 package2 ... packageN
  149.     s=gensub(/^([^=]+="\s*)([^\s].*[^\s])(\s*"\s*)$/,"\\2","g",s)
  150.    
  151.     split(s,pkg_names," ")
  152.     for (pkg_i in pkg_names){
  153.       pkg=pkg_names[pkg_i]
  154.       pkg_categories[pkg]=category
  155.       pkg_categories_canonical[pkg]=get_canonical_name(category)
  156.     }  
  157.   }
  158. }'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement