Merge pull request #1322 from huftis/update-nn-translation-2022-june
[gpodder.git] / tools / localdepends.py
blobe1e4265f2aa4848646eeb2e476d999bbaf128a82
1 #!/usr/bin/env python3
3 # gPodder dependency installer for running the CLI from the source tree
5 # Run "python localdepends.py" and it will download and inject dependencies,
6 # so you only need a standard Python installation for the command-line utility
8 # Thomas Perl <thp.io/about>; 2012-02-11
11 import io
12 import os
13 import re
14 import shutil
15 import sys
16 import tarfile
17 import tempfile
18 import urllib.error
19 import urllib.parse
20 import urllib.request
22 sys.stdout = sys.stderr
24 src_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'src'))
25 tmp_dir = tempfile.mkdtemp()
27 MODULES = [
28 # Module name, Regex-file chooser (1st group = location in "src/")
29 ('podcastparser', r'podcastparser-[0-9.]+/(podcastparser.py)'),
30 ('mygpoclient', r'mygpoclient-[0-9.]+/(mygpoclient/[^/]*\.py)')
34 def get_tarball_url(modulename):
35 url = 'http://pypi.python.org/pypi/' + modulename
36 html = urllib.request.urlopen(url).read().decode('utf-8')
37 match = re.search(r'(http[s]?://[^>]*%s-([0-9.]*)(?:\.post\d+)?\.tar\.gz)' % modulename, html)
38 return match.group(0) if match is not None else None
41 for module, required_files in MODULES:
42 print('Fetching', module, '...', end=' ')
43 tarball_url = get_tarball_url(module)
44 if tarball_url is None:
45 print('Cannot determine download URL for', module, '- aborting!')
46 break
47 data = urllib.request.urlopen(tarball_url).read()
48 print('%d KiB' % (len(data) // 1024))
49 print(' downloaded from %s' % tarball_url)
50 tar = tarfile.open(fileobj=io.BytesIO(data))
51 for name in tar.getnames():
52 match = re.match(required_files, name)
53 if match is not None:
54 target_name = match.group(1)
55 target_file = os.path.join(src_dir, target_name)
56 if os.path.exists(target_file):
57 print('Skipping:', target_file)
58 continue
60 target_dir = os.path.dirname(target_file)
61 if not os.path.isdir(target_dir):
62 os.mkdir(target_dir)
64 print('Extracting:', target_name)
65 tar.extract(name, tmp_dir)
66 shutil.move(os.path.join(tmp_dir, name), target_file)
68 shutil.rmtree(tmp_dir)