| 1 |
#!/usr/bin/python3 |
| 2 |
# -*- mode: python; coding: utf-8 -*- |
| 3 |
# Copyright © 2002 Colin Walters <[email protected]> |
| 4 |
|
| 5 |
import sys, re, os, string, getopt |
| 6 |
|
| 7 |
try: |
| 8 |
opts, args = getopt.getopt(sys.argv[1:], 'd', ['debug']) |
| 9 |
except getopt.GetoptError as e: |
| 10 |
sys.stderr.write("Error reading arguments: %s\n" % e) |
| 11 |
sys.exit(1) |
| 12 |
|
| 13 |
debug = 0 |
| 14 |
|
| 15 |
for (key, val) in opts: |
| 16 |
if key in ('-d', '--debug'): |
| 17 |
debug = 1 |
| 18 |
|
| 19 |
target_arch = args[0] |
| 20 |
|
| 21 |
begin_header = 'BEGIN LIST OF PACKAGES\n' |
| 22 |
end_header = 'END LIST OF PACKAGES\n' |
| 23 |
|
| 24 |
comment_re = re.compile("^\s+") |
| 25 |
depends_re = re.compile('([-a-z0-9+.]+(:[-a-z0-9]+)?)\s*(\\(.+?\\))?\s*(\\[(.+?)\\])?') |
| 26 |
|
| 27 |
line = sys.stdin.readline() |
| 28 |
|
| 29 |
while (not len(line) == 0) and (not line == begin_header): |
| 30 |
line = sys.stdin.readline() |
| 31 |
|
| 32 |
if len(line) == 0: |
| 33 |
sys.stderr.write("Couldn't find start token '%s'\n" % (begin_header,)) |
| 34 |
sys.exit(1) |
| 35 |
|
| 36 |
line = sys.stdin.readline() |
| 37 |
pkgs = [] |
| 38 |
while (not len(line) == 0) and (not line == end_header): |
| 39 |
if line == '\n' or comment_re.search(line): |
| 40 |
line = sys.stdin.readline() |
| 41 |
continue |
| 42 |
orlist = line.strip().split('|') |
| 43 |
orresult = [] |
| 44 |
for pkg in orlist: |
| 45 |
if debug: |
| 46 |
sys.stderr.write("processing pkg '%s'\n" %(pkg,)) |
| 47 |
match = depends_re.search(pkg) |
| 48 |
if match: |
| 49 |
pkgname = match.group(1).strip() |
| 50 |
verdep = match.group(3) |
| 51 |
if not verdep: |
| 52 |
verdep = '' |
| 53 |
verdep = verdep.strip() |
| 54 |
archdeps = match.group(5) |
| 55 |
if debug: |
| 56 |
sys.stderr.write("data: '%s' '%s' '%s'\n" % (pkgname, verdep, archdeps)) |
| 57 |
included = 1 |
| 58 |
if archdeps: |
| 59 |
archdeps = match.group(5).strip().split() |
| 60 |
included = 0 |
| 61 |
for arch in archdeps: |
| 62 |
if debug: |
| 63 |
sys.stderr.write('processing arch %s\n' % (arch,)) |
| 64 |
negated = 0 |
| 65 |
if arch[0] == '!': |
| 66 |
negated = 1 |
| 67 |
arch = arch[1:] |
| 68 |
if target_arch == arch and negated: |
| 69 |
if debug: |
| 70 |
sys.stderr.write('not including ') |
| 71 |
included = 0 |
| 72 |
break |
| 73 |
elif target_arch == arch and not negated: |
| 74 |
if debug: |
| 75 |
sys.stderr.write('including ') |
| 76 |
included = 1 |
| 77 |
elif target_arch != arch and negated: |
| 78 |
if debug: |
| 79 |
sys.stderr.write('including ') |
| 80 |
included = 1 |
| 81 |
if included: |
| 82 |
pkgval = pkgname + ' ' + verdep |
| 83 |
if debug: |
| 84 |
sys.stderr.write('\norresult += ' + pkgval + '\n') |
| 85 |
orresult.append(pkgval) |
| 86 |
if len(orresult) > 0: |
| 87 |
pkgs.append(orresult) |
| 88 |
line = sys.stdin.readline() |
| 89 |
|
| 90 |
if len(line) == 0: |
| 91 |
sys.stderr.write("Couldn't find end token '%s'\n" % (end_header,)) |
| 92 |
sys.exit(1) |
| 93 |
|
| 94 |
sys.stdout.write(', '.join([' | '.join([a.strip() for a in x]) for x in pkgs])) |