aboutsummaryrefslogtreecommitdiffstats
path: root/images/create_imgs_package.py
blob: 82b7d65d4154506a772a398b209fb8b6e6411bd3 (plain)
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
#!/usr/bin/env python
#
# Copyright 2014 Ettus Research LLC
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
"""
Command-line utility to create a .zip-file with the current image set.
"""

import re
import os
import uhdimgs
import glob
import subprocess
import argparse
import shutil

def clear_img_dir(img_root_dir):
    """ Removes non-image files from the images dir """
    globs = ["*.tag", "LICENSE"]
    for the_glob in globs:
        for filename in glob.iglob(os.path.join(img_root_dir, the_glob)):
            print 'Removing file from images directory: ', filename
            os.unlink(filename)

def get_zipfilename_from_cpack_output(cpoutput):
    """ Parses the output of the ZIP-file creating script
    and scrapes the actual file name. """
    regex = re.compile("\/build\/(?P<filename>[^\/]+\.zip)")
    results = regex.search(cpoutput)
    return results.group('filename')

def parse_args():
    """ Parse args, duh """
    parser = argparse.ArgumentParser(description='Link the current set of images to this commit.')
    parser.add_argument('--commit', default=None,
                       help='Supply a commit message to the changes to host/CMakeLists.txt.')
    parser.add_argument('-r', '--release-mode', default="",
                       help='Specify UHD_RELEASE_MODE. Typically "release" or "rc1" or similar.')
    parser.add_argument('--skip-edit', default=False, action='store_true',
                       help='Do not edit the CMakeLists.txt file.')
    parser.add_argument('--skip-move', default=False, action='store_true',
                       help='Do not move the archives after creating them.')
    parser.add_argument('--patch', help='Override patch version number.')
    return parser.parse_args()

def move_zip_to_repo(base_url, zipfilename):
    final_destination = os.path.join(base_url, zipfilename)
    if os.path.exists(final_destination):
        print "WARNING: A file with name {0} is already in the images repository.".format(zipfilename)
        print "Overwrite? [y/N]",
        ans = raw_input()
        if ans.strip().upper() != 'Y':
            return
        os.unlink(final_destination)
    shutil.move(zipfilename, base_url)

def main():
    " Go, go, go! "
    args = parse_args()
    img_root_dir = os.path.join(uhdimgs.get_images_dir(), 'images')
    os.chdir(uhdimgs.get_images_dir())
    print "== Clearing out the images directory..."
    clear_img_dir(img_root_dir)
    print "== Creating archives..."
    cpack_cmd = ["./make_zip.sh",]
    cpack_cmd.append(args.release_mode)
    if args.patch is not None:
        cpack_cmd.append("-DUHD_PATCH_OVERRIDE={}".format(args.patch))
    try:
        cpack_output = subprocess.check_output(cpack_cmd)
    except subprocess.CalledProcessError as e:
        print e.output
        raise SystemExit, 1
    zipfilename = get_zipfilename_from_cpack_output(cpack_output)
    print "Filename: ", zipfilename
    print "== Calculating MD5 sum of ZIP archive..."
    md5 = uhdimgs.md5_checksum(zipfilename)
    print 'MD5: ', md5
    base_url = uhdimgs.get_base_url()
    if not args.skip_move and uhdimgs.base_url_is_local(base_url) and os.access(base_url, os.W_OK):
        print "== Moving ZIP file to {0}...".format(base_url)
        move_zip_to_repo(base_url, zipfilename)
    print "== Updating CMakeLists.txt..."
    uhdimgs.update_main_cmake_file(md5, zipfilename)
    if args.commit is not None:
        print "== Committing changes..."
        subprocess.check_call(['git', 'commit', '-m', args.commit, uhdimgs.get_cmake_main_file()])
    print "== Done!"

if __name__ == "__main__":
    main()