source: internals/2011/cmce/trunk/gui/uck.py @ 5005

Last change on this file since 5005 was 5005, checked in by goncalo.pinheira, 9 years ago

CMCE: added current version

File size: 5.2 KB
Line 
1import subprocess
2import os
3import shutil
4import sys
5from collections import namedtuple
6
7def set_path():
8    global SCRIPTS_DIR
9    global LIBRARIES_DIR
10
11    if os.path.isdir("uck"):
12        assert os.path.isfile("uck/libraries/gui.sh")
13        SCRIPTS_DIR= "uck"
14        LIBRARIES_DIR= SCRIPTS_DIR+"/libraries"
15    elif os.path.isdir("/usr/lib/uck"):
16        SCRIPTS_DIR= "/usr/bin"
17        LIBRARIES_DIR= "/usr/lib/uck"
18    elif os.path.isdir("/usr/local/lib/uck"):
19        SCRIPTS_DIR= "/usr/local/bin"
20        LIBRARIES_DIR= "/usr/local/lib/uck"
21    else:
22        raise Exception("UCK doesn't seem to be installed")
23
24
25def shell(s, inp=None, assert_returncode=False):
26    shellreturn= namedtuple("shellreturn", "stdout stderr returncode")
27    p= subprocess.Popen(s, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
28    if inp:
29        p.stdin.write(inp)
30    p.stdin.close()
31    o,e,r=  p.stdout.read(), p.stderr.read(), p.wait()
32    if assert_returncode and r!=0:
33        raise Exception("return code != 0 in command: \n\n"+s+"\n\nstdout:\n"+o+"\n\nstderr:\n"+e+"\n\nreturncode:\n"+str(r))
34    return shellreturn(o,e,r)
35
36def available_language_packs():
37    return shell('''apt-cache pkgnames language-support | egrep '^language-support-.{2,7}$' | cut -b 18- | sort''').split("\n")[:-1]
38
39def available_boot_languages():
40    return open(LIBRARIES_DIR+"/langlist").read().split("\n")[:-1]
41
42
43def install_language_packs(languages, desktops=["kde", "gnome"]):
44    all_packs= shell("""apt-cache search "^language-" | sed 's/[        ].*//'""").stdout.split("\n")
45    to_install= []
46    for language in languages:
47        language_packs= filter( lambda x: language in x, all_packs)
48        to_install.extend( filter( lambda x: not "gnome" in x and not "kde" in x, language_packs ) )
49        for desktop in desktops:
50            to_install.extend( filter( lambda x: desktop in x, language_packs ))
51    install_packages( to_install )
52    installed_packs= shell("dpkg-query --show | cut -f1 | grep -E '^(language-pack|language-support)'").stdout.split("\n")
53    hasnt_wanted_language= lambda x: not any([l in x for l in languages])
54    extraneous_packs= filter(hasnt_wanted_language, installed_packs)
55    remove_packages( extraneous_packs )
56
57def install_packages(packages):
58    command=  "apt-get install --assume-yes --force-yes {0}".format(" ".join(packages))
59    shell(command, assert_returncode=True)
60
61def remove_packages(packages):
62    command= "apt-get --purge remove --assume-yes --force-yes {0}".format(" ".join(packages))
63    shell(command, assert_returncode=True)
64
65def check_iso(filename):
66     nl= shell("isoinfo -p -i \"$ISO_IMAGE\" | grep -i CASPER | wc -l").stdout
67     if int(nl)==0:
68         raise Exception("Cannot customize this ISO - unknown iso structure")
69
70def xauth_cookie():
71    return shell("xauth extract - $DISPLAY", assert_returncode=True).stdout
72
73def write_var(var, filename):
74    if isinstance(var, basestring):
75        pass
76    elif type(var)==bool:
77        var= "yes" if var else "no"
78    elif type(var)==list or type(var)==tuple:
79        assert all(map(lambda x: type(x)==str, var))
80        var= " ".join(var)
81    elif var is None:
82        var= ""
83    else:
84        raise Exception("Unrecognized variable type")
85    open(filename, "w").write(var)
86
87def run_customization(remaster_dir, source_iso, remove_win32_files=False, iso_description="Customized live CD", run_graphical_customization=False, language_packs=[], livecd_locales=[], livecd_locale=None, desktop_types=["gnome","kde"], print_output=True):
88    scripts_dir= SCRIPTS_DIR
89    libraries_dir= LIBRARIES_DIR
90    build_dir= remaster_dir+"/customization-scripts"
91
92    assert remaster_dir[-1]!="/"
93    if len(iso_description)>32:
94        raise Exception("ISO description too long (max 32 characters)")
95    if not os.path.exists(remaster_dir):
96        os.mkdir(remaster_dir)
97    else:
98        if os.path.exists(build_dir):
99            print "removing old build dir..."
100            shutil.rmtree(build_dir)
101    print "copying scripts, writing var..."
102    shutil.copytree(libraries_dir+"/customization-profiles/localized_cd/", build_dir )
103
104    write_var(language_packs, build_dir+"/language_packs")
105    write_var(livecd_locales, build_dir+"/livecd_locales")
106    write_var(livecd_locale,  build_dir+"/livecd_locale")
107    write_var(desktop_types,  build_dir+"/desktop_types")
108
109    write_var(run_graphical_customization, build_dir+"/run_manual_customizations")
110    if run_graphical_customization:
111        write_var(xauth_cookie(), build_dir+"/Xcookie")
112
113    write_var(iso_description,    build_dir+"/iso_description")
114    write_var(remove_win32_files, build_dir+"/remove_win32_files")
115    write_var(True,               build_dir+"/clean_desktop_manifest")
116    write_var("export DISPLAY=:0",build_dir+"/environment")
117
118    print "starting UCK build..."
119    command= '''export UCK_USERNAME=$USER ; {scripts_dir}/uck-remaster "{source_iso}" "{build_dir}" "{remaster_dir}"'''.format( **locals() )
120    if print_output:
121        process= subprocess.Popen(command, shell=True, stdout=sys.stdout, stderr=sys.stderr)
122    else:
123        process= subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
124    return process
125    #if tmp.returncode!=0:
126    #    raise Exception("Build failure\n"+tmp.stdout+"\n\n"+tmp.stderr)
127
128
129
130
131set_path()
Note: See TracBrowser for help on using the repository browser.