summaryrefslogtreecommitdiff
path: root/gitlab/config.py
diff options
context:
space:
mode:
authorKay-Uwe (Kiwi) Lorenz <kiwi@moduleworks.com>2021-04-18 11:21:19 +0200
committerNejc Habjan <hab.nejc@gmail.com>2021-04-18 11:59:45 +0200
commit91ffb8e97e213d2f14340b952630875995ecedb2 (patch)
tree86cb2f415cb265ac708d6c6ac152d760c03fe4f3 /gitlab/config.py
parent7a7c9fd932def75a2f2c517482784e445d83881a (diff)
downloadgitlab-91ffb8e97e213d2f14340b952630875995ecedb2.tar.gz
chore(config): allow simple commands without external script
Diffstat (limited to 'gitlab/config.py')
-rw-r--r--gitlab/config.py33
1 files changed, 27 insertions, 6 deletions
diff --git a/gitlab/config.py b/gitlab/config.py
index d9da5b3..c663bf8 100644
--- a/gitlab/config.py
+++ b/gitlab/config.py
@@ -17,9 +17,10 @@
import os
import configparser
+import shlex
import subprocess
from typing import List, Optional, Union
-from os.path import expanduser
+from os.path import expanduser, expandvars
from gitlab.const import USER_AGENT
@@ -56,6 +57,10 @@ class GitlabConfigMissingError(ConfigError):
pass
+class GitlabConfigHelperError(ConfigError):
+ pass
+
+
class GitlabConfigParser(object):
def __init__(
self, gitlab_id: Optional[str] = None, config_files: Optional[List[str]] = None
@@ -202,13 +207,29 @@ class GitlabConfigParser(object):
pass
def _get_values_from_helper(self):
- """Update attributes, which may get values from an external helper program"""
+ """Update attributes that may get values from an external helper program"""
for attr in HELPER_ATTRIBUTES:
value = getattr(self, attr)
if not isinstance(value, str):
continue
- if value.lower().strip().startswith(HELPER_PREFIX):
- helper = expanduser(value[len(HELPER_PREFIX) :].strip())
- value = subprocess.check_output([helper]).decode("utf-8").strip()
- setattr(self, attr, value)
+ if not value.lower().strip().startswith(HELPER_PREFIX):
+ continue
+
+ helper = value[len(HELPER_PREFIX) :].strip()
+ commmand = [expanduser(expandvars(token)) for token in shlex.split(helper)]
+
+ try:
+ value = (
+ subprocess.check_output(commmand, stderr=subprocess.PIPE)
+ .decode("utf-8")
+ .strip()
+ )
+ except subprocess.CalledProcessError as e:
+ stderr = e.stderr.decode().strip()
+ raise GitlabConfigHelperError(
+ f"Failed to read {attr} value from helper "
+ f"for {self.gitlab_id}:\n{stderr}"
+ ) from e
+
+ setattr(self, attr, value)