Debian Codegraph @

tasks/build_/_pkg_dir.py

raw symbols SCIP
1 # -*- coding: ascii -*-
2 #
3 # Copyright 2007 - 2025
4 # Andr\xe9 Malo or his licensors, as applicable
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 """
18 Build Tasks
19 ~~~~~~~~~~~
20
21 """
22
23 import contextlib as _contextlib
24 import os as _os
25 import tempfile as _tempfile
26
27 from ..util import package as _package
28
29
30 @_contextlib.contextmanager
31 def sdist(ctx, name):
32 """
33 Create temporary package directory as a context
34
35 Parameters:
36 name (str):
37 The sub-package name
38
39 Returns:
40 The directory name
41 """
42 dotsdist = ".sdist"
43 try:
44 with ctx.shell.root_dir():
45 ctx.shell.mkdir_p(dotsdist)
46 directory = _tempfile.mkdtemp(dir=dotsdist)
47
48 # cp target cannot exist before
49 target = _os.path.join(directory, "x")
50 ctx.shell.cp_r(".", target, ignore=_ignore(ctx))
51
52 ext_map = {
53 ".py": "setup.py",
54 ".toml": "pyproject.toml",
55 ".MANIFEST": "MANIFEST.in",
56 }
57 for ext, dest in ext_map.items():
58 dest = _os.path.join(target, dest)
59 ctx.shell.rm(dest)
60
61 src = _os.path.join(target, "setups", name + ext)
62 if _os.path.isfile(src):
63 with open(src, "rb") as fp:
64 content = fp.read().decode("latin-1")
65 if "$BASE" in content or "$VERSION" in content:
66 meta = _package.find_meta()
67 content = content.replace(
68 "$VERSION", meta["Version"]
69 ).replace("$BASE", meta["Name"])
70 with open(dest, "wb") as fp:
71 fp.write(content.encode("latin-1"))
72 else:
73 ctx.shell.cp(src, dest)
74 if not _os.path.isfile(_os.path.join(target, "MANIFEST.in")):
75 with open(_os.path.join(target, "MANIFEST.in"), "wb"):
76 pass
77 ctx.shell.rm_rf(_os.path.join(target, "setups"))
78
79 _os.chdir(target)
80 yield target
81 finally:
82 ctx.shell.rm_rf(dotsdist)
83
84
85 def _ignore(ctx):
86 """
87 Make ignore function (as expected by shutil.copytree)
88
89 Returns:
90 callable: The copytree ignore callback function
91 """
92 # Config option to add more files/directories to ignore
93 extra = ctx.get("setup_ignore")
94
95 # Collect all checked-in files, those are good.
96 with ctx.shell.root_dir():
97 files = set(
98 _os.path.normpath(item)
99 for item in ctx.run("git ls-files -z", hide=True).stdout.split(
100 "\0"
101 )
102 )
103 for item in list(files):
104 dirname = _os.path.dirname(item)
105 if dirname:
106 files.add(dirname)
107
108 root = []
109 extra = set(_os.path.normpath(item) for item in extra or ())
110
111 def ignorable(node, children):
112 """
113 Ignore function as called by shutil.copytree
114
115 Parameters:
116 node (str):
117 The directory
118
119 children (list):
120 The directory entries
121
122 Returns:
123 list: The entries to ignore
124 """
125 if not root:
126 root.append(node)
127 node = node[len(root[0]) + 1 :]
128
129 # Collect entries to ignore
130 result = []
131 for item in children:
132 path = _os.path.normpath(
133 _os.path.join(node, item) if node else item
134 )
135
136 # explicit ignores takes precedence
137 if path in extra:
138 result.append(item)
139
140 # If it looks weird, check back with the git list
141 elif item.startswith(".") or item.endswith(".egg-info"):
142 if path not in files:
143 result.append(item)
144
145 return result
146
147 return ignorable