aboutsummaryrefslogtreecommitdiffstats
path: root/build/gen_ins_pkg_script.py
blob: 0f756d074e6b8ccdf9005fe1765f30adf947fcc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import yaml, os, sys
from Cheetah.Template import Template

def get_file_list(root, arch):
    files = []

    dirs = os.listdir(os.path.join(root, 'roles'))

    for  dir in dirs:
        var_dir = os.path.join(root, 'roles', dir, 'vars')
        for name in ['main.yml', arch + r'.yml']:
            if os.path.exists(os.path.join(var_dir, name)):
                files.append(os.path.join(var_dir, name))

    return files

def get_packages_name_list(file_list, special_packages):
    package_name_list = []

    for file in file_list:
        datas = yaml.load(open(file))
        for key, value in datas.items():
            if not key.endswith("packages") and not key.endswith("packages_noarch"):
                continue

            if not value:
                continue

            if value in special_packages:
                continue

            if value not in package_name_list:
                package_name_list += value

    return package_name_list

def generate_download_script(root, arch, tmpl, docker_tmpl, default_packages, special_packages, special_packages_dir):
    package_name_list = get_packages_name_list(get_file_list(root, arch), special_packages) if root else []

    tmpl = Template(file=tmpl, searchList={'packages':package_name_list, 'default_packages':default_packages})
    with open('work/repo/install_packages.sh', 'w') as f:
        f.write(tmpl.respond())

    make_script = []
    for i in special_packages:
        name = 'make_' + i + '.sh'
        if os.path.exists(os.path.join('.', arch, name)):
            make_script.append(name)

    searchList = {'dir':os.path.join('.', arch), 'scripts':make_script}
    if os.path.exists(special_packages_dir):
        special_packages_names = [i for i in os.listdir(special_packages_dir) if os.path.isfile(i)]
        searchList.update({'spcial_packages':special_packages_names})

    Dockerfile=os.path.basename(docker_tmpl).split('.')[0]
    tmpl = Template(file=docker_tmpl, searchList=searchList)
    with open(os.path.join('work/repo', Dockerfile), 'w') as f:
        f.write(tmpl.respond())

if __name__=='__main__':
    # generate_download_script('ansible', 'Debian', 'Debian.tmpl')
    generate_download_script(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4],
                             sys.argv[5].split(' '), sys.argv[6].split(' '), sys.argv[7])