File: run_test_prep.go

package info (click to toggle)
zfind 0.4.7-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 320 kB
  • sloc: sh: 145; makefile: 5
file content (90 lines) | stat: -rw-r--r-- 2,655 bytes parent folder | download | duplicates (3)
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
package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"time"
)

var (
	startList = []string{"time", "year", "people", "way", "day", "thing"}
	wordList  = []string{"life", "world", "school", "state", "family", "student", "group", "country", "problem", "hand", "part", "place", "case", "week", "company", "system", "program", "work", "government", "number", "night", "point", "home", "water", "room", "mother", "area", "money", "story", "fact", "month", "lot", "right", "study", "book", "eye", "job", "word", "business", "issue", "side", "kind", "head", "house", "service", "friend", "father", "power", "hour", "game", "line", "end", "member", "law", "car", "city", "community", "name", "president", "team", "minute", "idea", "kid", "body", "information", "back", "face", "others", "level", "office", "door", "health", "person", "art", "war", "history", "party", "result", "change", "morning", "reason", "research", "moment", "air", "teacher", "force", "education"}
	extList   = []string{"txt", "md", "pdf", "jpg", "jpeg", "png", "mp4", "mp3", "csv"}
	startDate = time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
	endDate   = time.Date(2024, 12, 1, 0, 0, 0, 0, time.UTC)
	dateList  = []time.Time{}
	wordIdx   = 0
	extIdx    = 0
	dateIdx   = 0
)

func nextWord() string {
	word := wordList[wordIdx%len(wordList)]
	wordIdx++
	return word
}

func nextExt() string {
	ext := extList[extIdx%len(extList)]
	extIdx++
	return ext
}

func setDate(filename string, r int) {
	date := dateList[dateIdx%len(dateList)]
	m := 17 * dateIdx / len(dateList)
	date = date.Add(time.Duration(m) * time.Hour)
	dateIdx++
	os.Chtimes(filename, date, date)
}

func genFile(dir string, a int) {
	os.MkdirAll(dir, 0755)
	for i := 1; i <= 5; i++ {
		size := a*i*wordIdx*100 + extIdx
		file := nextWord() + "-" + nextWord()

		if i%3 == 0 {
			file += "-" + nextWord()
		}

		file += "." + nextExt()
		path := filepath.Join(dir, file)
		ioutil.WriteFile(path, make([]byte, size), 0644)
		setDate(path, size*size)
	}
}

func genDir(root string) {
	for _, start := range startList {

		for i := 1; i <= 5; i++ {
			dir := filepath.Join(root, start, nextWord())
			genFile(dir, 1)

			if wordIdx%3 == 0 {
				dir = filepath.Join(dir, nextWord())
				genFile(dir, 1)
			}
		}
	}
}

func main() {
	root := "/tmp/zfind"

	var c int64 = 50
	interval := (int64)(endDate.Sub(startDate).Seconds()) / c
	for i := range make([]int64, c) {
		dateList = append(dateList, startDate.Add(time.Duration(interval*(int64)(i))*time.Second))
	}

	if err := os.RemoveAll(root); err == nil {
		genDir(filepath.Join(root, "root"))
		fmt.Println("Ready.")
	} else {
		fmt.Println("Failed to clean")
	}
}