oe-pkgdata-util 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 two functions:
  23. # 1) glob - mapping of packages to their dev/dbg/doc/locale etc. counterparts.
  24. # 2) read-value - mapping of packagenames to their location in
  25. # pkgdata and then returns value of selected variable (e.g. PKGSIZE)
  26. # Could be extended in future to perform other useful querying functions on the
  27. # pkgdata though.
  28. #
  29. import sys
  30. import os
  31. import os.path
  32. import fnmatch
  33. import re
  34. def usage():
  35. print("syntax: oe-pkgdata-util glob [-d] <pkgdatadir> <vendor-os> <pkglist> \"<globs>\"\n \
  36. read-value [-d] <pkgdatadir> <vendor-os> <value-name> \"<package-name>_<package_architecture>\"");
  37. def glob(args):
  38. if len(args) < 4:
  39. usage()
  40. sys.exit(1)
  41. pkgdata_dir = args[0]
  42. target_suffix = args[1]
  43. pkglist_file = args[2]
  44. globs = args[3].split()
  45. if target_suffix.startswith("-"):
  46. target_suffix = target_suffix[1:]
  47. skipregex = re.compile("-locale-|^locale-base-|-dev$|-doc$|-dbg$|-staticdev$|^kernel-module-")
  48. mappedpkgs = set()
  49. with open(pkglist_file, 'r') as f:
  50. for line in f:
  51. fields = line.rstrip().split()
  52. if len(fields) < 2:
  53. continue
  54. pkg = fields[0]
  55. arch = fields[1]
  56. multimach_target_sys = "%s-%s" % (arch, target_suffix)
  57. # Skip packages for which there is no point applying globs
  58. if skipregex.search(pkg):
  59. if debug:
  60. print("%s -> !!" % pkg)
  61. continue
  62. # Skip packages that already match the globs, so if e.g. a dev package
  63. # is already installed and thus in the list, we don't process it any further
  64. # Most of these will be caught by skipregex already, but just in case...
  65. already = False
  66. for g in globs:
  67. if fnmatch.fnmatchcase(pkg, g):
  68. already = True
  69. break
  70. if already:
  71. if debug:
  72. print("%s -> !" % pkg)
  73. continue
  74. # Define some functions
  75. def revpkgdata(pkgn):
  76. return os.path.join(pkgdata_dir, multimach_target_sys, "runtime-reverse", pkgn)
  77. def fwdpkgdata(pkgn):
  78. return os.path.join(pkgdata_dir, multimach_target_sys, "runtime", pkgn)
  79. def readpn(pkgdata_file):
  80. pn = ""
  81. with open(pkgdata_file, 'r') as f:
  82. for line in f:
  83. if line.startswith("PN:"):
  84. pn = line.split(': ')[1].rstrip()
  85. return pn
  86. def readrenamed(pkgdata_file):
  87. renamed = ""
  88. pn = os.path.basename(pkgdata_file)
  89. with open(pkgdata_file, 'r') as f:
  90. for line in f:
  91. if line.startswith("PKG_%s:" % pn):
  92. renamed = line.split(': ')[1].rstrip()
  93. return renamed
  94. # Main processing loop
  95. for g in globs:
  96. mappedpkg = ""
  97. # First just try substitution (i.e. packagename -> packagename-dev)
  98. newpkg = g.replace("*", pkg)
  99. revlink = revpkgdata(newpkg)
  100. if os.path.exists(revlink):
  101. mappedpkg = os.path.basename(os.readlink(revlink))
  102. fwdfile = fwdpkgdata(mappedpkg)
  103. if os.path.exists(fwdfile):
  104. mappedpkg = readrenamed(fwdfile)
  105. if not os.path.exists(fwdfile + ".packaged"):
  106. mappedpkg = ""
  107. else:
  108. revlink = revpkgdata(pkg)
  109. if os.path.exists(revlink):
  110. # Check if we can map after undoing the package renaming (by resolving the symlink)
  111. origpkg = os.path.basename(os.readlink(revlink))
  112. newpkg = g.replace("*", origpkg)
  113. fwdfile = fwdpkgdata(newpkg)
  114. if os.path.exists(fwdfile):
  115. mappedpkg = readrenamed(fwdfile)
  116. else:
  117. # That didn't work, so now get the PN, substitute that, then map in the other direction
  118. pn = readpn(revlink)
  119. newpkg = g.replace("*", pn)
  120. fwdfile = fwdpkgdata(newpkg)
  121. if os.path.exists(fwdfile):
  122. mappedpkg = readrenamed(fwdfile)
  123. if not os.path.exists(fwdfile + ".packaged"):
  124. mappedpkg = ""
  125. else:
  126. # Package doesn't even exist...
  127. if debug:
  128. print "%s is not a valid package!" % (pkg)
  129. break
  130. if mappedpkg:
  131. if debug:
  132. print "%s (%s) -> %s" % (pkg, g, mappedpkg)
  133. mappedpkgs.add(mappedpkg)
  134. else:
  135. if debug:
  136. print "%s (%s) -> ?" % (pkg, g)
  137. if debug:
  138. print "------"
  139. print("\n".join(mappedpkgs))
  140. def read_value(args):
  141. if len(args) < 4:
  142. usage()
  143. sys.exit(1)
  144. pkgdata_dir = args[0]
  145. target_suffix = args[1]
  146. var = args[2]
  147. packages = args[3].split()
  148. if target_suffix.startswith("-"):
  149. target_suffix = target_suffix[1:]
  150. def readvar(pkgdata_file, var):
  151. val = ""
  152. with open(pkgdata_file, 'r') as f:
  153. for line in f:
  154. if line.startswith(var + ":"):
  155. val = line.split(': ')[1].rstrip()
  156. return val
  157. if debug:
  158. print "read-value('%s', '%s', '%s' '%s'" % (pkgdata_dir, target_suffix, var, packages)
  159. for package in packages:
  160. pkg_split = package.split('_')
  161. pkg_name = pkg_split[0]
  162. pkg_arch = '_'.join(pkg_split[1:])
  163. if debug:
  164. print "package: name: '%s', arch: '%s'" % (pkg_name, pkg_arch)
  165. multimach_target_sys = "%s-%s" % (pkg_arch, target_suffix)
  166. revlink = os.path.join(pkgdata_dir, multimach_target_sys, "runtime-reverse", pkg_name)
  167. if debug:
  168. print(revlink)
  169. if not os.path.exists(revlink):
  170. # [YOCTO #4227] try to drop -gnueabi from TARGET_OS
  171. multimach_target_sys = '-'.join(multimach_target_sys.split('-')[:-1])
  172. revlink = os.path.join(pkgdata_dir, multimach_target_sys, "runtime-reverse", pkg_name)
  173. if debug:
  174. print(revlink)
  175. if os.path.exists(revlink):
  176. mappedpkg = os.path.basename(os.readlink(revlink))
  177. qvar = var
  178. if qvar == "PKGSIZE":
  179. # append packagename
  180. qvar = "%s_%s" % (var, mappedpkg)
  181. print(readvar(revlink, qvar))
  182. # Too lazy to use getopt
  183. debug = False
  184. noopt = False
  185. args = []
  186. for arg in sys.argv[1:]:
  187. if arg == "--":
  188. noopt = True
  189. else:
  190. if not noopt:
  191. if arg == "-d":
  192. debug = True
  193. continue
  194. args.append(arg)
  195. if len(args) < 1:
  196. usage()
  197. sys.exit(1)
  198. if args[0] == "glob":
  199. glob(args[1:])
  200. elif args[0] == "read-value":
  201. read_value(args[1:])
  202. else:
  203. usage()
  204. sys.exit(1)