Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- This script list or download Python source files
- It was requested here: https://python-forum.io/Thread-downloading-source-for-every-version
- Dependencies: requests, requests_html
- Task: Remove external dependencies
- """
- import re
- import sys
- try:
- import requests
- import requests_html
- except ImportError:
- print('Please install requests and requests_html.', file=sys.stdout)
- sys.exit(255)
- def sorter(url):
- """
- This sorter is used to sort the
- python by version string.
- Early version have only two digits.
- """
- v1 = re.search(r'(\d+)\.(\d+)\.(\d+)', url)
- v2 = re.search(r'(\d+)\.(\d+)', url)
- if v1:
- return tuple(map(int, v1.groups()))
- elif v2:
- return tuple(map(int, [*v2.groups(), 0]))
- else:
- return (0,0,0)
- def get_python_sources(filetype='tgz'):
- """
- Grab all links from python.org/downloads/source/
- Return the source file archives sorted as a list.
- """
- url = 'https://www.python.org/downloads/source/'
- with requests_html.HTMLSession() as session:
- req = session.get(url)
- links = [link for link in req.html.links if link.endswith(filetype)]
- return sorted(links, key=sorter)
- def print_python_sources():
- """
- Print all python source links to stdout.
- """
- for link in get_python_sources():
- print(link)
- def download_python_sources():
- """
- Download all python source files to current location.
- """
- for link in get_python_sources():
- file = link.split('/')[-1]
- # find a better way to get the file name from url
- # maybe urllib.parse?
- # yarl is an alternative with more abstraction
- print('Downloading', file)
- req = requests.get(link, stream=True)
- with open(file, 'wb') as fd:
- for chunk in req.iter_content(chunk_size=64*1024**1):
- # what is the ideal chunk_size?
- # how to detect the best chunk_size?
- fd.write(chunk)
- def help():
- print('python3', sys.argv[0], '<list|download>')
- sys.exit(1)
- if __name__ == '__main__':
- if len(sys.argv) != 2:
- help()
- cmd = sys.argv[1].lower()
- if cmd not in ('download', 'list'):
- help()
- try:
- if cmd == 'list':
- print_python_sources()
- elif cmd == 'download':
- download_python_sources()
- except Exception as e:
- # catching all exceptions here, the're bubbleing up
- # extend this to handle different cases
- print('Error:', e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement