jw-pkg/scripts/projects.py

122 lines
3.5 KiB
Python
Raw Normal View History

#!/usr/bin/python -u
import sys
import argparse
from sets import Set
from os.path import expanduser
import re
proj_root = expanduser("~") + '/local/src/cvs.stable/proj'
def re_section(name):
return re.compile('[' + name + ']'
'.*?'
'(?=[)',
re.DOTALL)
# --------------------------------------------------------------------- helpers
def get_section(path, section):
r = ''
file = open(path)
pat = '[' + section + ']'
in_section = False
for line in file:
if (line.rstrip() == pat):
in_section = True
continue
if in_section:
if len(line) and line[0] == '[':
break
r = r + line
file.close()
return r.rstrip()
def get_value(path, section, key):
r = ()
file = open(path)
if not len(section):
for line in file:
r = re.findall(key + ' *= *(.*)', line)
if (len(r) > 0):
break
else:
in_section = False
pat = '[' + section + ']'
for line in file:
if (line.rstrip() == pat):
in_section = True
continue
if in_section:
if len(line) and line[0] == '[':
break
r = re.findall(key + ' *= *(.*)', line)
if (len(r) > 0):
break
file.close()
if len(r):
return r[0]
return ""
def add_modules_from_project_txt(buf, name, section, key, add_self, recursive):
if name in buf:
return
if add_self:
buf.add(name)
deps = get_value(proj_root + '/' + name + '/doc/share/project.txt', section, key).split()
for dep in deps:
buf.add(dep)
if recursive:
add_modules_from_project_txt(buf, dep, section, key, True, recursive)
def get_modules_from_project_txt(names, section, key, add_self, recursive):
r = Set()
for name in names:
add_modules_from_project_txt(r, name, section, key, add_self, recursive)
return r
# --------------------------------------------------------------------- commands
def cmd_test(args_):
parser = argparse.ArgumentParser(description='Test')
parser.add_argument('blah', default='', help='The blah argument')
args=parser.parse_args(args_)
print "blah = " + args.blah
def cmd_ldlibpath(args_):
parser = argparse.ArgumentParser(description='ldlibpath')
parser.add_argument('module', nargs='*', help='Modules')
args=parser.parse_args(args_)
deps = get_modules_from_project_txt(args.module, 'requires', 'run', True,
True)
r = ''
for m in deps:
r = r + ':' + proj_root + '/' + m + '/lib'
print r[1:]
def cmd_path(args_):
parser = argparse.ArgumentParser(description='path')
parser.add_argument('module', nargs='*', help='Modules')
args=parser.parse_args(args_)
deps = get_modules_from_project_txt(args.module, 'requires', 'run', True,
True)
r = ''
for m in deps:
r = r + ':' + proj_root + '/' + m + '/bin'
print r[1:]
global_args = []
for a in sys.argv[1::]:
global_args.append(a)
if a[0] != '-':
break
# -------------------------------------------------------------------- here we go
parser = argparse.ArgumentParser(description='Project metadata evaluation')
parser.add_argument('cmd', default='', help='Command')
parser.add_argument('arg', nargs='*', help='Command arguments')
args=parser.parse_args(global_args)
cmd = getattr(sys.modules[__name__], 'cmd_' + args.cmd);
cmd(sys.argv[(len(global_args) + 1)::])