summaryrefslogtreecommitdiffhomepage
path: root/ci/mtime_cache
blob: e296e3658325a76f0e3e8df233c257a9e24980ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env ruby

#
# mtime_cache
# Copyright (c) 2016 Borislav Stanimirov
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#

require 'digest/md5'
require 'json'
require 'fileutils'

VERSION = "1.0.2"

VERSION_TEXT = "mtime_cache v#{VERSION}"

USAGE = <<ENDUSAGE

Usage:
    mtime_cache [<globs>] [-g globfile] [-d] [-q|V] [-c cache]
ENDUSAGE

HELP = <<ENDHELP

    Traverse through globbed files, making a json cache based on their mtime.
    If a cache exists, changes the mtime of existing unchanged (based on MD5
    hash) files to the one in the cache.

    Options:

    globs           Ruby-compatible glob strings (ex some/path/**/*.java)
                    A extension pattern is allowd in the form %{pattern}
                    (ex some/path/*.{%{pattern1},%{pattern2}})
                    The globs support the following patterns:
                     %{cpp} - common C++ extensions

    -g, --globfile  A file with list of globs to perform (one per line)

    -?, -h, --help  Show this help message.
    -v, --version   Show the version number (#{VERSION})
    -q, --quiet     Don't log anything to stdout
    -V, --verbose   Show extra logging
    -d, --dryrun    Don't change any files on the filesystem
    -c, --cache     Specify the cache file for input and output.
                    [Default is .mtime_cache.json]

ENDHELP

param_arg = nil
ARGS = { :cache => '.mtime_cache.json', :globs => [] }

ARGV.each do |arg|
  case arg
    when '-g', '--globfile'   then param_arg = :globfile
    when '-h', '-?', '--help' then ARGS[:help] = true
    when '-v', '--version'    then ARGS[:ver] = true
    when '-q', '--quiet'      then ARGS[:quiet] = true
    when '-V', '--verbose'    then ARGS[:verbose] = true
    when '-d', '--dryrun'     then ARGS[:dry] = true
    when '-c', '--cache'      then param_arg = :cache
    else
      if param_arg
        ARGS[param_arg] = arg
        param_arg = nil
      else
        ARGS[:globs] << arg
      end
  end
end

def log(text, level = 0)
  return if ARGS[:quiet]
  return if level > 0 && !ARGS[:verbose]
  puts text
end

if ARGS[:ver] || ARGS[:help]
  log VERSION_TEXT
  exit if ARGS[:ver]
  log USAGE
  log HELP
  exit
end

if ARGS[:globs].empty? && !ARGS[:globfile]
  log 'Error: Missing globs'
  log USAGE
  exit 1
end

EXTENSION_PATTERNS = {
  :cpp => "c,cc,cpp,cxx,h,hpp,hxx,inl,ipp,inc,ixx"
}

cache_file = ARGS[:cache]

cache = {}

if File.file?(cache_file)
  log "Found #{cache_file}"
  cache = JSON.parse(File.read(cache_file))
  log "Read #{cache.length} entries"
else
  log "#{cache_file} not found. A new one will be created"
end

globs = ARGS[:globs].map { |g| g % EXTENSION_PATTERNS }

globfile = ARGS[:globfile]
if globfile
  File.open(globfile, 'r').each_line do |line|
    line.strip!
    next if line.empty?
    globs << line % EXTENSION_PATTERNS
  end
end

if globs.empty?
  log 'Error: No globs in globfile'
  log USAGE
  exit 1
end

files = {}
num_changed = 0

globs.each do |glob|
  Dir[glob].each do |file|
    next if !File.file?(file)

    mtime = File.mtime(file).to_i
    hash = Digest::MD5.hexdigest(File.read(file))

    cached = cache[file]

    if cached && cached['hash'] == hash && cached['mtime'] < mtime
      mtime = cached['mtime']

      log "mtime_cache: changing mtime of #{file} to #{mtime}", 1

      File.utime(File.atime(file), Time.at(mtime), file) if !ARGS[:dry]
      num_changed += 1
    else
      log "mtime_cache: NOT changing mtime of #{file}", 1
    end

    files[file] = { 'mtime' => mtime, 'hash' => hash }
  end
end

log "Changed mtime of #{num_changed} of #{files.length} files"
log "Writing #{cache_file}"

if !ARGS[:dry]
  dirname = File.dirname(cache_file)
  unless File.directory?(dirname)
    FileUtils.mkdir_p(dirname)
  end
  File.open(cache_file, 'w').write(JSON.pretty_generate(files))
end