{"action":"created","comment":{"url":"https://api.github.com/repos/hkumarmk/python-dbuild/pulls/comments/41359649","id":41359649,"diff_hunk":"@@ -0,0 +1,247 @@\n+#!/usr/bin/env python\n+import argparse\n+import sys\n+import os\n+import shutil\n+from dbuild import exceptions\n+\n+from jinja2 import Environment, FileSystemLoader\n+from docker import Client\n+from tempfile import mkdtemp\n+\n+\n+def docker_client(url='unix://var/run/docker.sock'):\n+    \"\"\" return docker client \"\"\"\n+    return Client(url)\n+\n+\n+def build_image(docker_client, path, tag, nocache=False):\n+    \"\"\" Build docker image\"\"\"\n+    lines = [line.values()[0] for line in docker_client.build(\n+        path=path, rm=True, forcerm=True, tag=tag, decode=True, nocache=nocache)]\n+    if any(isinstance(x, dict) for x in lines):\n+        message = ''\n+        for l in lines:\n+            if isinstance(l, dict):\n+                error = ''.join(l.values())\n+                message += error\n+            else:\n+                message += l\n+\n+        raise exceptions.DbuildDockerBuildFailedException(\n+            '''Docker build failed\n+Error message: %s\n+\n+Full build messages\n+\n+%s''' % (error, message))\n+    else:\n+        return ''.join(lines)\n+\n+\n+def create_container(docker_client, image, name=None, command=None, env=None,\n+                     disable_network=False, shared_volumes=None, cwd=None):\n+    \"\"\" create docker containers \"\"\"\n+    if shared_volumes:\n+        volumes = shared_volumes.values()\n+        binds = ['{}:{}'.format(k, v) for k, v in shared_volumes.iteritems()]\n+        host_config = docker_client.create_host_config(binds=binds)\n+    else:\n+        host_config = None\n+\n+    container = docker_client.create_container(\n+        image=image, name=name, command=command, environment=env,\n+        network_disabled=disable_network, volumes=volumes,\n+        working_dir=cwd, host_config=host_config)\n+    return container\n+\n+\n+def start_container(docker_client, container):\n+    \"\"\" Start docker container \"\"\"\n+    response = docker_client.start(container=container.get('Id'))\n+    return response\n+\n+\n+def wait_container(docker_client, container):\n+    \"\"\" Wait  for the container to finish execution \"\"\"\n+    rv = docker_client.wait(container=container)\n+    return rv\n+\n+\n+def container_logs(docker_client, container):\n+    \"\"\" Get container stdout and stderr \"\"\"\n+    return [log.strip() for log in docker_client.logs(container=container,\n+                                                      stream=True,\n+                                                      timestamps=True)]\n+\n+\n+def remove_container(docker_client, container, force=False):\n+    \"\"\" Remove docker container \"\"\"\n+    return docker_client.remove_container(container=container, force=force)\n+\n+\n+def create_docker_dir(dist, release):\n+    \"\"\"Create docker directory and populate it\"\"\"\n+    PATH = os.path.dirname(os.path.abspath(__file__))\n+    TMPL_ENV = Environment(\n+        autoescape=False,\n+        loader=FileSystemLoader(os.path.join(PATH, 'templates')),\n+        trim_blocks=False)\n+\n+    # Create docker_dir - a temporary directory which will have Dockerfile and\n+    # scripts to build the container.\n+    docker_dir = mkdtemp()\n+    dockerfile = os.path.join(docker_dir, 'Dockerfile')\n+\n+    ctxt = {'dist': dist, 'release': release,\n+            'maintainer': 'dbuild, dbuild@test.com'}\n+\n+    # Write Dockerfile under docker_dir\n+    with open(dockerfile, 'w') as d:\n+        dockerdata = TMPL_ENV.get_template('dockerfile.jinja').render(ctxt)\n+        d.write(dockerdata)\n+\n+    # Copy scripts under docker_dir\n+    shutil.copytree(os.path.join(PATH, 'scripts'),\n+                    os.path.join(docker_dir, 'scripts'))\n+    return docker_dir\n+\n+def docker_build(build_dir, build_type, source_dir='source', force_rm=False,\n+                 docker_url='unix://var/run/docker.sock', dist='ubuntu',\n+                 release='trusty', extra_repos_file='repos',\n+                 extra_repo_keys_file='keys', build_cache=True):\n+    c = docker_client(docker_url)\n+\n+    docker_path = create_docker_dir(dist, release)\n+\n+    print \"Starting %s Package Build\" % build_type\n+    image_tag = 'dbuild-' + dist + '/' + release\n+    response = build_image(c, docker_path, tag=image_tag, nocache=not build_cache)\n+    print response\n+    command=['bash', '-c', '']\n+\n+    if os.path.exists(os.path.join(build_dir, extra_repos_file)):\n+        command[2] += 'cp /build/%s \\\n+        /etc/apt/sources.list.d/dbuild-extra-repos.list && ' % extra_repos_file\n+    else:\n+        raise DbuildBuildFailedException('wrong extra repos file')\n+\n+    if os.path.exists(os.path.join(build_dir, extra_repo_keys_file)):\n+        command[2] += 'apt-key add /build/%s && ' % extra_repo_keys_file\n+    else:\n+        raise DbuildBuildFailedException('wrong extra repo keys file')\n+\n+    command[2] += 'export DEBIAN_FRONTEND=noninteractive; apt-get -y update \\\n+                   && apt-get -y dist-upgrade && '\n+\n+    if build_type == 'source':\n+        command[2] += 'dpkg-buildpackage -S -nc -uc -us'\n+        cwd = '/build/' + source_dir\n+    elif build_type == 'binary':\n+        command[2] += \"dpkg-source -x /build/*.dsc /build/pkgbuild/ && \\\n+                      cd /build/pkgbuild && \\\n+                      /usr/lib/pbuilder/pbuilder-satisfydepends && \\\n+                      dpkg-buildpackage\"\n+        cwd = '/build'\n+    else:\n+        shutil.rmtree(docker_path)\n+        raise exceptions.DbuildBuildFailedException(\n+            'Unknown build_type: %s' % build_type)","path":"dbuild/__init__.py","position":null,"original_position":149,"commit_id":"fcdab7bd0292789ca9e7b0dcbe991a8599ab0435","original_commit_id":"b867d236f7741d8f130d270741575dc4f426e221","user":{"login":"sorenh","id":160090,"avatar_url":"https://avatars.githubusercontent.com/u/160090?v=3","gravatar_id":"","url":"https://api.github.com/users/sorenh","html_url":"https://github.com/sorenh","followers_url":"https://api.github.com/users/sorenh/followers","following_url":"https://api.github.com/users/sorenh/following{/other_user}","gists_url":"https://api.github.com/users/sorenh/gists{/gist_id}","starred_url":"https://api.github.com/users/sorenh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sorenh/subscriptions","organizations_url":"https://api.github.com/users/sorenh/orgs","repos_url":"https://api.github.com/users/sorenh/repos","events_url":"https://api.github.com/users/sorenh/events{/privacy}","received_events_url":"https://api.github.com/users/sorenh/received_events","type":"User","site_admin":false},"body":"I don't understand?","created_at":"2015-10-07T07:34:55Z","updated_at":"2015-10-07T07:34:55Z","html_url":"https://github.com/hkumarmk/python-dbuild/pull/1#discussion_r41359649","pull_request_url":"https://api.github.com/repos/hkumarmk/python-dbuild/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/hkumarmk/python-dbuild/pulls/comments/41359649"},"html":{"href":"https://github.com/hkumarmk/python-dbuild/pull/1#discussion_r41359649"},"pull_request":{"href":"https://api.github.com/repos/hkumarmk/python-dbuild/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/hkumarmk/python-dbuild/pulls/1","id":46284745,"html_url":"https://github.com/hkumarmk/python-dbuild/pull/1","diff_url":"https://github.com/hkumarmk/python-dbuild/pull/1.diff","patch_url":"https://github.com/hkumarmk/python-dbuild/pull/1.patch","issue_url":"https://api.github.com/repos/hkumarmk/python-dbuild/issues/1","number":1,"state":"open","locked":false,"title":"Added the code for dbuild","user":{"login":"hkumarmk","id":6316902,"avatar_url":"https://avatars.githubusercontent.com/u/6316902?v=3","gravatar_id":"","url":"https://api.github.com/users/hkumarmk","html_url":"https://github.com/hkumarmk","followers_url":"https://api.github.com/users/hkumarmk/followers","following_url":"https://api.github.com/users/hkumarmk/following{/other_user}","gists_url":"https://api.github.com/users/hkumarmk/gists{/gist_id}","starred_url":"https://api.github.com/users/hkumarmk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hkumarmk/subscriptions","organizations_url":"https://api.github.com/users/hkumarmk/orgs","repos_url":"https://api.github.com/users/hkumarmk/repos","events_url":"https://api.github.com/users/hkumarmk/events{/privacy}","received_events_url":"https://api.github.com/users/hkumarmk/received_events","type":"User","site_admin":false},"body":"This is a working code which build debian packages in docker container.\r\n\r\nIt expect the source code is downloaded to build directory (in fact in a\r\nsubdirectory of it) and it will use that directory to build the source and\r\nbinary packages. For that, it will build a docker container from base image and\r\ninstall all required build tools, and will run the container with the build\r\ndirectory shared with the host.","created_at":"2015-09-29T14:31:46Z","updated_at":"2015-10-07T07:34:55Z","closed_at":null,"merged_at":null,"merge_commit_sha":"95142e0e1971eecdb18016d011ad3e7e79ba9ba5","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/hkumarmk/python-dbuild/pulls/1/commits","review_comments_url":"https://api.github.com/repos/hkumarmk/python-dbuild/pulls/1/comments","review_comment_url":"https://api.github.com/repos/hkumarmk/python-dbuild/pulls/comments{/number}","comments_url":"https://api.github.com/repos/hkumarmk/python-dbuild/issues/1/comments","statuses_url":"https://api.github.com/repos/hkumarmk/python-dbuild/statuses/fcdab7bd0292789ca9e7b0dcbe991a8599ab0435","head":{"label":"hkumarmk:initial_code","ref":"initial_code","sha":"fcdab7bd0292789ca9e7b0dcbe991a8599ab0435","user":{"login":"hkumarmk","id":6316902,"avatar_url":"https://avatars.githubusercontent.com/u/6316902?v=3","gravatar_id":"","url":"https://api.github.com/users/hkumarmk","html_url":"https://github.com/hkumarmk","followers_url":"https://api.github.com/users/hkumarmk/followers","following_url":"https://api.github.com/users/hkumarmk/following{/other_user}","gists_url":"https://api.github.com/users/hkumarmk/gists{/gist_id}","starred_url":"https://api.github.com/users/hkumarmk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hkumarmk/subscriptions","organizations_url":"https://api.github.com/users/hkumarmk/orgs","repos_url":"https://api.github.com/users/hkumarmk/repos","events_url":"https://api.github.com/users/hkumarmk/events{/privacy}","received_events_url":"https://api.github.com/users/hkumarmk/received_events","type":"User","site_admin":false},"repo":{"id":43372320,"name":"python-dbuild","full_name":"hkumarmk/python-dbuild","owner":{"login":"hkumarmk","id":6316902,"avatar_url":"https://avatars.githubusercontent.com/u/6316902?v=3","gravatar_id":"","url":"https://api.github.com/users/hkumarmk","html_url":"https://github.com/hkumarmk","followers_url":"https://api.github.com/users/hkumarmk/followers","following_url":"https://api.github.com/users/hkumarmk/following{/other_user}","gists_url":"https://api.github.com/users/hkumarmk/gists{/gist_id}","starred_url":"https://api.github.com/users/hkumarmk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hkumarmk/subscriptions","organizations_url":"https://api.github.com/users/hkumarmk/orgs","repos_url":"https://api.github.com/users/hkumarmk/repos","events_url":"https://api.github.com/users/hkumarmk/events{/privacy}","received_events_url":"https://api.github.com/users/hkumarmk/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/hkumarmk/python-dbuild","description":"Build debian packages in docker containers","fork":false,"url":"https://api.github.com/repos/hkumarmk/python-dbuild","forks_url":"https://api.github.com/repos/hkumarmk/python-dbuild/forks","keys_url":"https://api.github.com/repos/hkumarmk/python-dbuild/keys{/key_id}","collaborators_url":"https://api.github.com/repos/hkumarmk/python-dbuild/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/hkumarmk/python-dbuild/teams","hooks_url":"https://api.github.com/repos/hkumarmk/python-dbuild/hooks","issue_events_url":"https://api.github.com/repos/hkumarmk/python-dbuild/issues/events{/number}","events_url":"https://api.github.com/repos/hkumarmk/python-dbuild/events","assignees_url":"https://api.github.com/repos/hkumarmk/python-dbuild/assignees{/user}","branches_url":"https://api.github.com/repos/hkumarmk/python-dbuild/branches{/branch}","tags_url":"https://api.github.com/repos/hkumarmk/python-dbuild/tags","blobs_url":"https://api.github.com/repos/hkumarmk/python-dbuild/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/hkumarmk/python-dbuild/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/hkumarmk/python-dbuild/git/refs{/sha}","trees_url":"https://api.github.com/repos/hkumarmk/python-dbuild/git/trees{/sha}","statuses_url":"https://api.github.com/repos/hkumarmk/python-dbuild/statuses/{sha}","languages_url":"https://api.github.com/repos/hkumarmk/python-dbuild/languages","stargazers_url":"https://api.github.com/repos/hkumarmk/python-dbuild/stargazers","contributors_url":"https://api.github.com/repos/hkumarmk/python-dbuild/contributors","subscribers_url":"https://api.github.com/repos/hkumarmk/python-dbuild/subscribers","subscription_url":"https://api.github.com/repos/hkumarmk/python-dbuild/subscription","commits_url":"https://api.github.com/repos/hkumarmk/python-dbuild/commits{/sha}","git_commits_url":"https://api.github.com/repos/hkumarmk/python-dbuild/git/commits{/sha}","comments_url":"https://api.github.com/repos/hkumarmk/python-dbuild/comments{/number}","issue_comment_url":"https://api.github.com/repos/hkumarmk/python-dbuild/issues/comments{/number}","contents_url":"https://api.github.com/repos/hkumarmk/python-dbuild/contents/{+path}","compare_url":"https://api.github.com/repos/hkumarmk/python-dbuild/compare/{base}...{head}","merges_url":"https://api.github.com/repos/hkumarmk/python-dbuild/merges","archive_url":"https://api.github.com/repos/hkumarmk/python-dbuild/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/hkumarmk/python-dbuild/downloads","issues_url":"https://api.github.com/repos/hkumarmk/python-dbuild/issues{/number}","pulls_url":"https://api.github.com/repos/hkumarmk/python-dbuild/pulls{/number}","milestones_url":"https://api.github.com/repos/hkumarmk/python-dbuild/milestones{/number}","notifications_url":"https://api.github.com/repos/hkumarmk/python-dbuild/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/hkumarmk/python-dbuild/labels{/name}","releases_url":"https://api.github.com/repos/hkumarmk/python-dbuild/releases{/id}","created_at":"2015-09-29T14:23:38Z","updated_at":"2015-09-29T14:24:23Z","pushed_at":"2015-10-07T04:04:25Z","git_url":"git://github.com/hkumarmk/python-dbuild.git","ssh_url":"git@github.com:hkumarmk/python-dbuild.git","clone_url":"https://github.com/hkumarmk/python-dbuild.git","svn_url":"https://github.com/hkumarmk/python-dbuild","homepage":null,"size":196,"stargazers_count":0,"watchers_count":0,"language":"Shell","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":0,"default_branch":"master"}},"base":{"label":"hkumarmk:master","ref":"master","sha":"fa872179a21d3b31992b1d98f236d44a689add84","user":{"login":"hkumarmk","id":6316902,"avatar_url":"https://avatars.githubusercontent.com/u/6316902?v=3","gravatar_id":"","url":"https://api.github.com/users/hkumarmk","html_url":"https://github.com/hkumarmk","followers_url":"https://api.github.com/users/hkumarmk/followers","following_url":"https://api.github.com/users/hkumarmk/following{/other_user}","gists_url":"https://api.github.com/users/hkumarmk/gists{/gist_id}","starred_url":"https://api.github.com/users/hkumarmk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hkumarmk/subscriptions","organizations_url":"https://api.github.com/users/hkumarmk/orgs","repos_url":"https://api.github.com/users/hkumarmk/repos","events_url":"https://api.github.com/users/hkumarmk/events{/privacy}","received_events_url":"https://api.github.com/users/hkumarmk/received_events","type":"User","site_admin":false},"repo":{"id":43372320,"name":"python-dbuild","full_name":"hkumarmk/python-dbuild","owner":{"login":"hkumarmk","id":6316902,"avatar_url":"https://avatars.githubusercontent.com/u/6316902?v=3","gravatar_id":"","url":"https://api.github.com/users/hkumarmk","html_url":"https://github.com/hkumarmk","followers_url":"https://api.github.com/users/hkumarmk/followers","following_url":"https://api.github.com/users/hkumarmk/following{/other_user}","gists_url":"https://api.github.com/users/hkumarmk/gists{/gist_id}","starred_url":"https://api.github.com/users/hkumarmk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hkumarmk/subscriptions","organizations_url":"https://api.github.com/users/hkumarmk/orgs","repos_url":"https://api.github.com/users/hkumarmk/repos","events_url":"https://api.github.com/users/hkumarmk/events{/privacy}","received_events_url":"https://api.github.com/users/hkumarmk/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/hkumarmk/python-dbuild","description":"Build debian packages in docker containers","fork":false,"url":"https://api.github.com/repos/hkumarmk/python-dbuild","forks_url":"https://api.github.com/repos/hkumarmk/python-dbuild/forks","keys_url":"https://api.github.com/repos/hkumarmk/python-dbuild/keys{/key_id}","collaborators_url":"https://api.github.com/repos/hkumarmk/python-dbuild/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/hkumarmk/python-dbuild/teams","hooks_url":"https://api.github.com/repos/hkumarmk/python-dbuild/hooks","issue_events_url":"https://api.github.com/repos/hkumarmk/python-dbuild/issues/events{/number}","events_url":"https://api.github.com/repos/hkumarmk/python-dbuild/events","assignees_url":"https://api.github.com/repos/hkumarmk/python-dbuild/assignees{/user}","branches_url":"https://api.github.com/repos/hkumarmk/python-dbuild/branches{/branch}","tags_url":"https://api.github.com/repos/hkumarmk/python-dbuild/tags","blobs_url":"https://api.github.com/repos/hkumarmk/python-dbuild/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/hkumarmk/python-dbuild/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/hkumarmk/python-dbuild/git/refs{/sha}","trees_url":"https://api.github.com/repos/hkumarmk/python-dbuild/git/trees{/sha}","statuses_url":"https://api.github.com/repos/hkumarmk/python-dbuild/statuses/{sha}","languages_url":"https://api.github.com/repos/hkumarmk/python-dbuild/languages","stargazers_url":"https://api.github.com/repos/hkumarmk/python-dbuild/stargazers","contributors_url":"https://api.github.com/repos/hkumarmk/python-dbuild/contributors","subscribers_url":"https://api.github.com/repos/hkumarmk/python-dbuild/subscribers","subscription_url":"https://api.github.com/repos/hkumarmk/python-dbuild/subscription","commits_url":"https://api.github.com/repos/hkumarmk/python-dbuild/commits{/sha}","git_commits_url":"https://api.github.com/repos/hkumarmk/python-dbuild/git/commits{/sha}","comments_url":"https://api.github.com/repos/hkumarmk/python-dbuild/comments{/number}","issue_comment_url":"https://api.github.com/repos/hkumarmk/python-dbuild/issues/comments{/number}","contents_url":"https://api.github.com/repos/hkumarmk/python-dbuild/contents/{+path}","compare_url":"https://api.github.com/repos/hkumarmk/python-dbuild/compare/{base}...{head}","merges_url":"https://api.github.com/repos/hkumarmk/python-dbuild/merges","archive_url":"https://api.github.com/repos/hkumarmk/python-dbuild/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/hkumarmk/python-dbuild/downloads","issues_url":"https://api.github.com/repos/hkumarmk/python-dbuild/issues{/number}","pulls_url":"https://api.github.com/repos/hkumarmk/python-dbuild/pulls{/number}","milestones_url":"https://api.github.com/repos/hkumarmk/python-dbuild/milestones{/number}","notifications_url":"https://api.github.com/repos/hkumarmk/python-dbuild/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/hkumarmk/python-dbuild/labels{/name}","releases_url":"https://api.github.com/repos/hkumarmk/python-dbuild/releases{/id}","created_at":"2015-09-29T14:23:38Z","updated_at":"2015-09-29T14:24:23Z","pushed_at":"2015-10-07T04:04:25Z","git_url":"git://github.com/hkumarmk/python-dbuild.git","ssh_url":"git@github.com:hkumarmk/python-dbuild.git","clone_url":"https://github.com/hkumarmk/python-dbuild.git","svn_url":"https://github.com/hkumarmk/python-dbuild","homepage":null,"size":196,"stargazers_count":0,"watchers_count":0,"language":"Shell","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/hkumarmk/python-dbuild/pulls/1"},"html":{"href":"https://github.com/hkumarmk/python-dbuild/pull/1"},"issue":{"href":"https://api.github.com/repos/hkumarmk/python-dbuild/issues/1"},"comments":{"href":"https://api.github.com/repos/hkumarmk/python-dbuild/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/hkumarmk/python-dbuild/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/hkumarmk/python-dbuild/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/hkumarmk/python-dbuild/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/hkumarmk/python-dbuild/statuses/fcdab7bd0292789ca9e7b0dcbe991a8599ab0435"}}}}