示例#1
0
文件: wheels.py 项目: zainhub/stash
    def handle_install(self, src, dest):
        eptxtp = os.path.join(src, self.distinfo_name, "entry_points.txt")
        if not os.path.exists(eptxtp):
            if self.verbose:
                print("No entry_points.txt found, skipping.")
            return
        parser = configparser.ConfigParser()
        try:
            parser.read(eptxtp)
        except configparser.MissingSectionHeaderError:
            # print message and return
            if self.verbose:
                print("No section headers found in entry_points.txt, passing.")
                return
        if not parser.has_section("console_scripts"):
            if self.verbose:
                print("No console_scripts definition found, skipping.")
            return

        if create_command is None:
            if self.verbose:
                print("Warning: could not import create_command(); skipping.")
            return

        files_installed = []

        mdp = os.path.join(src, self.distinfo_name, "metadata.json")
        if os.path.exists(mdp):
            with open(mdp, "r") as fin:
                desc = json.load(fin).get("summary", "???")
        else:
            desc = "???"

        for command, definition in parser.items(section="console_scripts"):
            # name, loc = definition.replace(" ", "").split("=")
            modname, funcname = definition.split(":")
            if not command.endswith(".py"):
                command += ".py"
            path = create_command(command, (u"""'''%s'''
from %s import %s

if __name__ == "__main__":
    %s()
""" % (desc, modname, funcname, funcname)).encode("utf-8"))
            files_installed.append(path)
        return files_installed
示例#2
0
文件: pip.py 项目: stevewongv/stash
    def _run_setup_file(filename):
        """
        Transform and Run AST of the setup file
        """

        try:
            import pkg_resources
        except ImportError:
            # pkg_resources install may be in progress
            pkg_resources = None

        namespace = {
            '_setup_stub_': _setup_stub_,
            '__file__': filename,
            '__name__': '__main__',
            'setup_args': None,
            'setup_kwargs': None,
        }

        source_folder = os.path.dirname(filename)

        tree = ArchiveFileInstaller._get_cooked_ast(filename)
        codeobj = compile(tree, filename, 'exec')

        # Some setup files import the package to be installed and sometimes opens a file
        # in the source folder. So we modify sys.path and change directory into source folder.
        saved_cwd = os.getcwd()
        saved_sys_path = sys.path[:]
        os.chdir(source_folder)
        sys.path.insert(0, source_folder)
        try:
            exec(codeobj, namespace, namespace)
        finally:
            os.chdir(saved_cwd)
            sys.path = saved_sys_path

        args, kwargs = sys.modules['setuptools']._setup_params_
        # for k in sorted(kwargs.keys()): print('{}: {!r}'.format(k, kwargs[k]))

        if 'ext_modules' in kwargs:
            print('WARNING: Extension modules and skipped: {}'.format(
                kwargs['ext_modules']))

        packages = kwargs['packages'] if 'packages' in kwargs else []
        py_modules = kwargs['py_modules'] if 'py_modules' in kwargs else []

        if not packages and not py_modules:
            raise PipError(
                'failed to find packages or py_modules arguments in setup call'
            )

        package_dirs = kwargs.get('package_dir', {})
        use_2to3 = kwargs.get('use_2to3', False) and six.PY3

        files_installed = []

        # handle scripts
        # we handle them before the packages because they may be moved
        # while handling the packages
        scripts = kwargs.get("scripts", [])
        for script in scripts:
            print("Handling commandline script: {s}".format(s=script))
            cmdname = script.replace(os.path.dirname(script),
                                     "").replace("/", "")
            if not "." in cmdname:
                cmdname += ".py"
            scriptpath = os.path.join(source_folder, script)
            with open(scriptpath, "r") as fin:
                content = fin.read()
            cmdpath = create_command(cmdname, content)
            files_installed.append(cmdpath)

        packages = ArchiveFileInstaller._consolidated_packages(packages)
        for p in sorted(packages):  # folders or files under source root

            if p == '':  # no packages just files
                from_folder = os.path.join(source_folder,
                                           package_dirs.get(p, ''))
                for f in ArchiveFileInstaller._find_package_files(from_folder):
                    target_file = os.path.join(SITE_PACKAGES_FOLDER, f)
                    ArchiveFileInstaller._safe_move(
                        os.path.join(from_folder, f), target_file)
                    files_installed.append(target_file)
                    if use_2to3:
                        _stash('2to3 -w {} > /dev/null'.format(target_file))

            else:  # packages
                target_dir = os.path.join(SITE_PACKAGES_FOLDER, p)
                if p in package_dirs:
                    ArchiveFileInstaller._safe_move(
                        os.path.join(source_folder, package_dirs[p]),
                        target_dir)

                elif '' in package_dirs:
                    ArchiveFileInstaller._safe_move(
                        os.path.join(source_folder, package_dirs[''], p),
                        target_dir)

                else:
                    ArchiveFileInstaller._safe_move(
                        os.path.join(source_folder, p), target_dir)
                files_installed.append(target_dir)
                if use_2to3:
                    _stash(
                        """find {} --name '.py' | xargs -n 1 -I %% 2to3 -w %% > /dev/null"""
                        .format(target_dir))

        py_modules = ArchiveFileInstaller._consolidated_packages(py_modules)
        for p in sorted(
                py_modules
        ):  # files or folders where the file resides, e.g. ['file', 'folder.file']

            if '' in package_dirs:
                p = os.path.join(package_dirs[''], p)

            if os.path.isdir(os.path.join(source_folder, p)):  # folder
                target_dir = os.path.join(SITE_PACKAGES_FOLDER, p)
                ArchiveFileInstaller._safe_move(os.path.join(source_folder, p),
                                                target_dir)
                files_installed.append(target_dir)
                if use_2to3:
                    _stash(
                        """find {} --name '.py' | xargs -n 1 -I %% 2to3 -w %% > /dev/null"""
                        .format(target_dir))

            else:  # file
                target_file = os.path.join(SITE_PACKAGES_FOLDER, p + '.py')
                ArchiveFileInstaller._safe_move(
                    os.path.join(source_folder, p + '.py'), target_file)
                files_installed.append(target_file)
                if use_2to3:
                    _stash('2to3 -w {} > /dev/null'.format(target_file))

        # handle entry points
        entry_points = kwargs.get("entry_points", {})
        if isinstance(entry_points, (str, unicode)):
            if pkg_resources is not None:
                entry_points = {
                    s: c
                    for s, c in pkg_resources.split_sections(entry_points)
                }
            else:
                print(
                    "Warning: pkg_resources not available, skipping entry_points definitions."
                )
                entry_points = {}
        for epn in entry_points:
            ep = entry_points[epn]
            if isinstance(ep, (str, unicode)):
                ep = [ep]
            if epn == "console_scripts":
                for dec in ep:
                    name, loc = dec.replace(" ", "").split("=")
                    modname, funcname = loc.split(":")
                    if not name.endswith(".py"):
                        name += ".py"
                    desc = kwargs.get("description", "")
                    path = create_command(
                        name,
                        """'''{d}'''
from {m} import {n}

if __name__ == "__main__":
    {n}()
""".format(
                            m=modname,
                            n=funcname,
                            d=desc,
                        ),
                    )
                    files_installed.append(path)
            else:
                print("Warning: passing entry points for '{n}'.".format(n=epn))

        # Recursively Handle dependencies
        dependencies = kwargs.get('install_requires', [])
        return files_installed, dependencies
示例#3
0
文件: pip.py 项目: BBOOXX/stash
    def _run_setup_file(self, filename):
        """
        Transform and Run AST of the setup file
        """

        try:
            import pkg_resources
        except ImportError:
            # pkg_resources install may be in progress
            pkg_resources = None

        namespace = {
        '_setup_stub_': _setup_stub_,
        '__file__': filename,
        '__name__': '__main__',
        'setup_args': None,
        'setup_kwargs': None,
        }

        source_folder = os.path.dirname(filename)

        tree = ArchiveFileInstaller._get_cooked_ast(filename)
        codeobj = compile(tree, filename, 'exec')

        # Some setup files import the package to be installed and sometimes opens a file
        # in the source folder. So we modify sys.path and change directory into source folder.
        saved_cwd = os.getcwd()
        saved_sys_path = sys.path[:]
        os.chdir(source_folder)
        sys.path.insert(0, source_folder)
        try:
            exec(codeobj, namespace, namespace)
        finally:
            os.chdir(saved_cwd)
            sys.path = saved_sys_path

        args, kwargs = sys.modules['setuptools']._setup_params_
        # for k in sorted(kwargs.keys()): print('{}: {!r}'.format(k, kwargs[k]))

        if 'ext_modules' in kwargs:
            print('WARNING: Extension modules and skipped: {}'.format(kwargs['ext_modules']))

        packages = kwargs['packages'] if 'packages' in kwargs else []
        py_modules = kwargs['py_modules'] if 'py_modules' in kwargs else []

        if not packages and not py_modules:
            raise PipError('failed to find packages or py_modules arguments in setup call')

        package_dirs = kwargs.get('package_dir', {})
        use_2to3 = kwargs.get('use_2to3', False) and six.PY3

        files_installed = []

        # handle scripts
        # we handle them before the packages because they may be moved
        # while handling the packages
        scripts = kwargs.get("scripts", [])
        for script in scripts:
            if self.verbose:
                print("Handling commandline script: {s}".format(s=script))
            cmdname = script.replace(os.path.dirname(script), "").replace("/", "")
            if not "." in cmdname:
                cmdname += ".py"
            scriptpath = os.path.join(source_folder, script)
            with open(scriptpath, "r") as fin:
                content = fin.read()
            cmdpath = create_command(cmdname, content)
            files_installed.append(cmdpath)

        packages = ArchiveFileInstaller._consolidated_packages(packages)
        for p in sorted(packages):  # folders or files under source root

            if p == '':  # no packages just files
                from_folder = os.path.join(source_folder, package_dirs.get(p, ''))
                for f in ArchiveFileInstaller._find_package_files(from_folder):
                    target_file = os.path.join(SITE_PACKAGES_FOLDER, f)
                    ArchiveFileInstaller._safe_move(
                    os.path.join(from_folder, f),
                    target_file
                    )
                    files_installed.append(target_file)
                    if use_2to3:
                        _stash('2to3 -w {} > /dev/null'.format(target_file))

            else:  # packages
                target_dir = os.path.join(SITE_PACKAGES_FOLDER, p)
                if p in package_dirs:
                    ArchiveFileInstaller._safe_move(
                    os.path.join(source_folder, package_dirs[p]),
                    target_dir
                    )

                elif '' in package_dirs:
                    ArchiveFileInstaller._safe_move(
                    os.path.join(source_folder, package_dirs[''], p),
                    target_dir
                    )

                else:
                    ArchiveFileInstaller._safe_move(
                    os.path.join(source_folder, p),
                    target_dir
                    )
                files_installed.append(target_dir)
                if use_2to3:
                    _stash("""find {} --name '.py' | xargs -n 1 -I %% 2to3 -w %% > /dev/null""".format(target_dir))

        py_modules = ArchiveFileInstaller._consolidated_packages(py_modules)
        for p in sorted(py_modules):  # files or folders where the file resides, e.g. ['file', 'folder.file']

            if '' in package_dirs:
                p = os.path.join(package_dirs[''], p)

            if os.path.isdir(os.path.join(source_folder, p)):  # folder
                target_dir = os.path.join(SITE_PACKAGES_FOLDER, p)
                ArchiveFileInstaller._safe_move(
                os.path.join(source_folder, p),
                target_dir
                )
                files_installed.append(target_dir)
                if use_2to3:
                    _stash("""find {} --name '.py' | xargs -n 1 -I %% 2to3 -w %% > /dev/null""".format(target_dir))

            else:  # file
                target_file = os.path.join(SITE_PACKAGES_FOLDER, p + '.py')
                ArchiveFileInstaller._safe_move(
                os.path.join(source_folder, p + '.py'),
                target_file
                )
                files_installed.append(target_file)
                if use_2to3:
                    _stash('2to3 -w {} > /dev/null'.format(target_file))

        # handle entry points
        entry_points = kwargs.get("entry_points", {})
        if isinstance(entry_points, (str, unicode)):
            if pkg_resources is not None:
                entry_points = {s: c for s, c in pkg_resources.split_sections(entry_points)}
            else:
                print("Warning: pkg_resources not available, skipping entry_points definitions.")
                entry_points = {}
        for epn in entry_points:
            if self.verbose:
                print("Handling entrypoints for: " + epn)
            ep = entry_points[epn]
            if isinstance(ep, (str, unicode)):
                ep = [ep]
            if epn == "console_scripts":
                for dec in ep:
                    name, loc = dec.replace(" ", "").split("=")
                    modname, funcname = loc.split(":")
                    if not name.endswith(".py"):
                        name += ".py"
                    desc = kwargs.get("description", "")
                    path = create_command(
                    name,
                    b"""'''{d}'''
from {m} import {n}

if __name__ == "__main__":
    {n}()
""".format(
                    m=modname,
                    n=funcname,
                    d=desc,
                    ),
                    )
                    files_installed.append(path)
            else:
                print("Warning: passing entry points for '{n}'.".format(n=epn))

        # Recursively Handle dependencies
        dependencies = kwargs.get('install_requires', [])
        return files_installed, dependencies