[r62]: / trunk / cforge / cforge / Package / CFORGE / Scripts / checkout-prj.py  Maximize  Restore  History

Download this file

87 lines (74 with data), 2.4 kB

 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
import sys, os
import pysvn
import json

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 = [ 
		["<path to CODESYS project>", "---"]
	]

	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()

args = sys.argv[1].split(" ")
folder = ""
if len(args) == 1:
	folder = args[0].strip("/").split("/")[-1]

# username, password and path are specified at the command line
config = dict()
if len(sys.argv)>= 3:
	config['user'] = sys.argv[2]
	config['pass'] = sys.argv[3]
	config['folder'] = sys.argv[4]
elif hasUI:
	config = ui.Dialog("Checkout Project", Folder=True, Credentials=True, DefaultFolder=folder)

# iterate over all tools of the project, and checkout all SVN repositories,
# excluding the CODESYS folders
if config != None:
	tools = get_json(base_url + '/' + sys.argv[1])
	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(config['user'], config['pass'], url, os.path.join(config['folder'], repo))
			for root, dirs, files in os.walk(config['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(config['folder'], repo + '.commit_count'), "w")
			if f:
				f.write(str(details['commit_count']))
				f.close()