Let us walk on the 3-isogeny graph
Loading...
Searching...
No Matches
ResourceManager Class Reference

Public Member Functions

 __init__ (self)
 
 resource_exists (self, package_or_requirement, resource_name)
 
 resource_isdir (self, package_or_requirement, resource_name)
 
 resource_filename (self, package_or_requirement, resource_name)
 
 resource_stream (self, package_or_requirement, resource_name)
 
 resource_string (self, package_or_requirement, resource_name)
 
 resource_listdir (self, package_or_requirement, resource_name)
 
 extraction_error (self)
 
 get_cache_path (self, archive_name, names=())
 
 postprocess (self, tempname, filename)
 
 set_extraction_path (self, path)
 
 cleanup_resources (self, force=False)
 

Data Fields

 cached_files
 

Static Public Attributes

 extraction_path = None
 

Static Protected Member Functions

 _warn_unsafe_extraction_path (path)
 

Detailed Description

Manage resource extraction and packages

Definition at line 1195 of file __init__.py.

Constructor & Destructor Documentation

◆ __init__()

__init__ (   self)

Definition at line 1200 of file __init__.py.

1200 def __init__(self):
1201 self.cached_files = {}
1202

Referenced by Protocol.__init_subclass__().

Here is the caller graph for this function:

Member Function Documentation

◆ _warn_unsafe_extraction_path()

_warn_unsafe_extraction_path (   path)
staticprotected
If the default extraction path is overridden and set to an insecure
location, such as /tmp, it opens up an opportunity for an attacker to
replace an extracted file with an unauthorized payload. Warn the user
if a known insecure location is used.

See Distribute #375 for more details.

Definition at line 1289 of file __init__.py.

1289 def _warn_unsafe_extraction_path(path):
1290 """
1291 If the default extraction path is overridden and set to an insecure
1292 location, such as /tmp, it opens up an opportunity for an attacker to
1293 replace an extracted file with an unauthorized payload. Warn the user
1294 if a known insecure location is used.
1295
1296 See Distribute #375 for more details.
1297 """
1298 if os.name == 'nt' and not path.startswith(os.environ['windir']):
1299 # On Windows, permissions are generally restrictive by default
1300 # and temp directories are not writable by other users, so
1301 # bypass the warning.
1302 return
1303 mode = os.stat(path).st_mode
1304 if mode & stat.S_IWOTH or mode & stat.S_IWGRP:
1305 msg = (
1306 "Extraction path is writable by group/others "
1307 "and vulnerable to attack when "
1308 "used with get_resource_filename ({path}). "
1309 "Consider a more secure "
1310 "location (set with .set_extraction_path or the "
1311 "PYTHON_EGG_CACHE environment variable)."
1312 ).format(**locals())
1313 warnings.warn(msg, UserWarning)
1314
for i

◆ cleanup_resources()

cleanup_resources (   self,
  force = False 
)
Delete all extracted resource files and directories, returning a list
of the file and directory names that could not be successfully removed.
This function does not have any concurrency protection, so it should
generally only be called when the extraction path is a temporary
directory exclusive to a single process.  This method is not
automatically called; you must call it explicitly or register it as an
``atexit`` function if you wish to ensure cleanup of a temporary
directory used for extractions.

Definition at line 1359 of file __init__.py.

1359 def cleanup_resources(self, force=False):
1360 """
1361 Delete all extracted resource files and directories, returning a list
1362 of the file and directory names that could not be successfully removed.
1363 This function does not have any concurrency protection, so it should
1364 generally only be called when the extraction path is a temporary
1365 directory exclusive to a single process. This method is not
1366 automatically called; you must call it explicitly or register it as an
1367 ``atexit`` function if you wish to ensure cleanup of a temporary
1368 directory used for extractions.
1369 """
1370 # XXX
1371
1372

◆ extraction_error()

extraction_error (   self)
Give an error message for problems extracting file(s)

Definition at line 1233 of file __init__.py.

1233 def extraction_error(self):
1234 """Give an error message for problems extracting file(s)"""
1235
1236 old_exc = sys.exc_info()[1]
1237 cache_path = self.extraction_path or get_default_cache()
1238
1239 tmpl = textwrap.dedent(
1240 """
1241 Can't extract file(s) to egg cache
1242
1243 The following error occurred while trying to extract file(s)
1244 to the Python egg cache:
1245
1246 {old_exc}
1247
1248 The Python egg cache directory is currently set to:
1249
1250 {cache_path}
1251
1252 Perhaps your account does not have write access to this directory?
1253 You can change the cache directory by setting the PYTHON_EGG_CACHE
1254 environment variable to point to an accessible directory.
1255 """
1256 ).lstrip()
1257 err = ExtractionError(tmpl.format(**locals()))
1258 err.manager = self
1259 err.cache_path = cache_path
1260 err.original_error = old_exc
1261 raise err
1262

◆ get_cache_path()

get_cache_path (   self,
  archive_name,
  names = () 
)
Return absolute location in cache for `archive_name` and `names`

The parent directory of the resulting path will be created if it does
not already exist.  `archive_name` should be the base filename of the
enclosing egg (which may not be the name of the enclosing zipfile!),
including its ".egg" extension.  `names`, if provided, should be a
sequence of path name parts "under" the egg's extraction location.

This method should only be called by resource providers that need to
obtain an extraction location, and only for names they intend to
extract, as it tracks the generated names for possible cleanup later.

Definition at line 1263 of file __init__.py.

1263 def get_cache_path(self, archive_name, names=()):
1264 """Return absolute location in cache for `archive_name` and `names`
1265
1266 The parent directory of the resulting path will be created if it does
1267 not already exist. `archive_name` should be the base filename of the
1268 enclosing egg (which may not be the name of the enclosing zipfile!),
1269 including its ".egg" extension. `names`, if provided, should be a
1270 sequence of path name parts "under" the egg's extraction location.
1271
1272 This method should only be called by resource providers that need to
1273 obtain an extraction location, and only for names they intend to
1274 extract, as it tracks the generated names for possible cleanup later.
1275 """
1276 extract_path = self.extraction_path or get_default_cache()
1277 target_path = os.path.join(extract_path, archive_name + '-tmp', *names)
1278 try:
1279 _bypass_ensure_directory(target_path)
1280 except Exception:
1281 self.extraction_error()
1282
1283 self._warn_unsafe_extraction_path(extract_path)
1284
1285 self.cached_files[target_path] = 1
1286 return target_path
1287

◆ postprocess()

postprocess (   self,
  tempname,
  filename 
)
Perform any platform-specific postprocessing of `tempname`

This is where Mac header rewrites should be done; other platforms don't
have anything special they should do.

Resource providers should call this method ONLY after successfully
extracting a compressed resource.  They must NOT call it on resources
that are already in the filesystem.

`tempname` is the current (temporary) name of the file, and `filename`
is the name it will be renamed to by the caller after this routine
returns.

Definition at line 1315 of file __init__.py.

1315 def postprocess(self, tempname, filename):
1316 """Perform any platform-specific postprocessing of `tempname`
1317
1318 This is where Mac header rewrites should be done; other platforms don't
1319 have anything special they should do.
1320
1321 Resource providers should call this method ONLY after successfully
1322 extracting a compressed resource. They must NOT call it on resources
1323 that are already in the filesystem.
1324
1325 `tempname` is the current (temporary) name of the file, and `filename`
1326 is the name it will be renamed to by the caller after this routine
1327 returns.
1328 """
1329
1330 if os.name == 'posix':
1331 # Make the resource executable
1332 mode = ((os.stat(tempname).st_mode) | 0o555) & 0o7777
1333 os.chmod(tempname, mode)
1334

◆ resource_exists()

resource_exists (   self,
  package_or_requirement,
  resource_name 
)
Does the named resource exist?

Definition at line 1203 of file __init__.py.

1203 def resource_exists(self, package_or_requirement, resource_name):
1204 """Does the named resource exist?"""
1205 return get_provider(package_or_requirement).has_resource(resource_name)
1206

◆ resource_filename()

resource_filename (   self,
  package_or_requirement,
  resource_name 
)
Return a true filesystem path for specified resource

Definition at line 1211 of file __init__.py.

1211 def resource_filename(self, package_or_requirement, resource_name):
1212 """Return a true filesystem path for specified resource"""
1213 return get_provider(package_or_requirement).get_resource_filename(
1214 self, resource_name
1215 )
1216

◆ resource_isdir()

resource_isdir (   self,
  package_or_requirement,
  resource_name 
)
Is the named resource an existing directory?

Definition at line 1207 of file __init__.py.

1207 def resource_isdir(self, package_or_requirement, resource_name):
1208 """Is the named resource an existing directory?"""
1209 return get_provider(package_or_requirement).resource_isdir(resource_name)
1210

◆ resource_listdir()

resource_listdir (   self,
  package_or_requirement,
  resource_name 
)
List the contents of the named resource directory

Definition at line 1229 of file __init__.py.

1229 def resource_listdir(self, package_or_requirement, resource_name):
1230 """List the contents of the named resource directory"""
1231 return get_provider(package_or_requirement).resource_listdir(resource_name)
1232

◆ resource_stream()

resource_stream (   self,
  package_or_requirement,
  resource_name 
)
Return a readable file-like object for specified resource

Definition at line 1217 of file __init__.py.

1217 def resource_stream(self, package_or_requirement, resource_name):
1218 """Return a readable file-like object for specified resource"""
1219 return get_provider(package_or_requirement).get_resource_stream(
1220 self, resource_name
1221 )
1222

◆ resource_string()

resource_string (   self,
  package_or_requirement,
  resource_name 
)
Return specified resource as a string

Definition at line 1223 of file __init__.py.

1223 def resource_string(self, package_or_requirement, resource_name):
1224 """Return specified resource as a string"""
1225 return get_provider(package_or_requirement).get_resource_string(
1226 self, resource_name
1227 )
1228

◆ set_extraction_path()

set_extraction_path (   self,
  path 
)
Set the base path where resources will be extracted to, if needed.

If you do not call this routine before any extractions take place, the
path defaults to the return value of ``get_default_cache()``.  (Which
is based on the ``PYTHON_EGG_CACHE`` environment variable, with various
platform-specific fallbacks.  See that routine's documentation for more
details.)

Resources are extracted to subdirectories of this path based upon
information given by the ``IResourceProvider``.  You may set this to a
temporary directory, but then you must call ``cleanup_resources()`` to
delete the extracted files when done.  There is no guarantee that
``cleanup_resources()`` will be able to remove all extracted files.

(Note: you may not change the extraction path for a given resource
manager once resources have been extracted, unless you first call
``cleanup_resources()``.)

Definition at line 1335 of file __init__.py.

1335 def set_extraction_path(self, path):
1336 """Set the base path where resources will be extracted to, if needed.
1337
1338 If you do not call this routine before any extractions take place, the
1339 path defaults to the return value of ``get_default_cache()``. (Which
1340 is based on the ``PYTHON_EGG_CACHE`` environment variable, with various
1341 platform-specific fallbacks. See that routine's documentation for more
1342 details.)
1343
1344 Resources are extracted to subdirectories of this path based upon
1345 information given by the ``IResourceProvider``. You may set this to a
1346 temporary directory, but then you must call ``cleanup_resources()`` to
1347 delete the extracted files when done. There is no guarantee that
1348 ``cleanup_resources()`` will be able to remove all extracted files.
1349
1350 (Note: you may not change the extraction path for a given resource
1351 manager once resources have been extracted, unless you first call
1352 ``cleanup_resources()``.)
1353 """
1354 if self.cached_files:
1355 raise ValueError("Can't change extraction path, files already extracted")
1356
1357 self.extraction_path = path
1358

Field Documentation

◆ cached_files

cached_files

Definition at line 1201 of file __init__.py.

◆ extraction_path

extraction_path = None
static
Set the base path where resources will be extracted to, if needed.

If you do not call this routine before any extractions take place, the
path defaults to the return value of ``get_default_cache()``.  (Which
is based on the ``PYTHON_EGG_CACHE`` environment variable, with various
platform-specific fallbacks.  See that routine's documentation for more
details.)

Resources are extracted to subdirectories of this path based upon
information given by the ``IResourceProvider``.  You may set this to a
temporary directory, but then you must call ``cleanup_resources()`` to
delete the extracted files when done.  There is no guarantee that
``cleanup_resources()`` will be able to remove all extracted files.

(Note: you may not change the extraction path for a given resource
manager once resources have been extracted, unless you first call
``cleanup_resources()``.)

Definition at line 1198 of file __init__.py.


The documentation for this class was generated from the following file: