Completed
Push — master ( a13e50...889d8d )
by John
03:53
created

git_ignore()   A

Complexity

Conditions 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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