Advertisement
Vlad-00003

Nasm automatic compiler

Apr 16th, 2015
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.88 KB | None | 0 0
  1. import os
  2. import subprocess
  3. import fnmatch
  4.  
  5. def prog(sourcesPath):
  6.     global pathed
  7.     if nasm_check():
  8.         if os.path.isdir(sourcesPath):
  9.             print("Folder founded, compiling")
  10.             asm_compiller(sourcesPath)
  11.         else:
  12.             print('"sources" folder not found. Please specify your asm files dir: ')
  13.             userdir = input()
  14.             if os.path.isdir(userdir):
  15.                 print('User folder founded, compiling')
  16.                 pathed = userdir
  17.                 asm_compiller(userdir)
  18.             else:
  19.                 print('Folder not found')
  20.     else: print('nasm not found')
  21.  
  22.  
  23. def asm_compiller(path):
  24.     global errors
  25.     global repeated
  26.     for top, dirs, files in os.walk(path):
  27.         for nm in files:
  28.             if fnmatch.fnmatch(os.path.basename(os.path.join(top, nm)),'*.asm'):
  29.                 write_to_log('Comping ' + os.path.join(top, nm))
  30.                 if not nm in repeated:
  31.                     args = ['nasm', '-f', 'bin', os.path.join(top, nm), '-o', nm[:-3]+'com']
  32.                 else:
  33.                     args = ['nasm', '-f', 'bin', os.path.join(top, nm), '-o', nm[:-4]+'({}).com'.format(str(repeated.count(nm)))]
  34.                 p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  35.                 err = p.communicate()[0].decode("utf-8")
  36.                 if err:
  37.                     errors = errors + err + '\n'
  38.                     write_to_log(err)
  39.                 else:
  40.                     write_to_log('No errors')
  41.                 repeated.append(nm)
  42.            
  43. def nasm_check():
  44.     files = os.listdir(os.getcwd())
  45.     for i in files:
  46.         if i == 'nasm.exe':
  47.             return True
  48.     return False
  49.    
  50. def make_log():
  51.     f = open('compile.log','w')
  52.     f.write('')
  53.     f.close()
  54.  
  55. def write_to_log(txt):
  56.     f = open('compile.log','a')
  57.     f.write(txt+'\n')
  58.     f.close()
  59.    
  60. pathed = os.getcwd() + '\\sources'
  61. repeated=[]
  62. while True:
  63.     make_log()
  64.     repeated=[]
  65.     errors ='Error list:\n'
  66.     prog(pathed)
  67.     write_to_log('Totally compiled {} files.'.format(len(repeated)))
  68.     errors = errors + 'Check compile.log for more'
  69.     print (errors)
  70.     print('Again? (Enter to repeat, anything to quit)')
  71.     inp=input()
  72.     if inp: break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement