Skip to content

Instantly share code, notes, and snippets.

@baruchiro
Created December 8, 2019 13:09
Show Gist options
  • Save baruchiro/6fe94269efdfb41260f594b05ce68745 to your computer and use it in GitHub Desktop.
Save baruchiro/6fe94269efdfb41260f594b05ce68745 to your computer and use it in GitHub Desktop.
Find all missing dll's from .deps.json and download them
#!/usr/bin/python3
# args: <Project directory> <App Name>
# Example: ~/source/myapp/bin/Release/netcoreapp2.1/publish myapp
import json
import sys
import os
import urllib.request
import zipfile
import tempfile
from shutil import copyfile
def getDllPathToNuget(depsJson):
linuxTargetKeyName = [t for t in depsJson['targets'] if 'linux-x64' in t][0]
runtimesNugetToDlls = {k: v['runtime'].keys() for k, v in depsJson['targets'][linuxTargetKeyName].items() if 'runtime' in v}
nativesNugetToDlls = {k: v['native'].keys() for k, v in depsJson['targets'][linuxTargetKeyName].items() if 'native' in v}
runtimesDllToNuget = {}
for nuget, runtimes in runtimesNugetToDlls.items():
for runtime in runtimes:
runtimesDllToNuget[runtime] = nuget
for nuget, natives in nativesNugetToDlls.items():
for native in natives:
runtimesDllToNuget[native] = nuget
return runtimesDllToNuget
def downloadNuget(nugetNameAndVersion):
nugetUrl = 'https://api.nuget.org/v3-flatcontainer/{package}/{version}/{package}.{version}.nupkg'
package, version = nugetNameAndVersion.split('/')
tmpDir = tempfile.gettempdir()
downloadFilePath = os.path.join(tmpDir, package + '.zip')
if not os.path.exists(downloadFilePath):
downloadUrl = nugetUrl.format(package=package, version=version)
print(f'Downloading {nugetNameAndVersion} from {downloadUrl}')
urllib.request.urlretrieve(downloadUrl, downloadFilePath)
unzipPath = os.path.join(tmpDir, package, version)
if not os.path.exists(unzipPath):
print(f'Unzipping {downloadFilePath} to {unzipPath}')
os.makedirs(unzipPath)
with zipfile.ZipFile(downloadFilePath) as zip:
zip.extractall(unzipPath)
return unzipPath
appFolder = sys.argv[1]
appName = sys.argv[2]
depsJsonPath = os.path.join(appFolder, appName + '.deps.json')
with open(depsJsonPath) as depsJson:
data = json.load(depsJson)
runtimesDllToNuget = getDllPathToNuget(data)
notExistDllsKeys = []
for dll in runtimesDllToNuget.keys():
dllName = os.path.basename(dll)
dllInExecDir = os.path.join(appFolder, dllName)
if not os.path.exists(dllInExecDir):
print(f'dll missing: {dllName}')
notExistDllsKeys.append(dll)
# print(notExistDllsKeys)
for dll in notExistDllsKeys:
nugetFilesPath = downloadNuget(runtimesDllToNuget[dll])
dllExpectedPath = os.path.join(nugetFilesPath, dll)
dllDropPath = os.path.join(appFolder, os.path.basename(dll))
print(f'Coping {dllExpectedPath} to {dllDropPath}')
copyfile(dllExpectedPath, dllDropPath)
@baruchiro
Copy link
Author

Dirty script to execute the following algorithm:

  1. Find all the required DLLs in the app.deps.json file (by searching all nugets with runtime or native DLL under targets).
  2. If the DLL doesn't exist in the app dir, download the DLL from nuget.org.
  3. Unzip the whole nupck to NugetName/Version.
  4. Find the required DLL by the path from .deps.json, related to the NugetName/Version.
  5. Copy the DLL to the app dir.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment