Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an exclude patttern for wheels (and use it for flit-core) #689

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions flit_core/flit_core/common.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import ast
from contextlib import contextmanager
from glob import glob
import hashlib
import logging
import os
import os.path as osp
import sys

from pathlib import Path
Expand All @@ -12,6 +14,37 @@

from .versionno import normalise_version


class FilePatterns:
"""Manage a set of file inclusion/exclusion patterns relative to basedir"""
def __init__(self, patterns, basedir):
self.basedir = basedir

self.dirs = set()
self.files = set()

for pattern in patterns:
for path in sorted(glob(osp.join(basedir, pattern), recursive=True)):
rel = osp.relpath(path, basedir)
if osp.isdir(path):
self.dirs.add(rel)
else:
self.files.add(rel)

def match_file(self, rel_path):
if rel_path in self.files:
return True

return any(rel_path.startswith(d + os.sep) for d in self.dirs)

def match_dir(self, rel_path):
if rel_path in self.dirs:
return True

# Check if it's a subdirectory of any directory in the list
return any(rel_path.startswith(d + os.sep) for d in self.dirs)


class Module(object):
"""This represents the module/package that we are going to distribute
"""
Expand Down
14 changes: 13 additions & 1 deletion flit_core/flit_core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def prep_toml_config(d, path):
)

unknown_sections = set(dtool) - {
'metadata', 'module', 'scripts', 'entrypoints', 'sdist', 'external-data'
'metadata', 'module', 'scripts', 'entrypoints', 'sdist', 'wheel', 'external-data'
}
unknown_sections = [s for s in unknown_sections if not s.lower().startswith('x-')]
if unknown_sections:
Expand All @@ -155,6 +155,17 @@ def prep_toml_config(d, path):
exclude, 'exclude'
)

if 'wheel' in dtool:
unknown_keys = set(dtool['wheel']) - {'exclude'}
if unknown_keys:
raise ConfigError(
"Unknown keys in [tool.flit.wheel]:" + ", ".join(unknown_keys)
)

loaded_cfg.wheel_exclude_patterns = _check_glob_patterns(
dtool['wheel'].get('exclude', []), 'exclude'
)

data_dir = dtool.get('external-data', {}).get('directory', None)
if data_dir is not None:
toml_key = "tool.flit.external-data.directory"
Expand Down Expand Up @@ -255,6 +266,7 @@ def __init__(self):
self.referenced_files = []
self.sdist_include_patterns = []
self.sdist_exclude_patterns = []
self.wheel_exclude_patterns = []
self.dynamic_metadata = []
self.data_directory = None

Expand Down
35 changes: 2 additions & 33 deletions flit_core/flit_core/sdist.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from collections import defaultdict
from copy import copy
from glob import glob
from gzip import GzipFile
import io
import logging
Expand Down Expand Up @@ -34,36 +33,6 @@ def clean_tarinfo(ti, mtime=None):
return ti


class FilePatterns:
"""Manage a set of file inclusion/exclusion patterns relative to basedir"""
def __init__(self, patterns, basedir):
self.basedir = basedir

self.dirs = set()
self.files = set()

for pattern in patterns:
for path in sorted(glob(osp.join(basedir, pattern), recursive=True)):
rel = osp.relpath(path, basedir)
if osp.isdir(path):
self.dirs.add(rel)
else:
self.files.add(rel)

def match_file(self, rel_path):
if rel_path in self.files:
return True

return any(rel_path.startswith(d + os.sep) for d in self.dirs)

def match_dir(self, rel_path):
if rel_path in self.dirs:
return True

# Check if it's a subdirectory of any directory in the list
return any(rel_path.startswith(d + os.sep) for d in self.dirs)


class SdistBuilder:
"""Builds a minimal sdist

Expand All @@ -80,8 +49,8 @@ def __init__(self, module, metadata, cfgdir, reqs_by_extra, entrypoints,
self.entrypoints = entrypoints
self.extra_files = extra_files
self.data_directory = data_directory
self.includes = FilePatterns(include_patterns, str(cfgdir))
self.excludes = FilePatterns(exclude_patterns, str(cfgdir))
self.includes = common.FilePatterns(include_patterns, str(cfgdir))
self.excludes = common.FilePatterns(exclude_patterns, str(cfgdir))

@classmethod
def from_ini_path(cls, ini_path: Path):
Expand Down
10 changes: 7 additions & 3 deletions flit_core/flit_core/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def zip_timestamp_from_env() -> Optional[tuple]:

class WheelBuilder:
def __init__(
self, directory, module, metadata, entrypoints, target_fp, data_directory
self, directory, module, metadata, entrypoints, target_fp, data_directory,
exclude_patterns=(),
):
"""Build a wheel from a module/package
"""
Expand All @@ -70,6 +71,7 @@ def __init__(
self.metadata = metadata
self.entrypoints = entrypoints
self.data_directory = data_directory
self.excludes = common.FilePatterns(exclude_patterns, str(directory))

self.records = []
self.source_time_stamp = zip_timestamp_from_env()
Expand All @@ -87,7 +89,8 @@ def from_ini_path(cls, ini_path, target_fp):
module = common.Module(ini_info.module, directory)
metadata = common.make_metadata(module, ini_info)
return cls(
directory, module, metadata, entrypoints, target_fp, ini_info.data_directory
directory, module, metadata, entrypoints, target_fp, ini_info.data_directory,
ini_info.wheel_exclude_patterns,
)

@property
Expand Down Expand Up @@ -162,7 +165,8 @@ def copy_module(self):

for full_path in self.module.iter_files():
rel_path = osp.relpath(full_path, source_dir)
self._add_file(full_path, rel_path)
if not self.excludes.match_file(rel_path):
self._add_file(full_path, rel_path)

def add_pth(self):
with self._write_to_zip(self.module.name + ".pth") as f:
Expand Down
3 changes: 3 additions & 0 deletions flit_core/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ Source = "https://github.com/pypa/flit"

[tool.flit.sdist]
include = ["bootstrap_install.py", "build_dists.py"]

[tool.flit.wheel]
exclude = ["flit_core/tests"]