Browse Source

def docker_build()

Fabian Peter Hammerle 5 years ago
parent
commit
73f88b1081
1 changed files with 16 additions and 0 deletions
  1. 16 0
      rc.xsh

+ 16 - 0
rc.xsh

@@ -31,6 +31,8 @@ import datetime as dt
 import os
 import re
 import shutil
+import subprocess
+import sys
 
 # default locale
 # will be used for all non-explicitly set LC_* variables
@@ -98,6 +100,20 @@ def dpkg_which(cmd):
     assert len(matches) == 1
     return matches[0]
 
+def docker_build(dockerfile):
+    p = subprocess.Popen(['sudo', 'docker', 'build', '-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
+    p.stdin.write(dockerfile.encode())
+    p.stdin.close()
+    image_id_regex = re.compile(rb'^Successfully built (\S+)\n$')
+    image_id = None
+    for line in p.stdout:
+        sys.stdout.write(line.decode(sys.stdout.encoding))
+        image_id_match = image_id_regex.search(line)
+        if image_id_match:
+            image_id, = image_id_match.groups()
+    assert not image_id is None
+    return image_id
+
 def locate(*patterns, match_all=True, ignore_case=True):
     params = []
     if match_all: