def _finish_build(self): # Fix all shebangs to use the in-snap python. common.replace_in_file(self.rosdir, re.compile(r''), re.compile(r'#!.*python'), r'#!/usr/bin/env python') # Replace the CMAKE_PREFIX_PATH in _setup_util.sh setup_util_file = os.path.join(self.rosdir, '_setup_util.py') if os.path.isfile(setup_util_file): with open(setup_util_file, 'r+') as f: pattern = re.compile(r"CMAKE_PREFIX_PATH = '{}.*".format( self.rosdir)) replaced = pattern.sub('CMAKE_PREFIX_PATH = []', f.read()) f.seek(0) f.truncate() f.write(replaced) # Also replace the python usage in 10.ros.sh to use the in-snap python. ros10_file = os.path.join(self.rosdir, 'etc/catkin/profile.d/10.ros.sh') if os.path.isfile(ros10_file): with open(ros10_file, 'r+') as f: pattern = re.compile(r'/usr/bin/python') replaced = pattern.sub(r'python', f.read()) f.seek(0) f.truncate() f.write(replaced)
def _fix_shebangs(path): """Changes hard coded shebangs for files in _BIN_PATHS to use env.""" paths = [p for p in _BIN_PATHS if os.path.exists(os.path.join(path, p))] for p in [os.path.join(path, p) for p in paths]: common.replace_in_file(p, re.compile(r''), re.compile(r'#!.*python\n'), r'#!/usr/bin/env python\n')
def test_replace_in_file(self): os.makedirs('bin') # Place a few files with bad shebangs, and some files that shouldn't be # changed. files = [ { 'path': os.path.join('bin', '2to3'), 'contents': '#!/foo/bar/baz/python', 'expected': '#!/usr/bin/env python', }, { 'path': os.path.join('bin', 'snapcraft'), 'contents': '#!/foo/baz/python', 'expected': '#!/usr/bin/env python', }, { 'path': os.path.join('bin', 'foo'), 'contents': 'foo', 'expected': 'foo', } ] for file_info in files: with self.subTest(key=file_info['path']): with open(file_info['path'], 'w') as f: f.write(file_info['contents']) common.replace_in_file('bin', re.compile(r''), re.compile(r'#!.*python'), r'#!/usr/bin/env python') with open(file_info['path'], 'r') as f: self.assertEqual(f.read(), file_info['expected'])
def _finish_build(self): # Fix all shebangs to use the in-snap python. common.replace_in_file(self.rosdir, re.compile(r''), re.compile(r'#!.*python'), r'#!/usr/bin/env python') # Replace the CMAKE_PREFIX_PATH in _setup_util.sh setup_util_file = os.path.join(self.rosdir, '_setup_util.py') if os.path.isfile(setup_util_file): with open(setup_util_file, 'r+') as f: pattern = re.compile(r"CMAKE_PREFIX_PATH = '{}.*".format( self.rosdir)) replaced = pattern.sub('CMAKE_PREFIX_PATH = []', f.read()) f.seek(0) f.truncate() f.write(replaced) # Also replace the python usage in 10.ros.sh to use the in-snap python. ros10_file = os.path.join(self.rosdir, 'etc/catkin/profile.d/10.ros.sh') if os.path.isfile(ros10_file): with open(ros10_file, 'r+') as f: pattern = re.compile(r'/usr/bin/python') replaced = pattern.sub(r'python', f.read()) f.seek(0) f.truncate() f.write(replaced)
def build(self): super().build() # If setuptools is used, it tries to create files in the # dist-packages dir and import from there, so it needs to exist # and be in the PYTHONPATH. It's harmless if setuptools isn't # used. setup_file = os.path.join(self.builddir, 'setup.py') if not os.path.exists(setup_file): return os.makedirs(common.get_python2_path(self.installdir), exist_ok=True) self.run([ 'python2', setup_file, 'build_ext', '-I{}'.format(_get_python2_include(self.installdir)), 'install', '--install-layout=deb', '--prefix={}/usr'.format(self.installdir), ], cwd=self.builddir) # Fix all shebangs to use the in-snap python. common.replace_in_file(self.installdir, re.compile(r''), re.compile(r'#!.*python'), r'#!/usr/bin/env python')
def test_replace_in_file(self): os.makedirs('bin') # Place a few files with bad shebangs, and some files that shouldn't be # changed. files = [{ 'path': os.path.join('bin', '2to3'), 'contents': '#!/foo/bar/baz/python', 'expected': '#!/usr/bin/env python', }, { 'path': os.path.join('bin', 'snapcraft'), 'contents': '#!/foo/baz/python', 'expected': '#!/usr/bin/env python', }, { 'path': os.path.join('bin', 'foo'), 'contents': 'foo', 'expected': 'foo', }] for file_info in files: with self.subTest(key=file_info['path']): with open(file_info['path'], 'w') as f: f.write(file_info['contents']) common.replace_in_file('bin', re.compile(r''), re.compile(r'#!.*python'), r'#!/usr/bin/env python') with open(file_info['path'], 'r') as f: self.assertEqual(f.read(), file_info['expected'])
def _prepare_build(self): # Each Catkin package distributes .cmake files so they can be found via # find_package(). However, the Ubuntu packages pulled down as # dependencies contain .cmake files pointing to system paths (e.g. # /usr/lib, /usr/include, etc.). They need to be rewritten to point to # the install directory. def rewrite_paths(match): paths = match.group(1).strip().split(";") for i, path in enumerate(paths): # Rewrite this path if it's an absolute path and not already # within the install directory. if os.path.isabs(path) and not path.startswith(self.installdir): paths[i] = self.installdir + path return '"' + ";".join(paths) + '"' # Looking for any path-like string common.replace_in_file(self.rosdir, re.compile(r".*Config.cmake$"), re.compile(r'"(.*?/.*?)"'), rewrite_paths)
def _prepare_build(self): # Each Catkin package distributes .cmake files so they can be found via # find_package(). However, the Ubuntu packages pulled down as # dependencies contain .cmake files pointing to system paths (e.g. # /usr/lib, /usr/include, etc.). They need to be rewritten to point to # the install directory. def rewrite_paths(match): paths = match.group(1).strip().split(';') for i, path in enumerate(paths): # Rewrite this path if it's an absolute path and not already # within the install directory. if (os.path.isabs(path) and not path.startswith(self.installdir)): paths[i] = self.installdir + path return '"' + ';'.join(paths) + '"' # Looking for any path-like string common.replace_in_file(self.rosdir, re.compile(r'.*Config.cmake$'), re.compile(r'"(.*?/.*?)"'), rewrite_paths)
def build(self): super().build() # If setuptools is used, it tries to create files in the # dist-packages dir and import from there, so it needs to exist # and be in the PYTHONPATH. It's harmless if setuptools isn't # used. setup_file = os.path.join(self.builddir, 'setup.py') if not os.path.exists(setup_file): return os.makedirs(self.dist_packages_dir, exist_ok=True) self.run( ['python3', setup_file, 'install', '--install-layout=deb', '--prefix={}/usr'.format(self.installdir), '--root={}'.format(self.installdir)], cwd=self.builddir) # Fix all shebangs to use the in-snap python. common.replace_in_file(self.installdir, re.compile(r''), re.compile(r'#!.*python'), r'#!/usr/bin/env python')