Class: Flist::Flist

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

Overview

ファイル一覧クラス

Defined Under Namespace

Classes: Filelist

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, opts) ⇒ Flist

初期化



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

def initialize(hash, opts)
  # debug_utils_init
  # set_debug(true)
  set_debug(opts['verbose'])

  @flielists = []
  #      yamlfname = "env-a1.yaml"
  # YAML形式ファイル config.yml を読み込み、ファイルシステムの判定に用いる
  d_puts("opts['config']=#{opts['config']}")
  d_puts('#=====')
  config_from_file(opts['config'])
  # d_p(@config)
  # opts["env"]
  # determine_filesystemメソッドの返値であるハッシュには、
  # ホームディレクトリ(キー:home)、ドライブ(キー:Cや:d)を表す、UNIX式パスまたは
  # WINDOWS式パスが含まれる。
  # d_p("opts['env']=#{opts['env']}")
  d_puts(%(opts["env"]=#{opts['env']}))
  d_puts(%(@config=#{@config}))
  @dir = determine_filesystem(opts['env'])
  # ホームディレクトリまたはドライブのトップディレクトリの直下に引数で指定した
  # ファイルが存在し、かつ返値に含まれる絶対パスを用いてアクセス可能
  # @dir[:home]は絶対パスなので、@dir[:home]の直下に変数yamlfnameが存在する

  # 変数fnameは、Eruby形式のファイルのパスを表す
  # Erbuy形式のファイルをscope(コンテキスト)で展開されたものをYAML形式とみなして
  # 解釈して、ハッシュに変換する
  d_p(@dir)
  d_p(@dir['home'])
  d_p(opts['env'])
  d_puts("@dir=#{@dir}")
  fname = File.expand_path(opts['env'], @dir['home'])
  d_p("env fname=#{fname}")

  # scope = Object.new
  scope = self
  value_hash = {}
  # @dir.keys.each do |key|
  @dir.each do |key, value|
    #        value_hash[key.to_sym] = @dir[key]
    value_hash[key.to_sym] = value
  end
  d_p(value_hash)
  content = Ykutils::Erubyx.erubi_render_with_template_file(fname, scope, value_hash)
  yaml = Ykxutils.yaml_load_compati(content)

  # 指定エンコードディングに基づいてパスを扱うEncxクラスのインスタンス生成
  Encx::Encx.init('UTF-8')
  @encx = Encx::Encx.encx

  # 引数hashとして渡されたハッシュで指定された設定を用いてDB接続
  # d_puts("# hash=#{hash}")
  db_dir = hash['db_dir']
  # d_puts("db_dir=#{db_dir}")
  config_dir = hash['config_dir']
  env = hash['env']
  dbconfig = hash['dbconfig']
  # A method of the `FlistMgr` class.
  # output_dir = hash['output_dir']
  @pstore_dir = hash['pstore_dir']

  dbconfig_path = Arxutils_Sqlite3::Util.make_dbconfig_path(config_dir, dbconfig)
  log_path = Arxutils_Sqlite3::Util.make_log_path(db_dir, dbconfig)
  dbconnect = Arxutils_Sqlite3::Dbutil::Dbconnect.new(
    dbconfig_path,
    env,
    log_path
  )
  # DB接続
  register_time = dbconnect.connect
  # 接続したDBに対し、ActiveRecordのサブクラスを介してアクセスするFlistMgr
  @dbmgr = Dbutil::DbMgr.new(register_time)

  # 検索時のトップディレクトリのハッシュ
  # キーは、トップディレクトリをまとめるグループを表す文字列
  # d_p("yaml=#{yaml}")
  @topdirhs = yaml['flist']['topdirhs']
  # d_p("@topdirhs=#{@topdirhs}")
  # ログディレクトリへのパス
  @logdir = yaml['flist']['logdir']

  # システムのトップディレクトリの設定
  init_topx(yaml)

  init_skip_dirs(yaml)

  regist_dirz
end

Instance Attribute Details

#encxObject

Returns the value of attribute encx.



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

def encx
  @encx
end

Class Method Details

.make_instance_of_item_class(level, key, regexp, path) ⇒ Object

ファイル情報アイテムクラスのインスタンス生成



32
33
34
# File 'lib/flist/flist.rb', line 32

def self.make_instance_of_item_class(level, key, regexp, path)
  @item_class.new(level, key, regexp, path)
end

Instance Method Details

#config_from_file(fname) ⇒ Object

Reading a YAML file and storing the contents in the instance variable ‘@config`.



127
128
129
130
131
132
# File 'lib/flist/flist.rb', line 127

def
config_from_file(fname)
  # d_p("fname=#{fname}")
  @config = Ykxutils.yaml_load_file_compati(fname)
  # d_p(@config)
end

#determine_filesystem(fname) ⇒ Object

プラットフォームの判別をし、ホームディレクトリ、ドライブのトップディレクトリ を表す適切なパスを返す



136
137
138
139
140
141
142
143
144
# File 'lib/flist/flist.rb', line 136

def determine_filesystem(fname)
  d_puts("@config=#{@config}")
  d_puts('#####################')
  @config.find do |x|
    path = File.join(x['home'], fname)
    # d_p("df path=#{path}")
    File.exist?(path)
  end
end

#get_directory_list(dir_path, level) ⇒ Object

指定階層数分の指定ディレクトリ以下のサブディレクトリのリストを得る



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/flist/flist.rb', line 300

def get_directory_list(dir_path, level)
  # 引数s
  ary = []
  Dir.chdir(dir_path) do
    if level.positive?
      level -= 1
      Dir.glob('*').each do |pa|
        ary += get_directory_list(pa, level) if File.directory?(pa)
      end
    else
      ary << Dir.pwd
    end
  end
  ary
end

#init_skip_dirs(hash) ⇒ Object

スキップディレクトリ情報の初期化



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/flist/flist.rb', line 147

def init_skip_dirs(hash)
  # スキップディレクトリを表すハッシュ
  # キーはスキップディレクトリスキップディレクトリのパスの配列
  @skip_dirs = {}
  skip_dirs_array = hash['flist']['skip_dirs_array']
  skip_dirs_array.each do |x|
    x_str = File.expand_path(x)
    re_esc = Regexp.escape(x_str)
    # スキップディレクトリ判定用正規表現配列に追加
    @skip_dirs[x] = @encx.make_regexp("^#{re_esc}")
  end
end

#init_topx(yaml) ⇒ Object

システムのトップディレクトリ情報の初期化



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/flist/flist.rb', line 161

def init_topx(yaml)
  # このシステムのトップディレクトリを表すハッシュ
  # キーは番号、値はトップディレクトリのパスの配列
  @tops = yaml['flist']['tops']
  # yaml['flist']['tops']の内容を用途別に設定し直したハッシュ
  @topx = {}
  @topx_keys = %i[kind level all]
  @topx_keys.map { |x| @topx[x] ||= {} }
  # :allの場合は{}ではなく[]にする
  @topx[:all] = []

  @tops.each do |no, path_array|
    # d_puts "no=#{no}"
    # d_puts "path_array=#{path_array}"
    next unless path_array

    path_array.each do |path|
      ary = path.split('/')
      # 絶対パスは先頭が"/"で始まるため、配列の先頭が空文字列の場合は捨てて、ディレクトリ名、ファイル名のみを含むようにする
      ary.shift if ary[0] == ''
      # 絶対パスの階層の深さ
      level = ary.size
      # サブディレクトリであるかの判定に用いる
      re = @encx.make_regexp("^#{path}")
      # 検索時のトップディレクトリを表す
      itemx = self.class.make_instance_of_item_class(level, no, re, path)
      # 設定ファイルでの["flist"]["tops"]に対応するハッシュ
      # ここでは、["flist"]["tops"]のキー(番号)を表す
      # :kindは、次のキー(番号)に対応する絶対パスの配列を持つ
      @topx[:kind][no] ||= []
      @topx[:kind][no] << itemx
      # :levelは、次のキー(れ部ス数)に対応する絶対パスの配列を持つ
      @topx[:level][level] ||= []
      @topx[:level][level] << itemx
      # :allは、設定ファイルでの["flist"]["tops"]に対応するハッシ
      # に含まれる全ての絶対パスについて、それらに対応するStruct
      # クラスのサブクラスのインスタンスを含む
      @topx[:all] ||= []
      @topx[:all] << itemx
    end
  end
end

#list_aObject

設定ファイルの[‘tops]に格納されたトップディレクトリ以下の ディレクトリ、ファイルのリストを得る



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/flist/flist.rb', line 342

def list_a
  # d_puts '####### list_a'
  list_x = listx
  # d_puts list_x.size

  csvx = make_csvx
  # 設定ファイルの['flist']['tops]に格納されたトップディレクトリ
  # とその直下のサブディレクトリすべてのパス
  list_x.each do |x|
    top_path = @encx.convert(x)
    # dirzテーブルへ登録
    dirz = @dbmgr.dirz_add(top_path)
    ary = top_path.split('/')
    ary.shift
    level = ary.size

    filelist = Filelist.new(@dbmgr, csvx, @encx, @skip_dirs, dirz.id, top_path, level)
    # d_puts("list_a level=#{level} top_path=#{top_path}")
    filelist.scanx(top_path, level)
  end
  csvx.finish
  nil
end

#listup_repoObject

repoグループのファイル一覧



275
276
277
# File 'lib/flist/flist.rb', line 275

def listup_repo
  listup_x('repo')
end

#listup_simpleObject

simpleグループのファイル一覧



280
281
282
# File 'lib/flist/flist.rb', line 280

def listup_simple
  listup_x('simple')
end

#listup_simple2Object

simple2グループのファイル一覧



285
286
287
# File 'lib/flist/flist.rb', line 285

def listup_simple2
  listup_x('simple2')
end

#listup_simple3Object

simple3グループのファイル一覧



290
291
292
# File 'lib/flist/flist.rb', line 290

def listup_simple3
  listup_x('simple3')
end

#listup_x(group) ⇒ Object

設定ファイルので定義されたグループのうち、引数groupで指定されたグループに含まれる全ディレクトリ登録



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/flist/flist.rb', line 240

def listup_x(group)
  # キーはグループのディレクトリの直下に存在するディレクトリ
  # 値はハッシュ { "dir_id" => ディレクトリ番号, "fname" => ファイル名 }
  @topdirhs[group].each do |path, hash0|
    x_path = @encx.convert(path)
    # キーはグループのディレクトリの直下に存在するディレクトリ
    hash0.each do |dir, hash1|
      # d_puts "hash1=#{hash1}"
      fname = hash1['fname']
      x_fname = @encx.convert(fname)
      x_dir = @encx.convert(dir)
      x_logdir = @encx.convert(@logdir)

      csvpath = File.expand_path(File.join(x_logdir, x_fname))
      csvx = Csvx.new(csvpath)

      dir_id = hash1['dir_id']
      # d_puts "dir_id=#{dir_id}"
      # d_puts x_path
      # d_puts x_dir
      top_path = File.expand_path(File.join(x_path, x_dir))
      if File.exist?(top_path)
        filelist = Filelist.new(@dbmgr, csvx, @encx, @skip_dirs, dir_id, top_path, 0, group.to_sym)
        filelist.scan
        filelist.output
        @flielists << filelist
      else
        puts "Can't find #{top_path}"
      end
      csvx.finish
    end
  end
end

#listxObject

設定ファイルでの[“tops”]に設定されたディレクトリ以下 のディレクトリのリストを得る



318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/flist/flist.rb', line 318

def listx
  # 設定ファイルでの["flist"]["tops"]に対応するハッシュ
  # ここでは、["flist"]["tops"]のキー(番号)を表す
  # :kindは、次のキー(番号)に対応する絶対パスの配列を持つ

  # @topx[:kind]に含まれるディレクトリの直下のサブディレクトリを一つの配列として返す
  level = 1
  @topx[:kind].map do |_no, path_array|
    path_array.map do |item|
      get_directory_list(item.path, level)
    end
  end.flatten
end

#lxObject

ディレクトリIDが1の場合のDBに格納されたファイル情報取得



295
296
297
# File 'lib/flist/flist.rb', line 295

def lx
  Flistz.where(dir_id: 1).select(:id)
end

#make_csvxObject

全体のログファイルとしてCsvxクラスのインスタンス生成



333
334
335
336
337
338
# File 'lib/flist/flist.rb', line 333

def make_csvx
  x_fname = 'all.csv'
  x_logdir = @encx.convert(@logdir)
  csvpath = File.expand_path(File.join(x_logdir, x_fname))
  Csvx.new(csvpath)
end

#regist_dirzObject

設定ファイルので定義された全グループで指定された全ディレクトリ登録



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/flist/flist.rb', line 205

def regist_dirz
  # d_puts('########## regist_dirz S')
  # d_puts("@topdirhs.size=#{@topdirhs.size}")
  dirz_array = []
  # 検索時のトップディレクトリのハッシュ
  # キーは、トップディレクトリをまとめるグループを表す文字列
  @topdirhs.each do |_group, v|
    # d_puts("regist_dirz group=#{group}")
    # d_puts("regist_dirz v=#{v}")
    # キーはグループに対応するディレクトリ
    v.each do |path, v0|
      # d_puts("regist_dirz path=#{path}")
      # d_puts("regist_dirz v0=#{v0}")
      # d_puts("v0.class=#{v0.class}")
      # キーはグループのディレクトリの直下に存在するディレクトリ
      # 値はハッシュ { "dir_id" => ディレクトリ番号, "fname" => ファイル名 }
      v0.each do |dir, v1|
        # d_puts "dir=#{dir}"
        # d_puts "v1=#{v1}"
        x_dir = @encx.convert(dir)
        # d_puts "v1['dir_id']=#{v1['dir_id']}"
        x_str = @encx.convert(v1['fname'])
        full_path = File.expand_path(File.join(x_dir, x_str), path)
        # d_puts "regist_dirz path=#{full_path}"
        dirz = dirz_add(full_path) if File.exist?(full_path)
        dirz_array << dirz
        # d_puts '#~~~~~~'
      end
    end
  end
  # d_puts('########## regist_dirz E')
  dirz_array
end