-
Notifications
You must be signed in to change notification settings - Fork 22
/
test_dir_tree.py
executable file
·114 lines (92 loc) · 3.67 KB
/
test_dir_tree.py
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
#!/usr/bin/env python2
# -*- coding: utf8 -*-
"""
test_dir_tree.py: Make a fake dir hierarchy with files for test purposes
"""
#==============================================================================
# This Script makes a fake dir hierarchy with files for test purposes
#
# Uses the Lorem-Ipsum-Generator project by James Hales
# <jhales.perth@gmail.com> at http://code.google.com/p/lorem-ipsum-generator/
#==============================================================================
#==============================================================================
# Copyright 2010 joe di castro <joe@joedicastro.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#==============================================================================
__author__ = "joe di castro <joe@joedicastro.com>"
__license__ = "GNU General Public License version 3"
__date__ = "29/6/2012"
__version__ = "0.2"
try:
import sys
import os
import lipsum
from random import randint, randrange, choice
from textwrap import fill
except ImportError:
# Checks the installation of the necessary python modules
print((os.linesep * 2).join(["An error found importing one module:",
str(sys.exc_info()[1]), "You need to install it", "Stopping..."]))
sys.exit(-2)
def latin_words(generator):
"""Generate a list of latin words"""
words = generator.generate_paragraphs_plain(9).lower()
return list(set(words.replace('.', '').replace(',', '').split()))
def check_path(path):
"""If no exists a path, make it."""
if not os.path.exists(path):
os.mkdir(path)
def test_tree(path, min_dirs=7, max_dirs=79):
"""make a fake directory hierarchy with files for test purposes."""
lorem = lipsum.MarkupGenerator()
latins = latin_words(lorem)
dirs = latins[:randrange(min_dirs, max_dirs)]
files = [f for f in latins if f not in dirs][:len((dirs) * 3) - 3]
check_path(path)
for directory in dirs:
check_path(os.path.join(path, directory))
for fil in files:
filename = os.path.join(path, choice(dirs), '{0}.txt'.format(fil))
text = ''
size = randint(3, 524288) # Files not bigger than 512 Kbytes
sample = lorem.generate_paragraphs_plain(randrange(3, 9))
while len(text) < size:
text += os.linesep.join([fill(l, 79) for l in
sample.split(os.linesep)]) + os.linesep * 2
with open(filename, 'w') as out:
out.write(text[:size])
return dirs, files
def main():
"""Main section"""
# Create a test dir hierarchy
path = './test/'
folders, archives = test_tree(path)
print '{0} folders & {1} files created'.format(len(folders), len(archives))
if __name__ == "__main__":
main()
###############################################################################
# Changelog #
###############################################################################
#
# 0.2:
#
# * Wrap text paragraphs to 79 chars
# * Refactorization
#
# 0.1:
#
# * First attempt
#