Completed
Push — master ( a9206b...acfaee )
by John
02:28
created

download()   A

Complexity

Conditions 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
c 2
b 0
f 0
dl 0
loc 17
rs 9.4285
1
#!/usr/bin/env python3
2
"""Manually download dat files, if Git-LFS isn't working or something."""
3
4
import os
5
import subprocess
6
import requests
7
from bbarchivist.bbconstants import CAP, CFP, FLASHBAT, FLASHSH
8
9
__author__ = "Thurask"
10
__license__ = "WTFPL v2"
11
__copyright__ = "2015-2017 Thurask"
12
13
14
def prep_download(url, output_directory=None):
15
    """
16
    Prepare download.
17
18
    :param url: URL to download from.
19
    :type url: str
20
21
    :param output_directory: Download folder. Default is local.
22
    :type output_directory: str
23
    """
24
    if output_directory is None:
25
        output_directory = os.getcwd()
26
    lfname = url.split('/')[-1]
27
    fname = os.path.join(output_directory, lfname)
28
    sess = requests.Session()
29
    return lfname, fname, sess
30
31
32
def download_write(req, lfname, file):
33
    """
34
    Write downloaded chunks to file.
35
36
    :param req: Response object.
37
    :type req: requests.Response
38
39
    :param lfname: Filename.
40
    :type lfname: str
41
42
    :param file: Open (!!!) file handle. Output file.
43
    :type file: str
44
    """
45
    print("DOWNLOADING {0}".format(lfname))
46
    for chunk in req.iter_content(chunk_size=1024):
47
        file.write(chunk)
48
49
50
def download(url, output_directory=None):
51
    """
52
    Download file from given URL.
53
54
    :param url: URL to download from.
55
    :type url: str
56
57
    :param output_directory: Download folder. Default is local.
58
    :type output_directory: str
59
    """
60
    lfname, fname, sess = prep_download(url, output_directory)
61
    with open(fname, "wb") as file:
62
        req = sess.get(url, stream=True)
63
        if req.status_code == 200:  # 200 OK
64
            download_write(req, lfname, file)
65
        else:
66
            print("ERROR: HTTP {0} IN {1}".format(req.status_code, lfname))
67
68
69
def prep_file(datafile, filelist):
70
    """
71
    Prepare a file for download if necessary.
72
73
    :param datafile: Datafile instance.
74
    :type datafile: bbarchivist.bbconstants.Datafile
75
76
    :param filelist: List of files that need downloading.
77
    :type filelist: list(str)
78
    """
79
    basename = "https://github.com/thurask/bbarchivist/raw/master/bbarchivist/"
80
    afile = os.path.basename(datafile.location)
81
    try:
82
        if os.path.getsize(datafile.location) != datafile.size:
83
            filelist.append(basename + afile)
84
    except FileNotFoundError:
85
        filelist.append(basename + afile)
86
    return filelist
87
88
89
def git_ignore(datafile):
90
    """
91
    Update git index to deal with pesky issues w/r/t datafiles.
92
93
    :param datafile: Datafile instance.
94
    :type datafile: bbarchivist.bbconstants.Datafile
95
    """
96
    cmd = "git update-index --assume-unchanged {0}".format(datafile)
97
    subprocess.call(cmd, shell=True)
98
99
100
def main():
101
    """
102
    Download dat files, in lieu of Git-LFS.
103
    """
104
    files = []
105
    files = prep_file(CAP, files)
106
    files = prep_file(CFP, files)
107
    files = prep_file(FLASHBAT, files)
108
    files = prep_file(FLASHSH, files)
109
    outdir = os.path.join(os.getcwd(), "bbarchivist", "datfiles")
110
    if files:
111
        for file in files:
112
            download(file, outdir)
113
            git_ignore(os.path.join(outdir, os.path.basename(file)))
114
        print("GIT INDEX UPDATED, RE-TRACK IF UPDATE NEEDED")
115
    else:
116
        print("NOTHING TO DOWNLOAD!")
117
118
119
if __name__ == "__main__":
120
    main()
121