File indexing completed on 2024-05-12 04:22:35

0001 #!/bin/python3
0002 
0003 import requests
0004 
0005 import shutil
0006 import os
0007 from sys import platform
0008 
0009 projectURL = ""
0010 projectArtifact = ""
0011 
0012 if platform == "linux":
0013     import tarfile
0014     projectURL = "https://binary-factory.kde.org/job/Krita_Nightly_Appimage_Dependency_Build/api/json/"
0015     projectArtifact = "krita-appimage-deps.tar"
0016 elif platform == "darwin":
0017     raise RuntimeError("MacOS support is not yet implemented")
0018 elif platform == "win32":
0019     import zipfile
0020     projectURL = "https://binary-factory.kde.org/job/Krita_Nightly_Windows_Dependency_Build/api/json/"
0021     projectArtifact = "krita-deps.zip"
0022     
0023 
0024 localCachePath = os.environ.pop('KDECI_CACHE_PATH')
0025 
0026 def download_url_with_compression(url, filePath):
0027     headers = {'Accept-Encoding': 'gzip, deflate'}
0028     
0029     with requests.get(depsUrl, stream=True, headers=headers) as r:
0030         r.raise_for_status()
0031         with open(filePath, 'wb') as f:
0032             for chunk in r.iter_content(chunk_size=8192): 
0033                 # If you have chunk encoded response uncomment if
0034                 # and set chunk_size parameter to None.
0035                 #if chunk: 
0036                 f.write(chunk)
0037             f.flush()
0038 
0039 print ("Fetching job metadata...")
0040 
0041 res = requests.get(projectURL)
0042 
0043 if not res:
0044     print("Couldn't fetch the API object: {}".format(res.reason))
0045     res.raise_for_status
0046 
0047 jobUrl = res.json()['lastSuccessfulBuild']['url']
0048 
0049 print ("Last successful job URL: {}".format(jobUrl))
0050 
0051 if jobUrl is None:
0052     raise FileNotFoundError()
0053 
0054 depsUrl = '{}artifact/{}'.format(jobUrl, projectArtifact)
0055 
0056 kritaCacheDir = os.path.join(localCachePath, 'krita-deps')
0057 if not os.path.isdir(kritaCacheDir):
0058     os.makedirs(kritaCacheDir)
0059 
0060 filePath = os.path.join(kritaCacheDir, projectArtifact)
0061 urlFilePath = os.path.join(kritaCacheDir, '{}.url'.format(projectArtifact))
0062 
0063 shouldDownloadNewFile = True
0064 
0065 print("Cached file: {} (exists: {})".format(filePath, os.path.isfile(filePath)))
0066 print("Cached URL file: {} (exists: {})".format(urlFilePath, os.path.isfile(urlFilePath)))
0067 
0068 if os.path.isfile(filePath) and os.path.isfile(urlFilePath):
0069     existingFileUrl = ''
0070     with open(urlFilePath, 'r') as f:
0071         existingFileUrl = f.read()
0072 
0073     print("Existing file: {}".format(urlFilePath))
0074     print("Existing file URL: {}".format(existingFileUrl))
0075 
0076     shouldDownloadNewFile = existingFileUrl != depsUrl
0077 
0078 print ("Should download the deps: {}".format(shouldDownloadNewFile))
0079 
0080 if shouldDownloadNewFile:
0081     print ("Downloading deps: {}".format(depsUrl))
0082 
0083     if os.path.isfile(filePath):
0084         os.remove(filePath)
0085     if os.path.isfile(urlFilePath):
0086         os.remove(urlFilePath)
0087 
0088     download_url_with_compression(depsUrl, filePath)
0089     with open(urlFilePath, 'w') as f:
0090         f.write(depsUrl)
0091         f.flush()
0092 
0093 print ("Extracting deps...")
0094 
0095 file = None
0096 
0097 if platform == "linux":
0098     file = tarfile.open(filePath, mode='r')
0099 elif platform == "darwin":
0100     raise RuntimeError("MacOS support is not yet implemented")
0101 elif platform == "win32":
0102     file = zipfile.ZipFile(filePath, mode='r')
0103     
0104 file.extractall(path='_tmp')
0105 file.close()
0106 
0107 if os.path.isdir('_install'):
0108     shutil.rmtree('_install')
0109 
0110 if platform == "linux":
0111     shutil.move('_tmp/deps/usr', '_install')
0112 elif platform == "win32":
0113     shutil.move('_tmp\\deps-install', '_install')
0114 
0115     shutil.rmtree('_tmp')