-
Notifications
You must be signed in to change notification settings - Fork 1
/
ASTools.py
1643 lines (1390 loc) · 65 KB
/
ASTools.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
* File: ASTools.py
* Copyright (c) 2023 Loupe
* https://loupe.team
*
* This file is part of ASPython, licensed under the MIT License.
'''
'''
AS Tools
This package contains all functions necessary to perform actions on
AS projects outside of Automation Studio.
'''
import fnmatch
import os.path
import json
import pathlib
import shutil
import subprocess
from typing import Dict, Tuple, Sequence, Union, List, Optional
import xml.etree.ElementTree as ET
import logging
import sys
import re
import ctypes
import configparser
# TODO: Support finding as default build paths
# TODO: Build project wrapper
# TODO: Move a lot of functionality into classes
# TODO: Add ability to manage package files
# TODO: Switch to lxml
# TODO: Support SGC
# TODO: Support partial library exports, for example if SG4 is successful but SG4-arm fails
# This will require returning additional information or cleaning up partition exports ourselves
# TODO: Support ARSim
ASReturnCodes = {
"Errors-Warnings": 3,
"Errors": 2,
"Warnings": 1,
"None": 0
}
PVIReturnCodeText = {
0: 'Application completed successfully',
28320: 'File not found (.PIL file or "call" command)',
28321: 'Filename not specified (command line parameter)',
28322: 'Unable to load BRErrorLB.DLL ("ReadErrorLogBook" command)',
28323: 'DLL entry point not found ("ReadErrorLogBook" command)',
28324: 'BR module not found ("Download" command)',
28325: 'Syntax error in command line',
28326: 'Unable to start PVI Manager ("StartPVIMan" command)',
28327: 'Unknown command',
28328: 'Unable to connect ("Connection" command with "C" parameter)',
28329: 'Unable to establish connection in bootstrap loader mode',
28330: 'Error transferring operating system in bootstrap loader mode',
28331: 'Process aborted',
28332: 'The specified directory doesn\'t exist',
28333: 'No directory specified',
28334: 'The application used to create an AR update file wasn\'t found ("ARUpdateFileGenerate" command)',
28335: 'The specified AR base file (*.s*) is invalid ("ARUpdateFileGenerate" command)',
28336: 'Error creating the AR update file ("ARUpdateFileGenerate" command)',
28337: 'There is no valid connection to the PLC. In order to be able to read the CAN baud rate, the CAN ID or the CAN node number, you need a connection to the PLC',
28338: 'The specified logger module doesn\'t exist on PLC ("Logger" command)',
28339: 'The specified .br file is not a valid logger module ("Logger" command)',
28340: 'The .pil file does not contain any information about the AR version to be installed.',
28341: 'Transfer to the corresponding target system is not possible since the AR version on the target system does not yet support the transfer mode'
}
def getASPath(version:str) -> str:
base = "C:\\BrAutomation"
if version.lower() == 'base':
return base
else:
return os.path.join(base, version.upper(), 'Bin-en')
def getASBuildPath(version:str) -> str:
if version.lower() == 'base':
return getASPath('base')
else:
return os.path.join(getASPath(version), "BR.AS.Build.exe")
def getPVITransferPath(version:str) -> str:
base = getASPath('base')
return os.path.join(base, 'PVI', version, 'PVI', 'Tools', 'PVITransfer')
def ASProjetGetConfigs(project: str) -> [str]:
if(os.path.isfile(project)):
project = os.path.split(project)[0]
project = os.path.join(project, 'Physical')
configs = [d for d in os.listdir(project) if os.path.isdir(os.path.join(project, d))]
return configs
def batchBuildAsProject(project, ASPath:str, configurations=None, buildMode='Build', buildRUCPackage=True, tempPath='', logPath='', binaryPath='', simulation=False, additionalArg:Union[str,list,tuple]=None) -> subprocess.CompletedProcess:
if configurations is None: configurations = []
for config in configurations:
completedProcess = buildASProject(project, ASPath, configuration=config, buildMode=buildMode, buildRUCPackage=buildRUCPackage, tempPath=tempPath, logPath=logPath, binaryPath=binaryPath, simulation=simulation, additionalArg=additionalArg)
if completedProcess.returncode > ASReturnCodes["Warnings"]:
# Call out the end of a failed build
logging.info(f'Build for configuration {config} has completed with errors, see DEBUG logging for details')
return completedProcess
else:
# Call out the end of a successful build
logging.info(f'Build for configuration {config} has completed without errors, see DEBUG logging for details')
return completedProcess
def buildASProject(project, ASPath:str, configuration='', buildMode='Build', buildRUCPackage=True, tempPath='', binaryPath='', logPath='', simulation=False, additionalArg:Union[str,list,tuple]=None) -> subprocess.CompletedProcess:
commandLine = []
commandLine.append(ASPath)
commandLine.append('"' + os.path.abspath(project) + '"')
if configuration:
commandLine.append('-c')
commandLine.append(configuration)
# Possible valid values: Build, Rebuild, BuildAndTransfer, BuildAndCreateCompactFlash
if buildMode:
commandLine.append('-buildMode')
commandLine.append(buildMode) # Documentation says this needs " around value but so far testing proves not
if(buildMode.capitalize() == 'Rebuild'):
commandLine.append('-all')
if tempPath:
commandLine.append('-t')
commandLine.append(tempPath)
if binaryPath:
commandLine.append('-o')
commandLine.append(binaryPath)
if simulation:
commandLine.append('-simulation')
if buildRUCPackage:
commandLine.append('-buildRUCPackage')
if additionalArg:
if type(additionalArg) is str:
commandLine.append(additionalArg)
elif type(additionalArg) is list or type(additionalArg) is tuple:
commandLine.extend(additionalArg)
# Call out the beginning of the build
logging.info(f'Starting build for configuration {configuration}...')
# Execute the process, and retrieve the process object for further processing.
logging.debug(commandLine)
process = subprocess.Popen(commandLine, stdout=subprocess.PIPE, encoding="utf-8", errors='replace')
logging.info("Recording build log here: " + os.path.join(logPath, "build.log"))
with open(os.path.join(logPath, "build.log"), "w", encoding='utf-8') as f:
# TODO: find out if Jenkins is calling the script, and if not then don't augment the console message
while process.returncode == None:
raw = process.stdout.readline()
data = raw.rstrip()
f.write(raw)
if data != "":
# Search for the "warning" pattern.
warningMatch = re.search('warning [0-9]*:', data)
errorMatch = re.search('error [0-9]*:', data)
if (warningMatch != None):
logging.warning("\033[32m" + data +"\033[0m")
elif (errorMatch != None):
logging.error("\033[31m" + data +"\033[0m")
else:
logging.debug(data)
process.poll()
return process
def CreateARSimStructure(RUCPackage:str, destination:str, version:str, startSim:bool=False):
logging.info(f'Creating ARSim structure at {destination}')
RUCPath = os.path.dirname(RUCPackage)
RUCPil = os.path.join(RUCPath, 'CreateARSim.pil')
with open(RUCPil, 'w+') as f:
f.write(f'CreateARsimStructure "{RUCPackage}", "{destination}", "Start={int(startSim)}"\n')
# If ARsim is being started, add a line that waits for a connection to be established.
if startSim:
f.write('Connection "/IF=TCPIP /SA=1", "/DA=2 /DAIP=127.0.0.1 /REPO=11160", "WT=120"')
arguments = []
print('PVI version: ' + version)
arguments.append(os.path.join(getPVITransferPath(version), 'PVITransfer.exe'))
# arguments.append('-automatic') # startSim only works with automatic mode
arguments.append('-silent')
# arguments.append('-autoclose')
arguments.append(RUCPil)
logging.debug(arguments)
process = subprocess.run(arguments)
logging.debug(process)
if(process.returncode == 0):
logging.debug('ARSim created')
if startSim:
# This because silent and autoclose mode do not support starting arsim
pid = subprocess.Popen(os.path.join(destination, 'ar000loader.exe'), stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, creationflags=0x00000008)
else:
logging.debug(f'Error in creating ARSimStructure code {process.returncode}: {PVIReturnCodeText[process.returncode]}')
return process
class LibExportInfo(object):
def __init__(self, name, path, exception=None, lib=None):
self.name = name
self.path = path
self.lib = lib
self.exception = exception
super().__init__()
class ProjectExportInfo(object):
def __init__(self):
self._success = []
self._failed = []
super().__init__()
def addLibInfo(self, libInfo:LibExportInfo):
if libInfo.exception is None:
self._success.append(libInfo)
else:
self._failed.append(libInfo)
def extend(self, *exportInfo):
for info in exportInfo:
self._success.extend(info._success)
self._failed.extend(info._failed)
@property
def success(self) -> List[LibExportInfo]:
return self._success
@property
def failed(self) -> List[LibExportInfo]:
return self._failed
class Dependency:
def __init__(self, name:str, minVersion='', maxVersion=''):
self.name = name
self.minVersion = minVersion
self.maxVersion = maxVersion
class BuildConfig:
def __init__(self, name, path='', typ='sg4', hardware=''):
self.name = name
self.type = typ
self.hardware = hardware
self.path = path
class xmlAsFile:
def __init__(self, path: str, new_data:ET.ElementTree=None):
self.path = path
if (new_data == None):
self.read()
else:
# In this case we create new content based on type.
self._package = new_data
self.package.write(self.path, xml_declaration=True, encoding='utf-8', method='xml')
def read(self):
'''Reads AS xml file into xml tree'''
if not os.path.exists(self.path): raise FileNotFoundError(self.path)
self._package = ET.parse(self.path)
return self
def write(self):
'''Writes xml tree to file with AS Namespace'''
# TODO: This loses the <?AutomationStudio Version=4.4.6.71 SP?>. This shouldn't cause any issues though
# This can be solved by extracting xml stuff with file writing (function that returns xml as string) then modify and write that
ns = self._getASNamespace(self.package)
ET.register_namespace('', ns) # TODO: This is a ET global effect
self._indentXml(self.package.getroot()) # When we add items indent gets messed up
self.package.write(self.path, xml_declaration=True, encoding='utf-8', method='xml')
return self
def find(self, *levels) -> ET.Element:
path = '.'
for level in levels:
path += '/' + self.nameSpaceFormatted + level
return self.root.find(path)
def findall(self, *levels) -> List[ET.Element]:
path = '.'
for level in levels:
path += '/' + self.nameSpaceFormatted + level
return self.root.findall(path)
@property
def nameSpaceFormatted(self) -> str:
ns = self.nameSpace
if ns != '':
ns = '{' + ns + '}'
return ns
@property
def nameSpace(self) -> str:
return self._getASNamespace(self.package)
@property
def root(self) -> ET.Element:
return self.package.getroot()
@property
def package(self) -> ET.ElementTree:
return self._package
@property
def dirPath(self) -> str:
return os.path.dirname(self.path)
@property
def getXmlType(self) -> str:
'''Returns a string representation of xml type
Note: This is for debug and view purposes only at this point, API may change
'''
# TODO: Populates this list
# Package
# Library
# Program
# Hardware - Not Supported
ns = self._getASNamespace(self.package)
return ns.split('/')[-1]
@staticmethod
def _indentXml(elem: ET.Element, level=0) -> None:
'''Indent Element and sub elements'''
i = "\n" + level*" "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
xmlAsFile._indentXml(elem, level+1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
@staticmethod
def _getASNamespace(package: ET.ElementTree) -> str:
'''Get Automation Studio's namespace for xml files'''
ns = package.getroot().tag.split('}')
if ns[0][0] == '{' :
ns = ns[0][1:]
else:
ns = ''
return ns
# Examples: 'http://br-automation.co.at/AS/Package', 'http://br-automation.co.at/AS/Physical'
@staticmethod
def _getASNamespaceFormatted(package: ET.ElementTree) -> str:
'''Get Automation Studio's namespace for xml files formatted for ElementTree'''
ns = xmlAsFile._getASNamespace(package)
if ns != '':
ns = '{' + ns + '}'
return ns
class Library(xmlAsFile):
'''
TODO: Lib files appears to support <Files> or <Objects>
AS will change from Files to Objects when a sub folder is added
Using <Objects> when AS prefers <Files> is fine
Using <Files> when AS prefers <Objects> is also fine
If changed to a non preferred method, AS will change back everytime it edits the pkg file
'''
def __init__(self, path):
if(os.path.isdir(path)):
path = os.path.join(path, getLibraryType(path) + '.lby')
self.name = os.path.basename(os.path.dirname(path)) # Lib name is same as folder name
self._dependencies = []
super().__init__(path)
self._xmlTag = self._getXmlTag(self.package)
self._xmlTagChild = self._xmlTag[:-1] # We just want to remove the 's'
@property
def files(self) -> ET.Element:
return self.find(self._xmlTag)
@property
def fileList(self):
return self.findall(self._xmlTag, self._xmlTagChild)
@property
def dependencyList(self):
return self.findall('Dependencies', 'Dependency')
@property
def dependencies(self) -> List[Dependency]:
self._dependencies.clear()
for element in self.dependencyList:
self._dependencies.append(Dependency(element.get('ObjectName', 'Unknown'), element.get('FromVersion', ''), element.get('ToVersion', '')))
return self._dependencies
@property
def dependencyNames(self) -> List[str]:
names = []
for dep in self.dependencies:
names.append(dep.name)
return names
@property
def version(self) -> str:
return self.root.get("Version", '0')
@property
def description(self) -> str:
return self.root.get("Description", '')
@property
def type(self):
return getLibraryType(self.dirPath)
def addObject(self, *paths):
'''TODO: This should support packages'''
for path in paths:
if not os.path.isfile(path) and not os.path.isdir: raise FileNotFoundError(path)
name = os.path.split(path)[1]
newPath = os.path.join(self.path, name)
shutil.copyfile(path, newPath)
self._addObjectElement(newPath)
self.write()
def _addObjectElement(self, path):
element = self._createPkgElement(path, self._xmlTagChild)
self.files.append(element)
if(element.get('Type') == 'Package' and self._xmlTag != 'Objects'):
self._convertXmlTag(self._xmlTag, 'Objects')
def addDependency(self, *dependency):
for dependent in dependency:
if dependent is not Dependency: raise TypeError('Expected Dependency class got', type(dependent))
# TODO: Check if dependency exist if so update instead
self.dependencies.append(self._createDependencyElement(dependent))
def export(self, dest, buildFolder, buildConfigs, overwrite=False, binary=True, includeVersion=False) -> LibExportInfo:
path = os.path.join(dest, self.name)
if(includeVersion):
path = os.path.join(path, 'V%s' % self.version)
info = LibExportInfo(self.name, path, None, self)
try:
if overwrite and os.path.exists(path):
logging.debug('Export already exists, removing %s', path)
shutil.rmtree(path, onerror=self._rmtreeOnError)
# pathlib.Path(path).mkdir(parents=True, exist_ok=True) # Create directory if it does not exist
if binary:
self._collectBinaryLibrary(buildFolder, path, buildConfigs)
else:
self._collectSourceLibrary(self.dirPath, path)
except (FileNotFoundError, FileExistsError) as error:
logging.debug(error)
info.exception = error
return info
def synchronize(self):
objects = self.files
# Read Dir
items = [i for i in os.listdir(self.dirPath)]
usedItems = []
toRemove = []
# Update XML
for obj in objects:
if obj.text not in items:
# print('Removing:', element.text)
# Removing here will cause issues with loop
toRemove.append(obj)
else:
usedItems.append(obj.text)
for obj in toRemove:
objects.remove(obj)
for item in items:
if item not in usedItems:
if os.path.splitext(item)[1] != '.lby':
if item not in ('SG4', 'SG3', 'SGC'): # We don't want to add library file to files
self._addObjectElement(os.path.join(self.dirPath, item))
# Save
self.write()
def _convertXmlTag(self, fromTag: str, toTag: str):
childTag = toTag[:-1]
for elem in self.findall(fromTag):
# print(elem)
elem.tag = self.nameSpaceFormatted + toTag
for child in elem:
# print(child)
child.tag = self.nameSpaceFormatted + childTag
if toTag == 'Objects':
# We need to add type and so on
child.set('Type', 'File')
self._xmlTag = toTag
self._xmlTagChild = childTag
def _collectBinaryLibrary(self, buildFolder, dest, buildConfigs:List[BuildConfig]) -> None:
'''Copies all files for a binary library into dest'''
packageFileName = self.type + '.lby'
# buildPaths["source"]
builds = {}
# builds
for build in buildConfigs:
if builds.get(build.type) is None:
builds[build.type] = build
# Collect the required source files, while ignoring certain extensions.
self._collectSourceLibrary(self.dirPath, dest, ['.c','.st','.cpp','.git','.vscode','.gitignore','jenkinsfile','CMakeLists.txt'], True)
if builds.get("sg4") != None:
self._collectConfigBinary(buildFolder, builds["sg4"], self.name, os.path.join(dest, 'SG4')) # Collect SG4 Intel
if builds.get("sg4_arm") != None:
self._collectConfigBinary(buildFolder, builds["sg4_arm"], self.name, os.path.join(dest, 'SG4', 'Arm')) # Collect SG4 ARM
# TODO: Support SG3 and lower
os.rename(os.path.join(dest, packageFileName), os.path.join(dest, 'Binary.lby'))
newLib = Library(os.path.join(dest, 'Binary.lby'))
newLib.root.set('SubType','Binary')
newLib.synchronize()
# updateLibraryFile(os.path.join(dest, 'Binary.lby'))
return
@staticmethod
def _formatVersionString(version: str) -> str:
new_version_list = []
for x in version.split(sep='.'):
new_version_list.append(str(int(x)))
return '.'.join(new_version_list)
@staticmethod
def _createPkgElement(path: str, tag: str) -> ET.Element:
# Create the element from path to be added
attributes = {}
attributes['Type'] = getPkgType(path)
if attributes['Type'] == 'Library':
attributes['Language'] = getLibraryType(path)
if attributes['Type'] == 'Program':
attributes['Language'] = getProgramType(path)
element = ET.Element(tag, attrib=attributes)
element.text = os.path.split(path)[1]
element.tail = "\n" #+2*" " Just stick with newline for now
return element
@staticmethod
def _createDependencyElement(dependency:Dependency):
# Create the element from path to be added
attributes = {}
attributes['ObjectName'] = dependency.name
if(dependency.minVersion):
attributes['FromVersion'] = dependency.minVersion
if(dependency.maxVersion):
attributes['ToVersion'] = dependency.maxVersion
return ET.Element('Dependency', attributes)
@staticmethod
def _getXmlTag(package: ET.ElementTree) -> str:
namespace = Library._getASNamespaceFormatted(package)
for child in package.getroot():
if child.tag.replace(namespace, '') in ('Files', 'Objects'):
return child.tag.replace(namespace, '')
return 'Files' # If none is found. Probably not a .lby file
@staticmethod
def _rmtreeOnError(func, path, exc_info):
'''
Error handler for ``shutil.rmtree``.
If the error is due to an access error (read only file)
it attempts to add write permission and then retries.
If the error is for another reason it re-raises the error.
Usage : ``shutil.rmtree(path, onerror=onerror)``
'''
import stat
if not os.access(path, os.W_OK):
# Is the error an access error ?
# logging.debug('Access failed on: %s. Allowing access.', path)
os.chmod(path, stat.S_IWUSR)
func(path)
else:
raise Exception(*exc_info)
@staticmethod
def _collectSourceLibrary(sourceFolder: Union[str], dest: Union[str], excludes=None, ignoreFolders=False) -> None:
'''Copies all files for a source library into dest
Ignores excludes, a glob style sequence
'''
if excludes is None: excludes = []
def _ignorePatterns(path, names):
ignores = []
for name in names:
# First evaluate the filter list.
for item in excludes:
if name.lower().endswith(item.lower()):
ignores.append(name)
# Then add to it all folders if required.
if ignoreFolders and os.path.isdir(os.path.join(path, name)):
ignores.append(name)
return ignores
# TODO: This errors if directory already exists
# This function just doesn't support that
# dir_util.copy_tree() is an option but it might not support ignore
shutil.copytree(sourceFolder, dest, ignore=_ignorePatterns)
return
@staticmethod
def _collectConfigBinary(tempPath: str, config: BuildConfig, libraryName: str, dest) -> None:
'''Collects all binary files associated with a HW Config'''
pathlib.Path(dest).mkdir(parents=True, exist_ok=True) # Create directory if it does not exist
shutil.copy2(os.path.join(tempPath, 'Objects', config.name, config.hardware, libraryName + '.br'), dest) # Library.br
shutil.copy2(os.path.join(tempPath, 'Includes', libraryName + '.h'), dest) # Library.h
shutil.copy2(os.path.join(tempPath, 'Archives', config.name, config.hardware, 'lib' + libraryName + '.a'), dest) # libLibrary.a
return
@staticmethod
def _collectLogicalBinary(sourceFolder: Union[str], dest) -> None:
'''Collects all Logical View files required for a binary library'''
pathlib.Path(dest).mkdir(parents=True, exist_ok=True) # Create directory if it does not exist
validExtensions = ['fun', 'lby', 'var', 'typ', 'md']
for item in os.listdir(sourceFolder):
# Get file extension.
splitItem = item.split('.')
extension = splitItem[-1]
if extension in validExtensions:
shutil.copy(os.path.join(sourceFolder, item), dest)
return
class Project(xmlAsFile):
def __init__(self, path: str):
if(os.path.isdir(path)):
# If we are given a dir, find first project file
# If it doesn't exist super will error
projectFile = [f for f in os.listdir(path) if f.endswith('.apj')][0] # Use first .apj found in dir
path = os.path.join(path, projectFile)
# TODO: Improve error message for file not found
super().__init__(path)
self.name = os.path.basename(os.path.splitext(path)[0]) # Get project name from .apj path
self.sourcePath = os.path.join(self.dirPath, 'Logical')
self.physicalPath = os.path.join(self.dirPath, 'Physical')
self.tempPath = os.path.join(self.dirPath, 'Temp')
self.binaryPath = os.path.join(self.dirPath, 'Binaries')
self.cacheIgnore = ['_AS', 'Acp10*', 'Arnc0*', 'Mapp*', 'Motion', 'TRF_LIB', 'Mp*', 'As*']
self.libraries:List[Library] = []
self.cacheProject()
def _checkIgnore(self, iterable, ignores) -> List[str]:
if ignores is not None:
for ignore in ignores:
iterable[:] = [name for name in iterable if not fnmatch.fnmatch(name, ignore)]
return iterable
def _checkLibIgnore(self, libs:List[Library], ignores) -> List[Library]:
for ignore in ignores:
libs[:] = [lib for lib in libs if not fnmatch.fnmatch(lib.path, ignore)]
return libs
def _resetCache(self):
self.libraries.clear()
return
def cacheProject(self):
self._resetCache()
for root, dirs, files in os.walk(self.sourcePath, topdown=True):
dirs[:] = self._checkIgnore(dirs, self.cacheIgnore)
files[:] = self._checkIgnore(files, self.cacheIgnore)
for name in files:
if name.endswith('.lby'): # This is a library
try:
lib = Library(os.path.join(root, name))
self.libraries.append(lib)
except:
# Do nothing if this lib failed to be found.
pass
if name.endswith('.pkg'): # This is a package, and it could contain a link to a referenced library.
package = Package(os.path.join(root, name))
objects = package.findall('Objects', 'Object')
for item in objects:
# Look for referenced library entries, and add them to the list of libraries.
if (item.get('Type', '').lower() == 'library') & (item.get('Reference', '').lower() == 'true'):
path = convertAsPathToWinPath(item.text)
lib = Library(path)
self.libraries.append(lib)
return self
def exportLibraries(self, dest, overwrite=False, buildConfigs:List[BuildConfig]=None, blacklist:list=None, whitelist:list=None, binary=True, includeVersion=False) -> ProjectExportInfo:
if buildConfigs is None: buildConfigs = self.buildConfigs
if whitelist is None: whitelist = []
if blacklist is None: blacklist = []
# Determine which libraries to build.
exportLibs = []
# If there's a 'whitelist', use this as a permissive filter applied to full library list.
if len(whitelist) > 0:
# Convert the list to lower case.
whitelist = [el.lower() for el in whitelist]
for lib in self.libraries:
if lib.name.lower() in whitelist:
exportLibs.append(lib)
# If there's a 'blacklist', use this as a restrictive filter applied to full library list.
elif len(blacklist) > 0:
# Convert the list to lower case.
blacklist = [el.lower() for el in blacklist]
for lib in self.libraries:
if lib.name.lower() not in blacklist:
exportLibs.append(lib)
else:
exportLibs = self.libraries.copy()
exportInfo = ProjectExportInfo()
for lib in exportLibs:
print('Exporting ' + lib.name + '...')
result = lib.export(dest, self.tempPath, self.buildConfigs, overwrite=overwrite, binary=binary, includeVersion=includeVersion)
exportInfo.addLibInfo(result)
return exportInfo
def exportLibrary(self, library:Library, dest:str, overwrite=False, ignores:Union[tuple,list]=None, binary=True, includeVersion=False, withDependencies=True)-> ProjectExportInfo:
exportInfo = ProjectExportInfo()
if withDependencies:
# Filter dependencies
depNames = library.dependencyNames
depNames = self._checkIgnore(depNames, ignores)
depNames = self._checkIgnore(depNames, self.cacheIgnore)
dependencies = self.getLibrariesByName(depNames)
for dep in dependencies:
result = self.exportLibrary(dep, dest, ignores=ignores, overwrite=overwrite, binary=binary, includeVersion=includeVersion)
exportInfo.extend(result)
result = library.export(dest, self.tempPath, self.buildConfigs, overwrite=overwrite, binary=binary, includeVersion=includeVersion)
exportInfo.addLibInfo(result)
return exportInfo
def build(self, *configNames, buildMode='Build', buildRUCPackage=True, tempPath='', binaryPath='', simulation=False, additionalArgs:Union[str,list,tuple]=None):
for configName in configNames:
simulation_status = self.getHardwareParameter(configName, 'Simulation')
# Set simulation properly in hardware before building
if simulation_status == '':
self.setHardwareParameter(configName, 'Simulation', str(int(simulation)))
elif bool(int(simulation_status)) != simulation:
self.setHardwareParameter(configName, 'Simulation', str(int(simulation)))
# TODO: Support should be better supported for return status here. Probably a list
return batchBuildAsProject(self.path, getASBuildPath(self.ASVersion), configNames, buildMode, buildRUCPackage, tempPath=tempPath, logPath=self.dirPath, binaryPath=binaryPath, simulation=simulation, additionalArg=additionalArgs)
def createPIP(self, configName, destination):
logging.info(f'Creating PIP at {destination}')
# ASVersion is in the format AS45, whereas PVIVersion needs to be in the format V4.5.
pviVersion = self.ASVersion.replace('AS','',1)
pviVersion = 'V' + pviVersion[:1] + '.' + pviVersion[1:]
# Retrieve the configuration object based on the name.
config = self.getConfigByName(configName)
# Retrieve RUCPackage location (this automatically gets placed by AS in the Binaries/<Config>/<Hardware> folder, and has a default name).
RUCPackagePath = os.path.join(self.binaryPath, config.name, config.hardware, 'RUCPackage', 'RUCPackage.zip')
# Generate a .pil file that will contain a single instruction: CreatePIP.
RUCFolderPath = os.path.dirname(RUCPackagePath)
RUCPilPath = os.path.join(RUCFolderPath, 'CreatePIP.pil')
with open(RUCPilPath, 'w+') as f:
# TODO: may want to get so of the below options from arguments (i.e. initial install, forced reboot, etc).
f.write(f'CreatePIP "{RUCPackagePath}", "InstallMode=ForceReboot InstallRestriction=AllowPartitioning KeepPVValues=0 ExecuteInitExit=1 IgnoreVersion=1", "Default", "SupportLegacyAR=0", "DestinationDirectory={destination}"')
# Call PVITransfer.exe to run the .pil script that was just created.
arguments = []
arguments.append(os.path.join(getPVITransferPath(pviVersion), 'PVITransfer.exe'))
arguments.append('-automatic') # bypass GUI prompts
arguments.append('-silent') # don't show GUI at all
arguments.append(RUCPilPath)
arguments.append('-consoleOutput')
logging.debug(arguments)
process = subprocess.run(arguments)
logging.debug(process)
if(process.returncode == 0):
logging.debug('PIP created')
else:
logging.debug(f'Error in creating PIP, code {process.returncode}: {PVIReturnCodeText[process.returncode]}')
return process
def createArsim(self, *configNames, startSim = False):
'''*Deprecated* - see createSim'''
return self.createSim(configNames, startSim=startSim)
def createSim(self, *configNames, destination, startSim = False):
pviVersion = self.ASVersion.replace('AS','',1)
pviVersion = 'V' + pviVersion[:1] + '.' + pviVersion[1:]
for configName in configNames:
config = self.getConfigByName(configName)
CreateARSimStructure(os.path.join(self.binaryPath, config.name, config.hardware, 'RUCPackage', 'RUCPackage.zip'), destination, pviVersion, startSim=startSim)
pass
def startSim(self, configName:str, build=False):
pass
def getLibraryByName(self, libName:str) -> Library:
for lib in self.libraries:
if lib.name == libName:
return lib
return None
def getLibrariesByName(self, libNames:List[str]) -> List[Library]:
libraries = []
for lib in self.libraries:
if lib.name in libNames:
libraries.append(lib)
return libraries
def getConfigByName(self, configName:str) -> BuildConfig:
# TODO: This raises exception if no config is found, StopIteration. Should be more descriptive
return next(i for i in self.buildConfigs if i.name == configName)
def getConstantValue(self, filePath:str, varName:str):
# Retrieve the value of a constant variable defined in a .VAR file.
fullFilePath = os.path.join(self.dirPath, filePath)
f = open(fullFilePath, "r")
fileContents = f.read()
return re.search(varName + ".*'(.*)'", fileContents).group(1)
def getIniValue(self, filePath:str, sectionName:str, keyName:str):
# Retrieve the value of a key defined in a .ini file.
fullFilePath = os.path.join(self.dirPath, filePath)
config = configparser.ConfigParser()
config.read(fullFilePath)
return config[sectionName][keyName]
@property
def buildConfigs(self) -> List[BuildConfig]:
return self._getConfigs(self.physicalPath)
@property
def buildConfigNames(self) -> List[str]:
names = []
for config in self.buildConfigs:
names.append(config.name)
return names
@property
def ASVersion(self) -> str:
with open(self.path, 'r') as f:
return self._parseASVersion(f.read())
@staticmethod
def _parseASVersion(apj:str) -> str:
result = re.search('<?AutomationStudio Version="(.*)" ', apj).group(1).split('.')
version = ''.join(result[0:2])
return 'AS' + version
def getHardwareParameter(self, config, paramName) -> str:
# Retrieve the value of a parameter defined in the configuration's Hardware.hw file.
hardwareFile = xmlAsFile(os.path.join(self.physicalPath, config, 'Hardware.hw'))
element = hardwareFile.find("Module","Parameter[@ID='" + paramName + "']")
if not element is None:
attributes = element.attrib
return attributes['Value']
else:
return ''
def setHardwareParameter(self, config, paramName, paramValue):
# Write a value to a specified parameter in the configuration's Hardware.hw file.
hardwareFile = xmlAsFile(os.path.join(self.physicalPath, config, 'Hardware.hw'))
try:
attributes = hardwareFile.find("Module","Parameter[@ID='" + paramName + "']").attrib
attributes['Value'] = paramValue
hardwareFile.write()
except:
# Getting here means the element to write to doesn't exist. It needs to be created.
# Set up the element that needs to be created.
attributes = {}
attributes['ID'] = paramName
attributes['Value'] = paramValue
element = ET.Element('Parameter', attrib=attributes)
# Create a parent map to determine the PLC node where the element will be added.
parent_map = {c: p for p in hardwareFile.package.iter() for c in p}
# Use the ConfigurationID which should (?) always be there...
config_element = hardwareFile.find("Module","Parameter[@ID='ConfigurationID']")
for key, value in parent_map.items():
if config_element == key:
parent = value
# Now find the parent element, and append the new parameter.
parent_element = hardwareFile.find("Module[@Name='" + parent.attrib["Name"] + "']")
parent_element.append(element)
hardwareFile.write()
return
def _getConfigs(self, physicalPath: str) -> List[BuildConfig]:
'''Get list of build configurations from physical directory'''
physical = Package(os.path.join(self.physicalPath, 'Physical.pkg'))
objects = physical.findall('Objects', 'Object')
configurations = []
for config in objects:
if config.get('Type', '').lower() == 'configuration':
path = os.path.join(physicalPath, config.text)
configurations.append(BuildConfig(name=config.text, path=path, hardware=getHardwareFolderFromConfig(path)))
configurations[-1].type = getConfigType(configurations[-1])
return configurations
class Package(xmlAsFile):
'''TODO: Maybe if doesn't exist, create one'''
def __init__(self, path: str, new_pkg=False):
if(os.path.isdir(path)):
path = os.path.join(path, 'Package.pkg')
if (new_pkg):
package_element = ET.Element('Package')
package_element.set('xmlns', 'http://br-automation.co.at/AS/Package')
objects_element = ET.SubElement(package_element, 'Objects')
tree = ET.ElementTree(package_element)
if (new_pkg):
super().__init__(path, tree)
else:
super().__init__(path)
def synchPackageFile(self):
# TODO: Does not handle references
items = [i for i in os.listdir(self.dirPath)]
# TODO: update package with directory
objsText = {}
# Remove items not in dir from pkg
for element in self.objects:
if element.text not in items:
self._removePkgObject(element.text)
else:
objsText[element.text] = element
# Add items in dir to pkg
for item in items:
if item == os.path.split(self.path)[1]: continue # Ignore pkg file
if item not in objsText:
self._addPkgObject(path=os.path.join(self.dirPath, item))
self.write()
return self
def addObject(self, path, reference=False):
'''Copy file or folder to package and directory'''
name = os.path.basename(path)
newPath = os.path.join(self.dirPath, name)
if(os.path.dirname(path) != self.dirPath and not reference):
# If object is not in dir, add it
if os.path.isfile(path):