/
install.py
1043 lines (878 loc) · 34.5 KB
/
install.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
# (c) 2012-2014 Continuum Analytics, Inc. / http://continuum.io
# All Rights Reserved
#
# conda is distributed under the terms of the BSD 3-clause license.
# Consult LICENSE.txt or http://opensource.org/licenses/BSD-3-Clause.
""" This module contains:
* all low-level code for extracting, linking and unlinking packages
* a very simple CLI
These API functions have argument names referring to:
dist: canonical package name (e.g. 'numpy-1.6.2-py26_0')
pkgs_dir: the "packages directory" (e.g. '/opt/anaconda/pkgs' or
'/home/joe/envs/.pkgs')
prefix: the prefix of a particular environment, which may also
be the "default" environment (i.e. sys.prefix),
but is otherwise something like '/opt/anaconda/envs/foo',
or even any prefix, e.g. '/home/joe/myenv'
"""
from __future__ import absolute_import, division, print_function, unicode_literals
import errno
import functools
import json
import logging
import os
import re
import shlex
import shutil
import stat
import struct
import subprocess
import sys
import tarfile
import traceback
from collections import namedtuple
from .common.disk import backoff_unlink, exp_backoff_fn, rm_rf
from enum import Enum
from itertools import chain
from os.path import abspath, basename, dirname, isdir, isfile, islink, join, normcase, normpath
from . import CondaError
from .base.constants import UTF8
from .base.context import context
from .common.url import path_to_url
from .exceptions import CondaOSError, LinkError, PaddingError
from .lock import DirectoryLock, FileLock
from .models.channel import Channel
from .utils import on_win
# conda-build compatibility
from .common.disk import delete_trash, move_to_trash # NOQA
if on_win:
import ctypes
from ctypes import wintypes
CreateHardLink = ctypes.windll.kernel32.CreateHardLinkW
CreateHardLink.restype = wintypes.BOOL
CreateHardLink.argtypes = [wintypes.LPCWSTR, wintypes.LPCWSTR,
wintypes.LPVOID]
try:
CreateSymbolicLink = ctypes.windll.kernel32.CreateSymbolicLinkW
CreateSymbolicLink.restype = wintypes.BOOL
CreateSymbolicLink.argtypes = [wintypes.LPCWSTR, wintypes.LPCWSTR,
wintypes.DWORD]
except AttributeError:
CreateSymbolicLink = None
def win_hard_link(src, dst):
"Equivalent to os.link, using the win32 CreateHardLink call."
if not CreateHardLink(dst, src, None):
raise CondaOSError('win32 hard link failed')
def win_soft_link(src, dst):
"Equivalent to os.symlink, using the win32 CreateSymbolicLink call."
if CreateSymbolicLink is None:
raise CondaOSError('win32 soft link not supported')
if not CreateSymbolicLink(dst, src, isdir(src)):
raise CondaOSError('win32 soft link failed')
def win_conda_bat_redirect(src, dst, shell):
"""Special function for Windows XP where the `CreateSymbolicLink`
function is not available.
Simply creates a `.bat` file at `dst` which calls `src` together with
all command line arguments.
Works of course only with callable files, e.g. `.bat` or `.exe` files.
"""
from conda.utils import shells
try:
os.makedirs(os.path.dirname(dst))
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(os.path.dirname(dst)):
pass
else:
raise
# bat file redirect
if not os.path.isfile(dst + '.bat'):
with open(dst + '.bat', 'w') as f:
f.write('@echo off\ncall "%s" %%*\n' % src)
# TODO: probably need one here for powershell at some point
# This one is for bash/cygwin/msys
# set default shell to bash.exe when not provided, as that's most common
if not shell:
shell = "bash.exe"
# technically these are "links" - but islink doesn't work on win
if not os.path.isfile(dst):
with open(dst, "w") as f:
f.write("#!/usr/bin/env bash \n")
if src.endswith("conda"):
f.write('%s "$@"' % shells[shell]['path_to'](src+".exe"))
else:
f.write('source %s "$@"' % shells[shell]['path_to'](src))
# Make the new file executable
# http://stackoverflow.com/a/30463972/1170370
mode = os.stat(dst).st_mode
mode |= (mode & 292) >> 2 # copy R bits to X
os.chmod(dst, mode)
log = logging.getLogger(__name__)
stdoutlog = logging.getLogger('stdoutlog')
SHEBANG_REGEX = re.compile(br'^(#!((?:\\ |[^ \n\r])+)(.*))')
class FileMode(Enum):
text = 'text'
binary = 'binary'
def __str__(self):
return "%s" % self.value
LINK_HARD = 1
LINK_SOFT = 2
LINK_COPY = 3
link_name_map = {
LINK_HARD: 'hard-link',
LINK_SOFT: 'soft-link',
LINK_COPY: 'copy',
}
def _link(src, dst, linktype=LINK_HARD):
if linktype == LINK_HARD:
if on_win:
win_hard_link(src, dst)
else:
os.link(src, dst)
elif linktype == LINK_SOFT:
if on_win:
win_soft_link(src, dst)
else:
os.symlink(src, dst)
elif linktype == LINK_COPY:
# copy relative symlinks as symlinks
if not on_win and islink(src) and not os.readlink(src).startswith('/'):
os.symlink(os.readlink(src), dst)
else:
shutil.copy2(src, dst)
else:
raise CondaError("Did not expect linktype=%r" % linktype)
def _remove_readonly(func, path, excinfo):
os.chmod(path, stat.S_IWRITE)
func(path)
def warn_failed_remove(function, path, exc_info):
if exc_info[1].errno == errno.EACCES:
log.warn("Cannot remove, permission denied: {0}".format(path))
elif exc_info[1].errno == errno.ENOTEMPTY:
log.warn("Cannot remove, not empty: {0}".format(path))
else:
log.warn("Cannot remove, unknown reason: {0}".format(path))
def yield_lines(path):
"""Generator function for lines in file. Empty generator if path does not exist.
Args:
path (str): path to file
Returns:
iterator: each line in file, not starting with '#'
"""
try:
with open(path) as fh:
for line in fh:
line = line.strip()
if not line or line.startswith('#'):
continue
yield line
except (IOError, OSError) as e:
if e.errno == errno.ENOENT:
raise StopIteration
else:
raise
PREFIX_PLACEHOLDER = ('/opt/anaconda1anaconda2'
# this is intentionally split into parts,
# such that running this program on itself
# will leave it unchanged
'anaconda3')
# backwards compatibility for conda-build
prefix_placeholder = PREFIX_PLACEHOLDER
def read_has_prefix(path):
"""
reads `has_prefix` file and return dict mapping filepaths to tuples(placeholder, FileMode)
A line in `has_prefix` contains one of
* filepath
* placeholder mode filepath
mode values are one of
* text
* binary
"""
ParseResult = namedtuple('ParseResult', ('placeholder', 'filemode', 'filepath'))
def parse_line(line):
# placeholder, filemode, filepath
parts = tuple(x.strip('"\'') for x in shlex.split(line, posix=False))
if len(parts) == 1:
return ParseResult(PREFIX_PLACEHOLDER, FileMode.text, parts[0])
elif len(parts) == 3:
return ParseResult(parts[0], FileMode(parts[1]), parts[2])
else:
raise RuntimeError("Invalid has_prefix file at path: %s" % path)
parsed_lines = (parse_line(line) for line in yield_lines(path))
return {pr.filepath: (pr.placeholder, pr.filemode) for pr in parsed_lines}
def binary_replace(data, a, b):
"""
Perform a binary replacement of `data`, where the placeholder `a` is
replaced with `b` and the remaining string is padded with null characters.
All input arguments are expected to be bytes objects.
"""
if on_win and has_pyzzer_entry_point(data):
return replace_pyzzer_entry_point_shebang(data, a, b)
def replace(match):
occurances = match.group().count(a)
padding = (len(a) - len(b))*occurances
if padding < 0:
raise PaddingError(a, b, padding)
return match.group().replace(a, b) + b'\0' * padding
original_data_len = len(data)
pat = re.compile(re.escape(a) + b'([^\0]*?)\0')
data = pat.sub(replace, data)
assert len(data) == original_data_len
return data
def replace_long_shebang(mode, data):
if mode is FileMode.text:
shebang_match = SHEBANG_REGEX.match(data)
if shebang_match:
whole_shebang, executable, options = shebang_match.groups()
if len(whole_shebang) > 127:
executable_name = executable.decode(UTF8).split('/')[-1]
new_shebang = '#!/usr/bin/env %s%s' % (executable_name, options.decode(UTF8))
data = data.replace(whole_shebang, new_shebang.encode(UTF8))
else:
# TODO: binary shebangs exist; figure this out in the future if text works well
log.debug("TODO: binary shebangs exist; figure this out in the future if text works well")
return data
def has_pyzzer_entry_point(data):
pos = data.rfind(b'PK\x05\x06')
return pos >= 0
def replace_pyzzer_entry_point_shebang(all_data, placeholder, new_prefix):
"""Code adapted from pyzzer. This is meant to deal with entry point exe's created by distlib,
which consist of a launcher, then a shebang, then a zip archive of the entry point code to run.
We need to change the shebang.
https://bitbucket.org/vinay.sajip/pyzzer/src/5d5740cb04308f067d5844a56fbe91e7a27efccc/pyzzer/__init__.py?at=default&fileviewer=file-view-default#__init__.py-112 # NOQA
"""
# Copyright (c) 2013 Vinay Sajip.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
launcher = shebang = None
pos = all_data.rfind(b'PK\x05\x06')
if pos >= 0:
end_cdr = all_data[pos + 12:pos + 20]
cdr_size, cdr_offset = struct.unpack('<LL', end_cdr)
arc_pos = pos - cdr_size - cdr_offset
data = all_data[arc_pos:]
if arc_pos > 0:
pos = all_data.rfind(b'#!', 0, arc_pos)
if pos >= 0:
shebang = all_data[pos:arc_pos]
if pos > 0:
launcher = all_data[:pos]
if data and shebang and launcher:
if hasattr(placeholder, 'encode'):
placeholder = placeholder.encode('utf-8')
if hasattr(new_prefix, 'encode'):
new_prefix = new_prefix.encode('utf-8')
shebang = shebang.replace(placeholder, new_prefix)
all_data = b"".join([launcher, shebang, data])
return all_data
def replace_prefix(mode, data, placeholder, new_prefix):
if mode is FileMode.text:
data = data.replace(placeholder.encode(UTF8), new_prefix.encode(UTF8))
elif mode == FileMode.binary:
data = binary_replace(data, placeholder.encode(UTF8), new_prefix.encode(UTF8))
else:
raise RuntimeError("Invalid mode: %r" % mode)
return data
def update_prefix(path, new_prefix, placeholder=PREFIX_PLACEHOLDER, mode=FileMode.text):
if on_win and mode is FileMode.text:
# force all prefix replacements to forward slashes to simplify need to escape backslashes
# replace with unix-style path separators
new_prefix = new_prefix.replace('\\', '/')
path = os.path.realpath(path)
with open(path, 'rb') as fi:
original_data = data = fi.read()
data = replace_prefix(mode, data, placeholder, new_prefix)
if not on_win:
data = replace_long_shebang(mode, data)
if data == original_data:
return
st = os.lstat(path)
with exp_backoff_fn(open, path, 'wb') as fo:
fo.write(data)
os.chmod(path, stat.S_IMODE(st.st_mode))
def dist2pair(dist):
dist = str(dist)
if dist.endswith(']'):
dist = dist.split('[', 1)[0]
if dist.endswith('.tar.bz2'):
dist = dist[:-8]
parts = dist.split('::', 1)
return 'defaults' if len(parts) < 2 else parts[0], parts[-1]
def dist2quad(dist):
channel, dist = dist2pair(dist)
parts = dist.rsplit('-', 2) + ['', '']
return (str(parts[0]), str(parts[1]), str(parts[2]), str(channel))
def dist2name(dist):
return dist2quad(dist)[0]
def name_dist(dist):
return dist2name(dist)
def dist2filename(dist, suffix='.tar.bz2'):
return dist2pair(dist)[1] + suffix
def dist2dirname(dist):
return dist2filename(dist, '')
def create_meta(prefix, dist, info_dir, extra_info):
"""
Create the conda metadata, in a given prefix, for a given package.
"""
# read info/index.json first
with open(join(info_dir, 'index.json')) as fi:
meta = json.load(fi)
# add extra info, add to our intenral cache
meta.update(extra_info)
if not meta.get('url'):
meta['url'] = read_url(dist)
# write into <env>/conda-meta/<dist>.json
meta_dir = join(prefix, 'conda-meta')
if not isdir(meta_dir):
os.makedirs(meta_dir)
with open(join(meta_dir, dist2filename(dist, '.json')), 'w') as fo:
json.dump(meta, fo, indent=2, sort_keys=True)
if prefix in linked_data_:
load_linked_data(prefix, dist, meta)
def mk_menus(prefix, files, remove=False):
"""
Create cross-platform menu items (e.g. Windows Start Menu)
Passes all menu config files %PREFIX%/Menu/*.json to ``menuinst.install``.
``remove=True`` will remove the menu items.
"""
menu_files = [f for f in files
if (f.lower().startswith('menu/') and
f.lower().endswith('.json'))]
if not menu_files:
return
elif basename(abspath(prefix)).startswith('_'):
logging.warn("Environment name starts with underscore '_'. "
"Skipping menu installation.")
return
try:
import menuinst
except:
logging.warn("Menuinst could not be imported:")
logging.warn(traceback.format_exc())
return
for f in menu_files:
try:
menuinst.install(join(prefix, f), remove, prefix)
except:
stdoutlog.error("menuinst Exception:")
stdoutlog.error(traceback.format_exc())
def run_script(prefix, dist, action='post-link', env_prefix=None):
"""
call the post-link (or pre-unlink) script, and return True on success,
False on failure
"""
path = join(prefix, 'Scripts' if on_win else 'bin', '.%s-%s.%s' % (
name_dist(dist),
action,
'bat' if on_win else 'sh'))
if not isfile(path):
return True
if on_win:
try:
args = [os.environ['COMSPEC'], '/c', path]
except KeyError:
return False
else:
shell_path = '/bin/sh' if 'bsd' in sys.platform else '/bin/bash'
args = [shell_path, path]
env = os.environ
env[str('ROOT_PREFIX')] = sys.prefix
env[str('PREFIX')] = str(env_prefix or prefix)
env[str('PKG_NAME')], env[str('PKG_VERSION')], env[str('PKG_BUILDNUM')], _ = dist2quad(dist)
if action == 'pre-link':
env[str('SOURCE_DIR')] = str(prefix)
try:
subprocess.check_call(args, env=env)
except subprocess.CalledProcessError:
return False
return True
def read_url(dist):
res = package_cache().get(dist, {}).get('urls', (None,))
return res[0] if res else None
def read_icondata(source_dir):
import base64
try:
data = open(join(source_dir, 'info', 'icon.png'), 'rb').read()
return base64.b64encode(data).decode(UTF8)
except IOError:
pass
return None
def read_no_link(info_dir):
return set(chain(yield_lines(join(info_dir, 'no_link')),
yield_lines(join(info_dir, 'no_softlink'))))
# Should this be an API function?
def symlink_conda(prefix, root_dir, shell=None):
# do not symlink root env - this clobbers activate incorrectly.
# prefix should always be longer than, or outside the root dir.
if normcase(normpath(prefix)) in normcase(normpath(root_dir)):
return
if on_win:
where = 'Scripts'
symlink_fn = functools.partial(win_conda_bat_redirect, shell=shell)
else:
where = 'bin'
symlink_fn = os.symlink
if not isdir(join(prefix, where)):
os.makedirs(join(prefix, where))
symlink_conda_hlp(prefix, root_dir, where, symlink_fn)
def symlink_conda_hlp(prefix, root_dir, where, symlink_fn):
scripts = ["conda", "activate", "deactivate"]
prefix_where = join(prefix, where)
if not isdir(prefix_where):
os.makedirs(prefix_where)
for f in scripts:
root_file = join(root_dir, where, f)
prefix_file = join(prefix_where, f)
try:
# try to kill stale links if they exist
if os.path.lexists(prefix_file):
backoff_unlink(prefix_file)
# if they're in use, they won't be killed. Skip making new symlink.
if not os.path.lexists(prefix_file):
symlink_fn(root_file, prefix_file)
except (IOError, OSError) as e:
if (os.path.lexists(prefix_file) and
(e.errno in (errno.EPERM, errno.EACCES, errno.EROFS, errno.EEXIST))):
log.debug("Cannot symlink {0} to {1}. Ignoring since link already exists."
.format(root_file, prefix_file))
else:
raise
# ========================== begin API functions =========================
def try_hard_link(pkgs_dir, prefix, dist):
dist = dist2filename(dist, '')
src = join(pkgs_dir, dist, 'info', 'index.json')
dst = join(prefix, '.tmp-%s' % dist)
assert isfile(src), src
assert not isfile(dst), dst
try:
if not isdir(prefix):
os.makedirs(prefix)
_link(src, dst, LINK_HARD)
# Some file systems (at least BeeGFS) do not support hard-links
# between files in different directories. Depending on the
# file system configuration, a symbolic link may be created
# instead. If a symbolic link is created instead of a hard link,
# return False.
return not os.path.islink(dst)
except OSError:
return False
finally:
rm_rf(dst)
# ------- package cache ----- construction
# The current package cache does not support the ability to store multiple packages
# with the same filename from different channels. Furthermore, the filename itself
# cannot be used to disambiguate; we must read the URL from urls.txt to determine
# the source channel. For this reason, we now fully parse the directory and its
# accompanying urls.txt file so we can make arbitrary queries without having to
# read this data multiple times.
package_cache_ = {}
fname_table_ = {}
def add_cached_package(pdir, url, overwrite=False, urlstxt=False):
"""
Adds a new package to the cache. The URL is used to determine the
package filename and channel, and the directory pdir is scanned for
both a compressed and an extracted version of that package. If
urlstxt=True, this URL will be appended to the urls.txt file in the
cache, so that subsequent runs will correctly identify the package.
"""
package_cache()
if '/' in url:
dist = url.rsplit('/', 1)[-1]
else:
dist = url
url = None
if dist.endswith('.tar.bz2'):
fname = dist
dist = dist[:-8]
else:
fname = dist + '.tar.bz2'
xpkg = join(pdir, fname)
if not overwrite and xpkg in fname_table_:
return
if not isfile(xpkg):
xpkg = None
xdir = join(pdir, dist)
if not (isdir(xdir) and
isfile(join(xdir, 'info', 'files')) and
isfile(join(xdir, 'info', 'index.json'))):
xdir = None
if not (xpkg or xdir):
return
if url:
url = url
_, schannel = Channel(url).url_channel_wtf
prefix = '' if schannel == 'defaults' else schannel + '::'
xkey = xpkg or (xdir + '.tar.bz2')
fname_table_[xkey] = fname_table_[path_to_url(xkey)] = prefix
fkey = prefix + dist
rec = package_cache_.get(fkey)
if rec is None:
rec = package_cache_[fkey] = dict(files=[], dirs=[], urls=[])
if url and url not in rec['urls']:
rec['urls'].append(url)
if xpkg and xpkg not in rec['files']:
rec['files'].append(xpkg)
if xdir and xdir not in rec['dirs']:
rec['dirs'].append(xdir)
if urlstxt:
try:
with open(join(pdir, 'urls.txt'), 'a') as fa:
fa.write('%s\n' % url)
except IOError:
pass
def package_cache():
"""
Initializes the package cache. Each entry in the package cache
dictionary contains three lists:
- urls: the URLs used to refer to that package
- files: the full pathnames to fetched copies of that package
- dirs: the full pathnames to extracted copies of that package
Nominally there should be no more than one entry in each list, but
in theory this can handle the presence of multiple copies.
"""
if package_cache_:
return package_cache_
# Stops recursion
package_cache_['@'] = None
# import pdb; pdb.set_trace()
for pdir in context.pkgs_dirs:
try:
data = open(join(pdir, 'urls.txt')).read()
for url in data.split()[::-1]:
if '/' in url:
add_cached_package(pdir, url)
except IOError:
pass
if isdir(pdir):
for fn in os.listdir(pdir):
add_cached_package(pdir, fn)
del package_cache_['@']
return package_cache_
def cached_url(url):
package_cache()
return fname_table_.get(url)
def find_new_location(dist):
"""
Determines the download location for the given package, and the name
of a package, if any, that must be removed to make room. If the
given package is already in the cache, it returns its current location,
under the assumption that it will be overwritten. If the conflict
value is None, that means there is no other package with that same
name present in the cache (e.g., no collision).
"""
rec = package_cache().get(dist)
if rec:
return dirname((rec['files'] or rec['dirs'])[0]), None
fname = dist2filename(dist)
dname = fname[:-8]
# Look for a location with no conflicts
# On the second pass, just pick the first location
for p in range(2):
for pkg_dir in context.pkgs_dirs:
pkg_path = join(pkg_dir, fname)
prefix = fname_table_.get(pkg_path)
if p or prefix is None:
return pkg_dir, prefix + dname if p else None
# ------- package cache ----- fetched
def fetched():
"""
Returns the (set of canonical names) of all fetched packages
"""
return set(dist for dist, rec in package_cache().items() if rec['files'])
def is_fetched(dist):
"""
Returns the full path of the fetched package, or None if it is not in the cache.
"""
for fn in package_cache().get(dist, {}).get('files', ()):
return fn
def rm_fetched(dist):
"""
Checks to see if the requested package is in the cache; and if so, it removes both
the package itself and its extracted contents.
"""
rec = package_cache().get(dist)
if rec is None:
return
for fname in rec['files']:
del fname_table_[fname]
del fname_table_[path_to_url(fname)]
with FileLock(fname):
rm_rf(fname)
for fname in rec['dirs']:
with FileLock(fname):
rm_rf(fname)
del package_cache_[dist]
# ------- package cache ----- extracted
def extracted():
"""
return the (set of canonical names) of all extracted packages
"""
return set(dist for dist, rec in package_cache().items() if rec['dirs'])
def is_extracted(dist):
"""
returns the full path of the extracted data for the requested package,
or None if that package is not extracted.
"""
for fn in package_cache().get(dist, {}).get('dirs', ()):
return fn
def rm_extracted(dist):
"""
Removes any extracted versions of the given package found in the cache.
"""
rec = package_cache().get(dist)
if rec is None:
return
for fname in rec['dirs']:
with FileLock(fname):
rm_rf(fname)
if rec['files']:
rec['dirs'] = []
else:
del package_cache_[dist]
def extract(dist):
"""
Extract a package, i.e. make a package available for linkage. We assume
that the compressed package is located in the packages directory.
"""
rec = package_cache()[dist]
url = rec['urls'][0]
fname = rec['files'][0]
assert url and fname
pkgs_dir = dirname(fname)
path = fname[:-8]
with FileLock(path):
temp_path = path + '.tmp'
rm_rf(temp_path)
with tarfile.open(fname) as t:
t.extractall(path=temp_path)
rm_rf(path)
exp_backoff_fn(os.rename, temp_path, path)
if sys.platform.startswith('linux') and os.getuid() == 0:
# When extracting as root, tarfile will by restore ownership
# of extracted files. However, we want root to be the owner
# (our implementation of --no-same-owner).
for root, dirs, files in os.walk(path):
for fn in files:
p = join(root, fn)
os.lchown(p, 0, 0)
add_cached_package(pkgs_dir, url, overwrite=True)
# Because the conda-meta .json files do not include channel names in
# their filenames, we have to pull that information from the .json
# files themselves. This has made it necessary in virtually all
# circumstances to load the full set of files from this directory.
# Therefore, we have implemented a full internal cache of this
# data to eliminate redundant file reads.
linked_data_ = {}
def load_linked_data(prefix, dist, rec=None, ignore_channels=False):
schannel, dname = dist2pair(dist)
meta_file = join(prefix, 'conda-meta', dname + '.json')
if rec is None:
try:
with open(meta_file) as fi:
rec = json.load(fi)
except IOError:
return None
else:
linked_data(prefix)
url = rec.get('url')
fn = rec.get('fn')
if not fn:
fn = rec['fn'] = url.rsplit('/', 1)[-1] if url else dname + '.tar.bz2'
if fn[:-8] != dname:
log.debug('Ignoring invalid package metadata file: %s' % meta_file)
return None
channel = rec.get('channel')
if channel:
channel = channel.rstrip('/')
if not url or (url.startswith('file:') and channel[0] != '<unknown>'):
url = rec['url'] = channel + '/' + fn
channel, schannel = Channel(url).url_channel_wtf
rec['url'] = url
rec['channel'] = channel
rec['schannel'] = schannel
rec['link'] = rec.get('link') or True
if ignore_channels:
linked_data_[prefix][dname] = rec
else:
cprefix = '' if schannel == 'defaults' else schannel + '::'
linked_data_[prefix][str(cprefix + dname)] = rec
return rec
def delete_linked_data(prefix, dist, delete=True):
recs = linked_data_.get(prefix)
if recs and dist in recs:
del recs[dist]
if delete:
meta_path = join(prefix, 'conda-meta', dist2filename(dist, '.json'))
if isfile(meta_path):
backoff_unlink(meta_path)
def delete_linked_data_any(path):
'''Here, path may be a complete prefix or a dist inside a prefix'''
dist = ''
while True:
if path in linked_data_:
if dist:
delete_linked_data(path, dist)
return True
else:
del linked_data_[path]
return True
path, dist = os.path.split(path)
if not dist:
return False
def load_meta(prefix, dist):
"""
Return the install meta-data for a linked package in a prefix, or None
if the package is not linked in the prefix.
"""
return linked_data(prefix).get(dist)
def linked_data(prefix, ignore_channels=False):
"""
Return a dictionary of the linked packages in prefix.
"""
# Manually memoized so it can be updated
recs = linked_data_.get(prefix)
if recs is None:
recs = linked_data_[prefix] = {}
meta_dir = join(prefix, 'conda-meta')
if isdir(meta_dir):
for fn in os.listdir(meta_dir):
if fn.endswith('.json'):
load_linked_data(prefix, fn[:-5], ignore_channels=ignore_channels)
return recs
def linked(prefix, ignore_channels=False):
"""
Return the set of canonical names of linked packages in prefix.
"""
return set(linked_data(prefix, ignore_channels=ignore_channels).keys())
def is_linked(prefix, dist):
"""
Return the install metadata for a linked package in a prefix, or None
if the package is not linked in the prefix.
"""
# FIXME Functions that begin with `is_` should return True/False
return load_meta(prefix, dist)
def link(prefix, dist, linktype=LINK_HARD, index=None):
"""
Set up a package in a specified (environment) prefix. We assume that
the package has been extracted (using extract() above).
"""
log.debug("linking package %s with link type %s", dist, linktype)
index = index or {}
source_dir = is_extracted(dist)
assert source_dir is not None
pkgs_dir = dirname(source_dir)
log.debug('pkgs_dir=%r, prefix=%r, dist=%r, linktype=%r' %
(pkgs_dir, prefix, dist, linktype))
if not run_script(source_dir, dist, 'pre-link', prefix):
raise LinkError('Error: pre-link failed: %s' % dist)
info_dir = join(source_dir, 'info')
files = list(yield_lines(join(info_dir, 'files')))
has_prefix_files = read_has_prefix(join(info_dir, 'has_prefix'))
no_link = read_no_link(info_dir)
# for the lock issue
# may run into lock if prefix not exist
if not isdir(prefix):
os.makedirs(prefix)
with DirectoryLock(prefix), FileLock(source_dir):
for filepath in files:
src = join(source_dir, filepath)
dst = join(prefix, filepath)
dst_dir = dirname(dst)
if not isdir(dst_dir):
os.makedirs(dst_dir)
if os.path.exists(dst):
log.info("file exists, but clobbering: %r" % dst)
rm_rf(dst)
lt = linktype
if filepath in has_prefix_files or filepath in no_link or islink(src):
lt = LINK_COPY
try:
_link(src, dst, lt)
except OSError as e:
raise CondaOSError('failed to link (src=%r, dst=%r, type=%r, error=%r)' %
(src, dst, lt, e))
for filepath in sorted(has_prefix_files):
placeholder, mode = has_prefix_files[filepath]
try:
update_prefix(join(prefix, filepath), prefix, placeholder, mode)
except PaddingError:
raise PaddingError("ERROR: placeholder '%s' too short in: %s\n" %
(placeholder, dist))
# make sure that the child environment behaves like the parent,
# wrt user/system install on win
# This is critical for doing shortcuts correctly
if on_win:
nonadmin = join(sys.prefix, ".nonadmin")
if isfile(nonadmin):
open(join(prefix, ".nonadmin"), 'w').close()
if context.shortcuts:
mk_menus(prefix, files, remove=False)
if not run_script(prefix, dist, 'post-link'):
raise LinkError("Error: post-link failed for: %s" % dist)
meta_dict = index.get(dist + '.tar.bz2', {})
meta_dict['url'] = read_url(dist)
alt_files_path = join(prefix, 'conda-meta', dist2filename(dist, '.files'))
if isfile(alt_files_path):
# alt_files_path is a hack for noarch
meta_dict['files'] = list(yield_lines(alt_files_path))
else:
meta_dict['files'] = files
meta_dict['link'] = {'source': source_dir,
'type': link_name_map.get(linktype)}
if 'icon' in meta_dict:
meta_dict['icondata'] = read_icondata(source_dir)
create_meta(prefix, dist, info_dir, meta_dict)
def unlink(prefix, dist):
"""
Remove a package from the specified environment, it is an error if the