def test_strip_common_prefix_symlink(self): # Create tar file for testing os.makedirs(os.path.join('src', 'test_prefix')) file_to_tar = os.path.join('src', 'test_prefix', 'test.txt') open(file_to_tar, 'w').close() file_to_link = os.path.join('src', 'test_prefix', 'link.txt') os.symlink("./test.txt", file_to_link) self.assertTrue(os.path.islink(file_to_link)) def check_for_symlink(tarinfo): self.assertTrue(tarinfo.issym()) self.assertEqual(file_to_link, tarinfo.name) self.assertEqual( file_to_tar, os.path.normpath( os.path.join(os.path.dirname(file_to_tar), tarinfo.linkname))) return tarinfo tar = tarfile.open(os.path.join('src', 'test.tar'), 'w') tar.add(file_to_tar) tar.add(file_to_link, filter=check_for_symlink) tar.close() tar_source = sources.Tar(os.path.join('src', 'test.tar'), 'dst') os.mkdir('dst') tar_source.pull() # The 'test_prefix' part of the path should have been removed self.assertTrue(os.path.exists(os.path.join('dst', 'test.txt'))) self.assertTrue(os.path.exists(os.path.join('dst', 'link.txt')))
def _maven_tar(self): if self._maven_tar_handle is None: maven_uri = _MAVEN_URL.format(version=self._maven_version) self._maven_tar_handle = sources.Tar( maven_uri, self._maven_dir, source_checksum=self._maven_checksum ) return self._maven_tar_handle
def build(self): snapcraft.BasePlugin.build(self) mvn_cmd = ['mvn', '-f', 'distributions/openhab/pom.xml', 'package'] if self._use_proxy(): settings_path = os.path.join(self.partdir, 'm2', 'settings.xml') maven._create_settings(settings_path) mvn_cmd += ['-s', settings_path] if platform.machine() == 'armv7l': logger.warning('Setting up zulu jre for maven build') os.environ['JAVA_HOME'] = os.path.join(self.jredir, 'jre') os.environ['PATH'] = os.path.join( self.jredir, 'bin') + os.environ.get('PATH', 'not-set') self.run(mvn_cmd, self.sourcedir) tree = ElementTree.parse( os.path.join(self.sourcedir, 'distributions/openhab/pom.xml')) root = tree.getroot() parent = root.find('{http://maven.apache.org/POM/4.0.0}parent') version = parent.find( '{http://maven.apache.org/POM/4.0.0}version').text dist_package = os.path.join( self.sourcedir, 'distributions/openhab/target/openhab-' + version + '.tar.gz') sources.Tar(dist_package, self.builddir).pull() snapcraft.file_utils.link_or_copy_tree( self.builddir, self.installdir, copy_function=lambda src, dst: dump._link_or_copy( src, dst, self.installdir))
def test_pull_tarball_must_download_to_sourcedir(self, mock_prov): self.useFixture(fixtures.EnvironmentVariable('TERM', self.term)) self.useFixture( fixtures.EnvironmentVariable('no_proxy', 'localhost,127.0.0.1')) server = http.server.HTTPServer(('127.0.0.1', 0), FakeFileHTTPRequestHandler) server_thread = threading.Thread(target=server.serve_forever) self.addCleanup(server_thread.join) self.addCleanup(server.server_close) self.addCleanup(server.shutdown) server_thread.start() plugin_name = 'test_plugin' dest_dir = os.path.join('parts', plugin_name, 'src') os.makedirs(dest_dir) tar_file_name = 'test.tar' source = 'http://{}:{}/{file_name}'.format(*server.server_address, file_name=tar_file_name) tar_source = sources.Tar(source, dest_dir) tar_source.pull() mock_prov.assert_called_once_with(dest_dir) with open(os.path.join(dest_dir, tar_file_name), 'r') as tar_file: self.assertEqual('Test fake compressed file', tar_file.read())
def _ant_tar(self): if self._ant_tar_handle is None: ant_uri = _ANT_URL.format(version=self._ant_version) self._ant_tar_handle = sources.Tar( ant_uri, self._ant_dir, source_checksum=self._ant_checksum ) return self._ant_tar_handle
def test_strip_common_prefix_symlink(self): # Create tar file for testing os.makedirs(os.path.join("src", "test_prefix")) file_to_tar = os.path.join("src", "test_prefix", "test.txt") open(file_to_tar, "w").close() file_to_link = os.path.join("src", "test_prefix", "link.txt") os.symlink("./test.txt", file_to_link) self.assertTrue(os.path.islink(file_to_link)) def check_for_symlink(tarinfo): self.assertTrue(tarinfo.issym()) self.assertThat(file_to_link, Equals(tarinfo.name)) self.assertThat( file_to_tar, Equals( os.path.normpath( os.path.join(os.path.dirname(file_to_tar), tarinfo.linkname))), ) return tarinfo tar = tarfile.open(os.path.join("src", "test.tar"), "w") tar.add(file_to_tar) tar.add(file_to_link, filter=check_for_symlink) tar.close() tar_source = sources.Tar(os.path.join("src", "test.tar"), "dst") os.mkdir("dst") tar_source.pull() # The 'test_prefix' part of the path should have been removed self.assertTrue(os.path.exists(os.path.join("dst", "test.txt"))) self.assertTrue(os.path.exists(os.path.join("dst", "link.txt")))
def test_pull_tarball_must_download_to_sourcedir(self, mock_prov): plugin_name = 'test_plugin' dest_dir = os.path.join('parts', plugin_name, 'src') os.makedirs(dest_dir) tar_file_name = 'test.tar' source = 'http://{}:{}/{file_name}'.format(*self.server.server_address, file_name=tar_file_name) tar_source = sources.Tar(source, dest_dir) tar_source.pull() mock_prov.assert_called_once_with(dest_dir) with open(os.path.join(dest_dir, tar_file_name), 'r') as tar_file: self.assertEqual('Test fake compressed file', tar_file.read())
def test_strip_common_prefix(self): # Create tar file for testing os.makedirs(os.path.join('src', 'test_prefix')) file_to_tar = os.path.join('src', 'test_prefix', 'test.txt') open(file_to_tar, 'w').close() tar = tarfile.open(os.path.join('src', 'test.tar'), 'w') tar.add(file_to_tar) tar.close() tar_source = sources.Tar(os.path.join('src', 'test.tar'), 'dst') os.mkdir('dst') tar_source.pull() # The 'test_prefix' part of the path should have been removed self.assertTrue(os.path.exists(os.path.join('dst', 'test.txt')))
def test_strip_common_prefix(self): # Create tar file for testing os.makedirs(os.path.join("src", "test_prefix")) file_to_tar = os.path.join("src", "test_prefix", "test.txt") open(file_to_tar, "w").close() tar = tarfile.open(os.path.join("src", "test.tar"), "w") tar.add(file_to_tar) tar.close() tar_source = sources.Tar(os.path.join("src", "test.tar"), "dst") os.mkdir("dst") tar_source.pull() # The 'test_prefix' part of the path should have been removed self.assertTrue(os.path.exists(os.path.join("dst", "test.txt")))
def test_pull_twice_downloads_once(self, mock_prov): """If a source checksum is defined, the cache should be tried first.""" source = 'http://{}:{}/{file_name}'.format(*self.server.server_address, file_name='test.tar') expected_checksum = ('sha384/d9da1f5d54432edc8963cd817ceced83f7c6d61d3' '50ad76d1c2f50c4935d11d50211945ca0ecb980c04c98099' '085b0c3') tar_source = sources.Tar(source, self.path, source_checksum=expected_checksum) tar_source.pull() with mock.patch('requests.get', new=mock.Mock(wraps=requests.get)) as download_spy: tar_source.pull() self.assertThat(download_spy.call_count, Equals(0))
def test_pull_twice_downloads_once(self, mock_prov): """If a source checksum is defined, the cache should be tried first.""" source = 'http://{}:{}/{file_name}'.format(*self.server.server_address, file_name='test.tar') expected_checksum = ('sha384/1075c294bb52ea0f71c4349a60b00c110f187ccf1' 'f249ae5d83ae41285f9c4eefbed57271f31e7c0293acca6f' '347f369') tar_source = sources.Tar(source, self.path, source_checksum=expected_checksum) tar_source.pull() with mock.patch( 'snapcraft.sources.Tar.download', new=mock.Mock(wraps=sources.Tar.download)) as download_spy: tar_source.pull() self.assertEqual(download_spy.call_count, 0)
def test_pull_tarball_must_download_to_sourcedir(self, mock_prov): plugin_name = "test_plugin" dest_dir = os.path.join("parts", plugin_name, "src") os.makedirs(dest_dir) tar_file_name = "test.tar" source = "http://{}:{}/{file_name}".format(*self.server.server_address, file_name=tar_file_name) tar_source = sources.Tar(source, dest_dir) tar_source.pull() source_file = os.path.join(dest_dir, tar_file_name) mock_prov.assert_called_once_with(dest_dir, src=source_file, clean_target=False) with open(os.path.join(dest_dir, tar_file_name), "r") as tar_file: self.assertThat(tar_file.read(), Equals("Test fake file"))