Merge pull request #2 from din/abf-console-client:master

proposed new command 'info'
This commit is contained in:
dsilakov 2014-07-16 13:15:55 +04:00
commit edbde7e144
3 changed files with 332 additions and 215 deletions

43
abf.py
View file

@ -285,6 +285,16 @@ def parse_command_line():
parser_search.add_argument('query', action='store', help='a string to search for')
parser_search.set_defaults(func=search)
#list
# info
parser_info = subparsers.add_parser('info', help='get information about single instance')
info_choices = ['platforms', 'repositories', 'projects']
parser_info.add_argument('type', action='store', choices=info_choices, help='type of the instance')
parser_info.add_argument('-f', '--filter', action='store', help='The filter may be specified by defining multiple pairs <type>.<attribute>=<value> or <attribute>=<value>, where <type> is one of the following positional arguments: %s, <attribute> is the one of the instance fields or special attribute (page - using for pagination) and <value> - string, that can take asterisk (*) or anything else... Example: abf info projects -f platforms.name=rosa2012lts page=*' % info_choices, nargs='*')
parser_info.add_argument('-o', '--output', action='store', help='output format ', nargs='*')
parser_info.set_defaults(func=info_single)
# test
parser_test = subparsers.add_parser('test', help='Execute a set of internal datamodel tests')
parser_test.set_defaults(func=test)
@ -294,6 +304,39 @@ def parse_command_line():
command_line = parser.parse_args(sys.argv[1:])
def info_single():
st = command_line.type
cl = {'platforms': Platform, 'repositories': Repository, 'projects': Project}
if not command_line.filter:
log.debug('Filter can be specified with the following parameters:\n %s' % cl[st].filter_dict)
sf = None
else:
for param in command_line.filter:
try:
st, param = map(str, param.split('.'))
except:
pass
attr, value = map(str, param.split('='))
cl[st].filter_dict[attr]=value
log.debug('Filter setup for instance %s ' % st)
st = command_line.type
if not command_line.output:
log.debug('Output format can be specified with the following parameters:\n %s' % cl[st].required_fields)
so = [cl[st].required_fields[1]]
log.debug('Using default query format: %s' % so)
else:
so = command_line.output
res = cl[st].info(models)
info_out = []
for inst in res:
for param in so:
try:
print param + ':\t' + str(inst.params_dict[param])
except:
log.debug("Parameter %s not available:" % param)
def fix_default_config():
if not os.path.exists('/etc/abf/mock-urpm/configs/default.cfg'):
if os.getuid() != 0:

View file

@ -386,3 +386,14 @@ class AbfJson(object):
URL = "/api/v1/search.json"
GET = {'type': search_type, 'query': query, 'per_page': 100}
return self.get_url_contents(URL, GET=GET)
def get_list(self, list_type, page):
URL = "/api/v1/" + list_type +".json"
GET = {'page': page, 'per_page': 100 }
return self.get_url_contents(URL, GET=GET)
def get_projects_single(self, repo_id, page):
URL = "/api/v1/repositories/%d/projects.json" % repo_id
GET = {'page': page, 'per_page': 100 }
return self.get_url_contents(URL, GET=GET)

View file

@ -100,6 +100,7 @@ class Model(object):
class Platform(Model):
required_fields = ['id', 'name', 'description', 'parent_platform_id', 'created_at', 'updated_at', 'released',
'owner', 'visibility', 'platform_type', 'distrib_type', 'repositories']
filter_dict = { 'id': '*', 'name': '*', 'visibility': '*', 'owner': '*', 'platform_type': '*', 'repositories': '*', 'page': '1' }
def get_init_data(self, ID):
ID = str(ID)
@ -166,9 +167,32 @@ class Platform(Model):
platforms_out.append(p)
return platforms_out
@staticmethod
def info(models):
if Platform.filter_dict['page'] == '*':
num = 1
while 1:
res = models.jsn.get_list('platforms', num)
if not res['platforms']:
break
platforms += res['platforms']
num += 1
else:
res = models.jsn.get_list('platforms', Platform.filter_dict['page'])
platforms = res['platforms']
platforms = res['platforms']
platforms_out = []
for platform in platforms:
p = Platform(models, init_data=platform)
platforms_out.append(p)
for value in Platform.filter_dict:
if Platform.filter_dict[value] != '*' and value != 'page':
platforms_out = [i for i in platforms_out if str(Platform.filter_dict[value]) in str(i.params_dict[value]) ]
return platforms_out
class Repository(Model):
required_fields = ['id', 'name', 'created_at', 'updated_at', 'description', 'publish_without_qa', 'platform']
filter_dict = { 'id': '*', 'name': '*', 'page': '1' }
def get_init_data(self, ID):
ID = str(ID)
@ -189,6 +213,19 @@ class Repository(Model):
def __repr__(self):
return '%s/%s' % (self.platform.name, self.name)
@staticmethod
def info(models):
platform_info = Platform.info(models)
repo_info = []
for platform in platform_info:
repos = platform.params_dict['repositories']
for repo in repos:
repo_fin = Repository(models, repo.id)
repo_info.append(repo_fin)
for value in Repository.filter_dict:
if Repository.filter_dict[value] != '*' and value != 'page':
repo_info = [i for i in repo_info if str(Repository.filter_dict[value]) in str(i.params_dict[value]) ]
return repo_info
class Arch(Model):
@ -305,6 +342,8 @@ class Group(Model):
class Project(Model):
required_fields = ['id', 'name', 'created_at', 'updated_at', 'visibility', 'description', 'ancestry', 'has_issues',
'has_wiki', 'default_branch', 'is_package', 'owner', 'repositories', 'owner_type']
filter_dict = { 'id': '*', 'name': '*', 'page': '1' }
def get_init_data(self, proj_id):
log.debug("Reading project " + str(proj_id))
@ -364,6 +403,30 @@ class Project(Model):
def get_refs_list(self, models):
return self.models.jsn.get_git_refs_list(self.id)['refs_list']
@staticmethod
def info(models):
repo_info = Repository.info(models)
projects_info = []
projs = []
for repo in repo_info:
if Project.filter_dict['page'] == '*':
num = 1
while 1:
p = models.jsn.get_projects_single(repo.id, num)
if not p['repository']['projects']:
break
projs += p['repository']['projects']
num += 1
else:
p = models.jsn.get_projects_single(repo.id, Project.filter_dict['page'])
projs = p['repository']['projects']
for proj in projs:
pr = Project(models, init_data=proj)
projects_info.append(pr)
for value in Project.filter_dict:
if Project.filter_dict[value] != '*' and value != 'page':
projects_info = [i for i in projects_info if str(Project.filter_dict[value]) in str(i.params_dict[value]) ]
return projects_info
class BuildList(Model):
required_fields = ['id', 'container_path', 'status', 'status_string', 'package_version', 'project', 'created_at', 'updated_at',