def test_empty_install(self):
        pkg_dir, dist = self.create_dist(name='foo', version='1.0')
        install_dir = self.mkdtemp()

        install = DummyInstallCmd(dist)
        dist.command_obj['install_dist'] = install

        cmd = install_distinfo(dist)
        dist.command_obj['install_distinfo'] = cmd

        cmd.install_dir = install_dir
        cmd.ensure_finalized()
        cmd.run()

        self.checkLists(os.listdir(install_dir), ['foo-1.0.dist-info'])

        dist_info = os.path.join(install_dir, 'foo-1.0.dist-info')
        self.checkLists(os.listdir(dist_info),
                        ['METADATA', 'RECORD', 'REQUESTED', 'INSTALLER'])
        with open(os.path.join(dist_info, 'INSTALLER')) as fp:
            self.assertEqual(fp.read(), 'distutils')

        with open(os.path.join(dist_info, 'REQUESTED')) as fp:
            self.assertEqual(fp.read(), '')
        meta_path = os.path.join(dist_info, 'METADATA')
        self.assertTrue(Metadata(path=meta_path).check())
    def test_empty_install(self):
        pkg_dir, dist = self.create_dist(name='foo',
                                         version='1.0')
        install_dir = self.mkdtemp()

        install = DummyInstallCmd(dist)
        dist.command_obj['install_dist'] = install

        cmd = install_distinfo(dist)
        dist.command_obj['install_distinfo'] = cmd

        cmd.install_dir = install_dir
        cmd.ensure_finalized()
        cmd.run()

        self.checkLists(os.listdir(install_dir), ['foo-1.0.dist-info'])

        dist_info = os.path.join(install_dir, 'foo-1.0.dist-info')
        self.checkLists(os.listdir(dist_info),
                        ['METADATA', 'RECORD', 'REQUESTED', 'INSTALLER'])
        fp = open(os.path.join(dist_info, 'INSTALLER'))
        try:
            self.assertEqual(fp.read(), 'distutils')
        finally:
            fp.close()

        fp = open(os.path.join(dist_info, 'REQUESTED'))
        try:
            self.assertEqual(fp.read(), '')
        finally:
            fp.close()

        meta_path = os.path.join(dist_info, 'METADATA')
        self.assertTrue(Metadata(path=meta_path).check())
    def test_record(self):
        pkg_dir, dist = self.create_dist(name='foo', version='1.0')
        install_dir = self.mkdtemp()

        install = DummyInstallCmd(dist)
        dist.command_obj['install_dist'] = install

        fake_dists = os.path.join(os.path.dirname(__file__), 'fake_dists')
        fake_dists = os.path.realpath(fake_dists)

        # for testing, we simply add all files from _backport's fake_dists
        dirs = []
        for dir in os.listdir(fake_dists):
            full_path = os.path.join(fake_dists, dir)
            if (not dir.endswith('.egg') or dir.endswith('.egg-info') or
                    dir.endswith('.dist-info')) and os.path.isdir(full_path):
                dirs.append(full_path)

        for dir in dirs:
            for path, subdirs, files in os.walk(dir):
                install.outputs += [os.path.join(path, f) for f in files]
                install.outputs += [
                    os.path.join('path', f + 'c') for f in files
                    if f.endswith('.py')
                ]

        cmd = install_distinfo(dist)
        dist.command_obj['install_distinfo'] = cmd

        cmd.install_dir = install_dir
        cmd.ensure_finalized()
        cmd.run()

        dist_info = os.path.join(install_dir, 'foo-1.0.dist-info')

        expected = []
        for f in install.get_outputs():
            if (f.endswith(('.pyc', '.pyo')) or f == os.path.join(
                    install_dir, 'foo-1.0.dist-info', 'RECORD')):
                expected.append([f, '', ''])
            else:
                size = os.path.getsize(f)
                md5 = hashlib.md5()
                with open(f, 'rb') as fp:
                    md5.update(fp.read())
                hash = md5.hexdigest()
                expected.append([f, hash, str(size)])

        parsed = []
        with open(os.path.join(dist_info, 'RECORD'), 'r') as f:
            reader = csv.reader(f,
                                delimiter=',',
                                lineterminator=os.linesep,
                                quotechar='"')
            parsed = list(reader)

        self.maxDiff = None
        self.checkLists(parsed, expected)
    def test_resources(self):
        install_dir = self.mkdtemp()
        scripts_dir = self.mkdtemp()
        project_dir, dist = self.create_dist(
            name='Spamlib', version='0.1',
            data_files={'spamd': '{scripts}/spamd'})

        os.chdir(project_dir)
        self.write_file('spamd', '# Python script')
        sysconfig._SCHEMES.set(_get_default_scheme(), 'scripts', scripts_dir)
        sys.path.insert(0, install_dir)
        distutils2.database.disable_cache()
        self.addCleanup(sys.path.remove, install_dir)
        self.addCleanup(distutils2.database.enable_cache)

        cmd = install_dist(dist)
        cmd.outputs = ['spamd']
        cmd.install_lib = install_dir
        dist.command_obj['install_dist'] = cmd

        cmd = install_data(dist)
        cmd.install_dir = install_dir
        cmd.ensure_finalized()
        dist.command_obj['install_data'] = cmd
        cmd.run()

        cmd = install_distinfo(dist)
        cmd.ensure_finalized()
        dist.command_obj['install_distinfo'] = cmd
        cmd.run()

        # first a few sanity checks
        self.assertEqual(os.listdir(scripts_dir), ['spamd'])
        self.assertEqual(os.listdir(install_dir), ['Spamlib-0.1.dist-info'])

        # now the real test
        fn = os.path.join(install_dir, 'Spamlib-0.1.dist-info', 'RESOURCES')
        fp = open(fn)
        try:
            content = fp.read().strip()
        finally:
            fp.close()

        expected = 'spamd,%s' % os.path.join(scripts_dir, 'spamd')
        self.assertEqual(content, expected)

        # just to be sure, we also test that get_file works here, even though
        # packaging.database has its own test file
        fp = distutils2.database.get_file('Spamlib', 'spamd')
        try:
            content = fp.read()
        finally:
            fp.close()

        self.assertEqual('# Python script', content)
Ejemplo n.º 5
0
    def test_resources(self):
        install_dir = self.mkdtemp()
        scripts_dir = self.mkdtemp()
        project_dir, dist = self.create_dist(
            name='Spamlib',
            version='0.1',
            data_files={'spamd': '{scripts}/spamd'})

        os.chdir(project_dir)
        self.write_file('spamd', '# Python script')
        sysconfig._SCHEMES.set(_get_default_scheme(), 'scripts', scripts_dir)
        sys.path.insert(0, install_dir)
        distutils2.database.disable_cache()
        self.addCleanup(sys.path.remove, install_dir)
        self.addCleanup(distutils2.database.enable_cache)

        cmd = install_dist(dist)
        cmd.outputs = ['spamd']
        cmd.install_lib = install_dir
        dist.command_obj['install_dist'] = cmd

        cmd = install_data(dist)
        cmd.install_dir = install_dir
        cmd.ensure_finalized()
        dist.command_obj['install_data'] = cmd
        cmd.run()

        cmd = install_distinfo(dist)
        cmd.ensure_finalized()
        dist.command_obj['install_distinfo'] = cmd
        cmd.run()

        # first a few sanity checks
        self.assertEqual(os.listdir(scripts_dir), ['spamd'])
        self.assertEqual(os.listdir(install_dir), ['Spamlib-0.1.dist-info'])

        # now the real test
        fn = os.path.join(install_dir, 'Spamlib-0.1.dist-info', 'RESOURCES')
        with open(fn, encoding='utf-8') as fp:
            content = fp.read().strip()

        expected = 'spamd,%s' % os.path.join(scripts_dir, 'spamd')
        self.assertEqual(content, expected)

        # just to be sure, we also test that get_file works here, even though
        # packaging.database has its own test file
        with distutils2.database.get_file('Spamlib', 'spamd') as fp:
            content = fp.read()

        self.assertEqual('# Python script', content)
    def test_requested(self):
        pkg_dir, dist = self.create_dist(name='foo', version='1.0')
        install_dir = self.mkdtemp()

        install = DummyInstallCmd(dist)
        dist.command_obj['install_dist'] = install

        cmd = install_distinfo(dist)
        dist.command_obj['install_distinfo'] = cmd

        cmd.install_dir = install_dir
        cmd.requested = False
        cmd.ensure_finalized()
        cmd.run()

        dist_info = os.path.join(install_dir, 'foo-1.0.dist-info')
        self.checkLists(os.listdir(dist_info),
                        ['METADATA', 'RECORD', 'INSTALLER'])
    def test_installer(self):
        pkg_dir, dist = self.create_dist(name='foo', version='1.0')
        install_dir = self.mkdtemp()

        install = DummyInstallCmd(dist)
        dist.command_obj['install_dist'] = install

        cmd = install_distinfo(dist)
        dist.command_obj['install_distinfo'] = cmd

        cmd.install_dir = install_dir
        cmd.installer = 'bacon-python'
        cmd.ensure_finalized()
        cmd.run()

        dist_info = os.path.join(install_dir, 'foo-1.0.dist-info')
        with open(os.path.join(dist_info, 'INSTALLER')) as fp:
            self.assertEqual(fp.read(), 'bacon-python')
    def test_no_record(self):
        pkg_dir, dist = self.create_dist(name='foo',
                                         version='1.0')
        install_dir = self.mkdtemp()

        install = DummyInstallCmd(dist)
        dist.command_obj['install_dist'] = install

        cmd = install_distinfo(dist)
        dist.command_obj['install_distinfo'] = cmd

        cmd.install_dir = install_dir
        cmd.no_record = True
        cmd.ensure_finalized()
        cmd.run()

        dist_info = os.path.join(install_dir, 'foo-1.0.dist-info')
        self.checkLists(os.listdir(dist_info),
                        ['METADATA', 'REQUESTED', 'INSTALLER'])
    def test_requested(self):
        pkg_dir, dist = self.create_dist(name='foo',
                                         version='1.0')
        install_dir = self.mkdtemp()

        install = DummyInstallCmd(dist)
        dist.command_obj['install'] = install

        cmd = install_distinfo(dist)
        dist.command_obj['install_distinfo'] = cmd

        cmd.initialize_options()
        cmd.distinfo_dir = install_dir
        cmd.requested = False
        cmd.ensure_finalized()
        cmd.run()

        dist_info = os.path.join(install_dir, 'foo-1.0.dist-info')
        self.checkLists(os.listdir(dist_info),
                        ['METADATA', 'RECORD', 'INSTALLER'])
    def test_installer(self):
        pkg_dir, dist = self.create_dist(name='foo',
                                         version='1.0')
        install_dir = self.mkdtemp()

        install = DummyInstallCmd(dist)
        dist.command_obj['install'] = install

        cmd = install_distinfo(dist)
        dist.command_obj['install_distinfo'] = cmd

        cmd.initialize_options()
        cmd.distinfo_dir = install_dir
        cmd.installer = 'bacon-python'
        cmd.ensure_finalized()
        cmd.run()

        dist_info = os.path.join(install_dir, 'foo-1.0.dist-info')
        self.assertEqual(open(os.path.join(dist_info, 'INSTALLER')).read(),
                         'bacon-python')
    def test_record(self):
        pkg_dir, dist = self.create_dist(name='foo',
                                         version='1.0')
        install_dir = self.mkdtemp()

        install = DummyInstallCmd(dist)
        dist.command_obj['install_dist'] = install

        fake_dists = os.path.join(os.path.dirname(__file__), 'fake_dists')
        fake_dists = os.path.realpath(fake_dists)

        # for testing, we simply add all files from _backport's fake_dists
        dirs = []
        for dir in os.listdir(fake_dists):
            full_path = os.path.join(fake_dists, dir)
            if (not dir.endswith('.egg') or dir.endswith('.egg-info') or
                dir.endswith('.dist-info')) and os.path.isdir(full_path):
                dirs.append(full_path)

        for dir in dirs:
            for path, subdirs, files in os.walk(dir):
                install.outputs += [os.path.join(path, f) for f in files]
                install.outputs += [os.path.join('path', f + 'c')
                                    for f in files if f.endswith('.py')]

        cmd = install_distinfo(dist)
        dist.command_obj['install_distinfo'] = cmd

        cmd.install_dir = install_dir
        cmd.ensure_finalized()
        cmd.run()

        dist_info = os.path.join(install_dir, 'foo-1.0.dist-info')

        expected = []
        for f in install.get_outputs():
            if (f.endswith('.pyc') or f.endswith('.pyo') or f == os.path.join(
                install_dir, 'foo-1.0.dist-info', 'RECORD')):
                expected.append([f, '', ''])
            else:
                size = os.path.getsize(f)
                md5 = hashlib.md5()
                fp = open(f, 'rb')
                try:
                    md5.update(fp.read())
                finally:
                    fp.close()
                hash = md5.hexdigest()
                expected.append([f, hash, str(size)])

        parsed = []
        f = open(os.path.join(dist_info, 'RECORD'), 'r')
        try:
            reader = csv.reader(f, delimiter=',',
                                   lineterminator=os.linesep,
                                   quotechar='"')
            parsed = list(reader)
        finally:
            f.close()

        self.maxDiff = None
        self.checkLists(parsed, expected)
    def test_record_basic(self):
        install_dir = self.mkdtemp()
        modules_dest = os.path.join(install_dir, 'lib')
        scripts_dest = os.path.join(install_dir, 'bin')
        project_dir, dist = self.create_dist(
            name='Spamlib', version='0.1',
            py_modules=['spam'], scripts=['spamd'],
            ext_modules=[Extension('_speedspam', ['_speedspam.c'])])

        # using a real install_dist command is too painful, so we use a mock
        # class that's only a holder for options to be used by install_distinfo
        # and we create placeholder files manually instead of using build_*.
        # the install_* commands will still be consulted by install_distinfo.
        os.chdir(project_dir)
        self.write_file('spam', '# Python module')
        self.write_file('spamd', '# Python script')
        extmod = '_speedspam' + sysconfig.get_config_var('SO')
        self.write_file(extmod, '')

        install = DummyInstallCmd(dist)
        install.outputs = ['spam', 'spamd', extmod]
        install.install_lib = modules_dest
        install.install_scripts = scripts_dest
        dist.command_obj['install_dist'] = install

        cmd = install_distinfo(dist)
        cmd.ensure_finalized()
        dist.command_obj['install_distinfo'] = cmd
        cmd.run()

        # checksum and size are not hard-coded for METADATA as it is
        # platform-dependent (line endings)
        metadata = os.path.join(modules_dest, 'Spamlib-0.1.dist-info',
                                'METADATA')
        fp = open(metadata, 'rb')
        try:
            content = fp.read()
        finally:
            fp.close()

        metadata_size = str(len(content))
        metadata_md5 = hashlib.md5(content).hexdigest()

        record = os.path.join(modules_dest, 'Spamlib-0.1.dist-info', 'RECORD')
        fp = codecs.open(record, encoding='utf-8')
        try:
            content = fp.read()
        finally:
            fp.close()

        found = []
        for line in content.splitlines():
            filename, checksum, size = line.split(',')
            filename = os.path.basename(filename)
            found.append((filename, checksum, size))

        expected = [
            ('spam', '6ab2f288ef2545868effe68757448b45', '15'),
            ('spamd', 'd13e6156ce78919a981e424b2fdcd974', '15'),
            (extmod, 'd41d8cd98f00b204e9800998ecf8427e', '0'),
            ('METADATA', metadata_md5, metadata_size),
            ('INSTALLER', '44e3fde05f3f537ed85831969acf396d', '9'),
            ('REQUESTED', 'd41d8cd98f00b204e9800998ecf8427e', '0'),
            ('RECORD', '', ''),
        ]
        self.assertEqual(found, expected)
    def test_record_basic(self):
        install_dir = self.mkdtemp()
        modules_dest = os.path.join(install_dir, 'lib')
        scripts_dest = os.path.join(install_dir, 'bin')
        project_dir, dist = self.create_dist(
            name='Spamlib',
            version='0.1',
            py_modules=['spam'],
            scripts=['spamd'],
            ext_modules=[Extension('_speedspam', ['_speedspam.c'])])

        # using a real install_dist command is too painful, so we use a mock
        # class that's only a holder for options to be used by install_distinfo
        # and we create placeholder files manually instead of using build_*.
        # the install_* commands will still be consulted by install_distinfo.
        os.chdir(project_dir)
        self.write_file('spam', '# Python module')
        self.write_file('spamd', '# Python script')
        extmod = '_speedspam' + sysconfig.get_config_var('SO')
        self.write_file(extmod, '')

        install = DummyInstallCmd(dist)
        install.outputs = ['spam', 'spamd', extmod]
        install.install_lib = modules_dest
        install.install_scripts = scripts_dest
        dist.command_obj['install_dist'] = install

        cmd = install_distinfo(dist)
        cmd.ensure_finalized()
        dist.command_obj['install_distinfo'] = cmd
        cmd.run()

        # checksum and size are not hard-coded for METADATA as it is
        # platform-dependent (line endings)
        metadata = os.path.join(modules_dest, 'Spamlib-0.1.dist-info',
                                'METADATA')
        with open(metadata, 'rb') as fp:
            content = fp.read()

        metadata_size = str(len(content))
        metadata_md5 = hashlib.md5(content).hexdigest()

        record = os.path.join(modules_dest, 'Spamlib-0.1.dist-info', 'RECORD')
        with open(record, encoding='utf-8') as fp:
            content = fp.read()

        found = []
        for line in content.splitlines():
            filename, checksum, size = line.split(',')
            filename = os.path.basename(filename)
            found.append((filename, checksum, size))

        expected = [
            ('spam', '6ab2f288ef2545868effe68757448b45', '15'),
            ('spamd', 'd13e6156ce78919a981e424b2fdcd974', '15'),
            (extmod, 'd41d8cd98f00b204e9800998ecf8427e', '0'),
            ('METADATA', metadata_md5, metadata_size),
            ('INSTALLER', '44e3fde05f3f537ed85831969acf396d', '9'),
            ('REQUESTED', 'd41d8cd98f00b204e9800998ecf8427e', '0'),
            ('RECORD', '', ''),
        ]
        self.assertEqual(found, expected)