You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.3 KiB

  1. #/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. import os, sys
  4. import json
  5. # создаём разширеную базу пактов с предлизительно структурой
  6. #pkg_list ={"category": [{name: name_pkg, versions:[list_version]}]}
  7. # и клладём это всё в json
  8. #
  9. #
  10. def create_db():
  11. pkg_list ={}
  12. port_dir =["/var/lib/layman/", "/usr/portage"]
  13. if not os.path.exists('./pkgs.json'):
  14. print("Create ramdb")
  15. pkg_name =""
  16. all_pkgs = []
  17. with open('./pkgs.json', 'a') as fn:
  18. for p in port_dir:
  19. for d, dirs, files in os.walk(p):
  20. for f in files:
  21. if f.endswith('.ebuild'):
  22. #print(d +"/" + f)
  23. pkg_name =""
  24. try:
  25. #ver=int(f.replace('.ebuild', '').split('-')[-1][0])
  26. for pn in f.replace('.ebuild', '').split('-')[:-1]:
  27. pkg_name = pkg_name + pn + "-"
  28. except TypeError:
  29. for pn in f.replace('.ebuild', '').split('-')[:-2]:
  30. pkg_name = pkg_name + pn + "-"
  31. except Exception as e:
  32. print(e)
  33. if not pkg_name[:-1] in all_pkgs:
  34. print(str(d.split("/")[-2] +"/" + d.split("/")[-1] +"\n"))
  35. all_pkgs.append(str(d.split("/")[-2] +"/" + d.split("/")[-1]))
  36. if d.split("/")[-2] not in pkg_list.keys():
  37. pkg_list[str(d.split("/")[-2])] = []
  38. pkg_list[d.split("/")[-2]].append(d.split('/')[-1])
  39. print([d.split("/")[-2]])
  40. else:
  41. if d.split('/')[-1] not in pkg_list[d.split('/')[-2]]:
  42. pkg_list[d.split("/")[-2]].append(d.split('/')[-1])
  43. print([d.split('/')[-1]])
  44. fn.write(json.dumps({"all_pkgs": all_pkgs,"Catalog": pkg_list}))
  45. #print(len(pkg_list))
  46. print("Found:\t"+ str(len(pkg_list.keys())) + " category\n" + str(len(all_pkgs)) +"packages\n")
  47. def on_find(p_v):
  48. if not os.path.exists('./pkgs.json'):
  49. create_db()
  50. p = []
  51. ret_p =""
  52. ret = {}
  53. with open('./pkgs.json', 'r') as fn:
  54. #data = fn.read()
  55. pkg_list= fn.read()
  56. """
  57. pkg_list = data.split("\n")
  58. for i in pkg_list:
  59. if p_v in i and not i in p:
  60. print(i)
  61. p.append(str(i))
  62. #ret_p = ret_p +"\t" + i
  63. print("Find in template:\t" + str(len(p)))
  64. """
  65. #ret = {"Name": ret_p.split("\t")}
  66. #print(ret_p)
  67. print(p)
  68. return pkg_list #json.dumps(ret)
  69. if __name__ == '__main__':
  70. if not os.path.exists('./pkgs.txt'):
  71. create_db()
  72. if len(sys.argv) >=2:
  73. on_find(sys.argv[1])
  74. else:
  75. print("No element to find")