Created
June 4, 2015 14:28
-
-
Save u8sand/59c891cd4587717d3651 to your computer and use it in GitHub Desktop.
Random File Tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python | |
""" | |
This script was made to generate a big and random | |
file/folder structure quickly and easily. | |
Made by u8sand | |
""" | |
# util functions | |
import random | |
def randRange(R): | |
L=len(R) | |
if L==2: | |
return range(random.randint(R[0], R[1])) | |
elif L==1: | |
return range(R[0]) | |
else: | |
return range(0) | |
def randInt(R): | |
L=len(R) | |
if L==2: | |
return random.randint(R[0], R[1]) | |
elif L==1: | |
return R[0] | |
else: | |
return 0 | |
# argument parsing | |
import os | |
import sys | |
import argparse | |
class HelpfulParser(argparse.ArgumentParser): | |
def error(self, message): | |
sys.stderr.write('error: %s\n' % message) | |
self.print_help() | |
sys.exit(2) | |
parser = HelpfulParser(description='Generate a random file tree') | |
parser.add_argument('-w', '--words', type=str, metavar='file', default='words.txt', help='Specify file with all the words. (default: words.txt)') | |
parser.add_argument('-D', '--depth', type=str, metavar='R', default='1-5', help='Specify the random depth range. (default: 1-5)') | |
parser.add_argument('-d', '--directories', type=str, metavar='R', default='1-5', help='Specify the random range of directories per level. (default 1-5)') | |
parser.add_argument('-f', '--files', type=str, metavar='R', default='1-5', help='Specify the random range of files per directories. (default 1-5)') | |
parser.add_argument('-t', '--text', type=str, metavar='R', default='1-100', help='Specify the random range of text per file. (default 1-100)') | |
parser.add_argument('root', type=str, help='The root directory for the tree') | |
args = parser.parse_args() | |
depth=[int(i) for i in args.depth.split('-')] | |
directories=[int(i) for i in args.directories.split('-')] | |
files=[int(i) for i in args.files.split('-')] | |
text=[int(i) for i in args.text.split('-')] | |
root=args.root | |
words=args.words | |
if os.path.exists(root) and not os.path.isdir(root): | |
sys.stderr.write('\'%s\' already exists and is a file\n' % (root)) | |
sys.exit(2) | |
# generate file tree | |
from random import choice | |
fh=open(words, 'r') | |
words=[w.strip() for w in fh.readlines()] | |
fh.close() | |
Q=[(root, 0)] | |
while Q!=[]: | |
d,l=Q.pop() | |
if not os.path.exists(d): | |
os.mkdir(d) | |
if os.path.isdir(d): | |
for i in randRange(files): | |
# make files | |
f=open('%s/%s' % (d, choice(words)), 'w') | |
for i in randRange(text): | |
f.write(choice(words)) | |
f.write(choice([' ','\n','\t'])) | |
f.close() | |
for i in randRange(directories): | |
# make sub-directories | |
if l < randInt(depth): | |
Q.append(('%s/%s' % (d, choice(words)), l+1)) | |
else: | |
# pick a different directory | |
D=d.split('/') | |
D[-1]=choice(words) | |
Q.append(('/'.join(D), l)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment