Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import os, sys, traceback
- #for python 2.7
- #PURPOSE: Turn any file or directory into a python module with that file or directory's contents embedded as base64
- #USAGE: resgen.py <path>
- __version__ = 1.0
- __author__ = "Peaser"
- try:
- target = sys.argv[1]
- except:
- raise Warning, "No argument provided."
- allname = ""
- def normalize(st):
- b = []
- for i in st:
- if i in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_": #quick fix for non-number/letter chars
- b.append(i)
- return ''.join(b)
- def dirmake():
- global allname
- try:
- succeeded = []
- name = os.path.split(target)[1]+'.py'
- allname += name
- with open(name, 'a') as mod:
- for i in os.listdir(target):
- doc = open(target+'/'+i, 'rb').read()
- i = normalize(i)
- if i[0].isdigit():
- i ="d_"+i
- mod.write(i+' = """'+doc.encode("base64")+'"""\n\n')
- succeeded.append(i)
- mod.close()
- except:
- print traceback.format_exc()
- for i in succeeded:
- print i
- print "Module successfully created with "+str(len(succeeded))+" total variables."
- print "Module size: "+ str(os.stat(allname).st_size)+" Bytes."
- def filemake():
- global allname
- try:
- prename = os.path.splitext(os.path.split(target)[1])
- name = prename[0]+'_'+(prename[1]).replace('.','')+'.py'
- allname += name
- with open(name, 'a') as obj:
- item = open(target, 'rb').read().encode("base64")
- obj.write('content = """'+item+'"""\n\n')
- obj.close()
- except:
- print traceback.format_exc()
- print "Module successfully created from", target
- print "Module size: "+ str(os.stat(allname).st_size)+" Bytes."
- if os.path.isdir(target):
- dirmake()
- else:
- filemake()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement