-
Notifications
You must be signed in to change notification settings - Fork 92
/
site_import.py
132 lines (108 loc) · 3.92 KB
/
site_import.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
##############################################################
# written by Markus Nikulski
# 08 Mar 2022
##############################################################
import XMC_NBI
##############################################################
xmcServerIp = '192.168.162.50'
xmcClientID = 'DVDnjOaqMQ'
xmcSecret = '8c12a0e1-87f8-4b18-a8d1-2e8c74d27c65'
file = 'site.txt'
addAllow = True
deleteAllow = True
session = None
##############################################################
def login():
global session
session = XMC_NBI.XMC_NBI(xmcServerIp, xmcClientID, xmcSecret)
if session.error:
print( "ERROR: '%s'" % session.message )
exit(1)
##############################################################
def syncSitesToXmc( sites ):
changes = False
pending = False
xmcSites = session.getSites()
if session.error:
print( "ERROR: get sites failed '%s'" % session.message )
exit(1)
else:
for site in sorted(sites):
if site in xmcSites:
continue
else:
if addAllow:
session.addSite( site )
if session.error:
print( "ERROR: create site '%s' failed: '%s'" % (site,session.message) )
else:
print("INFO: create site '%s'" % site)
changes = True
else:
print("INFO: site '%s' have to be added" % site)
pending = True
for site in list( reversed(xmcSites) ):
if site in sites:
continue
else:
if deleteAllow:
if not site == "/World":
session.deleteSite( site )
if session.error:
print( "ERROR: delete site '%s' failed: '%s'" % (site,session.message) )
else:
print("INFO: delete site '%s'" % site)
changes = True
else:
print("INFO: site '%s' have to be deleted" % site)
pending = True
if pending:
print("WARN: not all in sync")
elif not changes:
print("INFO: all in sync")
##############################################################
def readFromFile(file):
data = []
try:
with open(file, 'r') as FILE_HANDLE:
content = FILE_HANDLE.readlines()
FILE_HANDLE.close()
print("INFO: read file '%s'" % file)
except IOError:
print("ERROR: read file '%s' failed" % file)
exit(1)
for line in (line.rstrip('\n') for line in content):
data.append( line )
#print("INFO: found %s sites in file" % len(sites) )
#for site in sorted( sites ):
# print( "\t%s" % site )
return data
##############################################################
def normalizeData(sites):
siteList = []
for site in sites:
if site not in siteList:
siteList.append( site )
else:
print("WRANING: site '%s' exist more than one time in the listing" % site)
itemCounts = [2,3,4,5,6,7,8,9]
siteToAdd = []
for site in siteList:
newSite = "/World"
items = site.split('/')
for count in itemCounts:
if len( items ) > count:
newSite = newSite + '/' + items[count]
if not newSite in siteList and not newSite in siteToAdd:
siteToAdd.append( newSite )
for site in siteToAdd:
siteList.append( site )
print("INFO: for data normalisation add site '%s' to the list" % site)
return siteList
##############################################################
sites = readFromFile( file )
sites = normalizeData( sites )
login()
syncSitesToXmc( sites )