LoginSignup
0
0

More than 1 year has passed since last update.

makeからunzipを呼び出すときの注意点

Posted at

makeからunzipを呼び出して起きた事件

Kaggleのタイタニックのデータの取得を自動化するために,以下のようなMakefileを書きました.
Kaggleから3つのcsvファイルが入ったzipファイルを取得して,それを解凍するだけです.

Kaggleのタイタニックのデータの取得
TARGETS=gender_submission.csv test.csv train.csv
ZIP=titanic.zip

all: $(TARGETS)

clean:
        rm $(TARGETS) $(ZIP)

rebuild: clean
        make

$(ZIP):
        kaggle competitions download -c titanic

%.csv: $(ZIP)
        unzip $<

これでmakeしてみると...

# make
...
unzip titanic.zip
Archive:  titanic.zip
replace gender_submission.csv? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
replace test.csv? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
replace train.csv? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
unzip titanic.zip
Archive:  titanic.zip
replace gender_submission.csv? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
replace test.csv? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
replace train.csv? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
unzip titanic.zip
Archive:  titanic.zip
replace gender_submission.csv? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
replace test.csv? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
replace train.csv? [y]es, [n]o, [A]ll, [N]one, [r]ename: n

なんかめっちゃ質問される...

原因

これはunzipを一回呼び出せば3つのcsvファイルが全てそろうのに,makeの野郎がcsvの個数の分だけunzipを呼び出しやがることで起きます.
一回目のunzipで3つのcsvファイルがそろい,二回目以降のunzipは同名のファイルが既に存在しているために上書きするかどうか聞いてきます.

対策

unzipされたかどうかを示すファイル.unzippedを用意します.
.unzippedが存在すれば既にunzipされており,存在しなければまだunzipされていません.
これをターゲットのcsvファイルの依存関係に挟むことで,unzipの呼び出し回数を一回にできます.

unzipが一回だけ呼び出されるmakefile
TARGETS=gender_submission.csv test.csv train.csv
UNZIPPED=.unzipped
ZIP=titanic.zip

all: $(TARGETS)

clean:
        rm $(TARGETS) $(ZIP)

rebuild: clean
        make

$(ZIP):
        kaggle competitions download -c titanic

$(UNZIPPED): $(ZIP)
        unzip $^
        touch $@

%.csv: $(UNZIPPED)
        :
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0