1
2
3 """
4 Function actually doing the work of calling the API and handling the
5 output.
6 """
7
8 import ConfigParser
9 import json
10 import os
11
12 import requests
13
14 import copr_exceptions
15
16
18 """ Retrieve the user information from the config file. """
19 config = ConfigParser.ConfigParser()
20 if not config.read(os.path.join(os.path.expanduser('~'), '.config',
21 'copr')):
22 raise copr_exceptions.CoprCliNoConfException(
23 'No configuration file "~/.config/copr" found.')
24 try:
25 username = config.get('copr-cli', 'username', None)
26 token = config.get('copr-cli', 'token', None)
27 except ConfigParser.Error, err:
28 raise copr_exceptions.CoprCliConfigException(
29 'Bad configuration file: %s' % err)
30 return {'username': username, 'token': token}
31
32
34 """ Retrieve the user information from the config file. """
35 config = ConfigParser.ConfigParser(
36 {'copr_url': 'http://copr-fe.cloud.fedoraproject.org'})
37 config.read(os.path.join(os.path.expanduser('~'), '.config',
38 'copr'))
39 copr_url = config.get('copr-cli', 'copr_url')
40 return '%s/api' % copr_url
41
42
44 """ List all the copr of a user. """
45 user = {}
46 if not username:
47 user = get_user()
48 del(user['token'])
49
50 copr_api_url = get_api_url()
51 url = '{0}/owned/'.format(copr_api_url)
52
53 if username:
54 user['username'] = username
55
56 req = requests.get(url, params=user)
57
58 if '<title>Sign in Coprs</title>' in req.text:
59 print 'Invalid API token'
60 return
61
62 output = json.loads(req.text)
63 if req.status_code != 200:
64 print 'Something went wrong:\n {0}'.format(output['error'])
65 return
66
67 output = json.loads(req.text)
68 columns = []
69 values = []
70 if 'repos' in output:
71 if output['repos']:
72 columns = ['name', 'description', 'repos', 'instructions']
73 values = []
74 for entry in output['repos']:
75 values.append([entry[key] for key in columns])
76 else:
77 columns = ['output']
78 values = ['No copr retrieved for user: "{0}"'.format(
79 user['username'])]
80 else:
81 columns = ['output']
82 values = ['Wrong output format returned by the server']
83
84 def _list_to_row(values, widths):
85 ''' Return a print ready version of the provided list '''
86 row = []
87 cnt = 0
88 for item in values:
89 max_width = widths[cnt]
90 cnt += 1
91 if not item:
92 item = ''
93 if cnt < len(values):
94 row.append(item.ljust(max_width + 1))
95 else:
96 row.append(item)
97 return row
98
99 if len(columns) > 1:
100 widths = {}
101 cnt = 0
102 for item in columns:
103 widths[cnt] = len(item)
104 cnt += 1
105 for row in values:
106 cnt = 0
107 for item in row:
108 if not item:
109 item = ''
110 widths[cnt] = max(widths[cnt], len(item))
111 cnt += 1
112
113 headers = '|'.join(_list_to_row(columns, widths))
114 print headers
115 print '-' * len(headers)
116 for row in values:
117 print "|".join(_list_to_row(row, widths))
118
119 else:
120 headers = columns[0]
121 print headers
122 print "-"*len(headers)
123 print values[0]
124
125
126 -def create(name, chroots=[], description=None, instructions=None,
127 repos=None, initial_pkgs=None):
128 """ Create a new copr. """
129 user = get_user()
130 copr_api_url = get_api_url()
131 URL = '{0}/copr/new/'.format(copr_api_url)
132
133 repos = None
134 if type(repos) == list():
135 repos = ' '.join(repos)
136 initial_pkgs = None
137 if type(initial_pkgs) == list():
138 initial_pkgs = ' '.join(initial_pkgs)
139 data = {'name': name,
140 'repos': repos,
141 'initial_pkgs': initial_pkgs,
142 'description': description,
143 'instructions': instructions
144 }
145 for chroot in chroots:
146 data[chroot] = 'y'
147
148 req = requests.post(URL,
149 auth=(user['username'], user['token']),
150 data=data)
151 if '<title>Sign in Coprs</title>' in req.text:
152 print 'Invalid API token'
153 return
154
155 output = json.loads(req.text)
156 if req.status_code != 200:
157 print 'Something went wrong:\n {0}'.format(output['error'])
158 else:
159 print output['message']
160
161
162 -def build(copr, pkgs, memory, timeout):
163 """ Build a new package into a given copr. """
164 user = get_user()
165 copr_api_url = get_api_url()
166 URL = '{0}/coprs/detail/{1}/{2}/new_build/'.format(
167 copr_api_url,
168 user['username'],
169 copr)
170
171 data = {'pkgs': ' '.join(pkgs),
172 'memory': memory,
173 'timeout': timeout
174 }
175
176 req = requests.post(URL,
177 auth=(user['username'], user['token']),
178 data=data)
179
180 if '<title>Sign in Coprs</title>' in req.text:
181 print 'Invalid API token'
182 return
183
184 output = json.loads(req.text)
185 if req.status_code != 200:
186 print 'Something went wrong:\n {0}'.format(output['error'])
187 else:
188 print output['message']
189