~brandontschaefer/libertine/enable-maliit

« back to all changes in this revision

Viewing changes to tools/libertine-launch

  • Committer: Stephen M. Webb
  • Date: 2015-07-31 20:45:21 UTC
  • mfrom: (74.1.4 libertine.liblibertine)
  • Revision ID: bregma@users.noreply.github.com-20150731204521-mkvzr9nf5vy617uo
Merge pull request #4 from ubuntu-libertine/libertine.libertine-launch

Libertine.libertine launch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python3
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
# Copyright (C) 2015 Canonical Ltd.
 
5
# Author: Christopher Townsend <christopher.townsend@canonical.com>
 
6
 
 
7
# This program is free software: you can redistribute it and/or modify
 
8
# it under the terms of the GNU General Public License as published by
 
9
# the Free Software Foundation; version 3 of the License.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 
 
19
import os
 
20
import sys
 
21
import json
 
22
import lxc
 
23
import subprocess
 
24
import shlex
 
25
import xdg.BaseDirectory as basedir
 
26
 
 
27
home_path = os.environ['HOME']
 
28
 
 
29
libertine_json_file_path = os.path.join(basedir.xdg_data_home, 'libertine', 'ContainersConfig.json')
 
30
libertine_container_path = basedir.save_cache_path('libertine-container')
 
31
libertine_userdata_path = os.path.join(basedir.xdg_data_home, 'libertine-container', 'user-data')
 
32
 
 
33
def get_container_type(container_id):
 
34
    container_type = ''
 
35
 
 
36
    with open(libertine_json_file_path) as fd:
 
37
        container_list = json.load(fd)
 
38
        fd.close()
 
39
 
 
40
    for container in container_list["containerList"]:
 
41
        if container["id"] == container_id:
 
42
            return container["type"]
 
43
 
 
44
    return ""
 
45
 
 
46
def get_container_path(container_id):
 
47
    return os.path.join(libertine_container_path, container_id)
 
48
 
 
49
def get_userdata_path(container_id):
 
50
    return os.path.join(libertine_userdata_path, container_id)
 
51
 
 
52
def build_proot_command(container_id):
 
53
    proot_cmd = "proot -R " + get_container_path(container_id)
 
54
 
 
55
    bind_mounts = " -b %s:%s" % (get_userdata_path(container_id), home_path)
 
56
 
 
57
    xdg_user_dirs = ['Documents', 'Music', 'Pictures', 'Videos']
 
58
    for user_dir in xdg_user_dirs:
 
59
        user_dir_path = os.path.join(home_path, user_dir)
 
60
        bind_mounts += " -b %s:%s" % (user_dir_path, user_dir_path)
 
61
 
 
62
    proot_cmd += bind_mounts
 
63
    return proot_cmd
 
64
 
 
65
def launch_lxc_application(container_id, app_exec_line):
 
66
    container = lxc.Container(container_id, libertine_container_path)
 
67
 
 
68
    if not container.running:
 
69
        if not container.start():
 
70
            print("Container failed to start")
 
71
            return
 
72
        if not container.wait("RUNNING", 10):
 
73
            print("Container failed to enter the RUNNING state")
 
74
            return
 
75
 
 
76
    if not container.get_ips(timeout=30):
 
77
        print("Not able to connect to the network.")
 
78
        return
 
79
 
 
80
    container.attach_wait(lxc.attach_run_command, app_exec_line)
 
81
 
 
82
def launch_chroot_application(container_id, app_exec_line):
 
83
    proot_cmd = build_proot_command(container_id)
 
84
 
 
85
    args = shlex.split(proot_cmd)
 
86
    args.extend(app_exec_line)
 
87
 
 
88
    subprocess.Popen(args).wait()
 
89
 
 
90
if __name__ == '__main__':
 
91
    container_id = sys.argv[1]
 
92
    app_exec_line = sys.argv[2:]
 
93
    container_type = get_container_type(container_id)
 
94
 
 
95
    if container_type == "lxc":
 
96
        launch_lxc_application(container_id, app_exec_line)
 
97
    elif container_type == "chroot":
 
98
        launch_chroot_application(container_id, app_exec_line)