DebianLinux互動式核心編譯指令碼 001#!/usr/bin/env python3002# -*- coding: UTF-8 -*-003"""004An interactive script for building linux kernel automatically005"""006__author__ = 'M@llon'007__version__ = ''008 009import os010import re011import shutil012import time013 014 015def test(v):016 test.result = v017 return v018 019 020if __name__ == '__main__':021 os.chdir(os.path.dirname(__file__))022 023 source_list = list()024 025 for entry_name in sorted(os.listdir()):026 if os.path.isfile(entry_name) and test(re.match('^(linux-(\d+\.\d+\.\d+))\.tar\.\w+$', entry_name)):027 m = test.result028 source_list.append({029 'file': entry_name,030 'dir': m.group(1),031 'version': m.group(2)032 })033 034 if not len(source_list):035 print('No source archive found.')036 exit(-1)037 038 index_list = list(range(0, len(source_list)))039 040 print('List of available source archives:')041 for i in index_list:042 print(' %d - %s' % (i, source_list[i]['file']))043 044 prompt = 'Choose one %s: ' % str(index_list)045 selected_index = None046 while True:047 try:048 selected_index = int(input(prompt))049 if selected_index in index_list:050 break051 except ValueError:052 pass053 054 selected_source = source_list[selected_index]055 056 extrace_package = None057 if os.path.exists(selected_source['dir']):058 if input('Directory "%s" already exists, remove and re-extract [y/N]? ' % selected_source['dir']).lower() == 'y':059 print('Removing directory "%s" ...' % selected_source['dir'])060 shutil.rmtree(selected_source['dir'])061 extrace_package = True062 else:063 extrace_package = False064 else:065 extrace_package = True066 067 if extrace_package:068 print('Extracting "%s" ...' % selected_source['file'])069 os.system('tar -axf %s' % selected_source['file'])070 071 os.chdir(selected_source['dir'])072 073 create_config = None074 if os.path.exists('.config'):075 if input('File ".config" already exists, re-create it [y/N]? ').lower() == 'y':076 os.remove('.config')077 create_config = True078 else:079 create_config = False080 else:081 create_config = True082 083 if create_config:084 print('Create configuration:')085 print(' 0 - minimal config by detecting only loaded module on current system (make localmodconfig)')086 print(' 1 - copy config from current system, usualy from "/boot/"')087 print(' 2 - default config from source distribution (make defconfig)')088 089 create_method = None090 while True:091 create_method = input('Choose one [0, 1, 2]: ')092 if create_method in ('0', '1', '2'):093 break094 if create_method == '0':095 os.system('make localmodconfig')096 elif create_method == '1':097 uname = os.popen('uname -r').read().split()[0]098 shutil.copyfile('/boot/config-%s' % uname, './.config')099 elif create_method == '2':100 os.system('make defconfig')101 102 print('Edit configuration:')103 print(' 0 - menuconfig (console)')104 print(' 1 - gconfig (gtk)')105 print(' 2 - xconfig (qt)')106 107 edit_method = None108 while True:109 edit_method = input('Choose one [0, 1, 2]: ')110 if edit_method in ('0', '1', '2'):111 break112 if edit_method == '0':113 os.system('make menuconfig')114 elif edit_method == '1':115 os.system('make gconfig')116 elif edit_method == '2':117 os.system('make xconfig')118 119 input('Press any key to start building, Ctrl-C to exit ...')120 121 start_time = time.time()122 123 os.system('make-kpkg clean')124 os.system('fakeroot make-kpkg -j5 --initrd --revision=%s --append-to-version=-amd64 kernel_image kernel_headers' % selected_source['version'])125 126 os.chdir('..')127 128 end_time = time.time()129 130 print('Time used: %.1f minutes' % ((end_time - start_time) / 60,))