Rakefile 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # FreeBSD Ports
  2. #
  3. # Douglas Thrift
  4. #
  5. # Rakefile
  6. require 'date'
  7. require 'ostruct'
  8. require 'pathname'
  9. require 'set'
  10. def in_portdir(&block)
  11. portdir = Pathname.new(Rake.original_dir)
  12. raise "#{portdir} is not a port directory" unless portdir.relative_path_from(@portsdir).to_s =~ %r{[^/]+/[^/]+}
  13. Dir.chdir(portdir) do
  14. block.call(portdir)
  15. end
  16. end
  17. def in_portstree(portstree, &block)
  18. portstree_zfs = `zfs get -H -o name mountpoint #{portstree.path}`.chomp
  19. sh "sudo zfs snapshot #{portstree_zfs}@clean"
  20. Dir.chdir(portstree.path) do
  21. block.call(portstree.path)
  22. end
  23. ensure
  24. sh "sudo zfs rollback #{portstree_zfs}@clean"
  25. sh "sudo zfs destroy #{portstree_zfs}@clean"
  26. end
  27. def update_portstree_map(map = {})
  28. `poudriere ports -l -q`.each_line do |line|
  29. line.chomp!
  30. if line =~ /^([^ ]+) +([^ ]+) +([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}) +([^ ]+)$/
  31. map[$1.to_sym] = OpenStruct.new(method: $2, timestamp: DateTime.parse($3), path: Pathname.new($4))
  32. else
  33. raise "unexpected line: #{line}"
  34. end
  35. end
  36. map
  37. end
  38. def clean_version(version)
  39. if version =~ /^(\d+\.\d+-RELEASE)(?:-p\d+)$/
  40. $1
  41. else
  42. version
  43. end
  44. end
  45. portstree_map = update_portstree_map
  46. portsdir = @portsdir = Pathname.pwd
  47. distdir = portsdir.join('distfiles')
  48. task default: %i(update_githead)
  49. desc 'Create or update the port checksum file (distinfo)'
  50. task :makesum do
  51. in_portdir do
  52. sh "make makesum DEV_WARNING_WAIT=0 DISTDIR=#{distdir}"
  53. end
  54. end
  55. %w(md5 sha1 sha256).each do |sum|
  56. desc "Show the #{sum.upcase} sums of the distfiles"
  57. task sum do
  58. sh "mkdir -p #{distdir}" unless distdir.exist?
  59. files = distdir.glob('**/*').select {|dist| dist.file?}.map {|dist| dist.relative_path_from(portsdir)}
  60. sh "#{sum} #{files.join(' ')}" unless files.empty?
  61. end
  62. end
  63. desc 'Remove the ports distfiles'
  64. task :distclean do
  65. in_portdir do
  66. sh "make distclean DISTDIR=#{distdir}"
  67. end
  68. end
  69. desc 'Verify the port using portlint'
  70. task portlint: :update_githead do
  71. in_portdir do |portdir|
  72. if githead_portstree.path.join(portdir.relative_path_from(portsdir), 'Makefile').exist?
  73. sh "portlint -C"
  74. else
  75. sh "portlint -A"
  76. end
  77. end
  78. end
  79. desc 'Show the port differences'
  80. task diff: :update_svnhead do |_, args|
  81. patch = args.extras.find {|arg| arg == 'patch'}
  82. in_portdir do |portdir|
  83. new_files = Set.new(`git ls-files`.each_line.map {|line| Pathname.new(line.chomp)})
  84. in_portstree(svnhead_portstree) do |path|
  85. rel_portdir = portdir.relative_path_from(portsdir)
  86. svnhead_portdir = path.join(rel_portdir)
  87. if svnhead_portdir.exist?
  88. old_files = Set.new(`svn ls -R #{svnhead_portdir}`.each_line.reject {|line| line =~ %r{/$}}.map {|line| Pathname.new(line.chomp)})
  89. else
  90. old_files = Set.new
  91. end
  92. sh "sudo mkdir -p #{svnhead_portdir}"
  93. Dir.chdir(svnhead_portdir) do
  94. new_files.each do |file|
  95. portdir_file = portdir.join(file)
  96. sh "sudo mkdir -p #{file.dirname}" unless file.dirname.exist?
  97. sh "sudo cp #{portdir_file} #{file}"
  98. end
  99. (new_files - old_files).each do |file|
  100. sh "sudo svn add --parents #{file}"
  101. end
  102. (old_files - new_files).each do |file|
  103. sh "sudo svn remove #{file}"
  104. end
  105. unless old_files.empty?
  106. if patch
  107. sh "svn diff > #{portdir.dirname.join("#{`make -V PKGNAME`.chomp}.diff")}"
  108. else
  109. sh "svn diff"
  110. end
  111. end
  112. end
  113. if old_files.empty?
  114. if patch
  115. sh "svn diff #{rel_portdir} > #{portsdir.join("#{portdir.basename}.diff")}"
  116. else
  117. sh "svn diff #{rel_portdir}"
  118. end
  119. end
  120. end
  121. end
  122. end
  123. desc 'Test the port with poudriere'
  124. task :testport, %i(jailname flavor) => :update_githead do |_, args|
  125. this_version = clean_version(`uname -r`.chomp)
  126. this_arch = `uname -m`.chomp
  127. this_jailname = nil
  128. `poudriere jail -l -q`.each_line do |line|
  129. line.chomp!
  130. if line =~ /^([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+).+$/
  131. jailname, version, arch, method = $1, $2, $3, $4
  132. if clean_version(version) == this_version && arch == this_arch && method == 'ftp'
  133. this_jailname = jailname
  134. break
  135. end
  136. else
  137. raise "unexpected line: #{line}"
  138. end
  139. end
  140. args.with_defaults(jailname: this_jailname)
  141. in_portdir do |portdir|
  142. files = `git ls-files`.each_line.map {|line| Pathname.new(line.chomp)}
  143. in_portstree(githead_portstree) do |path|
  144. rel_portdir = portdir.relative_path_from(portsdir)
  145. githead_portdir = path.join(rel_portdir)
  146. sh "sudo mkdir -p #{githead_portdir}"
  147. Dir.chdir(githead_portdir) do
  148. files.each do |file|
  149. portdir_file = portdir.join(file)
  150. sh "sudo mkdir -p #{file.dirname}" unless file.dirname.exist?
  151. sh "sudo cp #{portdir_file} #{file}"
  152. end
  153. end
  154. sh "sudo poudriere testport -j #{args.jailname} -o #{rel_portdir}#{args.flavor && "@#{args.flavor}"} -p githead"
  155. end
  156. end
  157. end
  158. {githead: 'git+https', svnhead: 'svn+https'}.each do |name, method|
  159. define_method("#{name}_portstree") do
  160. portstree_map[name]
  161. end
  162. desc "Create or update the #{name} portstree"
  163. task "update_#{name}" do |_, args|
  164. force = args.extras.find {|arg| arg == 'force'}
  165. portstree = portstree_map[name]
  166. if portstree
  167. raise "unexpected portstree method for #{name}: #{portstree[:method]} (expected: #{method})" if portstree[:method] != method
  168. if force || portstree.timestamp < Date.today
  169. sh "sudo poudriere ports -u -p #{name}"
  170. end
  171. else
  172. sh "sudo poudriere ports -c -m #{method} -p #{name}"
  173. update_portstree_map(portstree_map)
  174. end
  175. end
  176. end