def install_apt(): log = BuildLog() try: with open('dependencies.apt', 'r') as dep_file: for dep in dep_file: run(['sudo', 'apt-get', 'install', dep.strip(), '-y']) except Exception as e: log.exit_nicely('fail running apt-get install dependencies.apt', e)
class DependencyChecker: def __init__(self): self.deps = [] self.log = BuildLog() def add(self, name, title, command, required=True): self.deps.append(dict(name=name, title=title, command=command, required=required, have=False)) def check(self): self.log.write('[checking dependencies]', show_on_screen=True) for dep in self.deps: try: dep['have'] = False self.log.write('running {} - command {}'.format(dep['name'], dep['command'])) run(dep['command']) dep['have'] = True except: pass should_exit = False for dep in self.deps: self.log.write('[have {}? {}]'.format(dep['title'], 'yes' if dep['have'] else 'no'), show_on_screen=True) for dep in self.deps: if not dep['have'] and dep['required']: should_exit = True break if should_exit: self.log.exit_nicely( 'a required software is not present: {}' '\nconfigure your envoriment variable path.'.format(dep['title'])) def have(self, name): for d in self.deps: if d.name == name: return d.have return False
def __init__(self, local_dir, venv_dir, branch='3.1.0'): self.branch = branch self.local_dir = local_dir self.venv_dir = venv_dir self.ocv_dir = os.path.join(local_dir, '{}'.format(OPENCV_DIR)) self.ocv_build_dir = os.path.join(local_dir, '{}/{}'.format(BUILD_DIR, OPENCV_DIR)) self.ocv_contrib_dir = os.path.join(local_dir, '{}'.format(OPENCV_CONTRIB_DIR)) self.install_dir = os.path.join(self.local_dir, INSTALL_DIR) self.ffmpeg_dir = os.path.join(local_dir, 'ffmpeg') self.opencv_url = 'https://github.com/Itseez/opencv.git' self.contrib_url = 'https://github.com/Itseez/opencv_contrib.git' self.log = BuildLog() self.ocv_git = GitManager(self.branch, self.opencv_url) self.ocv_contrib_git = GitManager(self.branch, self.contrib_url)
class ExecutionManager: def __init__(self): self.commands = [] self.log = BuildLog() def print_usage(self): print('usage: setup.py <option>\n') print('available options:') for opt in self.commands: print('\t{}\n\t{}\n'.format(opt['param'], opt['description'])) self.log.exit_nicely('program usage error') def add(self, param, callback, description): self.commands.append(dict(param=param, callback=callback, description=description)) def process(self): should_print_usage = True for opt in self.commands: if opt['param'] in sys.argv: should_print_usage = False opt['callback']() break if should_print_usage: self.print_usage()
class GitManager: def __init__(self, branch, url): self.branch = branch self.url = url self.log = BuildLog() def clone_to(self, directory): if os.path.exists(os.path.join(directory, ".git")): self.log.write("GitManager - skiping download - allready have: " + self.url) return True self.log.write("GitManager - getting - branch {} of {} ".format(self.branch, self.url)) try: if not os.path.exists(directory): os.makedirs(directory) current_dir = os.getcwd() os.chdir(directory) # run(['git', 'clone', '-b', self.branch, '--single-branch', self.url, '.']) run(["git", "clone", self.url, "."]) run(["git", "checkout", self.branch]) except: os.chdir(current_dir) self.log.exit_nicely("git clone {} error".format(self.url)) os.chdir(current_dir)
def __init__(self, name='venv'): self.log = BuildLog() self.name = name
class VirtualenvManager: def __init__(self, name='venv'): self.log = BuildLog() self.name = name def get_name(self): return self.name def have_venv(self): return os.path.exists(get_python_path(self.get_name())) def init_venv(self): if self.have_venv(): return self.log.write('initializing python virtual environment {}'.format(self.get_name())) try: if is_windows(): run(['virtualenv', os.path.join(get_root_dir(), self.get_name())]) else: run(['virtualenv', '--python=python3.5', os.path.join(get_root_dir(), self.get_name())]) except Exception as e: self.log.exit_nicely('python virtual environment creation error', e) @staticmethod def in_virtual_env(name='venv'): return os.path.normpath(sys.executable) == os.path.normpath(get_python_path(name)) @classmethod def run_with(cls, params): vman = cls() if VirtualenvManager.in_virtual_env(vman.get_name()): run_params = [get_python_path(vman.get_name()), get_script_path()] + params try: run(run_params) except: vman.log.exit_nicely('failure when running with params {}') else: vman.log.exit_nicely('fail running with {}'.format(str(params))) def enter(self): if VirtualenvManager.in_virtual_env(self.get_name()): for c in ['--level=0', '--level=1']: if c in sys.argv: sys.argv.remove(c) return True params = sys.argv[:] if '--level=0' in params: params.remove('--level=0') params.append('--level=1') elif '--level=1' in params: self.log.exit_nicely('infinite loop while entering into python virtual environment') else: params.append('--level=0') params.append('--first-caller={}'.format(sys.executable)) self.init_venv() run_params = [get_python_path(self.get_name()), get_script_path()] + params try: run(run_params) except: self.log.exit_nicely('failure when running with {}'.format(str(params))) return False
def __init__(self, branch, url): self.branch = branch self.url = url self.log = BuildLog()
def __init__(self): self.commands = [] self.log = BuildLog()
def install_devlibs(): log = BuildLog() try: pip.main(['install', '-r', 'dependencies-dev.txt']) except Exception as e: log.exit_nicely('development libraries installation error', e)
def install_self(): log = BuildLog() try: pip.main(['install', '-e', '.']) except Exception as e: log.exit_nicely('application instalation error', e)
class OpencvCatcher: def __init__(self, local_dir, venv_dir, branch='3.1.0'): self.branch = branch self.local_dir = local_dir self.venv_dir = venv_dir self.ocv_dir = os.path.join(local_dir, '{}'.format(OPENCV_DIR)) self.ocv_build_dir = os.path.join(local_dir, '{}/{}'.format(BUILD_DIR, OPENCV_DIR)) self.ocv_contrib_dir = os.path.join(local_dir, '{}'.format(OPENCV_CONTRIB_DIR)) self.install_dir = os.path.join(self.local_dir, INSTALL_DIR) self.ffmpeg_dir = os.path.join(local_dir, 'ffmpeg') self.opencv_url = 'https://github.com/Itseez/opencv.git' self.contrib_url = 'https://github.com/Itseez/opencv_contrib.git' self.log = BuildLog() self.ocv_git = GitManager(self.branch, self.opencv_url) self.ocv_contrib_git = GitManager(self.branch, self.contrib_url) def copy_file(self, source, target): try: self.log.write('copying {} to {}'.format(source, target)) copy_file(source, target) except Exception as e: self.log.exit_nicely('fail while copying {}'.format(source), e) def check_ffmpeg(self): pass '''lib_exists = os.path.exists(os.path.join(self.ffmpeg_dir, '/usr/lib/libavcodec.a')) self.log.write('have ffmpeg ? {}'.format('yes' if lib_exists else 'no')) if not lib_exists: self.log.write('it will not re-build ffopencv, webm codec will be absent') self.log.write('https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu') self.log.write('compile ffmpeg and put it\'s libs and includes here: {}'.format(self.ffmpeg_dir)) self.log.write('for windows you can get ffmpeg pre-built binaries: https://ffmpeg.zeranoe.com/builds/') self.log.exit_nicely('you dont have ffmpeg libraries and/or includes')''' def compile_ffopencv(self): if not is_windows(): return self.check_ffmpeg() ffmpeg_idir = os.path.join(self.ffmpeg_dir, 'include') ffmpeg_ldir = os.path.join(self.ffmpeg_dir, 'lib') opencv_sdir = os.path.join(self.local_dir, '{}/modules/highgui/include'.format(OPENCV_DIR)) opencv_cap_dir = os.path.join(self.local_dir, '{}/modules/videoio/src'.format(OPENCV_DIR)) ffopencv_src = os.path.join(self.local_dir, '{}/3rdparty/ffmpeg/ffopencv.c'.format(OPENCV_DIR)) ffopencv_out = os.path.join(self.install_dir, 'opencv_ffmpeg.dll') if not os.path.exists(self.install_dir): os.makedirs(self.install_dir) build_command = [ 'gcc', '-Wall', '-shared', '-o', ffopencv_out, '-O2', '-x', 'c++', '-I{}'.format(opencv_cap_dir), '-I{}'.format(ffmpeg_idir), '-I{}'.format(opencv_sdir), ffopencv_src, '-L{}'.format(ffmpeg_ldir), '-lavformat.dll', '-lavcodec.dll', '-lavdevice.dll', '-lswscale.dll', '-lavutil.dll' ] try: run(build_command) except Exception as e: self.log.exit_nicely('fail compiling opencv_ffmpeg.dll comando {}'.format(str(build_command)), e) def configure_opencv(self): self.check_ffmpeg() if os.path.exists(self.ocv_build_dir): self.log.write( 'the opencv was already configured.' + ' if you want to reconfigure it remove the directory {}'.format(self.ocv_build_dir)) return self.log.write('Configuring opencv') try: os.makedirs(self.ocv_build_dir) current_dir = os.getcwd() os.chdir(self.ocv_build_dir) cmake_params = [ 'cmake', '-D', 'CMAKE_BUILD_TYPE=RELEASE', '-D', 'CMAKE_INSTALL_PREFIX={}'.format(self.ocv_build_dir), '-D', 'INSTALL_C_EXAMPLES=OFF', '-D', 'INSTALL_PYTHON_EXAMPLES=OFF', '-D', 'BUILD_EXAMPLES=OFF', '-D', 'USE_FFMPEG=ON', '-D', 'BUILD_opencv_python2=OFF', '-D', 'BUILD_opencv_python3=ON', '-D', 'OPENCV_EXTRA_MODULES_PATH={}/modules'.format(self.ocv_contrib_dir), '-D', 'PYTHON3_EXECUTABLE={}'.format(os.path.join(get_first_caller_dir(), 'python3.5')), '-D', 'FFMPEG_INCLUDE_DIR={}'.format(os.path.join(self.ffmpeg_dir, 'include')) ] if is_windows(): cmake_params += [ '-D', 'WITH_IPP=OFF', '-D', 'BUILD_opencv_line_descriptor=OFF', '-D', 'BUILD_opencv_datasets=OFF', '-D', 'WITH_DSHOW=OFF', '-D', 'BUILD_opencv_python2=OFF', '-D', 'PYTHON_INCLUDE_DIR={}'.format(get_python_include_dir()), '-D', 'PYTHON_LIBRARY={}'.format(os.path.join(get_python_libs_dir(), 'libpython35.a')) ] cmake_params += ['-D', 'CMAKE_SH='] cmake_params += ['-G', 'MinGW Makefiles'] cmake_params = cmake_params + [self.ocv_dir] run(cmake_params) os.chdir(current_dir) except Exception as e: print(str(e)) self.log.exit_nicely('falha configurando opencv.', e) def compile_opencv(self): self.log.write('compiling opencv') opencv_path = os.path.join(self.ocv_build_dir, 'lib/libopencv_core.so.3.1.0') if os.path.exists(opencv_path): self.log.write( 'the opencv was already compiled.' + ' if you want re-compile it remove the directory {}'.format(self.ocv_build_dir), True ) return try: current_dir = os.getcwd() os.chdir(self.ocv_build_dir) # run(['cmake', '--build', '.', '--config', 'Release', '--target', 'install']) run(['make']) os.chdir(current_dir) except Exception as e: self.log.exit_nicely('falha ao compilar opencv', e) def copy_compiled_files(self): files = [] source_dir = os.path.join(self.ocv_build_dir, "lib/") for root, dirs, lib_files in os.walk(source_dir): for f in lib_files: if f.startswith('libopencv_') and ( f.endswith('.so.3.1') or f.endswith('.so.3.1.0') or f.endswith('.so') ): files.append(f) try: for f in files: self.copy_file(os.path.join(self.ocv_build_dir, "lib/" + f), os.path.join(self.venv_dir, 'bin/' + f)) self.copy_file(os.path.join(self.ocv_build_dir, "lib/python3.5/dist-packages/cv2.cpython-35m-x86_64-linux-gnu.so"), os.path.join(self.venv_dir, 'lib/python3.5/cv2.cpython-35m-x86_64-linux-gnu.so')) except Exception as e: self.log.exit_nicely('fail copying opencv files', e) def install_numpy(self): try: pip.main(['install', 'numpy']) except Exception as E: return self.log.exit_nicely('fail installing numpy', e) def install(self): self.install_numpy() self.log.write('installing opencv {}'.format(self.branch)) self.ocv_git.clone_to(os.path.join(self.local_dir, OPENCV_DIR)) ocv_contrib = GitManager(self.branch, self.contrib_url) ocv_contrib.clone_to(os.path.join(self.local_dir, OPENCV_CONTRIB_DIR)) self.configure_opencv() self.compile_opencv() self.compile_ffopencv() self.copy_compiled_files()
def __init__(self): self.deps = [] self.log = BuildLog()