def test_install_two_packages(self): parser = argparse.ArgumentParser() main_.add_argparse_options(parser) with util.temporary_directory(prefix='glyco-install-test-') as tempdir: options = parser.parse_args(['pack', os.path.join(DATA_DIR, 'source_package'), os.path.join(DATA_DIR, 'installed_package'), '--output-dir', tempdir]) self.assertFalse(pack.pack(options)) wheel_paths = [os.path.join(tempdir, whl) for whl in os.listdir(tempdir)] options = parser.parse_args( ['install', wheel_paths[0], wheel_paths[1], '--install-dir', os.path.join(tempdir, 'local')]) install.install(options) self.assertTrue(os.path.isdir( os.path.join(tempdir, 'local', 'source_package'))) # Make sure the directory is not empty. self.assertTrue( len(os.listdir(os.path.join(tempdir, 'local', 'source_package'))) > 0) self.assertTrue(os.path.isdir( os.path.join(tempdir, 'local', 'source_package-0.0.1.dist-info'))) self.assertTrue(os.path.isdir( os.path.join(tempdir, 'local', 'installed_package'))) self.assertTrue( len(os.listdir( os.path.join(tempdir, 'local', 'installed_package'))) > 0) self.assertTrue(os.path.isdir( os.path.join(tempdir, 'local', 'installed_package-0.0.1.dist-info')))
def test_fetch_two_files(self): requester = None with util.temporary_directory(prefix='glyco-fetch-test-') as tempdir: cache = os.path.join(tempdir, 'cache') locations = [ os.path.join( DATA_DIR, 'wheels', 'source_package_valid_sha1-0.0.1-0_' '41128a9c3767ced3ec53b89b297e0268f1338b2b-py2-none-any.whl' ), os.path.join( DATA_DIR, 'wheels', 'installed_package-0.0.1-0_' '58a752f45f35a07c7d94149511b3af04bab11740-py2-none-any.whl' ) ] install_list = [{'location': 'file://' + location, 'location_type': 'file'} for location in locations] paths = install.fetch_packages( install_list, requester=requester, cache=cache, verbose=False) self.assertEqual(len(paths), 2) self.assertEqual(paths[0], locations[0]) self.assertEqual(paths[1], locations[1]) # The file is readily available, nothing should be copied to the cache. self.assertEqual(len(os.listdir(cache)), 0)
def test_reset_timestamps(self): with util.temporary_directory(prefix='glyco-zipfix-') as tempdir: # Create an archive zipname = os.path.join(tempdir, 'testfile.zip') with zipfile.ZipFile(zipname, 'w') as f: f.write(os.path.join(DATA_DIR, 'zipfix_test', 'file1.txt')) f.write(os.path.join(DATA_DIR, 'zipfix_test', 'file2.txt')) # Read original state with zipfile.ZipFile(zipname, 'r') as f: dt_orig = [info.date_time for info in f.infolist()] namelist_orig = f.namelist() namelist_orig.sort() hashes_orig = [hashlib.sha1(f.read(filename)).hexdigest() for filename in namelist_orig] # Reset zipfix.reset_all_timestamps_in_zip(zipname) # Make sure only timestamps have changed. with zipfile.ZipFile(zipname, 'r') as f: dt_new = [info.date_time for info in f.infolist()] namelist_new = f.namelist() namelist_new.sort() hashes_new = [hashlib.sha1(f.read(filename)).hexdigest() for filename in namelist_new] self.assertEqual(namelist_orig, namelist_new) self.assertEqual(hashes_orig, hashes_new) self.assertNotEqual(dt_orig, dt_new) for dt in dt_new: self.assertEqual(dt, (1980, 0, 0, 0, 0, 0))
def test_pack_bare_package(self): with util.Virtualenv(prefix='glyco-pack-test-') as venv: with util.temporary_directory( 'glyco-pack-test-output-') as tempdir: path = pack.pack_bare_package( venv, os.path.join(DATA_DIR, 'installed_package'), tempdir) self.assertTrue(path.startswith(tempdir))
def test_pack_bare_package(self): with util.Virtualenv(prefix='glyco-pack-test-') as venv: with util.temporary_directory('glyco-pack-test-output-') as tempdir: path = pack.pack_bare_package( venv, os.path.join(DATA_DIR, 'installed_package'), tempdir) self.assertTrue(path.startswith(tempdir))
def test_tempdir_no_error(self): with util.temporary_directory() as tempdir: self.assertTrue(os.path.isdir(tempdir)) # This should work. with open(os.path.join(tempdir, 'test_tempdir_no_error.txt'), 'w') as f: f.write('nonsensical content') # And everything should have been cleaned up afterward self.assertFalse(os.path.isdir(tempdir))
def test_pack_unhandled(self): parser = argparse.ArgumentParser() main_.add_argparse_options(parser) with util.temporary_directory('glyco-pack-test-') as tempdir: # DATA_DIR is not a package at all. options = parser.parse_args(['pack', DATA_DIR, '--output-dir', tempdir]) self.assertTrue(pack.pack(options)) self.assertEqual(len(os.listdir(tempdir)), 0)
def test_pack_unhandled(self): parser = argparse.ArgumentParser() main_.add_argparse_options(parser) with util.temporary_directory('glyco-pack-test-') as tempdir: # DATA_DIR is not a package at all. options = parser.parse_args( ['pack', DATA_DIR, '--output-dir', tempdir]) self.assertTrue(pack.pack(options)) self.assertEqual(len(os.listdir(tempdir)), 0)
def test_pack(self): parser = argparse.ArgumentParser() main_.add_argparse_options(parser) with util.temporary_directory(prefix='glyco-pack-test-') as tempdir: options = parser.parse_args(['pack', os.path.join(DATA_DIR, 'source_package'), os.path.join(DATA_DIR, 'installed_package'), '--output-dir', tempdir]) # False is turned into the 0 (success) return code. self.assertFalse(pack.pack(options)) self.assertEqual(len(os.listdir(tempdir)), 2)
def test_setup_virtualenv_relocatable(self): with util.temporary_directory() as tempdir: util.setup_virtualenv(tempdir, relocatable=True) # Use a separate process instead of activating the virtualenv for # test isolation. # Check that modules from the virtualenv are used. output = subprocess.check_output( [get_venv_python_path(tempdir), '-c', 'import wheel; print wheel.__file__']) self.assertTrue(output.startswith(tempdir))
def test_setup_virtualenv_relocatable(self): with util.temporary_directory() as tempdir: util.setup_virtualenv(tempdir, relocatable=True) # Use a separate process instead of activating the virtualenv for # test isolation. # Check that modules from the virtualenv are used. output = subprocess.check_output([ get_venv_python_path(tempdir), '-c', 'import wheel; print wheel.__file__' ]) self.assertTrue(output.startswith(tempdir))
def test_pack_partly_unhandled(self): parser = argparse.ArgumentParser() main_.add_argparse_options(parser) with util.temporary_directory('glyco-pack-test-') as tempdir: options = parser.parse_args(['pack', DATA_DIR, os.path.join(DATA_DIR, 'source_package'), '--output-dir', tempdir]) self.assertTrue(pack.pack(options)) # We should not have generated any wheel. self.assertEqual(len(os.listdir(tempdir)), 0)
def test_install_invalid_hash(self): parser = argparse.ArgumentParser() main_.add_argparse_options(parser) with util.temporary_directory(prefix='glyco-install-test-') as tempdir: options = parser.parse_args([ 'install', os.path.join( DATA_DIR, 'wheels', 'source_package_invalid_sha1-0.0.1-0_' + 'deadbeef3767ced3ec53b89b297e0268f1338b2b-py2-none-any.whl'), '--install-dir', os.path.join(tempdir, 'local')]) result = install.install(options) self.assertNotEqual(result, 0)
def test_pack(self): parser = argparse.ArgumentParser() main_.add_argparse_options(parser) with util.temporary_directory(prefix='glyco-pack-test-') as tempdir: options = parser.parse_args([ 'pack', os.path.join(DATA_DIR, 'source_package'), os.path.join(DATA_DIR, 'installed_package'), '--output-dir', tempdir ]) # False is turned into the 0 (success) return code. self.assertFalse(pack.pack(options)) self.assertEqual(len(os.listdir(tempdir)), 2)
def test_pack_partly_unhandled(self): parser = argparse.ArgumentParser() main_.add_argparse_options(parser) with util.temporary_directory('glyco-pack-test-') as tempdir: options = parser.parse_args([ 'pack', DATA_DIR, os.path.join(DATA_DIR, 'source_package'), '--output-dir', tempdir ]) self.assertTrue(pack.pack(options)) # We should not have generated any wheel. self.assertEqual(len(os.listdir(tempdir)), 0)
def test_fetch_one_http_file_invalid_name(self): requester = FakeHttp() with util.temporary_directory(prefix='glyco-fetch-test-') as tempdir: cache = os.path.join(tempdir, 'cache') location = 'https://a.random.host.com/invalid_name.whl' install_list = [{'location': location, 'location_type': 'http'}] with self.assertRaises(ValueError): install.fetch_packages( install_list, requester=requester, cache=cache, verbose=False) # Still nothing in the cache self.assertEqual(len(os.listdir(cache)), 0)
def test_fetch_one_http_file_invalid_sha1(self): requester = FakeHttp() with util.temporary_directory(prefix='glyco-fetch-test-') as tempdir: cache = os.path.join(tempdir, 'cache') location = ('https://a.random.host.com/' 'source_package_invalid_sha1-0.0.1-0_' 'deadbeef3767ced3ec53b89b297e0268f1338b2b-py2-none-any.whl') install_list = [{'location': location, 'location_type': 'http'}] with self.assertRaises(ValueError): install.fetch_packages( install_list, requester=requester, cache=cache, verbose=False) # An invalid file should be deleted from the cache. self.assertEqual(len(os.listdir(cache)), 0)
def test_fetch_one_http_file_http_error(self): requester = FakeHttp() with util.temporary_directory(prefix='glyco-fetch-test-') as tempdir: cache = os.path.join(tempdir, 'cache') location = ('https://a.broken.host.com/' 'source_package_valid_sha1-0.0.1-0_' '41128a9c3767ced3ec53b89b297e0268f1338b2b-py2-none-any.whl') install_list = [{'location': location, 'location_type': 'http'}] with self.assertRaises(ValueError): install.fetch_packages( install_list, requester=requester, cache=cache, verbose=False) # Still nothing in the cache self.assertEqual(len(os.listdir(cache)), 0)
def test_fetch_one_http_file_good(self): requester = FakeHttp() with util.temporary_directory(prefix='glyco-fetch-test-') as tempdir: cache = os.path.join(tempdir, 'cache') location = ('https://a.random.host.com/' 'source_package_valid_sha1-0.0.1-0_' '41128a9c3767ced3ec53b89b297e0268f1338b2b-py2-none-any.whl') install_list = [{'location': location, 'location_type': 'http'}] paths = install.fetch_packages( install_list, requester=requester, cache=cache, verbose=False) cache_files = [os.path.join(cache, filename) for filename in os.listdir(cache)] self.assertEqual(len(cache_files), 1) self.assertEqual(paths[0], cache_files[0])
def test_tempdir_with_exception(self): try: with util.temporary_directory() as tempdir: self.assertTrue(os.path.isdir(tempdir)) # Create a non-empty file to check that tempdir deletion works. with open(os.path.join(tempdir, 'test_tempdir_no_error.txt'), 'w') as f: f.write('nonsensical content') raise _UtilTestException() except _UtilTestException: pass # this is supposed to happen else: raise AssertionError('No exception was raised') # And everything should have been cleaned up afterward self.assertFalse(os.path.isdir(tempdir))
def test_fetch_one_invalid_file(self): requester = None with util.temporary_directory(prefix='glyco-fetch-test-') as tempdir: cache = os.path.join(tempdir, 'cache') location = os.path.join( DATA_DIR, 'wheels', 'source_package_invalid_sha1-0.0.1-0_' 'deadbeef3767ced3ec53b89b297e0268f1338b2b-py2-none-any.whl' ) install_list = [{'location': 'file://' + location, 'location_type': 'file'}] with self.assertRaises(ValueError): install.fetch_packages( install_list, requester=requester, cache=cache, verbose=False) # The file is readily available, nothing should be copied to the cache. self.assertEqual(len(os.listdir(cache)), 0)
def pack_bare_package(venv, path, wheelhouse, build_num=0, build_options=(), keep_directory=False): """Create a wheel file from an importable package containing a setup.cfg file. Args: path (str): directory containing the package to pack. wheelhouse (str): directory where to place the generated wheel file. build_num (int): rank of the build, only added to the filename. build_options (list of str): values passed as --global-option to pip. Returns: wheel_path (str): path to the generated wheel file. """ path = os.path.abspath(path) print 'Packing %s' % path # Generate a "standard" package with a setup.py file, and call # pack_local_package() cfg_file = os.path.join(path, 'setup.cfg') setup_py_content = get_setup_py_content(cfg_file) package_name = os.path.split(path)[-1] with util.temporary_directory(prefix="glyco-pack-bare-", keep_directory=keep_directory) as tempdir: if keep_directory: print ' Using temporary dir: %s' % tempdir with open(os.path.join(tempdir, 'setup.py'), 'w') as f: f.write(setup_py_content) shutil.copytree(path, os.path.join(tempdir, package_name), symlinks=True) wheel_path = pack_local_package(venv, tempdir, wheelhouse, build_num=build_num, build_options=build_options) return wheel_path
def pack_local_package(venv, path, wheelhouse, build_num=0, build_options=()): """Create a wheel file from package source available locally. Args: path (str): directory containing the package to pack. wheelhouse (str): directory where to place the generated wheel file. build_num (int): rank of the build, only added to the filename. build_options (list of str): values passed as --global-option to pip. Returns: wheel_path (str): path to the generated wheel file. """ print 'Packing %s' % path with util.temporary_directory() as tempdir: args = ['pip', 'wheel', '--no-index', '--no-deps', '--wheel-dir', tempdir] for op in build_options: args += ['--global-option', op] args += [path] venv.check_call(args) wheel_path = grab_wheel(tempdir, wheelhouse, build_num) return wheel_path
def pack_local_package(venv, path, wheelhouse, build_num=0, build_options=()): """Create a wheel file from package source available locally. Args: path (str): directory containing the package to pack. wheelhouse (str): directory where to place the generated wheel file. build_num (int): rank of the build, only added to the filename. build_options (list of str): values passed as --global-option to pip. Returns: wheel_path (str): path to the generated wheel file. """ print 'Packing %s' % path with util.temporary_directory() as tempdir: args = [ 'pip', 'wheel', '--no-index', '--no-deps', '--wheel-dir', tempdir ] for op in build_options: args += ['--global-option', op] args += [path] venv.check_call(args) wheel_path = grab_wheel(tempdir, wheelhouse, build_num) return wheel_path
def test_reset_timestamps(self): with util.temporary_directory(prefix='glyco-zipfix-') as tempdir: # Create an archive zipname = os.path.join(tempdir, 'testfile.zip') with zipfile.ZipFile(zipname, 'w') as f: f.write(os.path.join(DATA_DIR, 'zipfix_test', 'file1.txt')) f.write(os.path.join(DATA_DIR, 'zipfix_test', 'file2.txt')) # Read original state with zipfile.ZipFile(zipname, 'r') as f: dt_orig = [info.date_time for info in f.infolist()] namelist_orig = f.namelist() namelist_orig.sort() hashes_orig = [ hashlib.sha1(f.read(filename)).hexdigest() for filename in namelist_orig ] # Reset zipfix.reset_all_timestamps_in_zip(zipname) # Make sure only timestamps have changed. with zipfile.ZipFile(zipname, 'r') as f: dt_new = [info.date_time for info in f.infolist()] namelist_new = f.namelist() namelist_new.sort() hashes_new = [ hashlib.sha1(f.read(filename)).hexdigest() for filename in namelist_new ] self.assertEqual(namelist_orig, namelist_new) self.assertEqual(hashes_orig, hashes_new) self.assertNotEqual(dt_orig, dt_new) for dt in dt_new: self.assertEqual(dt, (1980, 0, 0, 0, 0, 0))