-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrand_create.py
145 lines (110 loc) · 4.68 KB
/
rand_create.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python
# encoding: utf-8
"""This script generates fake files and directories"""
import os
import argparse
import random
import string
# Useful override so that treats each space-separated word as an argument.
def convert_arg_line_to_args(self, arg_line):
return arg_line.split()
def create_garbage(size):
"""Create around size bytes of garbage"""
approx = int(size/1000)
max = random.randint(size - approx, size + approx)
chapter = ''.join(random.choice(string.printable) for _ in range(0, max))
return chapter
def create_dname():
"""Create a random name for a directory"""
max = random.randint(3, 20)
name = ''.join(random.choice(string.ascii_letters + string.digits)
for _ in range(0, max))
return name
def create_fname(ext):
"""Create a random name for a file"""
max = random.randint(3, 50)
name = ''.join(random.choice(string.ascii_letters + ' ' + string.digits)
for _ in range(0, max)).strip() + '.' + ext
return name
def create_file(size, exts, path):
"""Create a single file on a given path"""
f_name = create_fname(exts)
f = open(os.path.join(path, f_name), 'w')
f.write(create_garbage(size))
f.close()
def create_files(n, size, exts, paths):
"""Create files on a set of dirs"""
files_for_dir = int(n / len(paths))
for dir in paths:
for file in range(0, files_for_dir):
create_file(size, random.choice(exts), dir)
def create_subdirs(dirs, curr_path):
"""Create sub-directories"""
new_dirs = []
for dir in range(0, dirs):
d_name = create_dname()
current_path = os.path.join(curr_path, d_name)
try:
os.mkdir(current_path)
except OSError:
print('Not so lucky! A directory take exactly an existed name so \
faile to be created.')
new_dirs.append(current_path)
return new_dirs
def _parseargs():
parser = argparse.ArgumentParser(description='This tool is intended to \
create directory & files with random contents, random (or optionally real)\
name and real extensions (by default mostly used).')
group = parser.add_mutually_exclusive_group()
parser.add_argument('files', metavar='N', type=int,
help='number of files to be generated')
parser.add_argument('-d', '--directories', metavar='M', type=int,
default=0, help='number of directories to be generated\
(default: zero)')
parser.add_argument('-r', '--real', action='store_true',
help='use real name for files and directories')
group.add_argument('--ext-file', metavar='filename', type=str,
help='file containing a list of type of file extensions\
to be generated')
group.add_argument('--ext-list', metavar=('pdf', 'doc'), type=str,
nargs='+',
default=['jpg', 'png', 'mp3', 'pdf', 'doc', 'docx',
'ppt', 'zip', 'avi', 'mp4'], help='type of file\
extensions to be generated (default=["jpg", "png",\
"mp3", "pdf", "doc", "docx", "ppt", "zip", "avi",\
"mp4"])')
parser.add_argument('-l', '--level', type=int, default=0,
help='number of nested directories to be generated\
(default: no nested-dir)')
parser.add_argument('-e', '--existing', action='store_true',
help='use already existing sub-directories as first\
nested level (default: do not use already existing\
sub-directories)')
parser.add_argument('path', type=str,
help='path where create new dirs & files')
parser.add_argument('size', type=int,
help='average size of files to be generated in bytes')
args = parser.parse_args()
return args
def main(args):
# Just a toy.
print(args)
if not os.path.isdir(args.path):
try:
os.mkdir(args.path)
except OSError:
print('Error: faile to create', args.path, 'directory.')
exit(1)
path = args.path
sub_dirs = []
if args.existing:
for entry in os.listdir(path):
sub_dirs.append(os.path.join(path, entry))
if args.directories != 0:
sub_dirs.extend(create_subdirs(args.directories, path))
if len(sub_dirs) == 0: # use root path ONLY if no other dirs are used
sub_dirs.append(path)
create_files(args.files, args.size, args.ext_list, sub_dirs)
if __name__ == '__main__':
_args = _parseargs()
main(_args)