You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
4.7 KiB

  1. #!/usr/bin/env ruby
  2. #
  3. # mtime_cache
  4. # Copyright (c) 2016 Borislav Stanimirov
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to
  8. # deal in the Software without restriction, including without limitation the
  9. # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. # sell copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. # IN THE SOFTWARE.
  23. #
  24. require 'digest/md5'
  25. require 'json'
  26. require 'fileutils'
  27. VERSION = "1.0.2"
  28. VERSION_TEXT = "mtime_cache v#{VERSION}"
  29. USAGE = <<ENDUSAGE
  30. Usage:
  31. mtime_cache [<globs>] [-g globfile] [-d] [-q|V] [-c cache]
  32. ENDUSAGE
  33. HELP = <<ENDHELP
  34. Traverse through globbed files, making a json cache based on their mtime.
  35. If a cache exists, changes the mtime of existing unchanged (based on MD5
  36. hash) files to the one in the cache.
  37. Options:
  38. globs Ruby-compatible glob strings (ex some/path/**/*.java)
  39. A extension pattern is allowd in the form %{pattern}
  40. (ex some/path/*.{%{pattern1},%{pattern2}})
  41. The globs support the following patterns:
  42. %{cpp} - common C++ extensions
  43. -g, --globfile A file with list of globs to perform (one per line)
  44. -?, -h, --help Show this help message.
  45. -v, --version Show the version number (#{VERSION})
  46. -q, --quiet Don't log anything to stdout
  47. -V, --verbose Show extra logging
  48. -d, --dryrun Don't change any files on the filesystem
  49. -c, --cache Specify the cache file for input and output.
  50. [Default is .mtime_cache.json]
  51. ENDHELP
  52. param_arg = nil
  53. ARGS = { :cache => '.mtime_cache.json', :globs => [] }
  54. ARGV.each do |arg|
  55. case arg
  56. when '-g', '--globfile' then param_arg = :globfile
  57. when '-h', '-?', '--help' then ARGS[:help] = true
  58. when '-v', '--version' then ARGS[:ver] = true
  59. when '-q', '--quiet' then ARGS[:quiet] = true
  60. when '-V', '--verbose' then ARGS[:verbose] = true
  61. when '-d', '--dryrun' then ARGS[:dry] = true
  62. when '-c', '--cache' then param_arg = :cache
  63. else
  64. if param_arg
  65. ARGS[param_arg] = arg
  66. param_arg = nil
  67. else
  68. ARGS[:globs] << arg
  69. end
  70. end
  71. end
  72. def log(text, level = 0)
  73. return if ARGS[:quiet]
  74. return if level > 0 && !ARGS[:verbose]
  75. puts text
  76. end
  77. if ARGS[:ver] || ARGS[:help]
  78. log VERSION_TEXT
  79. exit if ARGS[:ver]
  80. log USAGE
  81. log HELP
  82. exit
  83. end
  84. if ARGS[:globs].empty? && !ARGS[:globfile]
  85. log 'Error: Missing globs'
  86. log USAGE
  87. exit 1
  88. end
  89. EXTENSION_PATTERNS = {
  90. :cpp => "c,cc,cpp,cxx,h,hpp,hxx,inl,ipp,inc,ixx"
  91. }
  92. cache_file = ARGS[:cache]
  93. cache = {}
  94. if File.file?(cache_file)
  95. log "Found #{cache_file}"
  96. cache = JSON.parse(File.read(cache_file))
  97. log "Read #{cache.length} entries"
  98. else
  99. log "#{cache_file} not found. A new one will be created"
  100. end
  101. globs = ARGS[:globs].map { |g| g % EXTENSION_PATTERNS }
  102. globfile = ARGS[:globfile]
  103. if globfile
  104. File.open(globfile, 'r').each_line do |line|
  105. line.strip!
  106. next if line.empty?
  107. globs << line % EXTENSION_PATTERNS
  108. end
  109. end
  110. if globs.empty?
  111. log 'Error: No globs in globfile'
  112. log USAGE
  113. exit 1
  114. end
  115. files = {}
  116. num_changed = 0
  117. globs.each do |glob|
  118. Dir[glob].each do |file|
  119. next if !File.file?(file)
  120. mtime = File.mtime(file).to_i
  121. hash = Digest::MD5.hexdigest(File.read(file))
  122. cached = cache[file]
  123. if cached && cached['hash'] == hash && cached['mtime'] < mtime
  124. mtime = cached['mtime']
  125. log "mtime_cache: changing mtime of #{file} to #{mtime}", 1
  126. File.utime(File.atime(file), Time.at(mtime), file) if !ARGS[:dry]
  127. num_changed += 1
  128. else
  129. log "mtime_cache: NOT changing mtime of #{file}", 1
  130. end
  131. files[file] = { 'mtime' => mtime, 'hash' => hash }
  132. end
  133. end
  134. log "Changed mtime of #{num_changed} of #{files.length} files"
  135. log "Writing #{cache_file}"
  136. if !ARGS[:dry]
  137. dirname = File.dirname(cache_file)
  138. unless File.directory?(dirname)
  139. FileUtils.mkdir_p(dirname)
  140. end
  141. File.open(cache_file, 'w').write(JSON.pretty_generate(files))
  142. end