1 # Copyright 2010 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 from __future__ import print_function
20 portage.proxy.lazyimport.lazyimport(globals(),
21 'portage.package.ebuild.config:check_config_instance,config',
22 'portage.package.ebuild.doebuild:doebuild_environment,' + \
24 'portage.package.ebuild.prepare_build_dirs:prepare_build_dirs',
27 from portage import OrderedDict, os, selinux, _encodings, \
28 _shell_quote, _unicode_encode
29 from portage.checksum import perform_md5, verify_all
30 from portage.const import BASH_BINARY, CUSTOM_MIRRORS_FILE, \
32 from portage.data import portage_gid, portage_uid, secpass, userpriv_groups
33 from portage.exception import FileNotFound, OperationNotPermitted, \
34 PermissionDenied, PortageException, TryAgain
35 from portage.localization import _
36 from portage.locks import lockfile, unlockfile
37 from portage.manifest import Manifest
38 from portage.output import colorize, EOutput
39 from portage.util import apply_recursive_permissions, \
40 apply_secpass_permissions, ensure_dirs, grabdict, shlex_split, \
41 varexpand, writemsg, writemsg_level, writemsg_stdout
42 from portage.process import spawn
44 _userpriv_spawn_kwargs = (
47 ("groups", userpriv_groups),
51 def _spawn_fetch(settings, args, **kwargs):
53 Spawn a process with appropriate settings for fetching, including
54 userfetch and selinux support.
57 global _userpriv_spawn_kwargs
59 # Redirect all output to stdout since some fetchers like
60 # wget pollute stderr (if portage detects a problem then it
61 # can send it's own message to stderr).
62 if "fd_pipes" not in kwargs:
64 kwargs["fd_pipes"] = {
65 0 : sys.stdin.fileno(),
66 1 : sys.stdout.fileno(),
67 2 : sys.stdout.fileno(),
70 if "userfetch" in settings.features and \
71 os.getuid() == 0 and portage_gid and portage_uid:
72 kwargs.update(_userpriv_spawn_kwargs)
76 if settings.selinux_enabled():
77 spawn_func = selinux.spawn_wrapper(spawn_func,
78 settings["PORTAGE_FETCH_T"])
80 # bash is an allowed entrypoint, while most binaries are not
81 if args[0] != BASH_BINARY:
82 args = [BASH_BINARY, "-c", "exec \"$@\"", args[0]] + args
84 # Ensure that EBUILD_PHASE is set to fetch, so that config.environ()
85 # does not filter the calling environment (which may contain needed
86 # proxy variables, as in bug #315421).
87 phase_backup = settings.get('EBUILD_PHASE')
88 settings['EBUILD_PHASE'] = 'fetch'
90 rval = spawn_func(args, env=settings.environ(), **kwargs)
92 if phase_backup is None:
93 settings.pop('EBUILD_PHASE', None)
95 settings['EBUILD_PHASE'] = phase_backup
99 _userpriv_test_write_file_cache = {}
100 _userpriv_test_write_cmd_script = ">> %(file_path)s 2>/dev/null ; rval=$? ; " + \
101 "rm -f %(file_path)s ; exit $rval"
103 def _userpriv_test_write_file(settings, file_path):
105 Drop privileges and try to open a file for writing. The file may or
106 may not exist, and the parent directory is assumed to exist. The file
107 is removed before returning.
109 @param settings: A config instance which is passed to _spawn_fetch()
110 @param file_path: A file path to open and write.
111 @return: True if write succeeds, False otherwise.
114 global _userpriv_test_write_file_cache, _userpriv_test_write_cmd_script
115 rval = _userpriv_test_write_file_cache.get(file_path)
119 args = [BASH_BINARY, "-c", _userpriv_test_write_cmd_script % \
120 {"file_path" : _shell_quote(file_path)}]
122 returncode = _spawn_fetch(settings, args)
124 rval = returncode == os.EX_OK
125 _userpriv_test_write_file_cache[file_path] = rval
128 def _checksum_failure_temp_file(distdir, basename):
130 First try to find a duplicate temp file with the same checksum and return
131 that filename if available. Otherwise, use mkstemp to create a new unique
132 filename._checksum_failure_.$RANDOM, rename the given file, and return the
133 new filename. In any case, filename will be renamed or removed before this
134 function returns a temp filename.
137 filename = os.path.join(distdir, basename)
138 size = os.stat(filename).st_size
140 tempfile_re = re.compile(re.escape(basename) + r'\._checksum_failure_\..*')
141 for temp_filename in os.listdir(distdir):
142 if not tempfile_re.match(temp_filename):
144 temp_filename = os.path.join(distdir, temp_filename)
146 if size != os.stat(temp_filename).st_size:
151 temp_checksum = perform_md5(temp_filename)
153 # Apparently the temp file disappeared. Let it go.
156 checksum = perform_md5(filename)
157 if checksum == temp_checksum:
161 fd, temp_filename = \
162 tempfile.mkstemp("", basename + "._checksum_failure_.", distdir)
164 os.rename(filename, temp_filename)
167 def _check_digests(filename, digests, show_errors=1):
169 Check digests and display a message if an error occurs.
170 @return True if all digests match, False otherwise.
172 verified_ok, reason = verify_all(filename, digests)
175 writemsg(_("!!! Previously fetched"
176 " file: '%s'\n") % filename, noiselevel=-1)
177 writemsg(_("!!! Reason: %s\n") % reason[0],
179 writemsg(_("!!! Got: %s\n"
180 "!!! Expected: %s\n") % \
181 (reason[1], reason[2]), noiselevel=-1)
185 def _check_distfile(filename, digests, eout, show_errors=1):
187 @return a tuple of (match, stat_obj) where match is True if filename
188 matches all given digests (if any) and stat_obj is a stat result, or
189 None if the file does not exist.
193 size = digests.get("size")
194 if size is not None and len(digests) == 1:
198 st = os.stat(filename)
201 if size is not None and size != st.st_size:
205 eout.ebegin(_("%s size ;-)") % os.path.basename(filename))
207 elif st.st_size == 0:
208 # Zero-byte distfiles are always invalid.
211 if _check_digests(filename, digests, show_errors=show_errors):
212 eout.ebegin("%s %s ;-)" % (os.path.basename(filename),
213 " ".join(sorted(digests))))
219 _fetch_resume_size_re = re.compile('(^[\d]+)([KMGTPEZY]?$)')
233 def fetch(myuris, mysettings, listonly=0, fetchonly=0, locks_in_subdir=".locks",use_locks=1, try_mirrors=1):
234 "fetch files. Will use digest file if available."
239 features = mysettings.features
240 restrict = mysettings.get("PORTAGE_RESTRICT","").split()
242 userfetch = secpass >= 2 and "userfetch" in features
243 userpriv = secpass >= 2 and "userpriv" in features
245 # 'nomirror' is bad/negative logic. You Restrict mirroring, not no-mirroring.
246 if "mirror" in restrict or \
247 "nomirror" in restrict:
248 if ("mirror" in features) and ("lmirror" not in features):
249 # lmirror should allow you to bypass mirror restrictions.
250 # XXX: This is not a good thing, and is temporary at best.
251 print(_(">>> \"mirror\" mode desired and \"mirror\" restriction found; skipping fetch."))
254 # Generally, downloading the same file repeatedly from
255 # every single available mirror is a waste of bandwidth
256 # and time, so there needs to be a cap.
257 checksum_failure_max_tries = 5
258 v = checksum_failure_max_tries
260 v = int(mysettings.get("PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS",
261 checksum_failure_max_tries))
262 except (ValueError, OverflowError):
263 writemsg(_("!!! Variable PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS"
264 " contains non-integer value: '%s'\n") % \
265 mysettings["PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS"], noiselevel=-1)
266 writemsg(_("!!! Using PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS "
267 "default value: %s\n") % checksum_failure_max_tries,
269 v = checksum_failure_max_tries
271 writemsg(_("!!! Variable PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS"
272 " contains value less than 1: '%s'\n") % v, noiselevel=-1)
273 writemsg(_("!!! Using PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS "
274 "default value: %s\n") % checksum_failure_max_tries,
276 v = checksum_failure_max_tries
277 checksum_failure_max_tries = v
280 fetch_resume_size_default = "350K"
281 fetch_resume_size = mysettings.get("PORTAGE_FETCH_RESUME_MIN_SIZE")
282 if fetch_resume_size is not None:
283 fetch_resume_size = "".join(fetch_resume_size.split())
284 if not fetch_resume_size:
285 # If it's undefined or empty, silently use the default.
286 fetch_resume_size = fetch_resume_size_default
287 match = _fetch_resume_size_re.match(fetch_resume_size)
288 if match is None or \
289 (match.group(2).upper() not in _size_suffix_map):
290 writemsg(_("!!! Variable PORTAGE_FETCH_RESUME_MIN_SIZE"
291 " contains an unrecognized format: '%s'\n") % \
292 mysettings["PORTAGE_FETCH_RESUME_MIN_SIZE"], noiselevel=-1)
293 writemsg(_("!!! Using PORTAGE_FETCH_RESUME_MIN_SIZE "
294 "default value: %s\n") % fetch_resume_size_default,
296 fetch_resume_size = None
297 if fetch_resume_size is None:
298 fetch_resume_size = fetch_resume_size_default
299 match = _fetch_resume_size_re.match(fetch_resume_size)
300 fetch_resume_size = int(match.group(1)) * \
301 2 ** _size_suffix_map[match.group(2).upper()]
303 # Behave like the package has RESTRICT="primaryuri" after a
304 # couple of checksum failures, to increase the probablility
305 # of success before checksum_failure_max_tries is reached.
306 checksum_failure_primaryuri = 2
307 thirdpartymirrors = mysettings.thirdpartymirrors()
309 # In the background parallel-fetch process, it's safe to skip checksum
310 # verification of pre-existing files in $DISTDIR that have the correct
311 # file size. The parent process will verify their checksums prior to
314 parallel_fetchonly = "PORTAGE_PARALLEL_FETCHONLY" in mysettings
315 if parallel_fetchonly:
318 check_config_instance(mysettings)
320 custommirrors = grabdict(os.path.join(mysettings["PORTAGE_CONFIGROOT"],
321 CUSTOM_MIRRORS_FILE), recursive=1)
325 if listonly or ("distlocks" not in features):
329 if "skiprocheck" in features:
332 if not os.access(mysettings["DISTDIR"],os.W_OK) and fetch_to_ro:
334 writemsg(colorize("BAD",
335 _("!!! For fetching to a read-only filesystem, "
336 "locking should be turned off.\n")), noiselevel=-1)
337 writemsg(_("!!! This can be done by adding -distlocks to "
338 "FEATURES in /etc/make.conf\n"), noiselevel=-1)
341 # local mirrors are always added
342 if "local" in custommirrors:
343 mymirrors += custommirrors["local"]
345 if "nomirror" in restrict or \
346 "mirror" in restrict:
347 # We don't add any mirrors.
351 mymirrors += [x.rstrip("/") for x in mysettings["GENTOO_MIRRORS"].split() if x]
353 skip_manifest = mysettings.get("EBUILD_SKIP_MANIFEST") == "1"
354 pkgdir = mysettings.get("O")
355 if not (pkgdir is None or skip_manifest):
356 mydigests = Manifest(
357 pkgdir, mysettings["DISTDIR"]).getTypeDigests("DIST")
359 # no digests because fetch was not called for a specific package
362 ro_distdirs = [x for x in \
363 shlex_split(mysettings.get("PORTAGE_RO_DISTDIRS", "")) \
367 for x in range(len(mymirrors)-1,-1,-1):
368 if mymirrors[x] and mymirrors[x][0]=='/':
369 fsmirrors += [mymirrors[x]]
372 restrict_fetch = "fetch" in restrict
373 custom_local_mirrors = custommirrors.get("local", [])
375 # With fetch restriction, a normal uri may only be fetched from
376 # custom local mirrors (if available). A mirror:// uri may also
377 # be fetched from specific mirrors (effectively overriding fetch
378 # restriction, but only for specific mirrors).
379 locations = custom_local_mirrors
381 locations = mymirrors
384 # Check for 'items' attribute since OrderedDict is not a dict.
385 if hasattr(myuris, 'items'):
386 for myfile, uri_set in myuris.items():
387 for myuri in uri_set:
388 file_uri_tuples.append((myfile, myuri))
391 file_uri_tuples.append((os.path.basename(myuri), myuri))
393 filedict = OrderedDict()
394 primaryuri_indexes={}
396 thirdpartymirror_uris = {}
397 for myfile, myuri in file_uri_tuples:
398 if myfile not in filedict:
400 for y in range(0,len(locations)):
401 filedict[myfile].append(locations[y]+"/distfiles/"+myfile)
402 if myuri[:9]=="mirror://":
403 eidx = myuri.find("/", 9)
405 mirrorname = myuri[9:eidx]
406 path = myuri[eidx+1:]
408 # Try user-defined mirrors first
409 if mirrorname in custommirrors:
410 for cmirr in custommirrors[mirrorname]:
411 filedict[myfile].append(
412 cmirr.rstrip("/") + "/" + path)
414 # now try the official mirrors
415 if mirrorname in thirdpartymirrors:
416 random.shuffle(thirdpartymirrors[mirrorname])
418 uris = [locmirr.rstrip("/") + "/" + path \
419 for locmirr in thirdpartymirrors[mirrorname]]
420 filedict[myfile].extend(uris)
421 thirdpartymirror_uris.setdefault(myfile, []).extend(uris)
423 if not filedict[myfile]:
424 writemsg(_("No known mirror by the name: %s\n") % (mirrorname))
426 writemsg(_("Invalid mirror definition in SRC_URI:\n"), noiselevel=-1)
427 writemsg(" %s\n" % (myuri), noiselevel=-1)
430 # Only fetch from specific mirrors is allowed.
432 if "primaryuri" in restrict:
433 # Use the source site first.
434 if myfile in primaryuri_indexes:
435 primaryuri_indexes[myfile] += 1
437 primaryuri_indexes[myfile] = 0
438 filedict[myfile].insert(primaryuri_indexes[myfile], myuri)
440 filedict[myfile].append(myuri)
441 primaryuris = primaryuri_dict.get(myfile)
442 if primaryuris is None:
444 primaryuri_dict[myfile] = primaryuris
445 primaryuris.append(myuri)
447 # Prefer thirdpartymirrors over normal mirrors in cases when
448 # the file does not yet exist on the normal mirrors.
449 for myfile, uris in thirdpartymirror_uris.items():
450 primaryuri_dict.setdefault(myfile, []).extend(uris)
457 if can_fetch and not fetch_to_ro:
458 global _userpriv_test_write_file_cache
462 dir_gid = portage_gid
463 if "FAKED_MODE" in mysettings:
464 # When inside fakeroot, directories with portage's gid appear
465 # to have root's gid. Therefore, use root's gid instead of
466 # portage's gid to avoid spurrious permissions adjustments
467 # when inside fakeroot.
472 for x in distdir_dirs:
473 mydir = os.path.join(mysettings["DISTDIR"], x)
474 write_test_file = os.path.join(
475 mydir, ".__portage_test_write__")
482 if st is not None and stat.S_ISDIR(st.st_mode):
483 if not (userfetch or userpriv):
485 if _userpriv_test_write_file(mysettings, write_test_file):
488 _userpriv_test_write_file_cache.pop(write_test_file, None)
489 if ensure_dirs(mydir, gid=dir_gid, mode=dirmode, mask=modemask):
491 # The directory has just been created
492 # and therefore it must be empty.
494 writemsg(_("Adjusting permissions recursively: '%s'\n") % mydir,
497 raise # bail out on the first error that occurs during recursion
498 if not apply_recursive_permissions(mydir,
499 gid=dir_gid, dirmode=dirmode, dirmask=modemask,
500 filemode=filemode, filemask=modemask, onerror=onerror):
501 raise OperationNotPermitted(
502 _("Failed to apply recursive permissions for the portage group."))
503 except PortageException as e:
504 if not os.path.isdir(mysettings["DISTDIR"]):
505 writemsg("!!! %s\n" % str(e), noiselevel=-1)
506 writemsg(_("!!! Directory Not Found: DISTDIR='%s'\n") % mysettings["DISTDIR"], noiselevel=-1)
507 writemsg(_("!!! Fetching will fail!\n"), noiselevel=-1)
510 not fetch_to_ro and \
511 not os.access(mysettings["DISTDIR"], os.W_OK):
512 writemsg(_("!!! No write access to '%s'\n") % mysettings["DISTDIR"],
516 distdir_writable = can_fetch and not fetch_to_ro
518 restrict_fetch_msg = False
520 for myfile in filedict:
524 1 partially downloaded
525 2 completely downloaded
529 orig_digests = mydigests.get(myfile, {})
530 size = orig_digests.get("size")
532 # Zero-byte distfiles are always invalid, so discard their digests.
533 del mydigests[myfile]
536 pruned_digests = orig_digests
537 if parallel_fetchonly:
540 pruned_digests["size"] = size
542 myfile_path = os.path.join(mysettings["DISTDIR"], myfile)
544 has_space_superuser = True
547 writemsg_stdout("\n", noiselevel=-1)
549 # check if there is enough space in DISTDIR to completely store myfile
550 # overestimate the filesize so we aren't bitten by FS overhead
551 if size is not None and hasattr(os, "statvfs"):
552 vfs_stat = os.statvfs(mysettings["DISTDIR"])
554 mysize = os.stat(myfile_path).st_size
556 if e.errno not in (errno.ENOENT, errno.ESTALE):
560 if (size - mysize + vfs_stat.f_bsize) >= \
561 (vfs_stat.f_bsize * vfs_stat.f_bavail):
563 if (size - mysize + vfs_stat.f_bsize) >= \
564 (vfs_stat.f_bsize * vfs_stat.f_bfree):
565 has_space_superuser = False
567 if not has_space_superuser:
575 writemsg(_("!!! Insufficient space to store %s in %s\n") % \
576 (myfile, mysettings["DISTDIR"]), noiselevel=-1)
578 if has_space_superuser:
579 writemsg(_("!!! Insufficient privileges to use "
580 "remaining space.\n"), noiselevel=-1)
582 writemsg(_("!!! You may set FEATURES=\"-userfetch\""
583 " in /etc/make.conf in order to fetch with\n"
584 "!!! superuser privileges.\n"), noiselevel=-1)
586 if distdir_writable and use_locks:
590 lock_kwargs["flags"] = os.O_NONBLOCK
593 file_lock = lockfile(myfile_path,
594 wantnewlockfile=1, **lock_kwargs)
596 writemsg(_(">>> File '%s' is already locked by "
597 "another fetcher. Continuing...\n") % myfile,
604 eout.quiet = mysettings.get("PORTAGE_QUIET") == "1"
605 match, mystat = _check_distfile(
606 myfile_path, pruned_digests, eout)
610 apply_secpass_permissions(myfile_path,
611 gid=portage_gid, mode=0o664, mask=0o2,
613 except PortageException as e:
614 if not os.access(myfile_path, os.R_OK):
615 writemsg(_("!!! Failed to adjust permissions:"
616 " %s\n") % str(e), noiselevel=-1)
620 if distdir_writable and mystat is None:
621 # Remove broken symlinks if necessary.
623 os.unlink(myfile_path)
627 if mystat is not None:
628 if stat.S_ISDIR(mystat.st_mode):
630 _("!!! Unable to fetch file since "
631 "a directory is in the way: \n"
632 "!!! %s\n") % myfile_path,
633 level=logging.ERROR, noiselevel=-1)
636 if mystat.st_size == 0:
639 os.unlink(myfile_path)
642 elif distdir_writable:
643 if mystat.st_size < fetch_resume_size and \
644 mystat.st_size < size:
645 # If the file already exists and the size does not
646 # match the existing digests, it may be that the
647 # user is attempting to update the digest. In this
648 # case, the digestgen() function will advise the
649 # user to use `ebuild --force foo.ebuild manifest`
650 # in order to force the old digests to be replaced.
651 # Since the user may want to keep this file, rename
652 # it instead of deleting it.
653 writemsg(_(">>> Renaming distfile with size "
654 "%d (smaller than " "PORTAGE_FETCH_RESU"
655 "ME_MIN_SIZE)\n") % mystat.st_size)
657 _checksum_failure_temp_file(
658 mysettings["DISTDIR"], myfile)
659 writemsg_stdout(_("Refetching... "
660 "File renamed to '%s'\n\n") % \
661 temp_filename, noiselevel=-1)
662 elif mystat.st_size >= size:
664 _checksum_failure_temp_file(
665 mysettings["DISTDIR"], myfile)
666 writemsg_stdout(_("Refetching... "
667 "File renamed to '%s'\n\n") % \
668 temp_filename, noiselevel=-1)
670 if distdir_writable and ro_distdirs:
672 for x in ro_distdirs:
673 filename = os.path.join(x, myfile)
674 match, mystat = _check_distfile(
675 filename, pruned_digests, eout)
677 readonly_file = filename
679 if readonly_file is not None:
681 os.unlink(myfile_path)
683 if e.errno not in (errno.ENOENT, errno.ESTALE):
686 os.symlink(readonly_file, myfile_path)
689 if fsmirrors and not os.path.exists(myfile_path) and has_space:
690 for mydir in fsmirrors:
691 mirror_file = os.path.join(mydir, myfile)
693 shutil.copyfile(mirror_file, myfile_path)
694 writemsg(_("Local mirror has file: %s\n") % myfile)
696 except (IOError, OSError) as e:
697 if e.errno not in (errno.ENOENT, errno.ESTALE):
702 mystat = os.stat(myfile_path)
704 if e.errno not in (errno.ENOENT, errno.ESTALE):
709 apply_secpass_permissions(
710 myfile_path, gid=portage_gid, mode=0o664, mask=0o2,
712 except PortageException as e:
713 if not os.access(myfile_path, os.R_OK):
714 writemsg(_("!!! Failed to adjust permissions:"
715 " %s\n") % str(e), noiselevel=-1)
717 # If the file is empty then it's obviously invalid. Remove
718 # the empty file and try to download if possible.
719 if mystat.st_size == 0:
722 os.unlink(myfile_path)
723 except EnvironmentError:
725 elif myfile not in mydigests:
726 # We don't have a digest, but the file exists. We must
727 # assume that it is fully downloaded.
730 if mystat.st_size < mydigests[myfile]["size"] and \
732 fetched = 1 # Try to resume this download.
733 elif parallel_fetchonly and \
734 mystat.st_size == mydigests[myfile]["size"]:
737 mysettings.get("PORTAGE_QUIET") == "1"
739 "%s size ;-)" % (myfile, ))
743 verified_ok, reason = verify_all(
744 myfile_path, mydigests[myfile])
746 writemsg(_("!!! Previously fetched"
747 " file: '%s'\n") % myfile, noiselevel=-1)
748 writemsg(_("!!! Reason: %s\n") % reason[0],
750 writemsg(_("!!! Got: %s\n"
751 "!!! Expected: %s\n") % \
752 (reason[1], reason[2]), noiselevel=-1)
753 if reason[0] == _("Insufficient data for checksum verification"):
757 _checksum_failure_temp_file(
758 mysettings["DISTDIR"], myfile)
759 writemsg_stdout(_("Refetching... "
760 "File renamed to '%s'\n\n") % \
761 temp_filename, noiselevel=-1)
765 mysettings.get("PORTAGE_QUIET", None) == "1"
766 digests = mydigests.get(myfile)
768 digests = list(digests)
771 "%s %s ;-)" % (myfile, " ".join(digests)))
773 continue # fetch any remaining files
775 # Create a reversed list since that is optimal for list.pop().
776 uri_list = filedict[myfile][:]
778 checksum_failure_count = 0
779 tried_locations = set()
782 # Eliminate duplicates here in case we've switched to
783 # "primaryuri" mode on the fly due to a checksum failure.
784 if loc in tried_locations:
786 tried_locations.add(loc)
788 writemsg_stdout(loc+" ", noiselevel=-1)
790 # allow different fetchcommands per protocol
791 protocol = loc[0:loc.find("://")]
793 global_config_path = GLOBAL_CONFIG_PATH
794 if mysettings['EPREFIX']:
795 global_config_path = os.path.join(mysettings['EPREFIX'],
796 GLOBAL_CONFIG_PATH.lstrip(os.sep))
798 missing_file_param = False
799 fetchcommand_var = "FETCHCOMMAND_" + protocol.upper()
800 fetchcommand = mysettings.get(fetchcommand_var)
801 if fetchcommand is None:
802 fetchcommand_var = "FETCHCOMMAND"
803 fetchcommand = mysettings.get(fetchcommand_var)
804 if fetchcommand is None:
806 _("!!! %s is unset. It should "
807 "have been defined in\n!!! %s/make.globals.\n") \
808 % (fetchcommand_var, global_config_path),
809 level=logging.ERROR, noiselevel=-1)
811 if "${FILE}" not in fetchcommand:
813 _("!!! %s does not contain the required ${FILE}"
814 " parameter.\n") % fetchcommand_var,
815 level=logging.ERROR, noiselevel=-1)
816 missing_file_param = True
818 resumecommand_var = "RESUMECOMMAND_" + protocol.upper()
819 resumecommand = mysettings.get(resumecommand_var)
820 if resumecommand is None:
821 resumecommand_var = "RESUMECOMMAND"
822 resumecommand = mysettings.get(resumecommand_var)
823 if resumecommand is None:
825 _("!!! %s is unset. It should "
826 "have been defined in\n!!! %s/make.globals.\n") \
827 % (resumecommand_var, global_config_path),
828 level=logging.ERROR, noiselevel=-1)
830 if "${FILE}" not in resumecommand:
832 _("!!! %s does not contain the required ${FILE}"
833 " parameter.\n") % resumecommand_var,
834 level=logging.ERROR, noiselevel=-1)
835 missing_file_param = True
837 if missing_file_param:
839 _("!!! Refer to the make.conf(5) man page for "
840 "information about how to\n!!! correctly specify "
841 "FETCHCOMMAND and RESUMECOMMAND.\n"),
842 level=logging.ERROR, noiselevel=-1)
843 if myfile != os.path.basename(loc):
849 mysize = os.stat(myfile_path).st_size
851 if e.errno not in (errno.ENOENT, errno.ESTALE):
857 writemsg(_("!!! File %s isn't fetched but unable to get it.\n") % myfile,
859 elif size is None or size > mysize:
860 writemsg(_("!!! File %s isn't fully fetched, but unable to complete it\n") % myfile,
863 writemsg(_("!!! File %s is incorrect size, "
864 "but unable to retry.\n") % myfile, noiselevel=-1)
869 if fetched != 2 and has_space:
870 #we either need to resume or start the download
873 mystat = os.stat(myfile_path)
875 if e.errno not in (errno.ENOENT, errno.ESTALE):
880 if mystat.st_size < fetch_resume_size:
881 writemsg(_(">>> Deleting distfile with size "
882 "%d (smaller than " "PORTAGE_FETCH_RESU"
883 "ME_MIN_SIZE)\n") % mystat.st_size)
885 os.unlink(myfile_path)
888 (errno.ENOENT, errno.ESTALE):
894 writemsg(_(">>> Resuming download...\n"))
895 locfetch=resumecommand
896 command_var = resumecommand_var
899 locfetch=fetchcommand
900 command_var = fetchcommand_var
901 writemsg_stdout(_(">>> Downloading '%s'\n") % \
902 re.sub(r'//(.+):.+@(.+)/',r'//\1:*password*@\2/', loc))
904 "DISTDIR": mysettings["DISTDIR"],
909 myfetch = shlex_split(locfetch)
910 myfetch = [varexpand(x, mydict=variables) for x in myfetch]
914 myret = _spawn_fetch(mysettings, myfetch)
918 apply_secpass_permissions(myfile_path,
919 gid=portage_gid, mode=0o664, mask=0o2)
922 except PortageException as e:
923 if not os.access(myfile_path, os.R_OK):
924 writemsg(_("!!! Failed to adjust permissions:"
925 " %s\n") % str(e), noiselevel=-1)
928 # If the file is empty then it's obviously invalid. Don't
929 # trust the return value from the fetcher. Remove the
930 # empty file and try to download again.
932 if os.stat(myfile_path).st_size == 0:
933 os.unlink(myfile_path)
936 except EnvironmentError:
939 if mydigests is not None and myfile in mydigests:
941 mystat = os.stat(myfile_path)
943 if e.errno not in (errno.ENOENT, errno.ESTALE):
949 if stat.S_ISDIR(mystat.st_mode):
950 # This can happen if FETCHCOMMAND erroneously
951 # contains wget's -P option where it should
954 _("!!! The command specified in the "
955 "%s variable appears to have\n!!! "
956 "created a directory instead of a "
957 "normal file.\n") % command_var,
958 level=logging.ERROR, noiselevel=-1)
960 _("!!! Refer to the make.conf(5) "
961 "man page for information about how "
962 "to\n!!! correctly specify "
963 "FETCHCOMMAND and RESUMECOMMAND.\n"),
964 level=logging.ERROR, noiselevel=-1)
967 # no exception? file exists. let digestcheck() report
968 # an appropriately for size or checksum errors
970 # If the fetcher reported success and the file is
971 # too small, it's probably because the digest is
972 # bad (upstream changed the distfile). In this
973 # case we don't want to attempt to resume. Show a
974 # digest verification failure to that the user gets
975 # a clue about what just happened.
976 if myret != os.EX_OK and \
977 mystat.st_size < mydigests[myfile]["size"]:
978 # Fetch failed... Try the next one... Kill 404 files though.
979 if (mystat[stat.ST_SIZE]<100000) and (len(myfile)>4) and not ((myfile[-5:]==".html") or (myfile[-4:]==".htm")):
980 html404=re.compile("<title>.*(not found|404).*</title>",re.I|re.M)
981 if html404.search(codecs.open(
982 _unicode_encode(myfile_path,
983 encoding=_encodings['fs'], errors='strict'),
984 mode='r', encoding=_encodings['content'], errors='replace'
987 os.unlink(mysettings["DISTDIR"]+"/"+myfile)
988 writemsg(_(">>> Deleting invalid distfile. (Improper 404 redirect from server.)\n"))
991 except (IOError, OSError):
996 # File is the correct size--check the checksums for the fetched
997 # file NOW, for those users who don't have a stable/continuous
998 # net connection. This way we have a chance to try to download
999 # from another mirror...
1000 verified_ok,reason = verify_all(mysettings["DISTDIR"]+"/"+myfile, mydigests[myfile])
1003 writemsg(_("!!! Fetched file: %s VERIFY FAILED!\n") % myfile,
1005 writemsg(_("!!! Reason: %s\n") % reason[0],
1007 writemsg(_("!!! Got: %s\n!!! Expected: %s\n") % \
1008 (reason[1], reason[2]), noiselevel=-1)
1009 if reason[0] == _("Insufficient data for checksum verification"):
1012 _checksum_failure_temp_file(
1013 mysettings["DISTDIR"], myfile)
1014 writemsg_stdout(_("Refetching... "
1015 "File renamed to '%s'\n\n") % \
1016 temp_filename, noiselevel=-1)
1018 checksum_failure_count += 1
1019 if checksum_failure_count == \
1020 checksum_failure_primaryuri:
1021 # Switch to "primaryuri" mode in order
1022 # to increase the probablility of
1025 primaryuri_dict.get(myfile)
1028 reversed(primaryuris))
1029 if checksum_failure_count >= \
1030 checksum_failure_max_tries:
1034 eout.quiet = mysettings.get("PORTAGE_QUIET", None) == "1"
1035 digests = mydigests.get(myfile)
1037 eout.ebegin("%s %s ;-)" % \
1038 (myfile, " ".join(sorted(digests))))
1046 elif mydigests!=None:
1047 writemsg(_("No digest file available and download failed.\n\n"),
1050 if use_locks and file_lock:
1051 unlockfile(file_lock)
1055 writemsg_stdout("\n", noiselevel=-1)
1057 if restrict_fetch and not restrict_fetch_msg:
1058 restrict_fetch_msg = True
1059 msg = _("\n!!! %s/%s"
1060 " has fetch restriction turned on.\n"
1061 "!!! This probably means that this "
1062 "ebuild's files must be downloaded\n"
1063 "!!! manually. See the comments in"
1064 " the ebuild for more information.\n\n") % \
1065 (mysettings["CATEGORY"], mysettings["PF"])
1067 level=logging.ERROR, noiselevel=-1)
1068 elif restrict_fetch:
1072 elif not filedict[myfile]:
1073 writemsg(_("Warning: No mirrors available for file"
1074 " '%s'\n") % (myfile), noiselevel=-1)
1076 writemsg(_("!!! Couldn't download '%s'. Aborting.\n") % myfile,
1082 failed_files.add(myfile)