Completed
Push — master ( d1933a...024ef5 )
by John
03:03
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
9
10
__author__ = "Thurask"
11
__license__ = "WTFPL v2"
12
__copyright__ = "Copyright 2015-2016 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
    with open(fname, "wb") as file:
30
        req = requests.get(url, stream=True)
31
        if req.status_code == 200:  # 200 OK
32
            print("DOWNLOADING {0}".format(lfname))
33
            for chunk in req.iter_content(chunk_size=1024):
34
                file.write(chunk)
35
        else:
36
            print("ERROR: HTTP {0} IN {1}".format(req.status_code, lfname))
37
38
39
def prep_file(datafile, filelist):
40
    """
41
    Prepare a file for download if necessary.
42
43
    :param datafile: Datafile instance.
44
    :type datafile: bbarchivist.bbconstants.Datafile
45
46
    :param filelist: List of files that need downloading.
47
    :type filelist: list(str)
48
    """
49
    basename = "https://github.com/thurask/bbarchivist/raw/master/bbarchivist/"
50
    afile = os.path.basename(datafile.location)
51
    try:
52
        if os.path.getsize(datafile.location) != datafile.size:
53
            filelist.append(basename + afile)
54
    except FileNotFoundError:
55
        filelist.append(basename + afile)
56
    return filelist
57
58
59
def git_ignore(datafile):
60
    """
61
    Update git index to deal with pesky issues w/r/t datafiles.
62
63
    :param datafile: Datafile instance.
64
    :type datafile: bbarchivist.bbconstants.Datafile
65
    """
66
    cmd = "git update-index --assume-unchanged {0}".format(datafile)
67
    subprocess.call(cmd, shell=True)
68
69
70
def main():
71
    """
72
    Download cap and cfp files, in lieu of Git-LFS.
73
    """
74
    files = []
75
    files = prep_file(CAP, files)
76
    files = prep_file(CFP, files)
77
    outdir = os.path.join(os.getcwd(), "bbarchivist")
78
    if files:
79
        for file in files:
80
            download(file, outdir)
81
            git_ignore(os.path.join(outdir, os.path.basename(file)))
82
        print("GIT INDEX UPDATED, RE-TRACK IF UPDATE NEEDED")
83
    else:
84
        print("NOTHING TO DOWNLOAD!")
85
86
87
if __name__ == "__main__":
88
    main()
89