Created
November 19, 2010 07:51
-
-
Save zdxerr/706238 to your computer and use it in GitHub Desktop.
Simple build, pack and test script in Python.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import shutil | |
| import subprocess | |
| from time import sleep | |
| class ExecutionError(Exception): | |
| """ Raised by run() if command returns non-zero exit code. """ | |
| pass | |
| def run(args, dir=None, input=None, silent=True, shell=False): | |
| cmd = subprocess.list2cmdline(args) | |
| if dir: | |
| os.chdir(dir) | |
| stdin = subprocess.PIPE if input else None | |
| stdout = subprocess.PIPE if silent else None | |
| stderr = subprocess.STDOUT | |
| if not silent: | |
| print 'build.py: ' + 'run >> ' + cmd | |
| p = subprocess.Popen(cmd, shell=shell, | |
| stdin=stdin, stdout=stdout, stderr=stderr) | |
| output, error = p.communicate(input) | |
| status = p.wait() | |
| if status: | |
| print output | |
| raise ExecutionError('build.py: run >> %r returned %d' % (cmd, status), | |
| output, status) | |
| return (status, | |
| output if output else '', | |
| error if error else '') | |
| compiler = u'gcc' | |
| project_folder = u'E:\profiler' | |
| source_folder = u'src' | |
| target_folder = u'bin' | |
| temp_folder = u'tmp' | |
| sources = ['circular_buffer', | |
| 'elogread', | |
| 'toolbar', | |
| 'statusbar', | |
| 'display', | |
| 'timeline', | |
| 'plot', | |
| 'boxplot2', | |
| 'view', | |
| 'profiler', | |
| 'clib_fct', | |
| 'getwb'] | |
| statics = ['icons'] | |
| target = 'Profiler.exe' | |
| def compile_flags(): | |
| ret, out, err = run(['pkg-config', '--cflags', 'gtk+-2.0']) | |
| gtk_compile_flags = out.strip().split(' ') | |
| clib_compile_flags = ['-ID:/dSPACE/CLib', | |
| '-ID:/dSPACE/DS1005/RTLib'] | |
| compile_flags = ['-Wall', '-O2'] + gtk_compile_flags + clib_compile_flags | |
| return compile_flags | |
| def link_flags(): | |
| ret, out, err = run(['pkg-config', '--libs', 'gtk+-2.0']) | |
| gtk_link_flags = out.strip().split(' ') | |
| link_flags = gtk_link_flags | |
| return link_flags | |
| #file extensions | |
| SOURCE_FILE_TYPE = 'c' | |
| HEADER_FILE_TYPE = 'h' | |
| OBJECT_FILE_TYPE = 'o' | |
| os.chdir(project_folder) | |
| cwd = os.getcwdu() | |
| # check sources | |
| source_files = [] | |
| header_files = [] | |
| object_files = [] | |
| compile_files = [] | |
| link_files = [] | |
| for source in sources: | |
| try: | |
| source_folder_content = os.listdir(source_folder) | |
| except os.error, err: | |
| print 'build.py: ' + 'Error: while listing directory >> ' + \ | |
| '\'' + source_folder + '\'' | |
| print err | |
| exit(1) | |
| (source_file, header_file) = ('.'.join([source, SOURCE_FILE_TYPE]), | |
| '.'.join([source, HEADER_FILE_TYPE])) | |
| if source_file in source_folder_content: | |
| source_files.append(source_file) | |
| else: | |
| print 'build.py: ' + 'Warning: source file not found >> ' + \ | |
| '\'' + source_file + '\'' | |
| if header_file in source_folder_content: | |
| header_files.append(header_file) | |
| else: | |
| print 'build.py: ' + 'Warning: header file not found >> ' + \ | |
| '\'' + header_file + '\'' | |
| if not source_files: | |
| print 'build.py: ' + 'Error: no source files found' | |
| exit(1) | |
| # create temp folder | |
| if not os.access(temp_folder, os.F_OK): | |
| os.mkdir(temp_folder) | |
| # test if files are already compiled | |
| for source_file in source_files: | |
| os.chdir(source_folder) | |
| source_modified = os.lstat(source_file).st_mtime | |
| os.chdir(cwd) | |
| os.chdir(temp_folder) | |
| object_file = source_file.rstrip(SOURCE_FILE_TYPE) + OBJECT_FILE_TYPE | |
| object_files.append(object_file) | |
| if os.access(object_file, os.F_OK): | |
| object_modified = os.lstat(object_file).st_mtime | |
| if object_modified <= source_modified: | |
| compile_files.append(source_file) | |
| else: | |
| compile_files.append(source_file) | |
| os.chdir(cwd) | |
| #compile | |
| for source_file in compile_files: | |
| object_file = source_file.rstrip(SOURCE_FILE_TYPE) + OBJECT_FILE_TYPE | |
| source_file_path = '/'.join([source_folder, source_file]) | |
| object_file_path = '/'.join([temp_folder, object_file]) | |
| ret, out, err = run([compiler, '-c', source_file_path] + \ | |
| ['-o', object_file_path] + \ | |
| compile_flags(), silent=False) | |
| if not (ret is 0): | |
| print 'build.py: ' + 'Error: compiling failed >> ' + source_file | |
| print out | |
| print err | |
| exit(ret) | |
| # create target folder | |
| if not os.access(target_folder, os.F_OK): | |
| os.mkdir(target_folder) | |
| # test if target is ok | |
| os.chdir(target_folder) | |
| if os.access(target, os.F_OK): | |
| target_modified = os.lstat(target).st_mtime | |
| os.chdir(cwd) | |
| os.chdir(temp_folder) | |
| for object_file in [source + '.o' for source in sources]: | |
| object_modified = os.lstat(object_file).st_mtime | |
| if target_modified <= object_modified: | |
| link_files.append(object_file) | |
| else: | |
| link_files = [source + '.o' for source in sources]; | |
| os.chdir(cwd) | |
| print os.getcwd() | |
| # link | |
| if link_files: | |
| target_path = '/'.join([target_folder, target]) | |
| object_file_paths = ['/'.join([temp_folder, object_file]) | |
| for object_file in object_files] | |
| print [compiler, '-o', target_path] + object_file_paths + link_flags() | |
| run([compiler, '-o', target_path] + object_file_paths + link_flags()) | |
| #check if static files are up to date | |
| # copy static files | |
| for static in statics: | |
| if os.access(static, os.F_OK): | |
| static_target = '/'.join([target_folder, static]) | |
| if os.path.isdir(static): | |
| if os.path.isdir(static_target): | |
| shutil.rmtree(static_target) | |
| shutil.copytree(static, static_target) | |
| else: | |
| shutil.copy2(static, static_target) | |
| else: | |
| print 'build.py: ' + 'Warning: static not found >> ' + static | |
| # run target | |
| (r, o, e) = run([target], dir=target_folder, silent=False, shell=True) | |
| print o, e | |
| def clean(): | |
| print 'build.py: ' + 'Cleaning...' | |
| def erororo(function, path, excinfo): | |
| print 'build.py: ' + 'Warning: Unable to remove >> ' + path | |
| shutil.rmtree(temp_folder, onerror=erororo) | |
| shutil.rmtree(target_folder, onerror=erororo) | |
| #clean() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment