reproducible.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. #
  4. import os
  5. import subprocess
  6. import bb
  7. def get_source_date_epoch_from_known_files(d, sourcedir):
  8. source_date_epoch = None
  9. newest_file = None
  10. known_files = set(["NEWS", "ChangeLog", "Changelog", "CHANGES"])
  11. for file in known_files:
  12. filepath = os.path.join(sourcedir, file)
  13. if os.path.isfile(filepath):
  14. mtime = int(os.lstat(filepath).st_mtime)
  15. # There may be more than one "known_file" present, if so, use the youngest one
  16. if not source_date_epoch or mtime > source_date_epoch:
  17. source_date_epoch = mtime
  18. newest_file = filepath
  19. if newest_file:
  20. bb.debug(1, "SOURCE_DATE_EPOCH taken from: %s" % newest_file)
  21. return source_date_epoch
  22. def find_git_folder(d, sourcedir):
  23. # First guess: WORKDIR/git
  24. # This is the default git fetcher unpack path
  25. workdir = d.getVar('WORKDIR')
  26. gitpath = os.path.join(workdir, "git/.git")
  27. if os.path.isdir(gitpath):
  28. return gitpath
  29. # Second guess: ${S}
  30. gitpath = os.path.join(sourcedir, ".git")
  31. if os.path.isdir(gitpath):
  32. return gitpath
  33. # Perhaps there was a subpath or destsuffix specified.
  34. # Go looking in the WORKDIR
  35. exclude = set(["build", "image", "license-destdir", "patches", "pseudo",
  36. "recipe-sysroot", "recipe-sysroot-native", "sysroot-destdir", "temp"])
  37. for root, dirs, files in os.walk(workdir, topdown=True):
  38. dirs[:] = [d for d in dirs if d not in exclude]
  39. if '.git' in dirs:
  40. return root
  41. bb.warn("Failed to find a git repository in WORKDIR: %s" % workdir)
  42. return None
  43. def get_source_date_epoch_from_git(d, sourcedir):
  44. if not "git://" in d.getVar('SRC_URI'):
  45. return None
  46. gitpath = find_git_folder(d, sourcedir)
  47. if not gitpath:
  48. return None
  49. # Check that the repository has a valid HEAD; it may not if subdir is used
  50. # in SRC_URI
  51. p = subprocess.run(['git', '--git-dir', gitpath, 'rev-parse', 'HEAD'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  52. if p.returncode != 0:
  53. bb.debug(1, "%s does not have a valid HEAD: %s" % (gitpath, p.stdout.decode('utf-8')))
  54. return None
  55. bb.debug(1, "git repository: %s" % gitpath)
  56. p = subprocess.run(['git', '--git-dir', gitpath, 'log', '-1', '--pretty=%ct'], check=True, stdout=subprocess.PIPE)
  57. return int(p.stdout.decode('utf-8'))
  58. def get_source_date_epoch_from_youngest_file(d, sourcedir):
  59. if sourcedir == d.getVar('WORKDIR'):
  60. # These sources are almost certainly not from a tarball
  61. return None
  62. # Do it the hard way: check all files and find the youngest one...
  63. source_date_epoch = None
  64. newest_file = None
  65. for root, dirs, files in os.walk(sourcedir, topdown=True):
  66. files = [f for f in files if not f[0] == '.']
  67. for fname in files:
  68. filename = os.path.join(root, fname)
  69. try:
  70. mtime = int(os.lstat(filename).st_mtime)
  71. except ValueError:
  72. mtime = 0
  73. if not source_date_epoch or mtime > source_date_epoch:
  74. source_date_epoch = mtime
  75. newest_file = filename
  76. if newest_file:
  77. bb.debug(1, "Newest file found: %s" % newest_file)
  78. return source_date_epoch
  79. def fixed_source_date_epoch():
  80. bb.debug(1, "No tarball or git repo found to determine SOURCE_DATE_EPOCH")
  81. return 0
  82. def get_source_date_epoch(d, sourcedir):
  83. return (
  84. get_source_date_epoch_from_git(d, sourcedir) or
  85. get_source_date_epoch_from_known_files(d, sourcedir) or
  86. get_source_date_epoch_from_youngest_file(d, sourcedir) or
  87. fixed_source_date_epoch() # Last resort
  88. )