#!/usr/bin/env python
#
# You should set the GITTOKEN environment variable
# to an access token for gitlab.cern.ch
#
# This script retrieves the full list of projects
# for a given group and enables the runner given 
# on the command line for each project.
#
import sys
import os
import requests
import json
import string
from pprint import pprint

url = 'https://gitlab.cern.ch/api/v3/'
token = os.environ['GITTOKEN']
runner  = sys.argv[1]
project = len(sys.argv) > 2 and sys.arv[2] or 'atlas-tdaq-software'

headers = { 'PRIVATE-TOKEN': token}

group = requests.get(url + 'groups/%s' % project, headers=headers).json()

for prj in group["projects"]:
    print prj['name'],
    print requests.post(url + 'projects/%d/runners' % prj['id'], headers=headers, params={ 'runner_id': runner})

    
