pkgbuild2json 2.19 KB
Newer Older
1 2 3
#!/usr/bin/python
# coding: utf-8

Seblu's avatar
Seblu committed
4
# Copyright © Sébastien Luttringer
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
#
# 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 2
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

'''Transform PKGBUILD into JSON'''

import argparse
import json
import logging
import os
import subprocess
import sys

def parse_argv():
    '''Parse command line arguments'''
    parser = argparse.ArgumentParser()
    parser.add_argument("--env", action="store_true",
                        help="print env into JSON")
    parser.add_argument("PKGBUILD", nargs="?", default="PKGBUILD")
    return parser.parse_args()

def pkgbuild2json(path):
    '''Print shell variable of a PKGBUILD'''
    # check PKGBUILD can be openned
    try:
        open(path, "r")
    except IOError as exp:
        errmsg = "Unable to open %s: %s" % (path, exp)
        logging.error(errmsg)
        exit(1)
    # use bash to export vars.
    # WARNING: CODE IS EXECUTED
    argv = ["bash", "-c",
            "set -a; source '%s'; exec '%s' --env" % (path, sys.argv[0])]
    proc = subprocess.Popen(argv, stdout=subprocess.PIPE, shell=False)
    stdout = proc.communicate("")[0]
    proc.wait()
    jpkgbuild = json.loads(stdout.decode())
    # remove original env variables
    for i in os.environ.keys():
        jpkgbuild.pop(i, "")
    return json.dumps(jpkgbuild)

def main():
    '''Program entry point'''
    # parser cmdline
    args = parse_argv()
    # print env into json
    if args.env:
        print(json.dumps(dict(os.environ.items())))
    else:
        print(pkgbuild2json(args.PKGBUILD))
    return 0

if __name__ == '__main__':
    main()

# vim:set ts=4 sw=4 et ai: