1
0
mirror of https://github.com/openSUSE/osc.git synced 2024-11-10 06:46:15 +01:00

- new meta command, replacing editmeta, editprj, createprj, editpac, createpac,

edituser. TODO: 
    - write usage info
    - implement -c|--create
    - implement -F|--file
    - check whether I broke usage of metadata copying/creation from withing
      linkpac or copypac
    - re-implement createpac and friends as aliasses to the new meta command -- I
      just commented them out for now
- fix metadata change detection, which no longer relies on the timestamp of the
  temporary file
- add close() in dgst(), which seemed to miss (I can't see a reason why it
  should not be there0
This commit is contained in:
Dr. Peter Poeml 2007-07-13 16:01:02 +00:00
parent 0e6dcd9a4a
commit 5a4f1e4d42
2 changed files with 202 additions and 180 deletions

View File

@ -132,102 +132,163 @@ class Osc(cmdln.Cmdln):
print '\n'.join(l)
@cmdln.option('-e', '--edit', action='store_true',
help='edit metadata')
@cmdln.option('-F', '--file', metavar='FILE',
help='File to read metadata from. \'-\' denotes standard input. '
'Per default, the program specified by the environmental variable EDITOR '
'is run on a temporary file.')
def do_meta(self, subcmd, opts, *args):
"""${cmd_name}: Shows meta information
"""${cmd_name}: Show meta information, or edit it
meta: Shows build service metadata of type <prj|pkg|prjconf|user>.
examples: osc meta Apache # show meta of project 'Apache'
osc meta Apache subversion # show meta of package 'subversion'
${cmd_usage}
${cmd_option_list}
"""
if not args:
print >>sys.stderr, 'Missing argument.'
self.do_help(['foo', 'meta'])
return 2
if len(args) == 2:
project = args[0]
package = args[1]
print ''.join(show_package_meta(conf.config['apiurl'], project, package))
print ''.join(show_files_meta(conf.config['apiurl'], project, package))
elif len(args) == 1:
project = args[0]
print ''.join(show_project_meta(conf.config['apiurl'], project))
@cmdln.alias("createpac")
def do_editpac(self, subcmd, opts, project, package):
"""${cmd_name}: Create package or edit package metadata
If the named package does not exist, it will be created.
${cmd_usage}
${cmd_option_list}
"""
edit_meta(project, package)
@cmdln.alias('createprj')
def do_editprj(self, subcmd, opts, project):
"""${cmd_name}: Create project or edit project metadata
If the named project does not exist, it will be created.
${cmd_usage}
${cmd_option_list}
"""
edit_meta(project, None)
def do_editmeta(self, subcmd, opts, *args):
"""${cmd_name}: Edit project/package meta information
If the named project or package does not exist, it will be created.
Examples:
osc editmeta Apache # edit meta of project 'Apache'
osc editmeta Apache apache2 # edit meta of package 'apache2'
${cmd_usage}
usage:
osc meta <prj|pkg|prjconf|user> [-e|--edit [-f|--file] [-c|--create]] ARGS...
${cmd_option_list}
"""
args = slash_split(args)
if not args:
print >>sys.stderr, 'Missing argument.'
self.do_help([None, 'editmeta'])
if not args or args[0] not in metatypes.keys():
print >>sys.stderr, 'Unknown meta type. Choose one of %s.' % ', '.join(metatypes)
return 2
if len(args) == 2:
project = args[0]
package = args[1]
edit_meta(project, package)
cmd = args[0]
del args[0]
elif len(args) == 1:
project = args[0]
edit_meta(project, None)
def do_edituser(self, subcmd, opts, *args):
"""${cmd_name}: Edit user meta information
If the named user id does not exist, it will be created.
${cmd_usage}
${cmd_option_list}
"""
if not args or len(args) != 1:
user = conf.config['user']
if cmd == 'pkg':
required_args = 2
else:
required_args = 1
if len(args) < required_args:
print >>sys.stderr, 'Too few arguments.'
return 2
if len(args) > required_args:
print >>sys.stderr, 'Too many arguments.'
return 2
# prj
if cmd == 'prj':
project = args[0]
if not opts.edit:
sys.stdout.write(''.join(show_project_meta(conf.config['apiurl'], project)))
else:
print 'XXX editing prj'
edit_meta(metatype='prj',
path_args = quote_plus(project),
template_args = (project, conf.config['user']))
# pkg
elif cmd == 'pkg':
project, package = args[0:2]
if not opts.edit:
sys.stdout.write(''.join(show_package_meta(conf.config['apiurl'], project, package)))
else:
print 'XXX editing pac'
edit_meta(metatype='pkg',
path_args = (quote_plus(project), quote_plus(package)),
template_args = (package, conf.config['user']))
# prjconf
elif cmd == 'prjconf':
project = args[0]
if not opts.edit:
sys.stdout.write(''.join(show_project_conf(conf.config['apiurl'], project)))
else:
print 'XXX editing prj'
edit_meta(metatype='prjconf',
path_args = quote_plus(project),
template_args = None)
# user
elif cmd == 'user':
user = args[0]
edit_user_meta(user)
if not opts.edit:
r = get_user_meta(conf.config['apiurl'], user)
if r:
sys.stdout.write(''.join(r))
else:
print 'XXX editing user'
edit_meta(metatype='user',
path_args = (quote_plus(user)),
template_args = (user, user))
# @cmdln.alias("createpac")
# def do_editpac(self, subcmd, opts, project, package):
# """${cmd_name}: Create package or edit package metadata
#
# If the named package does not exist, it will be created.
#
# ${cmd_usage}
# ${cmd_option_list}
# """
#
# edit_meta(project, package)
# @cmdln.alias('createprj')
# def do_editprj(self, subcmd, opts, project):
# """${cmd_name}: Create project or edit project metadata
#
# If the named project does not exist, it will be created.
#
# ${cmd_usage}
# ${cmd_option_list}
# """
#
# edit_meta(project, None)
# def do_editmeta(self, subcmd, opts, *args):
# """${cmd_name}: Edit project/package meta information
#
# If the named project or package does not exist, it will be created.
#
# Examples:
# osc editmeta Apache # edit meta of project 'Apache'
# osc editmeta Apache apache2 # edit meta of package 'apache2'
#
# ${cmd_usage}
# ${cmd_option_list}
# """
#
# args = slash_split(args)
#
# if not args:
# print >>sys.stderr, 'Missing argument.'
# self.do_help([None, 'editmeta'])
# return 2
#
# if len(args) == 2:
# project = args[0]
# package = args[1]
# edit_meta(project, package)
#
# elif len(args) == 1:
# project = args[0]
# edit_meta(project, None)
# def do_edituser(self, subcmd, opts, *args):
# """${cmd_name}: Edit user meta information
#
# If the named user id does not exist, it will be created.
#
# ${cmd_usage}
# ${cmd_option_list}
# """
#
# if not args or len(args) != 1:
# user = conf.config['user']
# else:
# user = args[0]
# edit_user_meta(user)
def do_linkpac(self, subcmd, opts, *args):
@ -959,18 +1020,18 @@ class Osc(cmdln.Cmdln):
p.clear_from_conflictlist(filename)
def do_usermeta(self, subcmd, opts, name):
"""${cmd_name}: Shows user metadata
Shows metadata about the buildservice user with the id NAME.
${cmd_usage}
${cmd_option_list}
"""
r = get_user_meta(conf.config['apiurl'], name)
if r:
print ''.join(r)
# def do_usermeta(self, subcmd, opts, name):
# """${cmd_name}: Shows user metadata
#
# Shows metadata about the buildservice user with the id NAME.
#
# ${cmd_usage}
# ${cmd_option_list}
# """
#
# r = get_user_meta(conf.config['apiurl'], name)
# if r:
# print ''.join(r)
def do_platforms(self, subcmd, opts, *args):

View File

@ -116,11 +116,11 @@ HERE
new_user_template = """\
<person>
<login>%(user)s</login>
<login>%s</login>
<email>PUT_EMAIL_ADDRESS_HERE</email>
<realname>PUT_REAL_NAME_HERE</realname>
<watchlist>
<project name="home:%(user)s"/>
<project name="home:%s"/>
</watchlist>
</person>
"""
@ -552,7 +552,7 @@ rev: %s
self.descr = descr
def update_pac_meta(self, template=new_package_templ):
def update_pac_meta(self, template=new_package_templ): # n.b.: not the same as Package.update_pacmeta
import tempfile
(fd, filename) = tempfile.mkstemp(prefix = 'osc_editmeta.', suffix = '.xml', dir = '/tmp')
@ -903,51 +903,37 @@ def show_package_meta(apiurl, prj, pac):
class metafile:
"""metafile that can be manipulated and is stored back after manipulation."""
def __init__(self, prj, pac, template=new_package_templ, change_is_required=True):
def __init__(self, metatype, path, template, change_is_required=True):
import tempfile
self.change_is_required = change_is_required
(fd, self.filename) = tempfile.mkstemp(prefix = 'osc_editmeta.', suffix = '.xml', dir = '/tmp')
if pac:
# package meta
self.url = makeurl(conf.config['apiurl'], ['source', prj, pac, '_meta'])
try:
m = http_GET(self.url).readlines()
except urllib2.HTTPError, e:
if e.code == 404:
m = template % (pac, conf.config['user'])
else:
print >>sys.stderr, 'error getting package meta for project \'%s\' package \'%s\':' % (prj, pac)
print >>sys.stderr, e
sys.exit(1)
else:
# project meta
self.url = makeurl(conf.config['apiurl'], ['source', prj, '_meta'])
try:
m = http_GET(self.url).readlines()
# when testing this offline:
#except urllib2.URLError, e:
# m = new_project_templ % (prj, conf.config['user'])
except urllib2.HTTPError, e:
if e.code == 404:
m = new_project_templ % (prj, conf.config['user'])
else:
print >>sys.stderr, 'error getting package meta for project \'%s\':' % prj
print >>sys.stderr, e
sys.exit(1)
self.url = makeurl(conf.config['apiurl'], [path])
try:
m = http_GET(self.url).readlines()
# when testing this offline: <-- what did I mean with that??
#except urllib2.URLError, e:
# m = template
except urllib2.HTTPError, e:
if e.code == 404:
m = template
else:
print >>sys.stderr, 'error getting metadata for type \'%s\', path \'%s\':' \
% (metatype, self.url)
print >>sys.stderr, e
sys.exit(1)
f = os.fdopen(fd, 'w')
f.write(''.join(m))
f.close()
self.timestamp = os.path.getmtime(self.filename)
self.hash_orig = dgst(self.filename)
def sync(self):
if self.change_is_required == True and os.path.getmtime(self.filename) == self.timestamp:
hash = dgst(self.filename)
if self.change_is_required == True and hash == self.hash_orig:
print 'File unchanged. Not saving.'
os.unlink(self.filename)
return True
@ -972,22 +958,31 @@ class metafile:
print >> sys.stderr, 'cannot save meta data - an unexpected error occured'
return False
#metatypes = { 'prj': { 'url': ['source', prj, '_meta'],
# 'template': new_project_templ,
# },
# 'pkg': { 'url' : ['source', prj, pac, '_meta'],
# 'template': new_package_templ,
# },
# 'prjconf': { 'url': ['source', prj, '_config'],
# 'template': None,
# },
# 'user': { 'url': ['person', quote_plus(user)],
# 'template': new_user_template,
# },
# }
def edit_meta(prj, pac, template=new_package_templ, change_is_required=True):
f=metafile(prj, pac, template, change_is_required)
# different types of metadata
metatypes = { 'prj': { 'path': 'source/%s/_meta',
'template': new_project_templ,
},
'pkg': { 'path' : 'source/%s/%s/_meta',
'template': new_package_templ,
},
'prjconf': { 'path': 'source/%s/_config',
'template': '',
},
'user': { 'path': 'person/%s',
'template': new_user_template,
},
}
def edit_meta(metatype, path_args, template_args, change_is_required=True):
path = metatypes[metatype]['path']
template = metatypes[metatype]['template']
if path_args:
path = path % path_args
if template_args:
template = template % template_args
f=metafile(metatype, path, template, change_is_required)
editor = os.getenv('EDITOR', default='vim')
while 1:
@ -1004,41 +999,6 @@ def edit_meta(prj, pac, template=new_package_templ, change_is_required=True):
break
def edit_user_meta(user, change_is_required=True):
import tempfile
u = makeurl(conf.config['apiurl'], ['person', quote_plus(user)])
try:
m = http_GET(u).readlines()
except urllib2.HTTPError, e:
if e.code == 404:
m = new_user_template % { 'user': user }
else:
print >>sys.stderr, 'error getting metadata for user \'%s\':' % user
print >>sys.stderr, e
sys.exit(1)
(fd, filename) = tempfile.mkstemp(prefix = 'osc_edituser.', suffix = '.xml', dir = '/tmp')
f = os.fdopen(fd, 'w')
f.write(''.join(m))
f.close()
timestamp = os.path.getmtime(filename)
editor = os.getenv('EDITOR', default='vim')
os.system('%s %s' % (editor, filename))
if change_is_required == True and os.path.getmtime(filename) == timestamp:
print 'File unchanged. Not saving.'
os.unlink(filename)
else:
print 'Sending meta data...',
http_PUT(u, file=filename)
os.unlink(filename)
print 'Done.'
def show_files_meta(apiurl, prj, pac, revision=None):
query = []
if revision:
@ -1133,6 +1093,7 @@ def dgst(file):
if not buf: break
s.update(buf)
return s.hexdigest()
f.close()
def binary(s):