2 ################################################################################
3 # Copyright 2023 highstreet technologies GmbH
5 # Licensed under the Apache License, Version 2.0 (the 'License');
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an 'AS IS' BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
22 from typing import List
26 def __init__(self, url, branch, dstFolder) -> None:
27 self.downloadUrl = url
28 self.publicUrlFormat = self.createPublicUrlFormat(url, branch)
29 print(f'fmt={self.publicUrlFormat}')
31 self.baseFolder = dstFolder
32 self.subfolder = self.createSubFolder(url, branch)
34 def createPublicUrlFormat(self, url:str, branch:str)->str:
35 if url.endswith('.git'):
37 if url.startswith('git@'):
38 url = 'https://'+url[4:]
39 fmt=url+'/raw/'+branch+'/{}'
41 def createSubFolder(self, gitUrl:str, branch:str) -> str:
42 regex = r"^[^\/]+\/\/(.*)$"
43 matches = re.finditer(regex, gitUrl)
46 if name.endswith('.git'):
49 hlp1 = name.split('/')
58 return '/'.join(tmp)+'/'+branch
60 def getDstFolder(self)->str:
61 return f'{self.baseFolder}/{self.subfolder}'
63 def exec(self, cmd:str):
64 output = subprocess.Popen(
65 cmd, shell=True, stdout=subprocess.PIPE).stdout.read()
68 def download(self) -> bool:
69 print(f'try to download repo {self.downloadUrl} to {self.getDstFolder()}')
70 self.exec(f'git clone --single-branch --branch {self.branch} {self.downloadUrl} {self.getDstFolder()}')
72 def getFilesFiltered(self, lst:List[str]=None, path=None, root=None, filter=['yaml','yml'])->List[str]:
76 root=str(self.getDstFolder())
78 path=self.getDstFolder()
79 if os.path.exists(path) and os.path.isdir(path):
80 # Iterate over all files and directories in the given path
81 for filename in os.listdir(path):
82 if filename.startswith("."):
85 # Get the absolute path of the file/directory
86 abs_path = os.path.join(path, filename)
87 # If it is a directory, recursively call this function on it
88 if os.path.isdir(abs_path):
89 self.getFilesFiltered(lst=lst, path=abs_path, root=root, filter=filter )
90 # If it is a file, print its absolute path
91 elif os.path.isfile(abs_path):
93 if abs_path.endswith(fi):
98 relpath=abs_path[len(root)+1:]
102 def urlAlreadyInData(self, data:List[dict], pubUrl:str, key='publicURL'):
104 if key in item and item[key]==pubUrl:
108 def createSchemaMap(self):
109 schemaMapFile = f'{self.baseFolder}/schema-map.json'
110 if os.path.isfile(schemaMapFile):
111 with open(schemaMapFile) as fp:
115 files = self.getFilesFiltered()
118 pubUrl = self.publicUrlFormat.format(file)
119 if self.urlAlreadyInData(data,pubUrl):
120 print(f'entry with url {pubUrl} already exists. ignoring')
124 'localURL': f'{self.subfolder}/{file}'
126 with open(schemaMapFile,'w') as fp:
129 def printHelp(msg:str = None):
132 print('Installation script for VES additional formats')
134 print(' install.py [OPTIONS]')
135 print(' -c CONFIG_FILE')
136 print(' -d DESTINATION_PATH')
141 configFilename = None
146 configFilename = args.pop(0)
148 dstPath = args.pop(0)
150 printHelp(f'bad parameter {arg}')
155 if configFilename is None or dstPath is None:
156 printHelp('missing parameter')
160 config = json.load(open(configFilename))
161 if not isinstance(config, list):
162 printHelp('invalid config json. has to be a array')
166 dlRepo = item['repository']
167 dlBranch = item['branch']
168 installer = Installer(dlRepo, dlBranch, dstPath)
170 installer.createSchemaMap()