/hgconf/uname.py

https://bitbucket.org/sborho/hgconfig · Python · 42 lines · 23 code · 4 blank · 15 comment · 6 complexity · 7d2f128758cc8d1158ff73a23aa3ca41 MD5 · raw file

  1. # Username precommit hook
  2. from mercurial import util
  3. import os
  4. def hook(ui, repo, **args):
  5. """Check if a username is configured.
  6. This function is intended to run as a precommit hook.
  7. [hooks]
  8. precommit.username = python:hgconf.uname.hook
  9. If a username is found to be configured via an hgrc or
  10. the HGUSER environment variable, the commit is allowed
  11. to continue. Else it launches a GUI dialog to allow the
  12. user to specify a username
  13. """
  14. user = os.environ.get("HGUSER") or ui.config("ui", "username")
  15. if user is not None:
  16. return 0
  17. try:
  18. # try Qt version first
  19. import PyQt4.QtGui
  20. from hgconf.uname_qt import UsernameDialogQt
  21. app = PyQt4.QtGui.QApplication([])
  22. dialog = UsernameDialogQt(ui, repo, False)
  23. dialog.show()
  24. app.exec_()
  25. except ImportError:
  26. # Fallback to GTK version
  27. import gtk
  28. from hgconf.uname_gtk import UsernameDialogGtk
  29. ud = UsernameDialogGtk(ui, repo, False)
  30. gtk.main()
  31. user = ui.config("ui", "username")
  32. if user is None:
  33. return 1 # Dialog did not set a username, do not allow commit
  34. else:
  35. return 0