bash.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. """Most bash-scripting is generated here.
  2. """
  3. BASH = '/bin/bash'
  4. BUG_avoid_Text_file_busy = True
  5. # http://stackoverflow.com/questions/1384398/usr-bin-perl-bad-interpreter-text-file-busy/
  6. def write_sub_script(ofs, script):
  7. # We use shebang + chmod so we can see the sub-script in 'top'.
  8. # In order to avoid '/bin/bash: bad interpreter: Text file busy',
  9. # we 'touch' the sub-script after chmod.
  10. # http://superuser.com/questions/934300/bin-bash-bad-interpreter-text-file-busy-even-though-the-file-editor-closed
  11. ofs.write('#!{}\n'.format(BASH))
  12. ofs.write('set -vex\n')
  13. ofs.write(script)
  14. if BUG_avoid_Text_file_busy:
  15. exe = BASH
  16. else:
  17. # We prefer to run via shebang b/c we want the script-name to appear to 'top',
  18. # but some users have a problem with that, e.g.
  19. # https://github.com/PacificBiosciences/FALCON/issues/269
  20. # Another idea never worked reliably:
  21. # chmod +x {sub_script_bfn}
  22. # touch {sub_script_bfn}
  23. # We are trying to avoid this problem:
  24. # /bin/bash: bad interpreter: Text file busy
  25. exe = ''
  26. return exe
  27. def write_script(script, script_fn, job_done_fn=None):
  28. if job_done_fn:
  29. script += '\ntouch {}\n'.format(job_done_fn)
  30. with open(script_fn, 'w') as ofs:
  31. exe = write_sub_script(ofs, script)