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 for key,value in self.publicUrlFormat.items():
30 print(f'fmt={key}->{value}')
32 self.baseFolder = dstFolder
33 self.subfolder = self.createSubFolder(url, branch)
35 def createPublicUrlFormat(self, url:str, branch:str)->dict:
37 if url.endswith('.git'):
39 if url.startswith('git@'):
40 url = 'https://'+url[4:]
41 fmt["raw"]=url+'/raw/'+branch+'/{}'
42 fmt["blob"]=url+'/blob/'+branch+'/{}'
44 def createSubFolder(self, gitUrl:str, branch:str) -> str:
45 regex = r"^[^\/]+\/\/(.*)$"
46 matches = re.finditer(regex, gitUrl)
49 if name.endswith('.git'):
52 hlp1 = name.split('/')
61 return '/'.join(tmp)+'/'+branch
63 def getDstFolder(self)->str:
64 return f'{self.baseFolder}/{self.subfolder}'
66 def exec(self, cmd:str):
67 output = subprocess.Popen(
68 cmd, shell=True, stdout=subprocess.PIPE).stdout.read()
71 def download(self) -> bool:
72 print(f'try to download repo {self.downloadUrl} to {self.getDstFolder()}')
73 self.exec(f'git clone --single-branch --branch {self.branch} {self.downloadUrl} {self.getDstFolder()}')
75 def getFilesFiltered(self, lst:List[str]=None, path=None, root=None, filter=['yaml','yml'])->List[str]:
79 root=str(self.getDstFolder())
81 path=self.getDstFolder()
82 if os.path.exists(path) and os.path.isdir(path):
83 # Iterate over all files and directories in the given path
84 for filename in os.listdir(path):
85 if filename.startswith("."):
88 # Get the absolute path of the file/directory
89 abs_path = os.path.join(path, filename)
90 # If it is a directory, recursively call this function on it
91 if os.path.isdir(abs_path):
92 self.getFilesFiltered(lst=lst, path=abs_path, root=root, filter=filter )
93 # If it is a file, print its absolute path
94 elif os.path.isfile(abs_path):
96 if abs_path.endswith(fi):
101 relpath=abs_path[len(root)+1:]
105 def urlAlreadyInData(self, data:List[dict], pubUrl:str, key='publicURL'):
107 if key in item and item[key]==pubUrl:
111 def createSchemaMap(self):
112 schemaMapFile = f'{self.baseFolder}/schema-map.json'
113 if os.path.isfile(schemaMapFile):
114 with open(schemaMapFile) as fp:
118 files = self.getFilesFiltered()
121 for key,value in self.publicUrlFormat.items():
122 pubUrl = value.format(file)
123 if self.urlAlreadyInData(data,pubUrl):
124 print(f'entry with url {pubUrl} already exists. ignoring')
128 'localURL': f'{self.subfolder}/{file}'
130 with open(schemaMapFile,'w') as fp:
133 def printHelp(msg:str = None):
136 print('Installation script for VES additional formats')
138 print(' install.py [OPTIONS]')
139 print(' -c CONFIG_FILE')
140 print(' -d DESTINATION_PATH')
145 configFilename = None
150 configFilename = args.pop(0)
152 dstPath = args.pop(0)
154 printHelp(f'bad parameter {arg}')
159 if configFilename is None or dstPath is None:
160 printHelp('missing parameter')
164 config = json.load(open(configFilename))
165 if not isinstance(config, list):
166 printHelp('invalid config json. has to be a array')
170 dlRepo = item['repository']
171 dlBranch = item['branch']
172 installer = Installer(dlRepo, dlBranch, dstPath)
174 installer.createSchemaMap()