summarylogtreecommitdiffstats
path: root/install_generated_packages.py
blob: a9d5a2d106c94fc42bff1e4e637d3e573f8704a3 (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
105
106
107
108
109
110
111
112
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function

import os, sys
from termcolor import colored, cprint
from argparse import ArgumentParser
import pickle
from subprocess import Popen, PIPE, STDOUT
import threading

lock = threading.Lock()


class PackageInstaller(object):

    def __init__(self, dir, options):
        self.dir = dir
        self.options = options

    def _generate_command(self, package):
        wait_cmd = " && read"
        cmd = "cd %s/%s" % (self.dir, package)

        # Before doing anything, review Git changes
        if self.options.git:
            cmd += " && git diff --color . " + wait_cmd

        # Make the package
        cmd += " && makepkg -if"

        # Add changes to the Git index
        if self.options.git:
            cmd += " && git add PKGBUILD"

        if self.options.aur or self.options.export_user != "":
            cmd += " && mkaurball -f"

        if self.options.export_user != "":
            cmd += " && burp -u %s -c devel `ls -c1 | grep src.tar.gz | head -n 1`" \
                % self.options.export_user

        return cmd

    def install(self, package):
        if not os.path.isfile("%s/%s/PKGBUILD" % (self.dir, package)):
            return

        lock.acquire()
        print(colored("\n\nInstalling %s " % package, 'red', attrs=['bold']))

        cmd = self._generate_command(package)
        # TODO: adapt to shell=False for security
        p = Popen(cmd, stdin=sys.stdin,
                  stdout=PIPE,
                  stderr=sys.stderr.fileno(),
                  shell=True, bufsize = 1)
        while p.poll() is None:
            out = p.stdout.read(1)
            sys.stdout.write(out)
            sys.stdout.flush()
        lock.release()


def main():
    parser = ArgumentParser(description="Install generated PKGBUILDs.")

    parser.add_argument('distro', default='hydro',
                        help='ROS distribution.')

    parser.add_argument('-a', '--aur', dest='aur', action='store_true',
                        default=False,
                        help='Make AUR tarball.')

    parser.add_argument('-e', '--export', dest='export_user', default='',
                        help="Export package to the AUR. "
                        "A username is expected.")

    parser.add_argument('-g', '--git', dest='git', action='store_true',
                        default=False,
                        help='Add changes of the PKGBUILD to the Git index.')

    args = parser.parse_args()
    distro = args.distro

    makepkg_dump_filename = "/tmp/import_catkin_packages/makepkg_%s.dump" \
                            % distro

    if not os.path.isfile(makepkg_dump_filename):
        print("Could not find dump file: %s" % makepkg_dump_filename)
        sys.exit()

    makepkg_dump = open(makepkg_dump_filename, "r")
    to_install = pickle.load(makepkg_dump)
    makepkg_dump.close()

    installer = PackageInstaller(to_install["directory"], options=args)

    try:
        for package in to_install["packages"]:
            installer.install(package=package)
            to_install["packages"].remove(package)
    except KeyboardInterrupt:
      pass

    makepkg_dump = open(makepkg_dump_filename, "wb")
    pickle.dump(to_install, makepkg_dump)
    makepkg_dump.close()

if __name__ == '__main__':
    main()