示例#1
0
 def assemble(self):
     if _check_path_overlap(self.name) and os.path.isdir(self.name):
         _rmtree(self.name)
     logger.info("Building COLLECT %s", self.tocbasename)
     os.makedirs(self.name)
     toc = add_suffix_to_extensions(self.toc)
     for inm, fnm, typ in toc:
         if not os.path.exists(fnm) or not os.path.isfile(fnm) and is_path_to_egg(fnm):
             # file is contained within python egg, it is added with the egg
             continue
         if os.pardir in os.path.normpath(inm) or os.path.isabs(inm):
             raise SystemExit('Security-Alert: try to store file outside '
                              'of dist-directory. Aborting. %r' % inm)
         tofnm = os.path.join(self.name, inm)
         todir = os.path.dirname(tofnm)
         if not os.path.exists(todir):
             os.makedirs(todir)
         if typ in ('EXTENSION', 'BINARY'):
             fnm = checkCache(fnm, strip=self.strip_binaries,
                              upx=(self.upx_binaries and (is_win or is_cygwin)),
                              dist_nm=inm)
         if typ != 'DEPENDENCY':
             shutil.copy(fnm, tofnm)
             try:
                 shutil.copystat(fnm, tofnm)
             except OSError:
                 logger.warn("failed to copy flags of %s", fnm)
         if typ in ('EXTENSION', 'BINARY'):
             os.chmod(tofnm, 0o755)
示例#2
0
    def assemble(self):
        if _check_path_overlap(self.name) and os.path.isdir(self.name):
            _rmtree(self.name)
        logger.info("Building BUNDLE %s", self.tocbasename)

        # Create a minimal Mac bundle structure
        os.makedirs(os.path.join(self.name, "Contents", "MacOS"))
        os.makedirs(os.path.join(self.name, "Contents", "Resources"))
        os.makedirs(os.path.join(self.name, "Contents", "Frameworks"))

        # Copy icns icon to Resources directory.
        if os.path.exists(self.icon):
            shutil.copy(self.icon,
                        os.path.join(self.name, 'Contents', 'Resources'))
        else:
            logger.warning("icon not found %s", self.icon)

        # Key/values for a minimal Info.plist file
        info_plist_dict = {
            "CFBundleDisplayName": self.appname,
            "CFBundleName": self.appname,

            # Required by 'codesign' utility.
            # The value for CFBundleIdentifier is used as the default unique
            # name of your program for Code Signing purposes.
            # It even identifies the APP for access to restricted OS X areas
            # like Keychain.
            #
            # The identifier used for signing must be globally unique. The usal
            # form for this identifier is a hierarchical name in reverse DNS
            # notation, starting with the toplevel domain, followed by the
            # company name, followed by the department within the company, and
            # ending with the product name. Usually in the form:
            #   com.mycompany.department.appname
            # Cli option --osx-bundle-identifier sets this value.
            "CFBundleIdentifier": self.bundle_identifier,
            "CFBundleExecutable": os.path.basename(self.exename),
            "CFBundleIconFile": os.path.basename(self.icon),
            "CFBundleInfoDictionaryVersion": "6.0",
            "CFBundlePackageType": "APPL",
            "CFBundleShortVersionString": self.version,
        }

        # Set some default values.
        # But they still can be overwritten by the user.
        if self.console:
            # Setting EXE console=True implies LSBackgroundOnly=True.
            info_plist_dict['LSBackgroundOnly'] = True
        else:
            # Let's use high resolution by default.
            info_plist_dict['NSHighResolutionCapable'] = True

        # Merge info_plist settings from spec file
        if isinstance(self.info_plist, dict) and self.info_plist:
            info_plist_dict.update(self.info_plist)

        plist_filename = os.path.join(self.name, "Contents", "Info.plist")
        with open(plist_filename, "wb") as plist_fh:
            plistlib.dump(info_plist_dict, plist_fh)

        links = []
        _QT_BASE_PATH = {'PySide2', 'PySide6', 'PyQt5', 'PySide6'}
        for inm, fnm, typ in self.toc:
            # Adjust name for extensions, if applicable
            inm, fnm, typ = add_suffix_to_extension(inm, fnm, typ)
            # Copy files from cache. This ensures that are used files with relative
            # paths to dynamic library dependencies (@executable_path)
            base_path = inm.split('/', 1)[0]
            if typ in ('EXTENSION', 'BINARY'):
                fnm = checkCache(fnm,
                                 strip=self.strip,
                                 upx=self.upx,
                                 upx_exclude=self.upx_exclude,
                                 dist_nm=inm,
                                 target_arch=self.target_arch,
                                 codesign_identity=self.codesign_identity,
                                 entitlements_file=self.entitlements_file)
            # Add most data files to a list for symlinking later.
            if typ == 'DATA' and base_path not in _QT_BASE_PATH:
                links.append((inm, fnm))
            else:
                tofnm = os.path.join(self.name, "Contents", "MacOS", inm)
                todir = os.path.dirname(tofnm)
                if not os.path.exists(todir):
                    os.makedirs(todir)
                if os.path.isdir(fnm):
                    # beacuse shutil.copy2() is the default copy function
                    # for shutil.copytree, this will also copy file metadata
                    shutil.copytree(fnm, tofnm)
                else:
                    shutil.copy(fnm, tofnm)

        logger.info('Moving BUNDLE data files to Resource directory')

        # Mac OS X Code Signing does not work when .app bundle contains
        # data files in dir ./Contents/MacOS.
        #
        # Put all data files in ./Resources and create symlinks in ./MacOS.
        bin_dir = os.path.join(self.name, 'Contents', 'MacOS')
        res_dir = os.path.join(self.name, 'Contents', 'Resources')
        for inm, fnm in links:
            tofnm = os.path.join(res_dir, inm)
            todir = os.path.dirname(tofnm)
            if not os.path.exists(todir):
                os.makedirs(todir)
            if os.path.isdir(fnm):
                # beacuse shutil.copy2() is the default copy function
                # for shutil.copytree, this will also copy file metadata
                shutil.copytree(fnm, tofnm)
            else:
                shutil.copy(fnm, tofnm)
            base_path = os.path.split(inm)[0]
            if base_path:
                if not os.path.exists(os.path.join(bin_dir, inm)):
                    path = ''
                    for part in iter(base_path.split(os.path.sep)):
                        # Build path from previous path and the next part of the base path
                        path = os.path.join(path, part)
                        try:
                            relative_source_path = os.path.relpath(
                                os.path.join(res_dir, path),
                                os.path.split(os.path.join(bin_dir, path))[0])
                            dest_path = os.path.join(bin_dir, path)
                            os.symlink(relative_source_path, dest_path)
                            break
                        except FileExistsError:
                            pass
                    if not os.path.exists(os.path.join(bin_dir, inm)):
                        relative_source_path = os.path.relpath(
                            os.path.join(res_dir, inm),
                            os.path.split(os.path.join(bin_dir, inm))[0])
                        dest_path = os.path.join(bin_dir, inm)
                        os.symlink(relative_source_path, dest_path)
            else:  # If path is empty, e.g., a top level file, try to just symlink the file
                os.symlink(
                    os.path.relpath(
                        os.path.join(res_dir, inm),
                        os.path.split(os.path.join(bin_dir, inm))[0]),
                    os.path.join(bin_dir, inm))

        # Sign the bundle
        logger.info('Signing the BUNDLE...')
        try:
            osxutils.sign_binary(self.name,
                                 self.codesign_identity,
                                 self.entitlements_file,
                                 deep=True)
        except Exception as e:
            logger.warning("Error while signing the bundle: %s", e)
            logger.warning("You will need to sign the bundle manually!")

        logger.info("Building BUNDLE %s completed successfully.",
                    self.tocbasename)