projects/guix·blob ruby-team · refs

65 lines · 2.5 KiB

1# -*- coding: utf-8 -*-
2# GNU Guix --- Functional package management for GNU
3# Copyright © 2021 Maxim Cournoyer <[email protected]>
4#
5# This file is part of GNU Guix.
6#
7# GNU Guix is free software; you can redistribute it and/or modify it
8# under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 3 of the License, or (at
10# your option) any later version.
11#
12# GNU Guix is distributed in the hope that it will be useful, but
13# WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20import os
21import site
22import sys
23
24# Commentary:
25#
26# Site-specific customization for Guix.
27#
28# The program below honors the GUIX_PYTHONPATH environment variable to
29# discover Python packages. File names appearing in this variable that match
30# a predefined versioned installation prefix are added to the sys.path. To be
31# considered, a Python package must be installed under the
32# 'lib/pythonX.Y/site-packages' directory, where X and Y are the major and
33# minor version numbers of the Python interpreter.
34#
35# Code:
36
37major_minor = '{}.{}'.format(*sys.version_info)
38site_packages_prefix = os.path.join(
39 'lib', 'python' + major_minor, 'site-packages')
40python_site = os.path.normpath(os.path.join(sys.prefix, site_packages_prefix))
41
42try:
43 all_sites_raw = os.environ['GUIX_PYTHONPATH'].split(os.path.pathsep)
44except KeyError:
45 all_sites_raw = []
46# Normalize paths, otherwise a trailing slash would cause it to not match.
47all_sites_norm = [os.path.normpath(p) for p in all_sites_raw]
48matching_sites = [p for p in all_sites_norm
49 if p.endswith(site_packages_prefix)]
50
51if matching_sites:
52 # Deduplicate the entries, append them to sys.path, and handle any
53 # .pth files they contain.
54 for s in matching_sites:
55 site.addsitedir(s)
56
57 # Move the entries that were appended to sys.path in front of
58 # Python's own site-packages directory. This enables Guix
59 # packages to override Python's bundled packages, such as 'pip'.
60 python_site_index = sys.path.index(python_site)
61 new_site_start_index = sys.path.index(matching_sites[0])
62 if python_site_index < new_site_start_index:
63 sys.path = (sys.path[:python_site_index]
64 + sys.path[new_site_start_index:]
65 + sys.path[python_site_index:new_site_start_index])