oe-pkgdata-util 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/usr/bin/env python
  2. # OpenEmbedded pkgdata utility
  3. #
  4. # Written by: Paul Eggleton <paul.eggleton@linux.intel.com>
  5. #
  6. # Copyright 2012 Intel Corporation
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License version 2 as
  10. # published by the Free Software Foundation.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. #
  22. # Currently only has one function - mapping of packages to their dev/dbg/doc/locale etc.
  23. # counterparts ("glob" command). Could be extended in future to perform other useful querying
  24. # functions on the pkgdata though.
  25. #
  26. import sys
  27. import os
  28. import os.path
  29. import fnmatch
  30. import re
  31. def usage():
  32. print("syntax: pkgdata-util glob [-d] <pkgdatadir> <vendor-os> <pkglist> \"<globs>\"");
  33. def glob(args):
  34. if len(args) < 4:
  35. usage()
  36. sys.exit(1)
  37. pkgdata_dir = args[0]
  38. target_suffix = args[1]
  39. pkglist_file = args[2]
  40. globs = args[3].split()
  41. if target_suffix.startswith("-"):
  42. target_suffix = target_suffix[1:]
  43. skipregex = re.compile("-locale-|^locale-base-|-dev$|-doc$|-dbg$|-staticdev$|^kernel-module-")
  44. mappedpkgs = set()
  45. with open(pkglist_file, 'r') as f:
  46. for line in f:
  47. fields = line.rstrip().split()
  48. if len(fields) < 2:
  49. continue
  50. pkg = fields[0]
  51. arch = fields[1]
  52. multimach_target_sys = "%s-%s" % (arch, target_suffix)
  53. # Skip packages for which there is no point applying globs
  54. if skipregex.search(pkg):
  55. if debug:
  56. print("%s -> !!" % pkg)
  57. continue
  58. # Skip packages that already match the globs, so if e.g. a dev package
  59. # is already installed and thus in the list, we don't process it any further
  60. # Most of these will be caught by skipregex already, but just in case...
  61. already = False
  62. for g in globs:
  63. if fnmatch.fnmatchcase(pkg, g):
  64. already = True
  65. break
  66. if already:
  67. if debug:
  68. print("%s -> !" % pkg)
  69. continue
  70. # Define some functions
  71. def revpkgdata(pkgn):
  72. return os.path.join(pkgdata_dir, multimach_target_sys, "runtime-reverse", pkgn)
  73. def fwdpkgdata(pkgn):
  74. return os.path.join(pkgdata_dir, multimach_target_sys, "runtime", pkgn)
  75. def readpn(pkgdata_file):
  76. pn = ""
  77. with open(pkgdata_file, 'r') as f:
  78. for line in f:
  79. if line.startswith("PN:"):
  80. pn = line.split(': ')[1].rstrip()
  81. return pn
  82. def readrenamed(pkgdata_file):
  83. renamed = ""
  84. pn = os.path.basename(pkgdata_file)
  85. with open(pkgdata_file, 'r') as f:
  86. for line in f:
  87. if line.startswith("PKG_%s:" % pn):
  88. renamed = line.split(': ')[1].rstrip()
  89. return renamed
  90. # Main processing loop
  91. for g in globs:
  92. mappedpkg = ""
  93. # First just try substitution (i.e. packagename -> packagename-dev)
  94. newpkg = g.replace("*", pkg)
  95. revlink = revpkgdata(newpkg)
  96. if os.path.exists(revlink):
  97. mappedpkg = os.path.basename(os.readlink(revlink))
  98. fwdfile = fwdpkgdata(mappedpkg)
  99. if os.path.exists(fwdfile):
  100. mappedpkg = readrenamed(fwdfile)
  101. if not os.path.exists(fwdfile + ".packaged"):
  102. mappedpkg = ""
  103. else:
  104. revlink = revpkgdata(pkg)
  105. if os.path.exists(revlink):
  106. # Check if we can map after undoing the package renaming (by resolving the symlink)
  107. origpkg = os.path.basename(os.readlink(revlink))
  108. newpkg = g.replace("*", origpkg)
  109. fwdfile = fwdpkgdata(newpkg)
  110. if os.path.exists(fwdfile):
  111. mappedpkg = readrenamed(fwdfile)
  112. else:
  113. # That didn't work, so now get the PN, substitute that, then map in the other direction
  114. pn = readpn(revlink)
  115. newpkg = g.replace("*", pn)
  116. fwdfile = fwdpkgdata(newpkg)
  117. if os.path.exists(fwdfile):
  118. mappedpkg = readrenamed(fwdfile)
  119. if not os.path.exists(fwdfile + ".packaged"):
  120. mappedpkg = ""
  121. else:
  122. # Package doesn't even exist...
  123. if debug:
  124. print "%s is not a valid package!" % (pkg)
  125. break
  126. if mappedpkg:
  127. if debug:
  128. print "%s (%s) -> %s" % (pkg, g, mappedpkg)
  129. mappedpkgs.add(mappedpkg)
  130. else:
  131. if debug:
  132. print "%s (%s) -> ?" % (pkg, g)
  133. if debug:
  134. print "------"
  135. print("\n".join(mappedpkgs))
  136. # Too lazy to use getopt
  137. debug = False
  138. noopt = False
  139. args = []
  140. for arg in sys.argv[1:]:
  141. if arg == "--":
  142. noopt = True
  143. else:
  144. if not noopt:
  145. if arg == "-d":
  146. debug = True
  147. continue
  148. args.append(arg)
  149. if len(args) < 1:
  150. usage()
  151. sys.exit(1)
  152. if args[0] == "glob":
  153. glob(args[1:])
  154. else:
  155. usage()
  156. sys.exit(1)