|
13 | 13 | import xml.etree.ElementTree as ElementTree
|
14 | 14 | import zipfile
|
15 | 15 | from os.path import exists, isdir, join
|
16 |
| -from subprocess import PIPE, CalledProcessError, Popen |
| 16 | +from subprocess import PIPE, CalledProcessError, Popen, check_output |
17 | 17 | from urllib.parse import urlparse
|
18 | 18 |
|
19 | 19 | from quark.utils import DirectoryContext as cd
|
@@ -826,7 +826,36 @@ def _gitlab_setup(self, url):
|
826 | 826 | self.gitlab_token = os.environ[var]
|
827 | 827 | return
|
828 | 828 |
|
829 |
| - raise QuarkError("Missing authentication token. Please set one of the following environment variables: %s" % (", ".join(env_vars))) |
| 829 | + # Try to retrieve the token from the system's keyring if the "keyring" package |
| 830 | + # is available. The CLI is used instead of the "keyring" module to avoid |
| 831 | + # PYTHONPATH, virtualenv, and import issues that could cause unexpected |
| 832 | + # failures. |
| 833 | + if shutil.which("keyring"): |
| 834 | + try: |
| 835 | + self.gitlab_token = check_output(["keyring", "get", "comelz/quark", "gitlab-token"]).decode("utf-8").strip() # fmt: skip |
| 836 | + return |
| 837 | + except CalledProcessError: |
| 838 | + pass |
| 839 | + |
| 840 | + msg = """ |
| 841 | +Missing GitLab authentication token. |
| 842 | +
|
| 843 | +Please set one of the following environment variables: %s. |
| 844 | +
|
| 845 | +You can also install the Python "keyring" package and store the token inside the |
| 846 | +system's keyring like so: |
| 847 | +
|
| 848 | + keyring set comelz/quark gitlab-token |
| 849 | +
|
| 850 | +Then enter the token when prompted. |
| 851 | +
|
| 852 | +The keyring package is available: |
| 853 | +- On Debian / Ubuntu: python3-keyring |
| 854 | +- On macOS: brew install keyring |
| 855 | +- On PyPI: https://pypi.org/project/keyring/ |
| 856 | +""" % (", ".join(env_vars)) |
| 857 | + |
| 858 | + raise QuarkError(msg) |
830 | 859 |
|
831 | 860 | def _parse_url(self, url):
|
832 | 861 | fragments = Subproject._parse_fragment(url) if url.fragment else {}
|
|
0 commit comments