to get the modules from an installed package Args: package: The distribution name or package name Returns: import_names: The modules that in the package distribution import_version: The version of those modules, should be same and identical package_name: Th
(package)
| 480 | |
| 481 | |
| 482 | def get_modules_from_package(package): |
| 483 | """ to get the modules from an installed package |
| 484 | |
| 485 | Args: |
| 486 | package: The distribution name or package name |
| 487 | |
| 488 | Returns: |
| 489 | import_names: The modules that in the package distribution |
| 490 | import_version: The version of those modules, should be same and identical |
| 491 | package_name: The package name, if installed by whl file, the package is unknown, should be passed |
| 492 | |
| 493 | """ |
| 494 | from zipfile import ZipFile |
| 495 | from tempfile import mkdtemp |
| 496 | from subprocess import check_output, STDOUT |
| 497 | from glob import glob |
| 498 | import hashlib |
| 499 | from urllib.parse import urlparse |
| 500 | from urllib import request as urllib2 |
| 501 | from packaging.requirements import Requirement |
| 502 | |
| 503 | def urlretrieve(url, filename, data=None, auth=None): |
| 504 | if auth is not None: |
| 505 | # https://docs.python.org/2.7/howto/urllib2.html#id6 |
| 506 | password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() |
| 507 | |
| 508 | # Add the username and password. |
| 509 | # If we knew the realm, we could use it instead of None. |
| 510 | username, password = auth |
| 511 | top_level_url = urlparse(url).netloc |
| 512 | password_mgr.add_password(None, top_level_url, username, password) |
| 513 | |
| 514 | handler = urllib2.HTTPBasicAuthHandler(password_mgr) |
| 515 | |
| 516 | # create "opener" (OpenerDirector instance) |
| 517 | opener = urllib2.build_opener(handler) |
| 518 | else: |
| 519 | opener = urllib2.build_opener() |
| 520 | |
| 521 | res = opener.open(url, data=data) |
| 522 | |
| 523 | headers = res.info() |
| 524 | |
| 525 | with open(filename, 'wb') as fp: |
| 526 | fp.write(res.read()) |
| 527 | |
| 528 | return filename, headers |
| 529 | |
| 530 | def compute_checksum(target, algorithm='sha256', blocksize=2**13): |
| 531 | hashtype = getattr(hashlib, algorithm) |
| 532 | hash_ = hashtype() |
| 533 | logger.debug('computing checksum', target=target, algorithm=algorithm) |
| 534 | with open(target, 'rb') as f: |
| 535 | for chunk in iter(lambda: f.read(blocksize), b''): |
| 536 | hash_.update(chunk) |
| 537 | result = hash_.hexdigest() |
| 538 | logger.debug('computed checksum', result=result) |
| 539 | return result |
no test coverage detected
searching dependent graphs…