verify-sig.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #! /usr/bin/env python
  2. # encoding: utf-8
  3. # Thomas Nagy, 2014-2015
  4. """
  5. A simple file for verifying signatures in signed waf files
  6. This script is meant for Python >= 2.6 and the encoding is bytes - latin-1
  7. Distributing detached signatures is boring
  8. """
  9. import sys, os, re, subprocess
  10. if __name__ == '__main__':
  11. try:
  12. infile = sys.argv[1]
  13. except IndexError:
  14. infile = 'waf'
  15. try:
  16. outfile1 = sys.argv[2]
  17. except IndexError:
  18. outfile1 = infile + '-sig'
  19. try:
  20. outfile2 = sys.argv[3]
  21. except IndexError:
  22. outfile2 = outfile1 + '.asc'
  23. f1 = open(outfile1, 'wb')
  24. f2 = open(outfile2, 'wb')
  25. f = open(infile, 'rb')
  26. try:
  27. txt = f.read()
  28. lastline = txt.decode('latin-1').splitlines()[-1] # just the last line
  29. if not lastline.startswith('#-----BEGIN PGP SIGNATURE-----'):
  30. print("ERROR: there is no signature to verify in %r :-/" % infile)
  31. sys.exit(1)
  32. sigtext = lastline.replace('\\n', '\n') # convert newlines
  33. sigtext = sigtext[1:] # omit the '# character'
  34. sigtext = sigtext.encode('latin-1') # python3
  35. f2.write(sigtext)
  36. f1.write(txt[:-len(lastline) - 1]) # one newline character was eaten from splitlines()
  37. finally:
  38. f.close()
  39. f1.close()
  40. f2.close()
  41. cmd = 'gpg --verify %s' % outfile2
  42. print("-> %r" % cmd)
  43. ret = subprocess.Popen(cmd, shell=True).wait()
  44. sys.exit(ret)