This repository has been archived by the owner on Apr 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-atfa-api.py
executable file
·108 lines (89 loc) · 3.27 KB
/
get-atfa-api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Universidade Federal do Rio de Janeiro
Escola Politécnica
Projeto Final de Graduação
Ambiente de Teste para Filtros Adaptativos
Pedro Angelo Medeiros Fonini <[email protected]>
Orientador: Markus Lima
"""
import sys
import pathlib
import urllib.request
from urllib.parse import urlparse
default_header_filename = 'atfa_api.h'
default_header_dir = pathlib.Path('src/')
default_url = 'https://github.com/fofoni/atfa-examples/raw/master/{}'.format(
default_header_filename)
if __name__ == '__main__':
### Get URL input and filesystem output files from command line options
import argparse
parser = argparse.ArgumentParser(description='Download ATFA API header')
parser.add_argument(
"url",
help="URL from which to download the header",
nargs='?',
default=default_url
)
parser.add_argument(
"outfile",
help="save the header in this location",
nargs='?',
)
parser.add_argument(
"-f", "--force",
help="download even if file already exists",
action='store_true',
)
args = parser.parse_args()
if not args.url:
raise ValueError("URL must be non-empty")
# deduce header filename from URL
url_path = urlparse(args.url).path
if not url_path.endswith('/'):
default_header_filename = pathlib.PurePosixPath(url_path).name
# deduce final header filename and location
if args.outfile is None:
# user didn't provide a path
header_dir = default_header_dir.resolve()
header_filename = default_header_filename
else:
# user did provide a path
outfile_path = pathlib.Path(args.outfile)
if outfile_path.is_dir():
header_dir = outfile_path.resolve()
header_filename = default_header_filename
else:
header_dir = outfile_path.parent.resolve()
header_filename = outfile_path.name
header_path = header_dir / header_filename
### Check if output file already exists
if not args.force and header_path.exists():
# if target file exists but is empty, silently override it
if header_path.stat().st_size != 0:
try:
header_path = header_path.relative_to(pathlib.Path.cwd())
except ValueError: pass
print(("Nonempty file ‘{}’ already exists: skipping download. Use"
" ‘-f’ to download and override.").format(header_path))
sys.exit(0)
# ignore `--force' when it would override a directory
if header_path.is_dir():
raise ValueError("OUTFILE is ‘{}’, which is a"
" directory".format(header_path))
### Download header file
with urllib.request.urlopen(args.url) as resp:
if resp.getcode() != 200:
raise RuntimeError("Could not download ‘{}’ from ‘{}’".format(
header_filename, args.url))
b = resp.read()
### Write downloaded data to output file
with header_path.open('wb') as f:
f.write(b)
### End
try:
header_path = header_path.relative_to(pathlib.Path.cwd())
except ValueError: pass
print("Successfully downloaded ATFA API header file ‘{}’".format(
header_path))