Example #1
0
    def test_read_dist_egginfo_json_alternative_name_args(self):
        package_json = {"dependencies": {"left-pad": "~1.1.1"}}

        # We will mock up a Distribution object with some fake metadata.
        mock_provider = MockProvider({
            'bower.json': json.dumps(package_json),
        })

        mock_dist = pkg_resources.Distribution(
            metadata=mock_provider, project_name='dummydist', version='0.0.0')

        results = calmjs_dist.read_dist_egginfo_json(
            mock_dist, filename='bower.json')

        self.assertEqual(results, package_json)

        working_set = pkg_resources.WorkingSet()
        working_set.add(mock_dist)

        self.assertEqual(package_json, calmjs_dist.read_egginfo_json(
            'dummydist', filename='bower.json', working_set=working_set))

        # Finally do the flattening
        flattened_json = {
            "dependencies": {"left-pad": "~1.1.1"}, "devDependencies": {}}
        self.assertEqual(flattened_json, calmjs_dist.flatten_dist_egginfo_json(
            [mock_dist], filename='bower.json', working_set=working_set))
        self.assertEqual(flattened_json, calmjs_dist.flatten_egginfo_json(
            ['dummydist'], filename='bower.json', working_set=working_set))
Example #2
0
    def test_read_dist_egginfo_json_alternative_name_args(self):
        package_json = {"dependencies": {"left-pad": "~1.1.1"}}

        # We will mock up a Distribution object with some fake metadata.
        mock_provider = MockProvider({
            'bower.json': json.dumps(package_json),
        })

        mock_dist = pkg_resources.Distribution(metadata=mock_provider,
                                               project_name='dummydist',
                                               version='0.0.0')

        results = calmjs_dist.read_dist_egginfo_json(mock_dist,
                                                     filename='bower.json')

        self.assertEqual(results, package_json)

        working_set = pkg_resources.WorkingSet()
        working_set.add(mock_dist)

        self.assertEqual(
            package_json,
            calmjs_dist.read_egginfo_json('dummydist',
                                          filename='bower.json',
                                          working_set=working_set))

        # Finally do the flattening
        flattened_json = {
            "dependencies": {
                "left-pad": "~1.1.1"
            },
            "devDependencies": {}
        }
        self.assertEqual(
            flattened_json,
            calmjs_dist.flatten_dist_egginfo_json([mock_dist],
                                                  filename='bower.json',
                                                  working_set=working_set))
        self.assertEqual(
            flattened_json,
            calmjs_dist.flatten_egginfo_json(['dummydist'],
                                             filename='bower.json',
                                             working_set=working_set))
Example #3
0
    def tests_flatten_egginfo_json_deps(self):
        # Quiet stdout from distutils logs
        stub_stdouts(self)
        make_dummy_dist(self, (
            ('requires.txt', '\n'.join([])),
            (self.pkgname, 'This is very NOT a package.json.'),
        ), 'security', '9999')

        framework = make_dummy_dist(self, (
            ('requires.txt', '\n'.join([
                'security',
            ])),
            (self.pkgname,
             json.dumps({
                 'name': 'framework',
                 'description': 'some framework',
                 'dependencies': {
                     'left-pad': '~1.1.1',
                 },
                 'devDependencies': {
                     'sinon': '~1.15.0',
                 },
             })),
        ), 'framework', '2.4')

        widget = make_dummy_dist(self, (
            ('requires.txt', '\n'.join([
                'framework>=2.1',
            ])),
            (self.pkgname,
             json.dumps({
                 'dependencies': {
                     'jquery': '~2.0.0',
                     'underscore': '~1.7.0',
                 },
             })),
        ), 'widget', '1.1')

        forms = make_dummy_dist(self, (
            ('requires.txt', '\n'.join([
                'framework>=2.2',
                'widget>=1.0',
            ])),
            (self.pkgname,
             json.dumps({
                 'dependencies': {
                     'backbone': '~1.3.0',
                     'jquery-ui': '~1.12.0',
                 },
             })),
        ), 'forms', '1.6')

        service = make_dummy_dist(self, (
            ('requires.txt', '\n'.join([
                'framework>=2.1',
            ])),
            (self.pkgname,
             json.dumps({
                 'dependencies': {
                     'underscore': '~1.8.0',
                 },
                 'devDependencies': {
                     'sinon': '~1.17.0',
                 },
             })),
        ), 'service', '1.1')

        site = make_dummy_dist(self, (
            ('requires.txt', '\n'.join([
                'framework>=2.1',
                'widget>=1.1',
                'forms>=1.6',
                'service>=1.1',
            ])),
            (self.pkgname,
             json.dumps({
                 'name': 'site',
                 'dependencies': {
                     'underscore': '~1.8.0',
                     'jquery': '~1.9.0',
                 },
             })),
        ), 'site', '2.0')

        answer = {
            'name': 'site',
            'dependencies': {
                'left-pad': '~1.1.1',
                'jquery': '~1.9.0',
                'backbone': '~1.3.0',
                'jquery-ui': '~1.12.0',
                'underscore': '~1.8.0',
            },
            'devDependencies': {
                'sinon': '~1.17.0',
            },
        }

        # WorkingSet is a frozen representation of the versions and
        # locations of all available package presented through sys.path
        # by default.  Here we just emulate it using our temporary path
        # created by our mock package definitions above.

        working_set = pkg_resources.WorkingSet([self._calmjs_testing_tmpdir])

        # Ensure that this works with a raw requirements object, that
        # should normally be automatically resolved from a name.
        result = calmjs_dist.flatten_dist_egginfo_json(
            [framework, widget, forms, service, site], working_set=working_set)
        self.assertEqual(result, answer)

        # Also a raw requirement (package) string on the other function.
        result = calmjs_dist.flatten_egginfo_json(['site'],
                                                  working_set=working_set)
        self.assertEqual(result, answer)
Example #4
0
    def pkg_manager_view(self,
                         package_names,
                         stream=None,
                         explicit=False,
                         **kw):
        """
        Returns the manifest JSON for the Python package name.  Default
        npm implementation calls for package.json.

        If this class is initiated using standard procedures, this will
        mimic the functionality of ``npm view`` but mostly for showing
        the dependencies.  This is done as a default action.

        Arguments:

        package_names
            The names of the python packages with their requirements to
            source the package.json from.
        stream
            If specified, the generated package.json will be written to
            there.
        explicit
            If True, the package names specified are the explicit list
            to search for - no dependency resolution will then be done.

        Returns the manifest json as a dict.
        """

        # For looking up the pkg_name to dist converter for explicit
        to_dists = {
            False: find_packages_requirements_dists,
            True: pkg_names_to_dists,
        }

        # assuming string, and assume whitespaces are invalid.
        pkg_names, malformed = convert_package_names(package_names)
        if malformed:
            msg = 'malformed package name(s) specified: %s' % ', '.join(
                malformed)
            raise ValueError(msg)

        if len(pkg_names) == 1:
            logger.info(
                "generating a flattened '%s' for '%s'",
                self.pkgdef_filename,
                pkg_names[0],
            )
        else:
            logger.info(
                "generating a flattened '%s' for packages {%s}",
                self.pkgdef_filename,
                ', '.join(pkg_names),
            )

        # remember the filename is in the context of the distribution,
        # not the filesystem.
        dists = to_dists[explicit](pkg_names)
        pkgdef_json = flatten_dist_egginfo_json(
            dists,
            filename=self.pkgdef_filename,
            dep_keys=self.dep_keys,
        )

        if pkgdef_json.get(self.pkg_name_field,
                           NotImplemented) is NotImplemented:
            # use the last item.
            pkg_name = Requirement.parse(pkg_names[-1]).project_name
            pkgdef_json[self.pkg_name_field] = pkg_name

        if stream:
            self.dump(pkgdef_json, stream)
            stream.write('\n')

        return pkgdef_json
Example #5
0
File: cli.py Project: calmjs/calmjs
    def pkg_manager_view(
            self, package_names, stream=None, explicit=False, **kw):
        """
        Returns the manifest JSON for the Python package name.  Default
        npm implementation calls for package.json.

        If this class is initiated using standard procedures, this will
        mimic the functionality of ``npm view`` but mostly for showing
        the dependencies.  This is done as a default action.

        Arguments:

        package_names
            The names of the python packages with their requirements to
            source the package.json from.
        stream
            If specified, the generated package.json will be written to
            there.
        explicit
            If True, the package names specified are the explicit list
            to search for - no dependency resolution will then be done.

        Returns the manifest json as a dict.
        """

        # For looking up the pkg_name to dist converter for explicit
        to_dists = {
            False: find_packages_requirements_dists,
            True: pkg_names_to_dists,
        }

        # assuming string, and assume whitespaces are invalid.
        pkg_names, malformed = convert_package_names(package_names)
        if malformed:
            msg = 'malformed package name(s) specified: %s' % ', '.join(
                malformed)
            raise ValueError(msg)

        if len(pkg_names) == 1:
            logger.info(
                "generating a flattened '%s' for '%s'",
                self.pkgdef_filename, pkg_names[0],
            )
        else:
            logger.info(
                "generating a flattened '%s' for packages {%s}",
                self.pkgdef_filename, ', '.join(pkg_names),
            )

        # remember the filename is in the context of the distribution,
        # not the filesystem.
        dists = to_dists[explicit](pkg_names)
        pkgdef_json = flatten_dist_egginfo_json(
            dists, filename=self.pkgdef_filename,
            dep_keys=self.dep_keys,
        )

        if pkgdef_json.get(
                self.pkg_name_field, NotImplemented) is NotImplemented:
            # use the last item.
            pkg_name = Requirement.parse(pkg_names[-1]).project_name
            pkgdef_json[self.pkg_name_field] = pkg_name

        if stream:
            self.dump(pkgdef_json, stream)
            stream.write('\n')

        return pkgdef_json
Example #6
0
    def tests_flatten_egginfo_json_deps(self):
        # Quiet stdout from distutils logs
        stub_stdouts(self)
        make_dummy_dist(self, (
            ('requires.txt', '\n'.join([
            ])),
            (self.pkgname, 'This is very NOT a package.json.'),
        ), 'security', '9999')

        framework = make_dummy_dist(self, (
            ('requires.txt', '\n'.join([
                'security',
            ])),
            (self.pkgname, json.dumps({
                'name': 'framework',
                'description': 'some framework',
                'dependencies': {
                    'left-pad': '~1.1.1',
                },
                'devDependencies': {
                    'sinon': '~1.15.0',
                },
            })),
        ), 'framework', '2.4')

        widget = make_dummy_dist(self, (
            ('requires.txt', '\n'.join([
                'framework>=2.1',
            ])),
            (self.pkgname, json.dumps({
                'dependencies': {
                    'jquery': '~2.0.0',
                    'underscore': '~1.7.0',
                },
            })),
        ), 'widget', '1.1')

        forms = make_dummy_dist(self, (
            ('requires.txt', '\n'.join([
                'framework>=2.2',
                'widget>=1.0',
            ])),
            (self.pkgname, json.dumps({
                'dependencies': {
                    'backbone': '~1.3.0',
                    'jquery-ui': '~1.12.0',
                },
            })),
        ), 'forms', '1.6')

        service = make_dummy_dist(self, (
            ('requires.txt', '\n'.join([
                'framework>=2.1',
            ])),
            (self.pkgname, json.dumps({
                'dependencies': {
                    'underscore': '~1.8.0',
                },
                'devDependencies': {
                    'sinon': '~1.17.0',
                },
            })),
        ), 'service', '1.1')

        site = make_dummy_dist(self, (
            ('requires.txt', '\n'.join([
                'framework>=2.1',
                'widget>=1.1',
                'forms>=1.6',
                'service>=1.1',
            ])),
            (self.pkgname, json.dumps({
                'name': 'site',
                'dependencies': {
                    'underscore': '~1.8.0',
                    'jquery': '~1.9.0',
                },
            })),
        ), 'site', '2.0')

        answer = {
            'name': 'site',
            'dependencies': {
                'left-pad': '~1.1.1',
                'jquery': '~1.9.0',
                'backbone': '~1.3.0',
                'jquery-ui': '~1.12.0',
                'underscore': '~1.8.0',
            },
            'devDependencies': {
                'sinon': '~1.17.0',
            },
        }

        # WorkingSet is a frozen representation of the versions and
        # locations of all available package presented through sys.path
        # by default.  Here we just emulate it using our temporary path
        # created by our mock package definitions above.

        working_set = pkg_resources.WorkingSet([self._calmjs_testing_tmpdir])

        # Ensure that this works with a raw requirements object, that
        # should normally be automatically resolved from a name.
        result = calmjs_dist.flatten_dist_egginfo_json(
            [framework, widget, forms, service, site], working_set=working_set)
        self.assertEqual(result, answer)

        # Also a raw requirement (package) string on the other function.
        result = calmjs_dist.flatten_egginfo_json(
            ['site'], working_set=working_set)
        self.assertEqual(result, answer)