gen-lockedsig-cache 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env python3
  2. #
  3. # SPDX-License-Identifier: GPL-2.0-only
  4. #
  5. import os
  6. import sys
  7. import shutil
  8. import errno
  9. import time
  10. def mkdir(d):
  11. try:
  12. os.makedirs(d)
  13. except OSError as e:
  14. if e.errno != errno.EEXIST:
  15. raise e
  16. # extract the hash from past the last colon to last underscore
  17. def extract_sha(filename):
  18. return filename.split(':')[7].split('_')[0]
  19. # get all files in a directory, extract hash and make
  20. # a map from hash to list of file with that hash
  21. def map_sha_to_files(dir_, prefix, sha_map):
  22. sstate_prefix_path = dir_ + '/' + prefix + '/'
  23. if not os.path.exists(sstate_prefix_path):
  24. return
  25. sstate_files = os.listdir(sstate_prefix_path)
  26. for f in sstate_files:
  27. try:
  28. sha = extract_sha(f)
  29. if sha not in sha_map:
  30. sha_map[sha] = []
  31. sha_map[sha].append(sstate_prefix_path + f)
  32. except IndexError:
  33. continue
  34. # given a prefix build a map of hash to list of files
  35. def build_sha_cache(prefix):
  36. sha_map = {}
  37. sstate_dir = sys.argv[2]
  38. map_sha_to_files(sstate_dir, prefix, sha_map)
  39. native_sstate_dir = sys.argv[2] + '/' + sys.argv[4]
  40. map_sha_to_files(native_sstate_dir, prefix, sha_map)
  41. return sha_map
  42. if len(sys.argv) < 5:
  43. print("Incorrect number of arguments specified")
  44. print("syntax: gen-lockedsig-cache <locked-sigs.inc> <input-cachedir> <output-cachedir> <nativelsbstring> [filterfile]")
  45. sys.exit(1)
  46. filterlist = []
  47. if len(sys.argv) > 5:
  48. print('Reading filter file %s' % sys.argv[5])
  49. with open(sys.argv[5]) as f:
  50. for l in f.readlines():
  51. if ":" in l:
  52. filterlist.append(l.rstrip())
  53. print('Reading %s' % sys.argv[1])
  54. sigs = []
  55. with open(sys.argv[1]) as f:
  56. for l in f.readlines():
  57. if ":" in l:
  58. task, sig = l.split()[0].rsplit(':', 1)
  59. if filterlist and not task in filterlist:
  60. print('Filtering out %s' % task)
  61. else:
  62. sigs.append(sig)
  63. print('Gathering file list')
  64. start_time = time.perf_counter()
  65. files = set()
  66. sstate_content_cache = {}
  67. for s in sigs:
  68. prefix = s[:2]
  69. prefix2 = s[2:4]
  70. if prefix not in sstate_content_cache:
  71. sstate_content_cache[prefix] = {}
  72. if prefix2 not in sstate_content_cache[prefix]:
  73. sstate_content_cache[prefix][prefix2] = build_sha_cache(prefix + "/" + prefix2)
  74. if s in sstate_content_cache[prefix][prefix2]:
  75. for f in sstate_content_cache[prefix][prefix2][s]:
  76. files.add(f)
  77. elapsed = time.perf_counter() - start_time
  78. print("Gathering file list took %.1fs" % elapsed)
  79. print('Processing files')
  80. for f in files:
  81. sys.stdout.write('Processing %s... ' % f)
  82. _, ext = os.path.splitext(f)
  83. if not ext in ['.tgz', '.siginfo', '.sig']:
  84. # Most likely a temp file, skip it
  85. print('skipping')
  86. continue
  87. dst = os.path.join(sys.argv[3], os.path.relpath(f, sys.argv[2]))
  88. destdir = os.path.dirname(dst)
  89. mkdir(destdir)
  90. src = os.path.realpath(f)
  91. if os.path.exists(dst):
  92. os.remove(dst)
  93. if (os.stat(src).st_dev == os.stat(destdir).st_dev):
  94. print('linking')
  95. try:
  96. os.link(src, dst)
  97. except OSError as e:
  98. print('hard linking failed, copying')
  99. shutil.copyfile(src, dst)
  100. else:
  101. print('copying')
  102. shutil.copyfile(src, dst)
  103. print('Done!')