forked from lbt/useful-scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinkprj.py
93 lines (81 loc) · 3.14 KB
/
linkprj.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/python
try:
from xml.etree import cElementTree as ET
except ImportError:
import cElementTree as ET
from osc import conf
def do_linkprj(self, subcmd, opts, new_project, link_to_project):
"""${cmd_name}: Creates a new project that links to another one
This command creates a new project that is a project link to another one.
${cmd_usage}
${cmd_option_list}
"""
new_prj_link = """\
<project name="%(name)s">
<title>%(name)s</title>
<description>
Project link to %(link_source)s
</description>
<link project="%(link_source)s"/>
<person role="maintainer" userid="%(user)s" />
<person role="bugowner" userid="%(user)s" />
</project>
"""
path = quote_plus(link_to_project),
kind = 'prj'
try:
data = meta_exists(metatype=kind,
path_args=path,
template_args=None,
create_new=False)
except urllib2.HTTPError, e:
if e.code == 404:
print "osc: Cannot link to non-existent project %s" % link_to_project
return False
path = quote_plus(new_project),
kind = 'prj'
try:
data = meta_exists(metatype=kind,
path_args=path,
template_args=None,
create_new=False)
except urllib2.HTTPError, e:
if e.code != 404:
raise
root = ET.fromstring(new_prj_link % { "name" : new_project,
"user" : conf.get_apiurl_usr(conf.config['apiurl']),
"link_source" : link_to_project
})
repos = {}
for repo in get_repos_of_project(conf.config['apiurl'], link_to_project):
if repo.name not in repos:
repos[repo.name] = []
repos[repo.name].append(repo.arch)
if not repos:
print "osc: no repositories found in %s" % link_to_project
return False
comment_element = ET.Comment(text="Repositories to build against project %s" % link_to_project )
root.append(comment_element)
for reponame, archs in repos.iteritems():
if not archs:
print "no archs enabled in %s, skipping" % reponame
continue
new_reponame = "%s_%s_%s" % (link_to_project.replace(":","_"),
reponame,
"_".join(archs))
repo_element = ET.Element('repository', name=new_reponame,
linkedbuild="localdep")
repo_element.append(ET.Element('path', repository=reponame, project=link_to_project))
for arch in archs:
arch_element = ET.Element('arch')
arch_element.text = arch
repo_element.append(arch_element)
root.append(repo_element)
xmlindent(root)
edit_meta(metatype=kind,
path_args=path,
edit=True,
data=ET.tostring(root))
else:
print "osc: project %s already exists" % new_project
return False