Coverage for kiertotie/elif.py: 0.00%
62 statements
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-04 18:44:05 +00:00
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-04 18:44:05 +00:00
1#! /usr/bin/env python
2"""Creating the fetch script for the files in the hierarchy from JSON proxy data."""
3import json
4import pathlib
5import random
6import sys
8BASE_URL = 'https://master.qt.io/'
9DASH = '-'
10ACTIONS = []
11DEFAULT_SCRIPT = 'fetch.sh'
12EASING = 3
13NL = '\n'
14SP = ' '
15ESP = '\\' + SP
16URL_ENC_SP = '%20'
17RATE = 2_000_000
18ENCODING = 'utf-8'
19TS_FORMAT = '%Y-%m-%d %H:%M:%S +00:00'
21if len(sys.argv) != 2:
22 print('elif.py json-file', file=sys.stderr)
23 sys.exit(2)
25store_path = pathlib.Path(sys.argv[1])
26with open(store_path, 'rt', encoding=ENCODING) as handle:
27 repo = json.load(handle)
29root_folder = store_path.name.split(DASH)[1]
30if root_folder == 'development':
31 root_folder += '_releases'
33folder_count = repo['count_folders']
34ACTIONS.append('#! /usr/bin/env bash')
35ACTIONS.append(f'# Derived root folder to be ({root_folder})')
36ACTIONS.append(f'echo "Initializing the tree below root folder with random waits between 1 and {EASING} secs"')
37transfers = repo['count_files']
38size_files_bytes = repo['size_files_bytes']
39ACTIONS.append(
40 f'# Detected {transfers} files with {size_files_bytes} bytes across {folder_count} folders below {root_folder}'
41)
42anchor = pathlib.Path.cwd()
43bytes_cum = 0
44for n, entry in enumerate(repo['tree']['files'], start=1):
45 entry_path = entry['path']
46 if entry_path == '.':
47 continue
48 path = pathlib.Path(entry_path)
49 size_bytes = entry['size']
50 secs_est = int(size_bytes / RATE)
51 secs_est_disp = 'less than a second' if secs_est < 1 else f'approx. {secs_est} seconds'
52 bytes_cum += size_bytes
53 nap = random.randint(1, EASING) # nosec B311
54 ACTIONS.append(f'echo sleeping for {nap} secs before transfering file {n} of {transfers}')
55 ACTIONS.append(f'sleep {nap}')
56 ACTIONS.append(f'cd {anchor}/{root_folder}/{path.parent} || exit 1')
57 ACTIONS.append('pwd')
58 ACTIONS.append(
59 f'echo started the transfer {n} of {transfers} requesting {size_bytes} bytes'
60 f' assuming {secs_est_disp} at "$(date +"%Y-%m-%d %H:%M:%S +00:00")"'
61 )
62 if SP not in str(path):
63 ACTIONS.append(f"echo curl -kORLs --limit-rate 2000k '{BASE_URL}{root_folder}/{path}'")
64 ACTIONS.append(f"curl -kORLs --limit-rate 2000k '{BASE_URL}{root_folder}/{path}'")
65 else:
66 path_url_enc = str(path).replace(SP, URL_ENC_SP)
67 path_local = f'{str(path.name).replace(SP, ESP)}'
68 ACTIONS.append(f"echo curl -kRLs --limit-rate 2000k '{BASE_URL}{root_folder}/{path_url_enc}' -o '{path_local}'")
69 ACTIONS.append(f"curl -kRLs --limit-rate 2000k '{BASE_URL}{root_folder}/{path_url_enc}' -o '{path_local}'")
70 ACTIONS.append(
71 f'echo transfer is complete {n} of {transfers} for cum. {bytes_cum} of'
72 f' tot. {size_files_bytes} bytes at "$(date +"%Y-%m-%d %H:%M:%S +00:00")"'
73 )
75ACTIONS.append('echo OK')
76ACTIONS.append('') # Final newline at end of fetch script
78with open(DEFAULT_SCRIPT, 'wt', encoding=ENCODING) as handle:
79 handle.write(NL.join(ACTIONS))