3 # Copyright 2017 Sandvine
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
23 from dateutil
import parser
24 from dateutil
.tz
import tzutc
25 from datetime
import datetime
28 arg_parser
=argparse
.ArgumentParser(description
="Tool to retrieve the latest build from the artifactory server")
29 arg_parser
.add_argument('--branch',default
=None)
30 arg_parser
.add_argument('repo')
31 arg_parser
.add_argument('--url',default
='https://artifactory-osm.etsi.org/)
32 arg_parser.add_argument('--keep
',default=5)
33 arg_parser.add_argument('--password
',default='')
34 arg_parser.add_argument('--debug
',default=None)
36 args = arg_parser.parse_args()
40 url = args.url + 'artifactory
/api
/storage
/' + args.repo + '/' + args.branch
41 delete_url = args.url + 'artifactory
/' + args.repo + '/' + args.branch
43 url = args.url + 'artifactory
/api
/storage
/' + args.repo
44 delete_url = args.url + 'artifactory
/' + args.repo
46 resp = requests.get(url)
47 jsonData = json.loads(resp.content)
49 # first entry is the latest build
50 filtered_list = filter(lambda x: x['uri
'].split('/')[1].isdigit(), jsonData['children
'])
51 folders_sorted = sorted(filtered_list, key=lambda x: int(x['uri
'].split('/')[1]),reverse=True)
53 if len(folders_sorted) < int(args.keep):
54 print("nothing to cleanup")
58 for entry in folders_sorted[int(args.keep):]:
60 print("going to delete {}".format(delete_url+entry['uri
']))
62 requests.delete(delete_url + entry['uri
'], auth=('admin
',args.password))
66 empty_trash_url=args.url + 'artifactory
/ui
/artifactactions
/emptytrash
'
67 headers = {'Content
-Type
': 'application
/json
'}
68 requests.post(empty_trash_url,headers=headers,auth=('admin
',args.password))