<commit_before>#
# Copyright (c) 2004-2006 rPath, Inc.
#
# This program is distributed under the terms of the Common Public License,
# version 1.0. A copy of this license should have been distributed with this
# source file in a file called LICENSE. If it is not present, the license
# is always available at http://www.opensource.org/licenses/cpl.php.
#
# This program is distributed in the hope that it will be useful, but
# without any warranty; without even the implied warranty of merchantability
# or fitness for a particular purpose. See the Common Public License for
# full details.
#

import os
import stat

from conary.lib import util
from conary.build import policy


# probably needs to migrate to some form of configuration
# need lib and %(lib)s bits for multilib
librarydirs = [
    '%(libdir)s/',
    '%(prefix)s/lib/',
    '%(essentiallibdir)s/',
    '/lib/',
    '%(krbprefix)s/%(lib)s/',
    '%(krbprefix)s/lib/',
    '%(x11prefix)s/%(lib)s/',
    '%(x11prefix)s/lib/',
    '%(prefix)s/local/%(lib)s/',
    '%(prefix)s/local/lib/',
]


class SharedLibrary(policy.PackagePolicy):
    """
    NAME
    ====

    B{C{r.SharedLibrary()}} - Mark system shared libraries

    SYNOPSIS
    ========

    C{r.SharedLibrary([I{filterexp}] || [I{subtrees=path}])}

    DESCRIPTION
    ===========

    The C{r.SharedLibrary()} policy marks system shared libraries such
    that C{ldconfig} will be run.

    C{r.SharedLibrary(subtrees=I{path})} to mark a path as containing
    shared libraries; C{r.SharedLibrary(I{filterexp})} to mark a file.

    C{r.SharedLibrary} does B{not} walk entire directory trees.  Every
    directory that you want to add must be passed in using the
    C{subtrees} keyword.

    EXAMPLES
    ========

    C{r.SharedLibrary(subtrees='%(libdir)s/mysql/')}

    Causes the C{%(libdir)s/mysql/} directory to be considered as a
    source of shared libraries; files in that directory will be marked
    as shared libraries, all appropriate actions will be taken at
    install time, and Conary policies enforcing appropriate practices
    for libraries will be enabled for that directory.
    """
    requires = (
        ('ExecutableLibraries', policy.REQUIRED),
        ('CheckSonames', policy.REQUIRED),
        ('NormalizeLibrarySymlinks', policy.REQUIRED),
        ('Provides', policy.REQUIRED),
        ('Requires', policy.REQUIRED),
    )

    invariantsubtrees = librarydirs
    invariantinclusions = [
	(r'..*\.so\..*', None, stat.S_IFDIR),
    ]
    recursive = False

    def postInit(self):
        # Provides and Requires need to know about librarydirs
        d = {'sonameSubtrees': librarydirs}
        self.recipe.Provides(**d)
        self.recipe.Requires(**d)

    def updateArgs(self, *args, **keywords):
	policy.PackagePolicy.updateArgs(self, *args, **keywords)
        if 'subtrees' in keywords:
            # share with other policies that need to know about shlibs
            d = {'subtrees': keywords['subtrees']}
            self.recipe.ExecutableLibraries(**d)
            self.recipe.CheckSonames(**d)
            self.recipe.NormalizeLibrarySymlinks(**d)
            # Provides and Requires need this information passed elsewise
            d = {'sonameSubtrees': keywords['subtrees']}
            self.recipe.Provides(**d)
            self.recipe.Requires(**d)

    def doFile(self, filename):
	fullpath = self.macros.destdir + filename
	if os.path.isfile(fullpath) and util.isregular(fullpath):
	    m = self.recipe.magic[filename]
	    if m and m.name == 'ELF' and 'soname' in m.contents:
                self.info(filename)
		self.recipe.autopkg.pathMap[filename].tags.set("shlib")


class FixupMultilibPaths(policy.DestdirPolicy):
    """
    NAME
    ====

    B{C{r.FixupMultilibPaths()}} - Fix up and warn about files installed in directories that do not allow side-by-side installation of multilib-capable libraries

    SYNOPSIS
    ========

    C{r.FixupMultilibPaths([I{filterexp}] I{exceptions=filterexp}])}

    DESCRIPTION
    ===========

    The C{r.FixupMultilibPaths()} policy fixes up and warns about programs
    which do not know about C{%(lib)s}, and attempt to install to C{lib}
    when they should be installing to C{lib64} instead.

    EXAMPLES
    ========

    C{r.FixupMultilibPaths(exceptions='.*')}

    This package is explicitly not multilib and the policy should not
    run (extremely rare).
    """
    requires = (
        ('ExecutableLibraries', policy.CONDITIONAL_SUBSEQUENT),
        ('Strip', policy.CONDITIONAL_SUBSEQUENT),
    )
    invariantinclusions = [
        '.*\.(so.*|a)$',
    ]

    def __init__(self, *args, **keywords):
        self.dirmap = {
            '/lib':            '/%(lib)s',
            '%(prefix)s/lib':  '%(libdir)s',
        }
        self.invariantsubtrees = self.dirmap.keys()
        policy.DestdirPolicy.__init__(self, *args, **keywords)

    def test(self):
        if self.macros['lib'] == 'lib':<commit_msg>clarify documentation of FixupMultilibPaths exceptions<commit_after>#
# Copyright (c) 2004-2006 rPath, Inc.
#
# This program is distributed under the terms of the Common Public License,
# version 1.0. A copy of this license should have been distributed with this
# source file in a file called LICENSE. If it is not present, the license
# is always available at http://www.opensource.org/licenses/cpl.php.
#
# This program is distributed in the hope that it will be useful, but
# without any warranty; without even the implied warranty of merchantability
# or fitness for a particular purpose. See the Common Public License for
# full details.
#

import os
import stat

from conary.lib import util
from conary.build import policy


# probably needs to migrate to some form of configuration
# need lib and %(lib)s bits for multilib
librarydirs = [
    '%(libdir)s/',
    '%(prefix)s/lib/',
    '%(essentiallibdir)s/',
    '/lib/',
    '%(krbprefix)s/%(lib)s/',
    '%(krbprefix)s/lib/',
    '%(x11prefix)s/%(lib)s/',
    '%(x11prefix)s/lib/',
    '%(prefix)s/local/%(lib)s/',
    '%(prefix)s/local/lib/',
]


class SharedLibrary(policy.PackagePolicy):
    """
    NAME
    ====

    B{C{r.SharedLibrary()}} - Mark system shared libraries

    SYNOPSIS
    ========

    C{r.SharedLibrary([I{filterexp}] || [I{subtrees=path}])}

    DESCRIPTION
    ===========

    The C{r.SharedLibrary()} policy marks system shared libraries such
    that C{ldconfig} will be run.

    C{r.SharedLibrary(subtrees=I{path})} to mark a path as containing
    shared libraries; C{r.SharedLibrary(I{filterexp})} to mark a file.

    C{r.SharedLibrary} does B{not} walk entire directory trees.  Every
    directory that you want to add must be passed in using the
    C{subtrees} keyword.

    EXAMPLES
    ========

    C{r.SharedLibrary(subtrees='%(libdir)s/mysql/')}

    Causes the C{%(libdir)s/mysql/} directory to be considered as a
    source of shared libraries; files in that directory will be marked
    as shared libraries, all appropriate actions will be taken at
    install time, and Conary policies enforcing appropriate practices
    for libraries will be enabled for that directory.
    """
    requires = (
        ('ExecutableLibraries', policy.REQUIRED),
        ('CheckSonames', policy.REQUIRED),
        ('NormalizeLibrarySymlinks', policy.REQUIRED),
        ('Provides', policy.REQUIRED),
        ('Requires', policy.REQUIRED),
    )

    invariantsubtrees = librarydirs
    invariantinclusions = [
	(r'..*\.so\..*', None, stat.S_IFDIR),
    ]
    recursive = False

    def postInit(self):
        # Provides and Requires need to know about librarydirs
        d = {'sonameSubtrees': librarydirs}
        self.recipe.Provides(**d)
        self.recipe.Requires(**d)

    def updateArgs(self, *args, **keywords):
	policy.PackagePolicy.updateArgs(self, *args, **keywords)
        if 'subtrees' in keywords:
            # share with other policies that need to know about shlibs
            d = {'subtrees': keywords['subtrees']}
            self.recipe.ExecutableLibraries(**d)
            self.recipe.CheckSonames(**d)
            self.recipe.NormalizeLibrarySymlinks(**d)
            # Provides and Requires need this information passed elsewise
            d = {'sonameSubtrees': keywords['subtrees']}
            self.recipe.Provides(**d)
            self.recipe.Requires(**d)

    def doFile(self, filename):
	fullpath = self.macros.destdir + filename
	if os.path.isfile(fullpath) and util.isregular(fullpath):
	    m = self.recipe.magic[filename]
	    if m and m.name == 'ELF' and 'soname' in m.contents:
                self.info(filename)
		self.recipe.autopkg.pathMap[filename].tags.set("shlib")


class FixupMultilibPaths(policy.DestdirPolicy):
    """
    NAME
    ====

    B{C{r.FixupMultilibPaths()}} - Fix up and warn about files installed in directories that do not allow side-by-side installation of multilib-capable libraries

    SYNOPSIS
    ========

    C{r.FixupMultilibPaths([I{filterexp}] I{exceptions=filterexp}])}

    DESCRIPTION
    ===========

    The C{r.FixupMultilibPaths()} policy fixes up and warns about programs
    which do not know about C{%(lib)s}, and attempt to install to C{lib}
    when they should be installing to C{lib64} instead.

    EXAMPLES
    ========

    C{r.FixupMultilibPaths(exceptions='.*')}

    The package being built is explicitly not multilib-safe, and
    therefore the policy should not run (extremely rare).
    """
    requires = (
        ('ExecutableLibraries', policy.CONDITIONAL_SUBSEQUENT),
        ('Strip', policy.CONDITIONAL_SUBSEQUENT),
    )
    invariantinclusions = [
        '.*\.(so.*|a)$',
    ]

    def __init__(self, *args, **keywords):
        self.dirmap = {
            '/lib':            '/%(lib)s',
            '%(prefix)s/lib':  '%(libdir)s',
        }
        self.invariantsubtrees = self.dirmap.keys()
        policy.DestdirPolicy.__init__(self, *args, **keywords)

    def test(self):
        if self.macros['lib'] == 'lib':
{'fraction_non_alphanumeric': 0.1299638989169675, 'fraction_numerical': 0.003208985158443642, 'mean_word_length': 3.0036130068245686, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5961335', 'n_tokens_mistral': 3193, 'n_tokens_neox': 3018, 'n_words': 864}