Updated EDB Windows + macOS installers pages for PG12
[pgweb/local.git] / tools / ftp / spider_yum.py
blob4dfd6191991a83967d87361f5e5565185fdd7814
1 #!/usr/bin/env python3
2 import argparse
3 import sys
4 import os
5 import re
6 import json
7 import requests
8 from collections import defaultdict
9 from tempfile import NamedTemporaryFile
11 re_platformdir = re.compile('^(\w+)-(\d+)-([^-]+)$')
13 if __name__ == "__main__":
14 parser = argparse.ArgumentParser(description="Spider repo RPMs")
15 parser.add_argument('yumroot', type=str, help='YUM root path')
16 parser.add_argument('target', type=str, help='Target URL or filename')
18 args = parser.parse_args()
20 platforms = defaultdict(list)
21 for repodir in os.listdir('{0}/reporpms'.format(args.yumroot)):
22 m = re_platformdir.match(repodir)
23 if m:
24 platname = m.group(1)
25 platver = m.group(2)
26 arch = m.group(3)
27 platforms['{0}-{1}'.format(platname, platver)].append(arch)
29 j = json.dumps({'platforms': platforms})
31 if args.target.startswith('http://') or args.target.startswith('https://'):
32 r = requests.put(
33 args.target,
34 data=j,
35 headers={
36 'Content-type': 'application/json',
37 'Host': 'www.postgresql.org',
40 if r.status_code != 200:
41 print("Failed to upload, code: %s" % r.status_code)
42 sys.exit(1)
44 if r.text != "NOT CHANGED" and r.text != "OK":
45 print("Failed to upload: %s" % x)
46 sys.exit(1)
47 else:
48 with NamedTemporaryFile(mode='w', dir=os.path.dirname(os.path.abspath(args.target))) as f:
49 f.write(j)
50 f.flush()
51 if os.path.isfile(args.target):
52 os.unlink(args.target)
53 os.link(f.name, args.target)