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 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 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 _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')