We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Preserving read-only flags on package data causing access denied when building
I'm working with a read-only source directory on Windows and running into access denied errors when calling `python setup.py bdist_wheel`. I noticed that it was only files included in package_data that were causing this, which helped me track it down to:

[setuptools/setuptools/command/build_py.py](
https://github.com/pypa/setuptools/blob/89155abb4222cf5a9dc81120e5c71e26b5af68f9/setuptools/command/build_py.py#L123)
`outf, copied = self.copy_file(srcfile, target)`

which is slightly different from its distutils version:

[cpython/Lib/distutils/command/build_py.py](
https://github.com/python/cpython/blob/d6345def68d3a0227253da26213dadb247f786db/Lib/distutils/command/build_py.py#L141)
`self.copy_file(os.path.join(src_dir, filename), target, preserve_mode=False)`

So it looks like it comes down to this preserve_mode flag.

I don't know if there is a good reason to preserve things like read-only flags? There's a [comment](https://github.com/python/cpython/blob/d6345def68d3a0227253da26213dadb247f786db/Lib/distutils/command/build_py.py#L74) about preserving mode further up in the distutils code, which seems to suggest not preserving is the way to go.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `setuptools/command/build_py.py`
Content:
```
1 from glob import glob
2 from distutils.util import convert_path
3 import distutils.command.build_py as orig
4 import os
5 import fnmatch
6 import textwrap
7 import io
8 import distutils.errors
9 import itertools
10 
11 from setuptools.extern import six
12 from setuptools.extern.six.moves import map, filter, filterfalse
13 
14 try:
15     from setuptools.lib2to3_ex import Mixin2to3
16 except ImportError:
17 
18     class Mixin2to3:
19         def run_2to3(self, files, doctests=True):
20             "do nothing"
21 
22 
23 class build_py(orig.build_py, Mixin2to3):
24     """Enhanced 'build_py' command that includes data files with packages
25 
26     The data files are specified via a 'package_data' argument to 'setup()'.
27     See 'setuptools.dist.Distribution' for more details.
28 
29     Also, this version of the 'build_py' command allows you to specify both
30     'py_modules' and 'packages' in the same setup operation.
31     """
32 
33     def finalize_options(self):
34         orig.build_py.finalize_options(self)
35         self.package_data = self.distribution.package_data
36         self.exclude_package_data = (self.distribution.exclude_package_data or
37                                      {})
38         if 'data_files' in self.__dict__:
39             del self.__dict__['data_files']
40         self.__updated_files = []
41         self.__doctests_2to3 = []
42 
43     def run(self):
44         """Build modules, packages, and copy data files to build directory"""
45         if not self.py_modules and not self.packages:
46             return
47 
48         if self.py_modules:
49             self.build_modules()
50 
51         if self.packages:
52             self.build_packages()
53             self.build_package_data()
54 
55         self.run_2to3(self.__updated_files, False)
56         self.run_2to3(self.__updated_files, True)
57         self.run_2to3(self.__doctests_2to3, True)
58 
59         # Only compile actual .py files, using our base class' idea of what our
60         # output files are.
61         self.byte_compile(orig.build_py.get_outputs(self, include_bytecode=0))
62 
63     def __getattr__(self, attr):
64         "lazily compute data files"
65         if attr == 'data_files':
66             self.data_files = self._get_data_files()
67             return self.data_files
68         return orig.build_py.__getattr__(self, attr)
69 
70     def build_module(self, module, module_file, package):
71         if six.PY2 and isinstance(package, six.string_types):
72             # avoid errors on Python 2 when unicode is passed (#190)
73             package = package.split('.')
74         outfile, copied = orig.build_py.build_module(self, module, module_file,
75                                                      package)
76         if copied:
77             self.__updated_files.append(outfile)
78         return outfile, copied
79 
80     def _get_data_files(self):
81         """Generate list of '(package,src_dir,build_dir,filenames)' tuples"""
82         self.analyze_manifest()
83         return list(map(self._get_pkg_data_files, self.packages or ()))
84 
85     def _get_pkg_data_files(self, package):
86         # Locate package source directory
87         src_dir = self.get_package_dir(package)
88 
89         # Compute package build directory
90         build_dir = os.path.join(*([self.build_lib] + package.split('.')))
91 
92         # Strip directory from globbed filenames
93         filenames = [
94             os.path.relpath(file, src_dir)
95             for file in self.find_data_files(package, src_dir)
96         ]
97         return package, src_dir, build_dir, filenames
98 
99     def find_data_files(self, package, src_dir):
100         """Return filenames for package's data files in 'src_dir'"""
101         patterns = self._get_platform_patterns(
102             self.package_data,
103             package,
104             src_dir,
105         )
106         globs_expanded = map(glob, patterns)
107         # flatten the expanded globs into an iterable of matches
108         globs_matches = itertools.chain.from_iterable(globs_expanded)
109         glob_files = filter(os.path.isfile, globs_matches)
110         files = itertools.chain(
111             self.manifest_files.get(package, []),
112             glob_files,
113         )
114         return self.exclude_data_files(package, src_dir, files)
115 
116     def build_package_data(self):
117         """Copy data files into build directory"""
118         for package, src_dir, build_dir, filenames in self.data_files:
119             for filename in filenames:
120                 target = os.path.join(build_dir, filename)
121                 self.mkpath(os.path.dirname(target))
122                 srcfile = os.path.join(src_dir, filename)
123                 outf, copied = self.copy_file(srcfile, target)
124                 srcfile = os.path.abspath(srcfile)
125                 if (copied and
126                         srcfile in self.distribution.convert_2to3_doctests):
127                     self.__doctests_2to3.append(outf)
128 
129     def analyze_manifest(self):
130         self.manifest_files = mf = {}
131         if not self.distribution.include_package_data:
132             return
133         src_dirs = {}
134         for package in self.packages or ():
135             # Locate package source directory
136             src_dirs[assert_relative(self.get_package_dir(package))] = package
137 
138         self.run_command('egg_info')
139         ei_cmd = self.get_finalized_command('egg_info')
140         for path in ei_cmd.filelist.files:
141             d, f = os.path.split(assert_relative(path))
142             prev = None
143             oldf = f
144             while d and d != prev and d not in src_dirs:
145                 prev = d
146                 d, df = os.path.split(d)
147                 f = os.path.join(df, f)
148             if d in src_dirs:
149                 if path.endswith('.py') and f == oldf:
150                     continue  # it's a module, not data
151                 mf.setdefault(src_dirs[d], []).append(path)
152 
153     def get_data_files(self):
154         pass  # Lazily compute data files in _get_data_files() function.
155 
156     def check_package(self, package, package_dir):
157         """Check namespace packages' __init__ for declare_namespace"""
158         try:
159             return self.packages_checked[package]
160         except KeyError:
161             pass
162 
163         init_py = orig.build_py.check_package(self, package, package_dir)
164         self.packages_checked[package] = init_py
165 
166         if not init_py or not self.distribution.namespace_packages:
167             return init_py
168 
169         for pkg in self.distribution.namespace_packages:
170             if pkg == package or pkg.startswith(package + '.'):
171                 break
172         else:
173             return init_py
174 
175         with io.open(init_py, 'rb') as f:
176             contents = f.read()
177         if b'declare_namespace' not in contents:
178             raise distutils.errors.DistutilsError(
179                 "Namespace package problem: %s is a namespace package, but "
180                 "its\n__init__.py does not call declare_namespace()! Please "
181                 'fix it.\n(See the setuptools manual under '
182                 '"Namespace Packages" for details.)\n"' % (package,)
183             )
184         return init_py
185 
186     def initialize_options(self):
187         self.packages_checked = {}
188         orig.build_py.initialize_options(self)
189 
190     def get_package_dir(self, package):
191         res = orig.build_py.get_package_dir(self, package)
192         if self.distribution.src_root is not None:
193             return os.path.join(self.distribution.src_root, res)
194         return res
195 
196     def exclude_data_files(self, package, src_dir, files):
197         """Filter filenames for package's data files in 'src_dir'"""
198         files = list(files)
199         patterns = self._get_platform_patterns(
200             self.exclude_package_data,
201             package,
202             src_dir,
203         )
204         match_groups = (
205             fnmatch.filter(files, pattern)
206             for pattern in patterns
207         )
208         # flatten the groups of matches into an iterable of matches
209         matches = itertools.chain.from_iterable(match_groups)
210         bad = set(matches)
211         keepers = (
212             fn
213             for fn in files
214             if fn not in bad
215         )
216         # ditch dupes
217         return list(_unique_everseen(keepers))
218 
219     @staticmethod
220     def _get_platform_patterns(spec, package, src_dir):
221         """
222         yield platform-specific path patterns (suitable for glob
223         or fn_match) from a glob-based spec (such as
224         self.package_data or self.exclude_package_data)
225         matching package in src_dir.
226         """
227         raw_patterns = itertools.chain(
228             spec.get('', []),
229             spec.get(package, []),
230         )
231         return (
232             # Each pattern has to be converted to a platform-specific path
233             os.path.join(src_dir, convert_path(pattern))
234             for pattern in raw_patterns
235         )
236 
237 
238 # from Python docs
239 def _unique_everseen(iterable, key=None):
240     "List unique elements, preserving order. Remember all elements ever seen."
241     # unique_everseen('AAAABBBCCDAABBB') --> A B C D
242     # unique_everseen('ABBCcAD', str.lower) --> A B C D
243     seen = set()
244     seen_add = seen.add
245     if key is None:
246         for element in filterfalse(seen.__contains__, iterable):
247             seen_add(element)
248             yield element
249     else:
250         for element in iterable:
251             k = key(element)
252             if k not in seen:
253                 seen_add(k)
254                 yield element
255 
256 
257 def assert_relative(path):
258     if not os.path.isabs(path):
259         return path
260     from distutils.errors import DistutilsSetupError
261 
262     msg = textwrap.dedent("""
263         Error: setup script specifies an absolute path:
264 
265             %s
266 
267         setup() arguments must *always* be /-separated paths relative to the
268         setup.py directory, *never* absolute paths.
269         """).lstrip() % path
270     raise DistutilsSetupError(msg)
271
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/setuptools/command/build_py.py b/setuptools/command/build_py.py
--- a/setuptools/command/build_py.py
+++ b/setuptools/command/build_py.py
@@ -120,7 +120,7 @@
                 target = os.path.join(build_dir, filename)
                 self.mkpath(os.path.dirname(target))
                 srcfile = os.path.join(src_dir, filename)
-                outf, copied = self.copy_file(srcfile, target)
+                outf, copied = self.copy_file(srcfile, target, preserve_mode=False)
                 srcfile = os.path.abspath(srcfile)
                 if (copied and
                         srcfile in self.distribution.convert_2to3_doctests):

{"golden_diff": "diff --git a/setuptools/command/build_py.py b/setuptools/command/build_py.py\n--- a/setuptools/command/build_py.py\n+++ b/setuptools/command/build_py.py\n@@ -120,7 +120,7 @@\n                 target = os.path.join(build_dir, filename)\n                 self.mkpath(os.path.dirname(target))\n                 srcfile = os.path.join(src_dir, filename)\n-                outf, copied = self.copy_file(srcfile, target)\n+                outf, copied = self.copy_file(srcfile, target, preserve_mode=False)\n                 srcfile = os.path.abspath(srcfile)\n                 if (copied and\n                         srcfile in self.distribution.convert_2to3_doctests):\n", "issue": "Preserving read-only flags on package data causing access denied when building\nI'm working with a read-only source directory on Windows and running into access denied errors when calling `python setup.py bdist_wheel`. I noticed that it was only files included in package_data that were causing this, which helped me track it down to:\r\n\r\n[setuptools/setuptools/command/build_py.py](\r\nhttps://github.com/pypa/setuptools/blob/89155abb4222cf5a9dc81120e5c71e26b5af68f9/setuptools/command/build_py.py#L123)\r\n`outf, copied = self.copy_file(srcfile, target)`\r\n\r\nwhich is slightly different from its distutils version:\r\n\r\n[cpython/Lib/distutils/command/build_py.py](\r\nhttps://github.com/python/cpython/blob/d6345def68d3a0227253da26213dadb247f786db/Lib/distutils/command/build_py.py#L141)\r\n`self.copy_file(os.path.join(src_dir, filename), target, preserve_mode=False)`\r\n\r\nSo it looks like it comes down to this preserve_mode flag.\r\n\r\nI don't know if there is a good reason to preserve things like read-only flags? There's a [comment](https://github.com/python/cpython/blob/d6345def68d3a0227253da26213dadb247f786db/Lib/distutils/command/build_py.py#L74) about preserving mode further up in the distutils code, which seems to suggest not preserving is the way to go.\n", "before_files": [{"content": "from glob import glob\nfrom distutils.util import convert_path\nimport distutils.command.build_py as orig\nimport os\nimport fnmatch\nimport textwrap\nimport io\nimport distutils.errors\nimport itertools\n\nfrom setuptools.extern import six\nfrom setuptools.extern.six.moves import map, filter, filterfalse\n\ntry:\n    from setuptools.lib2to3_ex import Mixin2to3\nexcept ImportError:\n\n    class Mixin2to3:\n        def run_2to3(self, files, doctests=True):\n            \"do nothing\"\n\n\nclass build_py(orig.build_py, Mixin2to3):\n    \"\"\"Enhanced 'build_py' command that includes data files with packages\n\n    The data files are specified via a 'package_data' argument to 'setup()'.\n    See 'setuptools.dist.Distribution' for more details.\n\n    Also, this version of the 'build_py' command allows you to specify both\n    'py_modules' and 'packages' in the same setup operation.\n    \"\"\"\n\n    def finalize_options(self):\n        orig.build_py.finalize_options(self)\n        self.package_data = self.distribution.package_data\n        self.exclude_package_data = (self.distribution.exclude_package_data or\n                                     {})\n        if 'data_files' in self.__dict__:\n            del self.__dict__['data_files']\n        self.__updated_files = []\n        self.__doctests_2to3 = []\n\n    def run(self):\n        \"\"\"Build modules, packages, and copy data files to build directory\"\"\"\n        if not self.py_modules and not self.packages:\n            return\n\n        if self.py_modules:\n            self.build_modules()\n\n        if self.packages:\n            self.build_packages()\n            self.build_package_data()\n\n        self.run_2to3(self.__updated_files, False)\n        self.run_2to3(self.__updated_files, True)\n        self.run_2to3(self.__doctests_2to3, True)\n\n        # Only compile actual .py files, using our base class' idea of what our\n        # output files are.\n        self.byte_compile(orig.build_py.get_outputs(self, include_bytecode=0))\n\n    def __getattr__(self, attr):\n        \"lazily compute data files\"\n        if attr == 'data_files':\n            self.data_files = self._get_data_files()\n            return self.data_files\n        return orig.build_py.__getattr__(self, attr)\n\n    def build_module(self, module, module_file, package):\n        if six.PY2 and isinstance(package, six.string_types):\n            # avoid errors on Python 2 when unicode is passed (#190)\n            package = package.split('.')\n        outfile, copied = orig.build_py.build_module(self, module, module_file,\n                                                     package)\n        if copied:\n            self.__updated_files.append(outfile)\n        return outfile, copied\n\n    def _get_data_files(self):\n        \"\"\"Generate list of '(package,src_dir,build_dir,filenames)' tuples\"\"\"\n        self.analyze_manifest()\n        return list(map(self._get_pkg_data_files, self.packages or ()))\n\n    def _get_pkg_data_files(self, package):\n        # Locate package source directory\n        src_dir = self.get_package_dir(package)\n\n        # Compute package build directory\n        build_dir = os.path.join(*([self.build_lib] + package.split('.')))\n\n        # Strip directory from globbed filenames\n        filenames = [\n            os.path.relpath(file, src_dir)\n            for file in self.find_data_files(package, src_dir)\n        ]\n        return package, src_dir, build_dir, filenames\n\n    def find_data_files(self, package, src_dir):\n        \"\"\"Return filenames for package's data files in 'src_dir'\"\"\"\n        patterns = self._get_platform_patterns(\n            self.package_data,\n            package,\n            src_dir,\n        )\n        globs_expanded = map(glob, patterns)\n        # flatten the expanded globs into an iterable of matches\n        globs_matches = itertools.chain.from_iterable(globs_expanded)\n        glob_files = filter(os.path.isfile, globs_matches)\n        files = itertools.chain(\n            self.manifest_files.get(package, []),\n            glob_files,\n        )\n        return self.exclude_data_files(package, src_dir, files)\n\n    def build_package_data(self):\n        \"\"\"Copy data files into build directory\"\"\"\n        for package, src_dir, build_dir, filenames in self.data_files:\n            for filename in filenames:\n                target = os.path.join(build_dir, filename)\n                self.mkpath(os.path.dirname(target))\n                srcfile = os.path.join(src_dir, filename)\n                outf, copied = self.copy_file(srcfile, target)\n                srcfile = os.path.abspath(srcfile)\n                if (copied and\n                        srcfile in self.distribution.convert_2to3_doctests):\n                    self.__doctests_2to3.append(outf)\n\n    def analyze_manifest(self):\n        self.manifest_files = mf = {}\n        if not self.distribution.include_package_data:\n            return\n        src_dirs = {}\n        for package in self.packages or ():\n            # Locate package source directory\n            src_dirs[assert_relative(self.get_package_dir(package))] = package\n\n        self.run_command('egg_info')\n        ei_cmd = self.get_finalized_command('egg_info')\n        for path in ei_cmd.filelist.files:\n            d, f = os.path.split(assert_relative(path))\n            prev = None\n            oldf = f\n            while d and d != prev and d not in src_dirs:\n                prev = d\n                d, df = os.path.split(d)\n                f = os.path.join(df, f)\n            if d in src_dirs:\n                if path.endswith('.py') and f == oldf:\n                    continue  # it's a module, not data\n                mf.setdefault(src_dirs[d], []).append(path)\n\n    def get_data_files(self):\n        pass  # Lazily compute data files in _get_data_files() function.\n\n    def check_package(self, package, package_dir):\n        \"\"\"Check namespace packages' __init__ for declare_namespace\"\"\"\n        try:\n            return self.packages_checked[package]\n        except KeyError:\n            pass\n\n        init_py = orig.build_py.check_package(self, package, package_dir)\n        self.packages_checked[package] = init_py\n\n        if not init_py or not self.distribution.namespace_packages:\n            return init_py\n\n        for pkg in self.distribution.namespace_packages:\n            if pkg == package or pkg.startswith(package + '.'):\n                break\n        else:\n            return init_py\n\n        with io.open(init_py, 'rb') as f:\n            contents = f.read()\n        if b'declare_namespace' not in contents:\n            raise distutils.errors.DistutilsError(\n                \"Namespace package problem: %s is a namespace package, but \"\n                \"its\\n__init__.py does not call declare_namespace()! Please \"\n                'fix it.\\n(See the setuptools manual under '\n                '\"Namespace Packages\" for details.)\\n\"' % (package,)\n            )\n        return init_py\n\n    def initialize_options(self):\n        self.packages_checked = {}\n        orig.build_py.initialize_options(self)\n\n    def get_package_dir(self, package):\n        res = orig.build_py.get_package_dir(self, package)\n        if self.distribution.src_root is not None:\n            return os.path.join(self.distribution.src_root, res)\n        return res\n\n    def exclude_data_files(self, package, src_dir, files):\n        \"\"\"Filter filenames for package's data files in 'src_dir'\"\"\"\n        files = list(files)\n        patterns = self._get_platform_patterns(\n            self.exclude_package_data,\n            package,\n            src_dir,\n        )\n        match_groups = (\n            fnmatch.filter(files, pattern)\n            for pattern in patterns\n        )\n        # flatten the groups of matches into an iterable of matches\n        matches = itertools.chain.from_iterable(match_groups)\n        bad = set(matches)\n        keepers = (\n            fn\n            for fn in files\n            if fn not in bad\n        )\n        # ditch dupes\n        return list(_unique_everseen(keepers))\n\n    @staticmethod\n    def _get_platform_patterns(spec, package, src_dir):\n        \"\"\"\n        yield platform-specific path patterns (suitable for glob\n        or fn_match) from a glob-based spec (such as\n        self.package_data or self.exclude_package_data)\n        matching package in src_dir.\n        \"\"\"\n        raw_patterns = itertools.chain(\n            spec.get('', []),\n            spec.get(package, []),\n        )\n        return (\n            # Each pattern has to be converted to a platform-specific path\n            os.path.join(src_dir, convert_path(pattern))\n            for pattern in raw_patterns\n        )\n\n\n# from Python docs\ndef _unique_everseen(iterable, key=None):\n    \"List unique elements, preserving order. Remember all elements ever seen.\"\n    # unique_everseen('AAAABBBCCDAABBB') --> A B C D\n    # unique_everseen('ABBCcAD', str.lower) --> A B C D\n    seen = set()\n    seen_add = seen.add\n    if key is None:\n        for element in filterfalse(seen.__contains__, iterable):\n            seen_add(element)\n            yield element\n    else:\n        for element in iterable:\n            k = key(element)\n            if k not in seen:\n                seen_add(k)\n                yield element\n\n\ndef assert_relative(path):\n    if not os.path.isabs(path):\n        return path\n    from distutils.errors import DistutilsSetupError\n\n    msg = textwrap.dedent(\"\"\"\n        Error: setup script specifies an absolute path:\n\n            %s\n\n        setup() arguments must *always* be /-separated paths relative to the\n        setup.py directory, *never* absolute paths.\n        \"\"\").lstrip() % path\n    raise DistutilsSetupError(msg)\n", "path": "setuptools/command/build_py.py"}], "after_files": [{"content": "from glob import glob\nfrom distutils.util import convert_path\nimport distutils.command.build_py as orig\nimport os\nimport fnmatch\nimport textwrap\nimport io\nimport distutils.errors\nimport itertools\n\nfrom setuptools.extern import six\nfrom setuptools.extern.six.moves import map, filter, filterfalse\n\ntry:\n    from setuptools.lib2to3_ex import Mixin2to3\nexcept ImportError:\n\n    class Mixin2to3:\n        def run_2to3(self, files, doctests=True):\n            \"do nothing\"\n\n\nclass build_py(orig.build_py, Mixin2to3):\n    \"\"\"Enhanced 'build_py' command that includes data files with packages\n\n    The data files are specified via a 'package_data' argument to 'setup()'.\n    See 'setuptools.dist.Distribution' for more details.\n\n    Also, this version of the 'build_py' command allows you to specify both\n    'py_modules' and 'packages' in the same setup operation.\n    \"\"\"\n\n    def finalize_options(self):\n        orig.build_py.finalize_options(self)\n        self.package_data = self.distribution.package_data\n        self.exclude_package_data = (self.distribution.exclude_package_data or\n                                     {})\n        if 'data_files' in self.__dict__:\n            del self.__dict__['data_files']\n        self.__updated_files = []\n        self.__doctests_2to3 = []\n\n    def run(self):\n        \"\"\"Build modules, packages, and copy data files to build directory\"\"\"\n        if not self.py_modules and not self.packages:\n            return\n\n        if self.py_modules:\n            self.build_modules()\n\n        if self.packages:\n            self.build_packages()\n            self.build_package_data()\n\n        self.run_2to3(self.__updated_files, False)\n        self.run_2to3(self.__updated_files, True)\n        self.run_2to3(self.__doctests_2to3, True)\n\n        # Only compile actual .py files, using our base class' idea of what our\n        # output files are.\n        self.byte_compile(orig.build_py.get_outputs(self, include_bytecode=0))\n\n    def __getattr__(self, attr):\n        \"lazily compute data files\"\n        if attr == 'data_files':\n            self.data_files = self._get_data_files()\n            return self.data_files\n        return orig.build_py.__getattr__(self, attr)\n\n    def build_module(self, module, module_file, package):\n        if six.PY2 and isinstance(package, six.string_types):\n            # avoid errors on Python 2 when unicode is passed (#190)\n            package = package.split('.')\n        outfile, copied = orig.build_py.build_module(self, module, module_file,\n                                                     package)\n        if copied:\n            self.__updated_files.append(outfile)\n        return outfile, copied\n\n    def _get_data_files(self):\n        \"\"\"Generate list of '(package,src_dir,build_dir,filenames)' tuples\"\"\"\n        self.analyze_manifest()\n        return list(map(self._get_pkg_data_files, self.packages or ()))\n\n    def _get_pkg_data_files(self, package):\n        # Locate package source directory\n        src_dir = self.get_package_dir(package)\n\n        # Compute package build directory\n        build_dir = os.path.join(*([self.build_lib] + package.split('.')))\n\n        # Strip directory from globbed filenames\n        filenames = [\n            os.path.relpath(file, src_dir)\n            for file in self.find_data_files(package, src_dir)\n        ]\n        return package, src_dir, build_dir, filenames\n\n    def find_data_files(self, package, src_dir):\n        \"\"\"Return filenames for package's data files in 'src_dir'\"\"\"\n        patterns = self._get_platform_patterns(\n            self.package_data,\n            package,\n            src_dir,\n        )\n        globs_expanded = map(glob, patterns)\n        # flatten the expanded globs into an iterable of matches\n        globs_matches = itertools.chain.from_iterable(globs_expanded)\n        glob_files = filter(os.path.isfile, globs_matches)\n        files = itertools.chain(\n            self.manifest_files.get(package, []),\n            glob_files,\n        )\n        return self.exclude_data_files(package, src_dir, files)\n\n    def build_package_data(self):\n        \"\"\"Copy data files into build directory\"\"\"\n        for package, src_dir, build_dir, filenames in self.data_files:\n            for filename in filenames:\n                target = os.path.join(build_dir, filename)\n                self.mkpath(os.path.dirname(target))\n                srcfile = os.path.join(src_dir, filename)\n                outf, copied = self.copy_file(srcfile, target, preserve_mode=False)\n                srcfile = os.path.abspath(srcfile)\n                if (copied and\n                        srcfile in self.distribution.convert_2to3_doctests):\n                    self.__doctests_2to3.append(outf)\n\n    def analyze_manifest(self):\n        self.manifest_files = mf = {}\n        if not self.distribution.include_package_data:\n            return\n        src_dirs = {}\n        for package in self.packages or ():\n            # Locate package source directory\n            src_dirs[assert_relative(self.get_package_dir(package))] = package\n\n        self.run_command('egg_info')\n        ei_cmd = self.get_finalized_command('egg_info')\n        for path in ei_cmd.filelist.files:\n            d, f = os.path.split(assert_relative(path))\n            prev = None\n            oldf = f\n            while d and d != prev and d not in src_dirs:\n                prev = d\n                d, df = os.path.split(d)\n                f = os.path.join(df, f)\n            if d in src_dirs:\n                if path.endswith('.py') and f == oldf:\n                    continue  # it's a module, not data\n                mf.setdefault(src_dirs[d], []).append(path)\n\n    def get_data_files(self):\n        pass  # Lazily compute data files in _get_data_files() function.\n\n    def check_package(self, package, package_dir):\n        \"\"\"Check namespace packages' __init__ for declare_namespace\"\"\"\n        try:\n            return self.packages_checked[package]\n        except KeyError:\n            pass\n\n        init_py = orig.build_py.check_package(self, package, package_dir)\n        self.packages_checked[package] = init_py\n\n        if not init_py or not self.distribution.namespace_packages:\n            return init_py\n\n        for pkg in self.distribution.namespace_packages:\n            if pkg == package or pkg.startswith(package + '.'):\n                break\n        else:\n            return init_py\n\n        with io.open(init_py, 'rb') as f:\n            contents = f.read()\n        if b'declare_namespace' not in contents:\n            raise distutils.errors.DistutilsError(\n                \"Namespace package problem: %s is a namespace package, but \"\n                \"its\\n__init__.py does not call declare_namespace()! Please \"\n                'fix it.\\n(See the setuptools manual under '\n                '\"Namespace Packages\" for details.)\\n\"' % (package,)\n            )\n        return init_py\n\n    def initialize_options(self):\n        self.packages_checked = {}\n        orig.build_py.initialize_options(self)\n\n    def get_package_dir(self, package):\n        res = orig.build_py.get_package_dir(self, package)\n        if self.distribution.src_root is not None:\n            return os.path.join(self.distribution.src_root, res)\n        return res\n\n    def exclude_data_files(self, package, src_dir, files):\n        \"\"\"Filter filenames for package's data files in 'src_dir'\"\"\"\n        files = list(files)\n        patterns = self._get_platform_patterns(\n            self.exclude_package_data,\n            package,\n            src_dir,\n        )\n        match_groups = (\n            fnmatch.filter(files, pattern)\n            for pattern in patterns\n        )\n        # flatten the groups of matches into an iterable of matches\n        matches = itertools.chain.from_iterable(match_groups)\n        bad = set(matches)\n        keepers = (\n            fn\n            for fn in files\n            if fn not in bad\n        )\n        # ditch dupes\n        return list(_unique_everseen(keepers))\n\n    @staticmethod\n    def _get_platform_patterns(spec, package, src_dir):\n        \"\"\"\n        yield platform-specific path patterns (suitable for glob\n        or fn_match) from a glob-based spec (such as\n        self.package_data or self.exclude_package_data)\n        matching package in src_dir.\n        \"\"\"\n        raw_patterns = itertools.chain(\n            spec.get('', []),\n            spec.get(package, []),\n        )\n        return (\n            # Each pattern has to be converted to a platform-specific path\n            os.path.join(src_dir, convert_path(pattern))\n            for pattern in raw_patterns\n        )\n\n\n# from Python docs\ndef _unique_everseen(iterable, key=None):\n    \"List unique elements, preserving order. Remember all elements ever seen.\"\n    # unique_everseen('AAAABBBCCDAABBB') --> A B C D\n    # unique_everseen('ABBCcAD', str.lower) --> A B C D\n    seen = set()\n    seen_add = seen.add\n    if key is None:\n        for element in filterfalse(seen.__contains__, iterable):\n            seen_add(element)\n            yield element\n    else:\n        for element in iterable:\n            k = key(element)\n            if k not in seen:\n                seen_add(k)\n                yield element\n\n\ndef assert_relative(path):\n    if not os.path.isabs(path):\n        return path\n    from distutils.errors import DistutilsSetupError\n\n    msg = textwrap.dedent(\"\"\"\n        Error: setup script specifies an absolute path:\n\n            %s\n\n        setup() arguments must *always* be /-separated paths relative to the\n        setup.py directory, *never* absolute paths.\n        \"\"\").lstrip() % path\n    raise DistutilsSetupError(msg)\n", "path": "setuptools/command/build_py.py"}]}