Class: Flist::Flist::Filelist

Inherits:
Object
  • Object
show all
Includes:
Ykutils::DebugUtils
Defined in:
lib/flist/flist/filelist.rb

Overview

詳細ファイルリストクラス

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dbmgr, csvx, encx, skip_dirs = {}, dir_id = 0, top_dir = '', top_level = 0, mode = '') ⇒ Filelist

Returns a new instance of Filelist.

Raises:



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
# File 'lib/flist/flist/filelist.rb', line 27

def initialize(dbmgr, csvx, encx, skip_dirs = {}, dir_id = 0, top_dir = '',
               top_level = 0, mode = '')
  @dbmgr = dbmgr
  @csvx = csvx
  @encx = encx
  @skip_dirs = skip_dirs

  # パス名判別用正規表現
  @hash = {}
  @hash[:dot_directory] = @encx.make_regexp('^(\.|_).+')
  @hash[:node_repository] = @encx.make_regexp('[^_]_packages')
  @hash[:node_modules] = @encx.make_regexp('node_modules|bower_modules')
  @hash[:temp] = @encx.make_regexp('te?mp', Regexp::IGNORECASE)
  @hash[:cvs] = @encx.make_regexp('cvs', Regexp::IGNORECASE)

  # 設定ファイルの"topdirhs"で定義されたグループに含まれるサブディレクトリを表すID
  @dir_id = dir_id
  raise InvalidDirIdError, "invalid @dir_id=#{@dir_id}" unless @dir_id

  # 全Itemを収めるハッシュ(キーはそのItemを指すパス)
  @items = {}
  # Itemのうち、ディレクトリを収めるハッシュ(キーはそのItemを指すパス)
  @dirs = {}
  # Itemのうち、ファイルを収めるハッシュ(キーはそのItemを指すパス)
  @files = {}

  # このFilelistのトップディレクトリ(トップディレクトリより下位のディレクトリ、ファイルの情報を持つ)
  @top_dir = top_dir
  # このFilelistのトップディレクトリの階層の深さ
  @top_level = top_level
  @mode = mode
end

Class Method Details

.make_instance_of_fileinfo_class(atime, ctime, mtime) ⇒ Object



15
16
17
# File 'lib/flist/flist/filelist.rb', line 15

def self.make_instance_of_fileinfo_class(atime, ctime, mtime)
  @fileinfo_class.new(atime, ctime, mtime)
end

.make_instance_of_item_class(level = nil, kind = nil, repo = nil, path = nil, path_conv = nil, project = nil, desc = nil, comment = nil, atime = nil, ctime = nil, mtime = nil) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/flist/flist/filelist.rb', line 19

def self.make_instance_of_item_class(level = nil, kind = nil, repo = nil, path = nil,
                                     path_conv = nil, project = nil, desc = nil,
                                     comment = nil,
                                     atime = nil, ctime = nil, mtime = nil)
  @item_class.new(level, kind, repo, path, path_conv, project, desc,
                  comment, atime, ctime, mtime)
end

Instance Method Details

#bundle_repositoryObject



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/flist/flist/filelist.rb', line 139

def bundle_repository
  abs_path = nil
  config_path = '.bundle/config'
  if Dir.exist?(config_path)
    config = get_config(config_path)
    @skip_dirs[bundle_pn.to_s] = @encx.make_regexp("^#{config}") if config

    abs_path = Dir.open(config['BUNDLE_PATH'], &:pwd) if config['BUNDLE_PATH'] && Dir.exist?(config['BUNDLE_PATH'])
  end
  abs_path
end

#dot_directory?(basename) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
116
117
118
119
120
# File 'lib/flist/flist/filelist.rb', line 110

def dot_directory?(basename)
  d_puts "In dot_directory? basename=#{basename}"
  ret = @encx.compare(basename, @hash[:dot_directory])
  if ret
    d_puts 'T'
  else
    d_puts 'F'
  end
  d_puts "In dot_directory? basename=#{basename}|ret=#{ret}"
  ret
end

#get_config(path) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/flist/flist/filelist.rb', line 101

def get_config(path)
  alist = File.open(path) do |f|
    f.readlines.grep_v(/^-/).collect do |x|
      x.chomp.split(/:/)
    end
  end
  Hash[*alist.flatten(1)]
end

#ignore_directory?(basename) ⇒ Boolean

Returns:

  • (Boolean)


155
156
157
158
159
160
161
162
163
# File 'lib/flist/flist/filelist.rb', line 155

def ignore_directory?(basename)
  ret = @encx.compare(basename, @hash[:temp])
  unless ret
    d_puts '# ignore_directory? 2'
    ret = @encx.compare(basename, @hash[:cvs])
  end
  d_puts "# ignore_directory? ret=#{ret}"
  ret
end

#make_fileinfo(path) ⇒ Object



165
166
167
168
169
170
171
172
173
# File 'lib/flist/flist/filelist.rb', line 165

def make_fileinfo(path)
  raise InvalidMakeItemArgsError, "path=#{path}" unless path.instance_of?(String)

  atime = File.atime(path)
  ctime = File.ctime(path)
  mtime = File.mtime(path)
  self.class.make_instance_of_fileinfo_class(atime, ctime, mtime)
  # @@fileinfo_class.new( atime , ctime, mtime )
end

#need_skip?(path_s) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
# File 'lib/flist/flist/filelist.rb', line 60

def need_skip?(path_s)
  ret = nil
  ret = @skip_dirs.keys.find { |x| @encx.compare(path_s, x) } if @skip_dirs
  ret_value = !ret.nil?
  d_puts "need_skip? path_s=#{path_s}|ret_value=#{ret_value}"
  ret_value
end

#node_repository?(basename) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
125
126
127
128
129
130
131
# File 'lib/flist/flist/filelist.rb', line 122

def node_repository?(basename)
  d_puts "In node_repository? path=#{basename}"
  ret = @encx.compare(basename, @hash[:node_modules])
  unless ret
    d_puts '# node_repository? 2'
    ret = @encx.compare(basename, @hash[:node_repository])
  end
  d_puts "# node_repository? basename=#{basename}|ret=#{ret}"
  ret
end

#non_skip_files(abs_dir_path, level, level_limit) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/flist/flist/filelist.rb', line 235

def non_skip_files(abs_dir_path, level, level_limit)
  next_level = level + 1
  next_level_limit = level_limit - 1
  Dir.chdir(abs_dir_path) do
    d_puts "non_skip_files chdir #{abs_dir_path}"
    repo = nil
    dirs = []
    @cur_dir = Dir.getwd
    bundle_repo = bundle_repository

    if next_level_limit <= 0
      Dir.foreach('.') do |x|
        next if ['.', '..'].include?(x)

        # d_puts "scanx x=#{x}"
        # d_puts "=========#{x.encoding}"
        # d_puts "@cur_dir=#{@cur_dir}"
        x_str = @encx.convert(x)
        repo, dir = scanx_sub(x_str, bundle_repo, next_level, next_level_limit, repo)
        dirs << dir if dir
      end
      unless repo.nil?
        dirs.each do |d|
          scanx(d, next_level, next_level_limit)
        end
      end
    end
  end
end

#outputObject



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/flist/flist/filelist.rb', line 309

def output
  d_puts 'In output'

  @items.each do |_k, v|
    d_puts "v.path.encoding=#{v.path.encoding}"

    if @csv
      @csvx.csv << [@dir_id, v.level, v.kind, v.repo, v.path, v.project, '', v.comment, v.atime, v.ctime,
                    v.mtime]
    end
    d_puts '#### Flielist'
    d_puts "dir_id=#{@dir_id}"
    d_puts "level=#{v.level}"
    d_puts "kind=#{v.kind}"
    d_puts "repo=#{v.repo}"
    d_puts "path=#{v.path_conv}"
    d_puts "project=#{v.project}"
    d_puts "desc=#{v.desc}"
    d_puts "comment=#{v.comment}"
    d_puts "atime=#{v.atime}"
    d_puts "ctime=#{v.ctime}"
    d_puts "mtime=#{v.mtime}"
    d_puts '#### Flilelist End'
    raise InvalidDirIdError, "invalid @dir_id=#{@dir_id}" unless @dir_id
    raise InvalidRepoError, "invalid v.repo=#{v.repo}" unless v.repo
    raise InvalidCommitError, "invalid v.comment=#{v.comment}" unless v.comment

    @dbmgr.flistz_add(@dir_id, v.level, v.kind, v.repo, v.path, v.project,
                      v.desc, v.comment, v.atime, v.ctime, v.mtime)
  end
end

#project_directory?(_abs_path) ⇒ Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/flist/flist/filelist.rb', line 151

def project_directory?(_abs_path)
  false
end

#project_directory_filesObject



224
225
226
227
228
229
230
231
232
233
# File 'lib/flist/flist/filelist.rb', line 224

def project_directory_files
  dir = @encx.convert(@cur_dir)
  x_str = @encx.convert(x)
  d_puts "1 #{dir}"
  register_item(kind, level, '', dir, make_fileinfo(x_str), 1)
  @dirs[dir] = @encx.make_regexp("^#{abs_dir_path}") unless @dirs[dir]

  # プロジェクトを格納するディレクトリは、今後スキップさせる
  @skip_dirs[dir] = @encx.make_regexp("^#{abs_dir_path}") unless skip_dirs[dir]
end

#register_item(kind, level, repo, path, fileinfo, project = 0) ⇒ Object

ハッシュ@itemsにpathをキーに登録する



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/flist/flist/filelist.rb', line 82

def register_item(kind, level, repo, path, fileinfo, project = 0)
  d_puts 'register_item'
  d_puts "path=#{path}"
  cur_item = self.class.make_instance_of_item_class
  # @cur_item = @@item_class.new
  cur_item.kind = kind
  cur_item.level = level
  cur_item.repo = repo
  cur_item.path_conv = @encx.convert(path)
  cur_item.path = path
  cur_item.project = project
  cur_item.comment = ''
  cur_item.atime = time2datetime_utc(fileinfo.atime)
  cur_item.ctime = time2datetime_utc(fileinfo.ctime)
  cur_item.mtime = time2datetime_utc(fileinfo.mtime)

  @items[path] = cur_item
end

#repository?(basename, bundle_repo) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
136
137
# File 'lib/flist/flist/filelist.rb', line 133

def repository?(basename, bundle_repo)
  ret = dot_directory?(basename) or bundle_repo == basename or node_repository?(basename)
  d_puts "In repository? basename=#{basename} bundle_repo=#{bundle_repo}|ret=#{ret}"
  ret
end

#repository_files(kind, level, repo, dir, fileinfo, path_s) ⇒ Object



175
176
177
178
179
180
181
182
# File 'lib/flist/flist/filelist.rb', line 175

def repository_files(kind, level, repo, dir, fileinfo, path_s)
  register_item(kind, level, repo, dir, fileinfo, 1)

  @dirs[path_s] = @encx.make_regexp("^#{path_s}") unless @dirs[path_s]
  @skip_dirs[path_s] = @encx.make_regexp("^#{path_s}") unless @skip_dirs[path_s]

  [repo, dir]
end

#scanObject



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/flist/flist/filelist.rb', line 285

def scan
  d_puts('In scan')
  # 調査対象のトップディレクトリ
  # @top_dirはディレクトリ名(フルパスではない)である
  Dir.chdir(@top_dir) do
    @topd_dir = Dir.getwd
  end
  # 調査対象のトップディレクトリが絶対パス、相対パスで指定された時を考慮して、
  # トップディレクトリのbasename部分をとりだして調べる。
  #
  # 無視すべきディレクトリ名パターンにマッチすれば、@top_dir以下を調べない
  return if ignore_directory?(File.basename(@top_dir))

  Dir.chdir(File.join(@top_dir, '..')) do
    @cur_dir = Dir.getwd
  end
  d_puts "scan @cur_dir=#{@cur_dir} call scanx @top_dir=#{@top_dir}"
  next_level = 0
  next_level_limit = 2
  scanx(@top_dir, next_level, next_level_limit)

  d_puts '########## scan End'
end

#scanx(abs_dir_path, level, level_limit) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/flist/flist/filelist.rb', line 265

def scanx(abs_dir_path, level, level_limit)
  d_puts "######### scanx abs_dir_path=#{abs_dir_path} level=#{level} level_limit=#{level_limit}"
  next_level = level + 1
  next_level_limit = level_limit - 1
  # d_puts "=scanx====#{Dir.pwd}"
  # d_puts "abs_dir_path=#{abs_dir_path}"
  # d_puts "abs_dir_path.encoding=#{abs_dir_path.encoding}"

  # プロジェクトを格納するディレクトリであれば、
  # (TO DO) project_directory?を実装する
  if project_directory?(abs_dir_path)
    project_directory_files
  else
    return if next_level_limit <= 0

    # スッキプすべきとして登録されたディレクトリまたは、そのサブディレクトリであれば、スキップする
    non_skip_files(abs_dir_path, next_level, next_level_limit) unless need_skip?(abs_dir_path)
  end
end

#scanx_sub(item, bundle_repo, level, _level_limit, repo) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/flist/flist/filelist.rb', line 184

def scanx_sub(item, bundle_repo, level, _level_limit, repo)
  dir = nil
  if File.directory?(item)
    kind = 'D'
    dir = @encx.convert(@cur_dir)
    unless item.instance_of?(String)
      p item
      raise InvalidSacnxSubArgumentError, "item=#{item}"
    end
    x_item = @encx.convert(item)
    path_s = File.expand_path(File.join(dir, x_item))
    if repository?(x_item, bundle_repo)
      # カレントディレクトリの直下にリポジトリが存在すれば、カレントディレクトリをプロジェクトとして登録する。リポジトリ自体は登録しない(管理外とする)。
      d_puts "REPOSITORY #{dir}|#{path_s}"
      # return nil
      unless x_item.instance_of?(String)
        p x_item
        raise InvalidSacnxSubArgumentError, "x_item=#{x_item}"
      end
      return repository_files(kind, level, x_item, dir, make_fileinfo(x_item), path_s)
    elsif ignore_directory?(x_item)
      d_puts "SKIP_DIRECTORY #{x_item}|#{path_s}"
      @skip_dirs[path_s] = @encx.make_regexp("^#{path_s}")
    else
      d_puts "REGISTER_DIRECTORY #{x_item}|#{path_s}"
      register_item(kind, level, '', path_s, make_fileinfo(x_item), 0)
      dir = path_s
    end
  elsif @mode == :simple
    kind = 'F'

    dir = @encx.convert(@cur_dir)
    x_item = @encx.convert(item)
    path_s = File.expand_path(File.join(dir, x_item))
    register_item(kind, level, '', path_s, make_fileinfo(x_item), 0)
  end

  [repo, dir]
end

#time2datetime_utc(time) ⇒ Object



68
69
70
71
# File 'lib/flist/flist/filelist.rb', line 68

def time2datetime_utc(time)
  # UTC
  DateTime.new(time.year, time.month, time.day, time.hour, time.min, time.sec, time.utc_offset)
end

#update_item(kind, level, repo, path, _project = 0) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/flist/flist/filelist.rb', line 73

def update_item(kind, level, repo, path, _project = 0)
  @items[path].kind = kind
  @items[path].level = level
  @items[path].repo = repo
  @items[path].path = path
  @items[path].project = preoject
end