--- a +++ b/branches/13-jobs/cforge/cforge/Package/CFORGE/Scripts/job.py @@ -0,0 +1,91 @@ +import sys, os +import pysvn +import json +import build +import commit + +print("test") + +hasWebClient = True +try: + from System.Net import WebClient +except: + import requests + hasWebClient = False + +hasUI = True +try: + import ui +except: + hasUI = False + +base_url = "https://forge.codesys.com/rest" +base_svn = "https://forge.codesys.com/svn/" + +# This is a cforge command (script file) +# this will be run as ironpython script. +# the filename of this script is automatically the corresponding cforge command +# with some magic functions you can easily integrate it nicely into cforge tool + +# cforge_usage: +# here you can return an array of all possible arguments and options that can +# be passed to this command script +def cforge_usage(): + help = [ + ["json job string", "---"] + ] + + return help + +def get_json(url): + r = "" + if hasWebClient: + web_client = WebClient() + r = web_client.DownloadData(url) + else: + r = requests.get(url).content + + j = json.loads(bytes(r).decode('utf-8')) + return j + +if len(sys.argv) <= 1: + print("Oh, there are no arguments. Perhaps you forgot something?") + sys.exit() + +# Job is passed as first argument +job = json.loads(sys.argv[1]) +print("job...") +print(job) +sys.exit() + +job["folder"] = '\tmp' + +# iterate over all tools of the project, and checkout all SVN repositories, +# excluding the CODESYS folders +if job != None: + tools = get_json(base_url + '/' + job["project"]) + for tool in tools['tools']: + if tool['name'] == 'svn': + repo = tool['url'][1:-1].replace('/', ',') + url = base_svn + repo + print(url) + # checkout + pysvn.svn_checkout_all(job['user'], job['pass'], url, os.path.join(job['folder'], repo)) + for root, dirs, files in os.walk(job['folder']): + for file in files: + if file == "meta.profile": + pysvn.svn_remove_local(root) + print('\n') + # store commit count + details = get_json(base_url + tool['url']) + f = open(os.path.join(job['folder'], repo + '.commit_count'), "w") + if f: + f.write(str(details['commit_count'])) + f.close() + +# Exeucte all actions, passed to the job +for action in job['actions']: + if action['cmd'] == 'build': + build.do(os.path.join(job['folder'], action['repo'])) + if action['cmd'] == 'commit': + commit.do(os.path.join(job['folder'], action['repo']), job["user"], job["pass"], action["msg"])