Advertisement
Peaser

resgen

Jul 7th, 2014
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. #!/usr/bin/python
  2. import os, sys, traceback
  3.  
  4. #for python 2.7
  5.  
  6. #PURPOSE: Turn any file or directory into a python module with that file or directory's contents embedded as base64
  7.  
  8. #USAGE: resgen.py <path>
  9.  
  10.  
  11.  
  12. __version__ = 1.0
  13. __author__ = "Peaser"
  14.  
  15. try:
  16.     target = sys.argv[1]
  17. except:
  18.     raise Warning, "No argument provided."
  19.  
  20. allname = ""
  21.  
  22. def normalize(st):
  23.     b = []
  24.     for i in st:
  25.         if i in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_": #quick fix for non-number/letter chars
  26.             b.append(i)
  27.     return ''.join(b)
  28.  
  29. def dirmake():
  30.     global allname
  31.     try:
  32.         succeeded = []
  33.         name = os.path.split(target)[1]+'.py'
  34.         allname += name
  35.         with open(name, 'a') as mod:
  36.             for i in os.listdir(target):
  37.                 doc = open(target+'/'+i, 'rb').read()
  38.                 i = normalize(i)
  39.                 if i[0].isdigit():
  40.                     i ="d_"+i
  41.                 mod.write(i+' = """'+doc.encode("base64")+'"""\n\n')
  42.                 succeeded.append(i)
  43.         mod.close()
  44.     except:
  45.         print traceback.format_exc()
  46.     for i in succeeded:
  47.         print i
  48.     print "Module successfully created with "+str(len(succeeded))+" total variables."
  49.     print "Module size: "+ str(os.stat(allname).st_size)+" Bytes."
  50.  
  51. def filemake():
  52.     global allname
  53.     try:
  54.         prename = os.path.splitext(os.path.split(target)[1])
  55.         name = prename[0]+'_'+(prename[1]).replace('.','')+'.py'
  56.         allname += name
  57.         with open(name, 'a') as obj:
  58.             item = open(target, 'rb').read().encode("base64")
  59.             obj.write('content = """'+item+'"""\n\n')
  60.         obj.close()
  61.     except:
  62.         print traceback.format_exc()
  63.  
  64.     print "Module successfully created from", target
  65.     print "Module size: "+ str(os.stat(allname).st_size)+" Bytes."
  66.    
  67.    
  68. if os.path.isdir(target):
  69.     dirmake()
  70. else:
  71.     filemake()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement