PEX, n. A self-contained python environment.
| 134 | |
| 135 | |
| 136 | class PEX(object): # noqa: T000 |
| 137 | """PEX, n. |
| 138 | |
| 139 | A self-contained python environment. |
| 140 | """ |
| 141 | |
| 142 | class Error(Exception): |
| 143 | pass |
| 144 | |
| 145 | class NotFound(Error): |
| 146 | pass |
| 147 | |
| 148 | class InvalidEntryPoint(Error): |
| 149 | pass |
| 150 | |
| 151 | class ResourceBindingError(Error): |
| 152 | pass |
| 153 | |
| 154 | @classmethod |
| 155 | def _resolve_resource_path( |
| 156 | cls, |
| 157 | name, # type: str |
| 158 | resource, # type: str |
| 159 | ): |
| 160 | # type: (...) -> str |
| 161 | |
| 162 | rel_path = os.path.normpath(os.path.join(*resource.split("/"))) |
| 163 | if os.path.isabs(resource) or rel_path.startswith(os.pardir): |
| 164 | raise cls.ResourceBindingError( |
| 165 | "The following resource binding spec is invalid: {name}={resource}\n" |
| 166 | "The resource path {resource} must be relative to the `sys.path`.".format( |
| 167 | name=name, resource=resource |
| 168 | ) |
| 169 | ) |
| 170 | |
| 171 | for entry in sys.path: |
| 172 | value = os.path.join(entry, rel_path) |
| 173 | if os.path.isfile(value): |
| 174 | return value |
| 175 | |
| 176 | raise cls.ResourceBindingError( |
| 177 | "There was no resource file {resource} found on the `sys.path` corresponding to " |
| 178 | "the given resource binding spec `{name}={resource}`".format( |
| 179 | resource=resource, name=name |
| 180 | ) |
| 181 | ) |
| 182 | |
| 183 | @classmethod |
| 184 | def _clean_environment(cls, env=None, strip_pex_env=True): |
| 185 | if not strip_pex_env: |
| 186 | return |
| 187 | env = env or os.environ |
| 188 | for key in list(env): |
| 189 | if key and key.startswith("PEX_"): |
| 190 | del env[key] |
| 191 | |
| 192 | def __init__( |
| 193 | self, |
no outgoing calls