Пример #1
0
def install_package(name, version, dest_directory, npm_directory):
    if is_recognized_github_uri(version):
        # In the GitHub URI case, the files are already unzipped.
        package_dir = find_dir_for_github_uri(version, npm_directory)
        shutil.copytree(package_dir, dest_directory)
    else:
        available_versions = find_available_versions(name, npm_directory)
        semver_range = version if version != 'latest' else '*'
        matching_versions = find_version_in_range(available_versions, semver_range)
        if not matching_versions:
            raise Exception('No package found for %s@%s' % (name, version))

        # In the common case, package_dir contains two entries:
        # - package.tgz
        # - package/package.json
        package_dir = os.path.join(npm_directory, name, matching_versions[-1])

        # The package.tgz has one root entry, package/, which contains all of the package contents.
        #
        # In practice, I have seen one exception, which is [email protected] (and other versions of defs)
        # where the root folder is named "defs" instead of "package".
        tgz = os.path.join(package_dir, 'package.tgz')
        temp_dir = tempfile.mkdtemp()
        subprocess.check_call(['tar', '-xzf', tgz], cwd=temp_dir)
        tar_folder = os.listdir(temp_dir)[0]
        shutil.move(os.path.join(temp_dir, tar_folder), dest_directory)
        shutil.rmtree(temp_dir)
Пример #2
0
    def write_dependencies(self, output_dir):
        package_to_version_set = {}
        for config in self._package_manager.get_configs():
            src_path = config['packageRootAbsolutePath']
            package_json = os.path.join(src_path, 'package.json')
            self._process_package_json(
                package_json,
                package_to_version_set,
                include_dev_dependencies=config['includeDevDependencies'])

        # Write deps based on package_to_version_set.
        # Leveraging semver from npm makes this fairly straightforward.
        for package, versions in package_to_version_set.items():
            package_dir = os.path.join(self._dot_npm_directory, package)
            if not os.path.isdir(package_dir):
                raise Exception(
                    'ERROR: Could not find directory for package %s at %s' %
                    (package, package_dir))
            available_versions = os.listdir(package_dir)
            for version in versions:
                if is_recognized_github_uri(version):
                    add_github_uri_to_output_dir(version, output_dir, package)
                    continue

                semver_range = version if version != 'latest' else '*'
                matching_versions = find_version_in_range(
                    available_versions, semver_range)
                if not matching_versions:
                    # Note that there are other valid version formats, such as local dependencies
                    # and URL formats that we have not added logic for
                    # (https://docs.npmjs.com/files/package.json#git-urls-as-dependencies).
                    # Currently, we can get away with this because these formats are not used by our
                    # transitive dependencies, so we may have to expand what we support in the
                    # future.
                    raise Exception('No package found for %s@%s' %
                                    (package, version))
                else:
                    # By default, we pick the newest version available.
                    desired_version = matching_versions[-1]

                    if version == 'latest':
                        logging.warn(
                            'Warning: choosing "latest" among what is locally available for %s: (%s).',
                            package, desired_version)
                    src_dir = os.path.join(package_dir, desired_version)
                    dest_dir = os.path.join(output_dir, package,
                                            desired_version)
                    if not os.path.isdir(dest_dir):
                        mkdirs(os.path.dirname(dest_dir))
                        shutil.copytree(src_dir, dest_dir)
Пример #3
0
    def write_dependencies(self, output_dir):
        package_to_version_set = {}
        for config in self._package_manager.get_configs():
            if config.get('excludeFromRelease', False):
                continue

            src_path = config['packageRootAbsolutePath']
            package_json = os.path.join(src_path, 'package.json')
            self._process_package_json(package_json,
                                       package_to_version_set,
                                       include_dev_dependencies=config['includeDevDependencies'])

        # Write deps based on package_to_version_set.
        # Leveraging semver from npm makes this fairly straightforward.
        for package, versions in package_to_version_set.items():
            package_dir = os.path.join(self._dot_npm_directory, package)
            if not os.path.isdir(package_dir):
                raise Exception('ERROR: Could not find directory for package %s at %s' % (package, package_dir))
            available_versions = os.listdir(package_dir)
            for version in versions:
                if is_recognized_github_uri(version):
                    add_github_uri_to_output_dir(version, output_dir, package)
                    continue

                semver_range = version if version != 'latest' else '*'
                matching_versions = find_version_in_range(available_versions, semver_range)
                if not matching_versions:
                    # Note that there are other valid version formats, such as local dependencies
                    # and URL formats that we have not added logic for
                    # (https://docs.npmjs.com/files/package.json#git-urls-as-dependencies).
                    # Currently, we can get away with this because these formats are not used by our
                    # transitive dependencies, so we may have to expand what we support in the
                    # future.
                    raise Exception('No package found for %s@%s' % (package, version))
                else:
                    # By default, we pick the newest version available.
                    desired_version = matching_versions[-1]

                    if version == 'latest':
                        logging.warn(
                            'Warning: choosing "latest" among what is locally available for %s: (%s).',
                            package,
                            desired_version)
                    src_dir = os.path.join(package_dir, desired_version)
                    dest_dir = os.path.join(output_dir, package, desired_version)
                    if not os.path.isdir(dest_dir):
                        mkdirs(os.path.dirname(dest_dir))
                        shutil.copytree(src_dir, dest_dir)