-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandfile.py
executable file
·95 lines (76 loc) · 2.98 KB
/
randfile.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
#!/usr/bin/env python -u
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Maciek Ruckgaber <[email protected]>
#
from __future__ import print_function
import argparse
import string
import random
from os import listdir
from os.path import isfile, join
def randomString(size=32, chars="uppercase,lowercase,digits"):
"""
Creates a random string of specified size and type
Args:
size: (int) Desired length of generated string
chars : (str) Desired types to use separated by "," : (uppercase, lowercase, digits)
Returns:
Random generated string
"""
opt = chars.split(",")
use_chars = ""
if 'uppercase' in opt:
use_chars = use_chars + string.ascii_uppercase
if 'lowercase' in opt:
use_chars = use_chars + string.ascii_lowercase
if 'digits' in opt:
use_chars = use_chars + string.digits
return ''.join(random.choice(use_chars) for x in range(size))
def mkfilename(ext="php"):
return "%s.%s" % (randomString(size=5,chars='lowercase'),ext)
def create_file(ext):
with open(mkfilename(ext=ext),'w') as fl:
max_lines = random.randint(1,10)
for i in range(0,max_lines):
chunks = random.randint(1,5)
print(" ".join([ randomString(size=6) for j in range(0,chunks) ]), file=fl)
def modify_file(fname):
with open(fname,'r+') as fl:
lns = fl.readlines()
nlns = []
for ln in lns:
nlns.append(randomString(size=6)) if random.randint(0,1) else ln
fl.seek(0)
print(" ".join(nlns), file=fl)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="""
Creates / modifies files with random strings, useful for giving crash courses on
Git or other version control systems.
""")
parser.add_argument("action", type=str, choices=['create','modify'],
help=""" Action to perform. The \"create\" arg would create -n number of files in the
current directory. The \"modify\" action will randomly modify -n files in the current
dir. """)
parser.add_argument("-n","--number",type=int, default=1, help="Number of files to randomly create / modify")
parser.add_argument("-x","--extension",type=str, default="php",help="Extension for created files")
args = parser.parse_args()
if args.action == 'create':
for i in range(0,args.number):
create_file(args.extension)
elif args.action == 'modify':
files = [ f for f in listdir(".") if isfile(join(".",f)) ]
random.shuffle(files)
for i in range(0,args.number):
modify_file(files[i])