-
Notifications
You must be signed in to change notification settings - Fork 16
/
wagon.py
1539 lines (1248 loc) · 46.5 KB
/
wagon.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
########
# Copyright (c) 2014 GigaSpaces Technologies Ltd. All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# * See the License for the specific language governing permissions and
# * limitations under the License.
from __future__ import print_function
import os
import sys
import time
import json
import shlex
import shutil
import tarfile
import zipfile
import logging
import argparse
import tempfile
import subprocess
import importlib.metadata
import sysconfig
import venv
from io import StringIO
from threading import Thread
from contextlib import closing
from shutil import which
from pkginfo import Wheel
try:
import urllib.error
from urllib.request import urlopen
from urllib.request import URLopener
except ImportError:
import urllib
from urllib import urlopen
from urllib import URLopener
try:
from distro import linux_distribution
except ImportError:
try:
from platform import linux_distribution
except ImportError:
linux_distribution = None
DESCRIPTION = \
'''Create and install wheel based packages with their dependencies'''
METADATA_FILE_NAME = 'package.json'
DEFAULT_WHEELS_PATH = 'wheels'
DEFAULT_FILES_PATH = 'files'
DEFAULT_INDEX_SOURCE_URL_TEMPLATE = 'https://pypi.python.org/pypi/{0}/json'
IS_VIRTUALENV = sys.prefix != sys.base_prefix
PLATFORM = sys.platform
IS_WIN = (os.name == 'nt')
IS_DARWIN = (PLATFORM == 'darwin')
IS_LINUX = PLATFORM.startswith('linux')
ALL_PLATFORMS_TAG = 'any'
PROCESS_POLLING_INTERVAL = 0.1
def setup_logger():
handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(message)s')
handler.setFormatter(formatter)
logger = logging.getLogger('wagon')
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
return logger
logger = setup_logger()
def set_verbose():
# TODO: This is a very naive implementation. We should really
# use a logging configuration based on different levels of
# verbosity.
# The default level should be something in the middle and
# different levels of `--verbose` and `--quiet` flags should be
# supported.
global verbose
verbose = True
def is_verbose():
global verbose
try:
return verbose
except NameError:
verbose = False
return verbose
class PipeReader(Thread):
def __init__(self, fd, process, logger, log_level):
Thread.__init__(self)
self.fd = fd
self.process = process
self.logger = logger
self.log_level = log_level
self._aggr = StringIO()
self.aggr = ''
def run(self):
while self.process.poll() is None:
output = self.fd.readline().strip().decode('utf-8')
if len(output) > 0:
self._aggr.write(output)
self.logger.log(self.log_level, output)
else:
time.sleep(PROCESS_POLLING_INTERVAL)
self.aggr = self._aggr.getvalue()
def _run(cmd, suppress_errors=False, suppress_output=False):
"""Execute a command
"""
if is_verbose():
logger.debug('Executing: %r', cmd)
process = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stderr_log_level = logging.NOTSET if suppress_errors else logging.ERROR
stdout_log_level = logging.NOTSET if suppress_output else logging.DEBUG
stdout_thread = PipeReader(
process.stdout, process, logger, stdout_log_level)
stderr_thread = PipeReader(
process.stderr, process, logger, stderr_log_level)
stdout_thread.start()
stderr_thread.start()
while process.poll() is None:
time.sleep(PROCESS_POLLING_INTERVAL)
stdout_thread.join()
stderr_thread.join()
process.aggr_stdout = stdout_thread.aggr
process.aggr_stderr = stderr_thread.aggr
return process
class WagonError(Exception):
pass
def _construct_wheel_command(wheels_path='package',
wheel_args=None,
requirement_files=None,
package=None, pip_path=None):
pip = [pip_path] if pip_path else _pip()
wheel_cmd = pip + [
'wheel',
'--wheel-dir', wheels_path,
'--find-links', wheels_path
]
if wheel_args:
if not isinstance(wheel_args, list):
wheel_args = shlex.split(wheel_args, posix=not IS_WIN)
wheel_cmd += wheel_args
if requirement_files:
for req_file in requirement_files:
wheel_cmd += ['-r', req_file]
if package:
wheel_cmd.append(package)
return wheel_cmd
def wheel(package,
requirement_files=None,
wheels_path='package',
wheel_args=None,
pip_path=None):
logger.info('Downloading Wheels for %s...', package)
if requirement_files:
wheel_command = _construct_wheel_command(
wheels_path,
wheel_args,
requirement_files,
pip_path=pip_path)
process = _run(wheel_command)
if not process.returncode == 0:
raise WagonError('Failed to download wheels for: {0}'.format(
requirement_files))
wheel_command = _construct_wheel_command(
wheels_path,
wheel_args,
package=package,
pip_path=pip_path)
process = _run(wheel_command)
if not process.returncode == 0:
raise WagonError(
'Failed to download wheels for: {0}'.format(package))
wheels = _get_downloaded_wheels(wheels_path)
return wheels
def _pip(venv=None):
pip_module = 'pip'
return [_get_python_path(venv), '-m', pip_module]
def _construct_pip_command(package,
wheels_path,
venv,
requirement_files=None,
upgrade=False,
install_args=None):
requirement_files = requirement_files or []
pip_command = _pip(venv) + ['install']
for req_file in requirement_files:
pip_command.extend(['-r', req_file])
pip_command += [
package, '--no-index', '--find-links', wheels_path, '--pre'
]
if upgrade:
pip_command.append('--upgrade')
if install_args:
if not isinstance(install_args, list):
install_args = shlex.split(install_args, posix=not IS_WIN)
pip_command += install_args
return pip_command
def install_package(package,
wheels_path,
venv=None,
requirement_files=None,
upgrade=False,
install_args=None):
"""Install a Python package.
Can specify a specific version.
Can specify a prerelease.
Can specify a venv to install in.
Can specify a list of paths or urls to requirement txt files.
Can specify a local wheels_path to use for offline installation.
Can request an upgrade.
"""
requirement_files = requirement_files or []
logger.info('Installing %s...', package)
if venv and not os.path.isdir(venv):
raise WagonError('virtualenv {0} does not exist'.format(venv))
pip_command = _construct_pip_command(
package,
wheels_path,
venv,
requirement_files,
upgrade,
install_args)
if IS_VIRTUALENV and not venv:
logger.info('Installing within current virtualenv')
result = _run(pip_command)
if not result.returncode == 0:
raise WagonError(
'Could not install package: {0} (`{1}` returned `{2}`)'.format(
package, pip_command, result.aggr_stderr))
def _get_downloaded_wheels(path):
return sorted([filename for filename in os.listdir(path)
if os.path.splitext(filename)[1].lower() == '.whl'])
def _open_url(url):
try:
response = urlopen(url)
# Sometimes bytes are returned here and sometimes strings.
try:
response.text = response.read().decode('utf-8')
except UnicodeDecodeError:
response.text = response.read()
response.code = 200
except urllib.error.HTTPError as ex:
response = type('obj', (object,), {'code': ex.code})
return response
def _download_file(url, destination):
logger.info('Downloading %s to %s...', url, destination)
response = _open_url(url)
if not response.code == 200:
raise WagonError(
"Failed to download file. Request to {0} "
"failed with HTTP Error: {1}".format(url, response.code))
final_url = response.geturl()
if final_url != url and is_verbose():
logger.debug('Redirected to %s', final_url)
f = URLopener()
f.retrieve(final_url, destination)
def _http_request(url):
response = _open_url(url)
if response.code == 200:
return response.text
else:
# TODO: Fix message. Not generic enough
raise WagonError(
"Failed to retrieve info for package. Request to {0} "
"failed with HTTP Error: {1}".format(url, response.code))
def _zip(source, destination):
logger.info('Creating zip archive: %s...', destination)
with closing(zipfile.ZipFile(destination, 'w')) as zip_file:
for root, _, files in os.walk(source):
for filename in files:
file_path = os.path.join(root, filename)
source_dir = os.path.dirname(source)
zip_file.write(
file_path, os.path.relpath(file_path, source_dir))
def _unzip(archive, destination):
logger.debug('Extracting zip %s to %s...', archive, destination)
with closing(zipfile.ZipFile(archive, 'r')) as zip_file:
zip_file.extractall(destination)
def _tar(source, destination):
logger.info('Creating tgz archive: %s...', destination)
with closing(tarfile.open(destination, 'w:gz')) as tar:
tar.add(source, arcname=os.path.basename(source))
def _untar(archive, destination):
logger.debug('Extracting tgz %s to %s...', archive, destination)
with closing(tarfile.open(name=archive)) as tar:
tar.extractall(path=destination, members=tar.getmembers())
def _get_wheel_tags(wheel_name):
filename, _ = os.path.splitext(os.path.basename(wheel_name))
return filename.split('-')
def _get_platform_from_wheel_name(wheel_name):
"""Extract the platform of a wheel from its file name.
"""
return _get_wheel_tags(wheel_name)[-1]
def _get_platform_for_set_of_wheels(wheels_path):
"""For any set of wheel files, extracts a single platform.
Since a set of wheels created or downloaded on one machine can only
be for a single platform, if any wheel in the set has a platform
which is not `any`, it will be used with one exception:
In Linux, a wagon can contain wheels for both manylinux1 and linux.
If, at any point we find that a wheel has `linux` as a platform,
it will be used since it means it doesn't cross-fit all distros.
If a platform other than `any` was not found, `any` will be assumed
"""
real_platform = ''
for wheel in _get_downloaded_wheels(wheels_path):
platform = _get_platform_from_wheel_name(
os.path.join(wheels_path, wheel))
if 'linux' in platform and 'manylinux' not in platform:
# Means either linux_x64_86 or linux_i686 on all wheels
# If, at any point, a wheel matches this, it will be
# returned so it'll only match that platform.
return platform
elif platform != ALL_PLATFORMS_TAG:
# Means it can be either Windows, OSX or manylinux1 on all wheels
real_platform = platform
return real_platform or ALL_PLATFORMS_TAG
def _get_python_version():
version = sys.version_info
return 'py{0}{1}'.format(version[0], version[1])
def get_platform():
return sysconfig.get_platform().replace('.', '_').replace('-', '_')
def _get_os_properties():
"""Retrieve distribution properties.
**Note that platform.linux_distribution and platform.dist are deprecated
and will be removed in Python 3.7. By that time, distro will become
mandatory.
"""
os_properties = {
'distribution': None,
'distribution_version': None,
'distribution_release': None,
}
try:
distribution, version, release = linux_distribution(
full_distribution_name=False,
)
os_properties['distribution'] = distribution.lower()
os_properties['distribution_version'] = version.lower()
os_properties['distribution_release'] = release.lower()
except TypeError:
pass
return os_properties
def _get_python_path(venv=None):
if not venv:
return sys.executable
if IS_WIN:
return os.path.join(venv, 'Scripts', 'python.exe')
else:
return os.path.join(venv, 'bin', 'python')
def _check_installed(package, venv=None):
process = _run(_pip(venv) + ['freeze'])
pkgs = ['{0}=='.format(package), '{0}=='.format(package.replace('_', '-'))]
if any(package_name in process.aggr_stdout for package_name in pkgs):
logger.debug('Package %s is installed in %s', package, venv)
return True
logger.debug('Package %s is not installed in %s', package, venv)
return False
def _make_virtualenv(virtualenv_dir=None):
if not virtualenv_dir:
virtualenv_dir = tempfile.mkdtemp()
logger.debug('Creating Virtualenv %s...', virtualenv_dir)
venv.create(virtualenv_dir, with_pip=True)
return virtualenv_dir
def _get_package_info_from_pypi(source):
pypi_url = DEFAULT_INDEX_SOURCE_URL_TEMPLATE.format(source)
if is_verbose():
logger.debug('Getting metadata for %s from %s...', source, pypi_url)
package_data = json.loads(_http_request(pypi_url))
return package_data['info']
def _get_wagon_version():
return importlib.metadata.distribution('wagon').version
def _set_python_versions(python_versions=None):
if python_versions:
return ['py{0}'.format(version) for version in python_versions]
else:
return [_get_python_version()]
def _set_python_requires(wheels_path):
python_requires = set()
wheels = _get_downloaded_wheels(wheels_path)
for _wheel in wheels:
wheel_path = os.path.join(wheels_path, _wheel)
package = Wheel(wheel_path)
if package.requires_python:
python_requires.add(package.requires_python)
return ','.join(python_requires)
def _get_name_and_version_from_setup(source_path):
def get_arg(arg_type, setuppy_path):
return _run([
sys.executable, setuppy_path, '--' + arg_type
]).aggr_stdout.strip()
logger.debug('setup.py file found. Retrieving name and version...')
setuppy_path = os.path.join(source_path, 'setup.py')
package_name = get_arg('name', setuppy_path)
package_version = get_arg('version', setuppy_path)
return package_name, package_version
def _handle_output_file(filepath, force):
if os.path.isfile(filepath):
if force:
logger.info('Removing previous archive...')
os.remove(filepath)
else:
raise WagonError(
'Destination archive already exists: {0}. You can use '
'the -f flag to overwrite.'.format(filepath))
def _generate_metadata_file(workdir,
archive_name,
platform,
python_versions,
python_requires,
package_name,
package_version,
build_tag,
package_source,
wheels,
files):
"""Generate a metadata file for the package.
"""
logger.debug('Generating Metadata...')
metadata = {
'created_by_wagon_version': _get_wagon_version(),
'archive_name': archive_name,
'supported_platform': platform,
'supported_python_versions': python_versions,
'python_requires': python_requires,
'build_server_os_properties': {
'distribution': None,
'distribution_version': None,
'distribution_release': None,
},
'package_name': package_name,
'package_version': package_version,
'package_build_tag': build_tag,
'package_source': package_source,
'wheels': wheels,
'files': files,
}
if IS_LINUX and platform != ALL_PLATFORMS_TAG:
metadata.update(
{
'build_server_os_properties': _get_os_properties(),
}
)
formatted_metadata = json.dumps(metadata, indent=4, sort_keys=True)
if is_verbose():
logger.debug('Metadata is: %s', formatted_metadata)
output_path = os.path.join(workdir, METADATA_FILE_NAME)
with open(output_path, 'w') as f:
logger.debug('Writing metadata to file: %s', output_path)
f.write(formatted_metadata)
def _set_archive_name(package_name,
package_version,
python_versions,
platform,
build_tag=''):
"""Set the format of the output archive file.
We should aspire for the name of the archive to be
as compatible as possible with the wheel naming convention
described here:
https://www.python.org/dev/peps/pep-0491/#file-name-convention,
as we're basically providing a "wheel" of our package.
"""
package_name = package_name.replace('-', '_')
python_versions = '.'.join(python_versions)
archive_name_tags = [
package_name,
package_version,
python_versions,
'none',
platform,
]
if build_tag:
archive_name_tags.insert(2, build_tag)
archive_name = '{0}.wgn'.format('-'.join(archive_name_tags))
return archive_name
def get_source_name_and_version(source):
"""Retrieve the source package's name and version.
If the source is a path, the name and version will be retrieved
by querying the setup.py file in the path.
If the source is PACKAGE_NAME==PACKAGE_VERSION, they will be used as
the name and version.
If the source is PACKAGE_NAME, the version will be extracted from
the wheel of the latest version.
"""
if os.path.isfile(os.path.join(source, 'setup.py')):
package_name, package_version = \
_get_name_and_version_from_setup(source)
# TODO: maybe we don't want to be that explicit and allow using >=
# elif any(symbol in source for symbol in ['==', '>=', '<=']):
elif '==' in source:
base_name, package_version = source.split('==')
package_name = _get_package_info_from_pypi(base_name)['name']
else:
package_info = _get_package_info_from_pypi(source)
package_name = package_info['name']
package_version = package_info['version']
return package_name, package_version
def _create_wagon_archive(source_path, archive_path, archive_format='tar.gz'):
if archive_format.lower() == 'zip':
_zip(source_path, archive_path)
elif archive_format.lower() == 'tar.gz':
_tar(source_path, archive_path)
else:
raise WagonError(
'Unsupported archive format to create: {0} '
'(Must be one of [zip, tar.gz]).'.format(archive_format.lower()))
def get_source(source):
"""Return a pip-installable source
If the source is a url to a package's tar file,
this will download the source and extract it to a temporary directory.
If the source is neither a url nor a local path, and is not provided
as PACKAGE_NAME==PACKAGE_VERSION, the provided source string
will be regarded as the source, which, by default, will assume
that the string is a name of a package in PyPI.
"""
def extract_source(source, destination):
if tarfile.is_tarfile(source):
_untar(source, destination)
elif zipfile.is_zipfile(source):
_unzip(source, destination)
else:
raise WagonError(
'Failed to extract {0}. Please verify that the '
'provided file is a valid zip or tar.gz '
'archive'.format(os.path.basename(source)))
source = os.path.join(
destination, [d for d in next(os.walk(destination))[1]][0])
return source
logger.debug('Retrieving source...')
if '://' in source:
split = source.split('://')
schema = split[0]
if schema in ['file', 'http', 'https']:
tmpdir = tempfile.mkdtemp()
fd, tmpfile = tempfile.mkstemp()
os.close(fd)
try:
_download_file(source, tmpfile)
source = extract_source(tmpfile, tmpdir)
finally:
os.remove(tmpfile)
else:
raise WagonError('Source URL type {0} is not supported'.format(
schema))
elif os.path.isfile(source):
tmpdir = tempfile.mkdtemp()
try:
source = extract_source(source, tmpdir)
except Exception:
shutil.rmtree(tmpdir)
raise
elif os.path.isdir(os.path.expanduser(source)):
source = os.path.expanduser(source)
elif '==' in source:
base_name, version = source.split('==')
source = _get_package_info_from_pypi(base_name)['name']
source = '{0}=={1}'.format(source, version)
else:
source = _get_package_info_from_pypi(source)['name']
logger.debug('Source is: %s', source)
return source
def _get_metadata(source_path):
with open(os.path.join(source_path, METADATA_FILE_NAME)) as metadata_file:
metadata = json.loads(metadata_file.read())
return metadata
def create(source,
requirement_files=None,
force=False,
keep_wheels=False,
archive_destination_dir='.',
python_versions=None,
validate_archive=False,
wheel_args='',
archive_format='zip',
build_tag='',
pip_paths=None,
supported_platform=None,
add_file=None):
"""Create a Wagon archive and returns its path.
Package name and version are extracted from the setup.py file
of the `source` or from the PACKAGE_NAME==PACKAGE_VERSION if the source
is a PyPI package.
Supported `python_versions` must be in the format e.g [33, 27, 2, 3]..
`force` will remove any excess dirs or archives before creation.
`requirement_files` can be either a link/local path to a
requirements.txt file or just `.`, in which case requirement files
will be automatically extracted from either the GitHub archive URL
or the local path provided provided in `source`.
"""
_assert_linux_distribution_exists()
logger.info('Creating archive for %s...', source)
processed_source = get_source(source)
if os.path.isdir(processed_source) and not \
os.path.isfile(os.path.join(processed_source, 'setup.py')):
raise WagonError(
'Source directory must contain a setup.py file')
package_name, package_version = get_source_name_and_version(
processed_source)
tempdir = tempfile.mkdtemp()
workdir = os.path.join(tempdir, package_name)
wheels_path = os.path.join(workdir, DEFAULT_WHEELS_PATH)
files_path = os.path.join(workdir, DEFAULT_FILES_PATH)
files = []
pip_paths = pip_paths if pip_paths else [None]
try:
for pip_path in pip_paths:
wheels = wheel(
processed_source,
requirement_files,
wheels_path,
wheel_args,
pip_path)
finally:
if processed_source != source:
shutil.rmtree(processed_source, ignore_errors=True)
platform = _get_platform_for_set_of_wheels(wheels_path)
if supported_platform is not None:
platform = supported_platform
elif 'manylinux' in platform:
# this is a hack to support Cloudify 5.1, who doesn't handle
# manylinux wagons. Rename manylinux{1,2010,2014} to linux.
# handle new wheel naming convention manylinux*.manylinux*
# by getting the last part of platform
# example :
# >>> arch='manylinux_2_5_x86_64.manylinux1_x86_64'
# >>> arch != 'x86_64' and arch.count('_')>0:
# ... arch = arch.partition('_')[2]
# >>> arch
# 'x86_64'
# >>> arch="manylinux_2_5_x86_64"
# >>> while arch != 'x86_64' and arch.count('_')>0:
# ... arch = arch.partition('_')[2]
# >>> arch
# 'x86_64'
# >>> arch="manylinux_2_12_i686.manylinux2010_i686"
# >>> while arch != 'x86_64' and arch.count('_')>0:
# ... arch = arch.partition('_')[2]
# >>> arch
# 'i686'
arch = platform
while arch != 'x86_64' and arch.count('_') > 0:
arch = arch.partition('_')[2]
platform = 'linux_{0}'.format(arch or 'x86_64')
if is_verbose():
logger.debug('Platform is: %s', platform)
python_versions = _set_python_versions(python_versions)
python_requires = _set_python_requires(wheels_path)
if not os.path.isdir(archive_destination_dir):
os.makedirs(archive_destination_dir)
archive_name = _set_archive_name(
package_name, package_version, python_versions, platform, build_tag)
archive_path = os.path.join(archive_destination_dir, archive_name)
_handle_output_file(archive_path, force)
if add_file:
for source_path in add_file:
if _validate_file_path(source_path):
os.makedirs(
files_path,
exist_ok=True,
)
filename = os.path.basename(source_path)
destination_path = os.path.join(
files_path,
filename,
)
shutil.copy(
source_path,
destination_path,
)
files.append(
os.path.basename(
destination_path,
)
)
_generate_metadata_file(
workdir,
archive_name,
platform,
python_versions,
python_requires,
package_name,
package_version,
build_tag,
source,
wheels,
files,
)
_create_wagon_archive(workdir, archive_path, archive_format)
if not keep_wheels:
logger.debug('Removing work directory...')
shutil.rmtree(tempdir, ignore_errors=True)
if validate_archive:
validate(archive_path)
logger.info('Wagon created successfully at: %s', archive_path)
return archive_path
def _is_platform_supported(supported_platform, machine_platform):
if not IS_LINUX and machine_platform != supported_platform:
return False
elif IS_LINUX:
if 'manylinux' not in supported_platform \
and machine_platform != supported_platform:
return False
machine_arch = machine_platform.split('_')[1]
supported_arch = supported_platform.split('_')[1]
if supported_arch != machine_arch:
return False
return True
def install(source,
venv=None,
requirement_files=None,
upgrade=False,
ignore_platform=False,
install_args=''):
"""Install a Wagon archive.
This can install in a provided `venv` or in the current
virtualenv in case one is currently active.
`upgrade` is merely pip's upgrade.
`ignore_platform` will allow to ignore the platform check, meaning
that if an archive was created for a specific platform (e.g. win32),
and the current platform is different, it will still attempt to
install it.
Platform check will fail on the following:
If not linux and no platform match (e.g. win32 vs. darwin)
If linux and:
architecture doesn't match (e.g. manylinux1_x86_64 vs. linux_i686)
wheel not manylinux and no platform match (linux_x86_64 vs. linux_i686)
"""
requirement_files = requirement_files or []
logger.info('Installing %s', source)
processed_source = get_source(source)
metadata = _get_metadata(processed_source)
def raise_unsupported_platform(machine_platform):
# TODO: Print which platform is supported?
raise WagonError(
'Platform unsupported for wagon ({0})'.format(
machine_platform))
try:
supported_platform = metadata['supported_platform']
if not ignore_platform and supported_platform != ALL_PLATFORMS_TAG:
logger.debug(
'Validating Platform %s is supported...', supported_platform)
machine_platform = get_platform()
if not _is_platform_supported(
supported_platform, machine_platform):
raise_unsupported_platform(machine_platform)
wheels_path = os.path.join(processed_source, DEFAULT_WHEELS_PATH)
install_package(
metadata['package_name'],
wheels_path,
venv,
requirement_files,
upgrade,
install_args)
finally:
# Install can only be done on local or remote archives.
# This means that a temporary directory is always created
# with the sources to install within it. This is why we can allow
# ourselves to delete the parent dir without worrying.
# TODO: Make this even less dangerous by changing `get_source`
# to return the directory to delete instead. Much safer.
if processed_source != source:
shutil.rmtree(os.path.dirname(
processed_source), ignore_errors=True)
def validate(source):
"""Validate a Wagon archive. Return True if succeeds, False otherwise.
It also prints a list of all validation errors.
This will test that some of the metadata is solid, that
the required wheels are present within the archives and that
the package is installable.
Note that if the metadata file is corrupted, validation
of the required wheels will be corrupted as well, since validation
checks that the required wheels exist vs. the list of wheels
supplied in the `wheels` key.
"""
logger.info('Validating %s', source)
processed_source = get_source(source)
metadata = _get_metadata(processed_source)
wheels_path = os.path.join(processed_source, DEFAULT_WHEELS_PATH)
validation_errors = []
logger.debug('Verifying that all required files exist...')
for wheel in metadata['wheels']:
if not os.path.isfile(os.path.join(wheels_path, wheel)):
validation_errors.append(
'{0} is missing from the archive'.format(wheel))
logger.debug('Testing package installation...')
tmpenv = _make_virtualenv()
try:
install(source=processed_source, venv=tmpenv)
if not _check_installed(metadata['package_name'], tmpenv):
validation_errors.append(
'{0} failed to install (Reason unknown)'.format(
metadata['package_name']))
finally:
shutil.rmtree(tmpenv)
if validation_errors:
logger.info('Validation failed!')
for error in validation_errors:
logger.info(error)
logger.info('Source can be found at: %s', processed_source)
else:
logger.info('Validation Passed!')
if processed_source != source:
shutil.rmtree(processed_source)
return validation_errors
def show(source):
"""Merely returns the metadata for the provided archive.
"""
if is_verbose():
logger.info('Retrieving Metadata for: %s', source)
processed_source = get_source(source)
metadata = _get_metadata(processed_source)
shutil.rmtree(processed_source)