forked from conan-io/conan
/
export.py
221 lines (184 loc) · 8.88 KB
/
export.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import ast
import os
import shutil
from conans.client.cmd.export_linter import conan_linter
from conans.client.file_copier import FileCopier
from conans.client.output import ScopedOutput
from conans.client.source import get_scm_data
from conans.errors import ConanException
from conans.model.manifest import FileTreeManifest
from conans.model.scm import SCM
from conans.paths import CONAN_MANIFEST, CONANFILE
from conans.util.files import save, rmdir, is_dirty, set_dirty, mkdir, load
from conans.util.log import logger
from conans.search.search import search_recipes
def export_alias(reference, target_reference, client_cache):
if reference.name != target_reference.name:
raise ConanException("An alias can only be defined to a package with the same name")
conanfile = """
from conans import ConanFile
class AliasConanfile(ConanFile):
alias = "%s"
""" % str(target_reference)
export_path = client_cache.export(reference)
mkdir(export_path)
save(os.path.join(export_path, CONANFILE), conanfile)
mkdir(client_cache.export_sources(reference))
digest = FileTreeManifest.create(export_path)
digest.save(export_path)
def cmd_export(conanfile_path, conanfile, reference, keep_source, output, client_cache):
""" Export the recipe
param conanfile_path: the original source directory of the user containing a
conanfile.py
"""
logger.debug("Exporting %s" % conanfile_path)
output.highlight("Exporting package recipe")
conan_linter(conanfile_path, output)
for field in ["url", "license", "description"]:
field_value = getattr(conanfile, field, None)
if not field_value:
output.warn("Conanfile doesn't have '%s'.\n"
"It is recommended to add it as attribute" % field)
conan_ref_str = str(reference)
# Maybe a platform check could be added, but depends on disk partition
refs = search_recipes(client_cache, conan_ref_str, ignorecase=True)
if refs and reference not in refs:
raise ConanException("Cannot export package with same name but different case\n"
"You exported '%s' but already existing '%s'"
% (conan_ref_str, " ".join(str(s) for s in refs)))
with client_cache.conanfile_write_lock(reference):
_export_conanfile(conanfile_path, conanfile.output, client_cache, conanfile, reference,
keep_source)
def _capture_export_scm_data(conanfile, conanfile_dir, destination_folder, output, paths, conan_ref):
scm_src_file = paths.scm_folder(conan_ref)
if os.path.exists(scm_src_file):
os.unlink(scm_src_file)
scm_data = get_scm_data(conanfile)
if not scm_data or not (scm_data.capture_origin or scm_data.capture_revision):
return
scm = SCM(scm_data, conanfile_dir)
if scm_data.url == "auto":
origin = scm.get_remote_url()
if not origin:
raise ConanException("Repo origin cannot be deduced by 'auto'")
if os.path.exists(origin):
output.warn("Repo origin looks like a local path: %s" % origin)
origin = origin.replace("\\", "/")
output.success("Repo origin deduced by 'auto': %s" % origin)
scm_data.url = origin
if scm_data.revision == "auto":
scm_data.revision = scm.get_revision()
output.success("Revision deduced by 'auto': %s" % scm_data.revision)
# Generate the scm_folder.txt file pointing to the src_path
src_path = scm.get_repo_root()
save(scm_src_file, src_path.replace("\\", "/"))
# Parsing and replacing the SCM field
conanfile_path = os.path.join(destination_folder, "conanfile.py")
content = load(conanfile_path)
lines = content.splitlines(True)
tree = ast.parse(content)
to_replace = []
for item in tree.body:
if isinstance(item, ast.ClassDef):
statements = item.body
for i, stmt in enumerate(item.body):
if isinstance(stmt, ast.Assign) and len(stmt.targets) == 1:
if isinstance(stmt.targets[0], ast.Name) and stmt.targets[0].id == "scm":
try:
next_line = statements[i+1].lineno - 1
except IndexError:
next_line = stmt.lineno
replace = [line for line in lines[(stmt.lineno-1):next_line]
if line.strip()]
to_replace.append("".join(replace).lstrip())
break
if len(to_replace) != 1:
raise ConanException("The conanfile.py defines more than one class level 'scm' attribute")
new_text = "scm = " + ",\n ".join(str(scm_data).split(",")) + "\n"
content = content.replace(to_replace[0], new_text)
save(conanfile_path, content)
def _export_conanfile(conanfile_path, output, paths, conanfile, conan_ref, keep_source):
exports_folder = paths.export(conan_ref)
exports_source_folder = paths.export_sources(conan_ref, conanfile.short_paths)
previous_digest = _init_export_folder(exports_folder, exports_source_folder)
_execute_export(conanfile_path, conanfile, exports_folder, exports_source_folder, output)
shutil.copy2(conanfile_path, os.path.join(exports_folder, CONANFILE))
_capture_export_scm_data(conanfile, os.path.dirname(conanfile_path), exports_folder,
output, paths, conan_ref)
digest = FileTreeManifest.create(exports_folder, exports_source_folder)
if previous_digest and previous_digest == digest:
output.info("The stored package has not changed")
modified_recipe = False
digest = previous_digest # Use the old one, keep old timestamp
else:
output.success('A new %s version was exported' % CONANFILE)
output.info('Folder: %s' % exports_folder)
modified_recipe = True
digest.save(exports_folder)
source = paths.source(conan_ref, conanfile.short_paths)
remove = False
if is_dirty(source):
output.info("Source folder is corrupted, forcing removal")
remove = True
elif modified_recipe and not keep_source and os.path.exists(source):
output.info("Package recipe modified in export, forcing source folder removal")
output.info("Use the --keep-source, -k option to skip it")
remove = True
if remove:
output.info("Removing 'source' folder, this can take a while for big packages")
try:
# remove only the internal
rmdir(source)
except BaseException as e:
output.error("Unable to delete source folder. "
"Will be marked as corrupted for deletion")
output.warn(str(e))
set_dirty(source)
def _init_export_folder(destination_folder, destination_src_folder):
previous_digest = None
try:
if os.path.exists(destination_folder):
if os.path.exists(os.path.join(destination_folder, CONAN_MANIFEST)):
previous_digest = FileTreeManifest.load(destination_folder)
# Maybe here we want to invalidate cache
rmdir(destination_folder)
os.makedirs(destination_folder)
except Exception as e:
raise ConanException("Unable to create folder %s\n%s" % (destination_folder, str(e)))
try:
if os.path.exists(destination_src_folder):
rmdir(destination_src_folder)
os.makedirs(destination_src_folder)
except Exception as e:
raise ConanException("Unable to create folder %s\n%s" % (destination_src_folder, str(e)))
return previous_digest
def _execute_export(conanfile_path, conanfile, destination_folder, destination_source_folder,
output):
if isinstance(conanfile.exports, str):
conanfile.exports = (conanfile.exports, )
if isinstance(conanfile.exports_sources, str):
conanfile.exports_sources = (conanfile.exports_sources, )
origin_folder = os.path.dirname(conanfile_path)
def classify_patterns(patterns):
patterns = patterns or []
included, excluded = [], []
for p in patterns:
if p.startswith("!"):
excluded.append(p[1:])
else:
included.append(p)
return included, excluded
included_exports, excluded_exports = classify_patterns(conanfile.exports)
included_sources, excluded_sources = classify_patterns(conanfile.exports_sources)
try:
os.unlink(os.path.join(origin_folder, CONANFILE + 'c'))
except OSError:
pass
copier = FileCopier(origin_folder, destination_folder)
for pattern in included_exports:
copier(pattern, links=True, excludes=excluded_exports)
copier = FileCopier(origin_folder, destination_source_folder)
for pattern in included_sources:
copier(pattern, links=True, excludes=excluded_sources)
package_output = ScopedOutput("%s export" % output.scope, output)
copier.report(package_output)