Skip to content

Commit

Permalink
Add pkgbuild2json from my old absr repository
Browse files Browse the repository at this point in the history
  • Loading branch information
seblu committed Apr 9, 2013
1 parent 8f7f9ef commit 1c7fb1e
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions pkgbuild2json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/python
# coding: utf-8

# pkgbuild2json - Transform PKGBUILD into JSON
# Copyright © 2012 Sébastien Luttringer
#
# 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:

0 comments on commit 1c7fb1e

Please sign in to comment.