aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/pull/utils.py
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2019-08-12 01:17:07 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2019-08-12 01:17:07 +0300
commite113fb7431ebd18c1bc6f7641ceb376df9699dea (patch)
tree4f8ebdbe8dc328b3bb09981065ad59b11890232a /pull/utils.py
parentrename source files (diff)
downloadcgitize-e113fb7431ebd18c1bc6f7641ceb376df9699dea.tar.gz
cgitize-e113fb7431ebd18c1bc6f7641ceb376df9699dea.zip
split pull/pull.py
Diffstat (limited to '')
-rw-r--r--pull/utils.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/pull/utils.py b/pull/utils.py
new file mode 100644
index 0000000..2ce7cbe
--- /dev/null
+++ b/pull/utils.py
@@ -0,0 +1,39 @@
+import contextlib
+import logging
+import os
+import subprocess
+
+
+def check_output(*args, stdout=subprocess.PIPE, **kwargs):
+ result = subprocess.run(args, stdout=stdout, stderr=subprocess.STDOUT,
+ encoding='utf-8', **kwargs)
+ try:
+ result.check_returncode()
+ if stdout != subprocess.DEVNULL:
+ if result.stdout is None:
+ logging.debug('%s', args)
+ else:
+ logging.debug('%s\n%s', args, result.stdout)
+ return result.returncode == 0, result.stdout
+ except subprocess.CalledProcessError as e:
+ if stdout != subprocess.DEVNULL:
+ logging.error('%s\n%s', e, e.output)
+ return e.returncode == 0, e.output
+
+
+def run(*args, discard_output=False, **kwargs):
+ if discard_output:
+ success, _ = check_output(*args, stdout=subprocess.DEVNULL, **kwargs)
+ else:
+ success, _ = check_output(*args, **kwargs)
+ return success
+
+
+@contextlib.contextmanager
+def chdir(new_cwd):
+ old_cwd = os.getcwd()
+ os.chdir(new_cwd)
+ try:
+ yield
+ finally:
+ os.chdir(old_cwd)