Skip to content

Commit

Permalink
Merge pull request #1112 from lamarrr/conda-support-patch
Browse files Browse the repository at this point in the history
[conda] fixed service component bug
  • Loading branch information
lumaxis authored May 7, 2024
2 parents 04be0cf + 112645c commit 78665b7
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 57 deletions.
122 changes: 65 additions & 57 deletions routes/originConda.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const condaChannels = {
'anaconda-r': 'https://repo.anaconda.com/pkgs/r',
'conda-forge': 'https://conda.anaconda.org/conda-forge'
}
const condaCache = new Cache()

async function fetchCondaChannelData(channel) {
const key = `${channel}-channelData`
Expand All @@ -35,66 +34,75 @@ async function fetchCondaRepoData(channel, subdir) {
return repoData
}

router.get(
'/:channel/:subdir/:name/revisions',
asyncMiddleware(async (request, response) => {
let { channel, subdir, name } = request.params
channel = encodeURIComponent(channel)
subdir = encodeURIComponent(subdir)
name = encodeURIComponent(channel)
if (!condaChannels[channel]) {
return response.status(404).send(`Unrecognized Conda channel ${channel}`)
}
let channelData = await fetchCondaChannelData(channel)
if (!channelData.packages[name]) {
return response.status(404).send(`Package ${name} not found in Conda channel ${channel}`)
}
if (subdir !== '-' && !channelData.subdirs.find(x => x == subdir)) {
return response.status(404).send(`Subdir ${subdir} is non-existent in Conda channel ${channel}`)
}
let revisions = []
let subdirs = subdir === '-' ? channelData.packages[name].subdirs : [subdir]
for (let subdir of subdirs) {
const repoData = await fetchCondaRepoData(channel, subdir)
if (repoData['packages']) {
Object.entries(repoData['packages']).forEach(([, packageData]) => {
if (packageData.name === name) {
revisions.push(`${subdir}:${packageData.version}-${packageData.build}`)
}
})
}
if (repoData['packages.conda']) {
Object.entries(repoData['packages.conda']).forEach(([, packageData]) => {
if (packageData.name === name) {
revisions.push(`${subdir}:${packageData.version}-${packageData.build}`)
}
})
}
}
return response.status(200).send(uniq(revisions))
})
)
router.get('/:channel/:subdir/:name/revisions', asyncMiddleware(getOriginCondaRevisions()))

router.get(
'/:channel/:name',
asyncMiddleware(async (request, response) => {
let { channel, name } = request.params
channel = encodeURIComponent(channel)
name = encodeURIComponent(name)
if (!condaChannels[channel]) {
return response.status(404).send([])
async function getOriginCondaRevisions(request, response) {
let { channel, subdir, name } = request.params
channel = encodeURIComponent(channel)
subdir = encodeURIComponent(subdir)
name = encodeURIComponent(name)
if (!condaChannels[channel]) {
return response.status(404).send(`Unrecognized Conda channel ${channel}`)
}
let channelData = await fetchCondaChannelData(channel)
if (!channelData.packages[name]) {
return response.status(404).send(`Package ${name} not found in Conda channel ${channel}`)
}
if (subdir !== '-' && !channelData.subdirs.find(x => x == subdir)) {
return response
.status(404)
.send(`Subdir ${subdir} is non-existent in Conda channel ${channel}, subdirs: ${channelData.subdirs}`)
}
let revisions = []
let subdirs = subdir === '-' ? channelData.packages[name].subdirs : [subdir]
for (let subdir of subdirs) {
const repoData = await fetchCondaRepoData(channel, subdir)
if (repoData['packages']) {
Object.entries(repoData['packages']).forEach(([, packageData]) => {
if (packageData.name === name) {
revisions.push(`${subdir}:${packageData.version}-${packageData.build}`)
}
})
}
let channelData = await fetchCondaChannelData(channel)
let matches = Object.entries(channelData.packages)
.filter(([packageName]) => packageName.includes(name))
.map(([packageName]) => {
return { id: packageName }
if (repoData['packages.conda']) {
Object.entries(repoData['packages.conda']).forEach(([, packageData]) => {
if (packageData.name === name) {
revisions.push(`${subdir}:${packageData.version}-${packageData.build}`)
}
})
return response.status(200).send(matches)
})
)
}
}
return response.status(200).send(uniq(revisions))
}

router.get('/:channel/:name', asyncMiddleware(getOriginConda))

async function getOriginConda(request, response) {
let { channel, name } = request.params
channel = encodeURIComponent(channel)
name = encodeURIComponent(name)
if (!condaChannels[channel]) {
return response.status(404).send(`Unrecognized Conda channel ${channel}`)
}
let channelData = await fetchCondaChannelData(channel)
let matches = Object.entries(channelData.packages)
.filter(([packageName]) => packageName.includes(name))
.map(([packageName]) => {
return { id: packageName }
})
return response.status(200).send(matches)
}

let condaCache

function setup(cache = new Cache(), testFlag = false) {
condaCache = cache

if (testFlag) {
router._getOriginConda = getOriginConda
router._getOriginCondaRevisions = getOriginCondaRevisions
}

function setup() {
return router
}

Expand Down
66 changes: 66 additions & 0 deletions test/routes/origins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) The Linux Foundation and others. Licensed under the MIT license.
// SPDX-License-Identifier: MIT

const { expect } = require('chai')
const httpMocks = require('node-mocks-http')
const sinon = require('sinon')
const originCondaRoutes = require('../../routes/originConda')

describe('Conda origin routes', () => {
it('accepts a good revisions GET request', async () => {
const request = createGetOriginCondaRevisionsRequest()
const response = httpMocks.createResponse()
const stubCondaCache = createStubCondaCache({
'conda-forge-linux-64-repoData': {
packages: [
{
name: 'tensorflow',
version: '2.15.0',
build: 'cuda120py39hb94c71b_3'
}
],
subdirs: ['linux-64']
},
'conda-forge-channelData': {
packages: {
tensorflow: {}
},
subdirs: ['linux-64']
}
})
const router = createRoutes(stubCondaCache)
await router._getOriginCondaRevisions(request, response)
expect(response.statusCode).to.be.eq(200)
expect(response._getData()).to.be.deep.equal(['linux-64:2.15.0-cuda120py39hb94c71b_3'])
expect(stubCondaCache.get.calledTwice).to.be.true
})
})

function createGetOriginCondaRevisionsRequest() {
return httpMocks.createRequest({
method: 'GET',
url: 'origins/conda/conda-forge/linux-64/tensorflow/revisions',
baseUrl: 'https://dev.clearlydefined.io',
params: {
channel: 'conda-forge',
subdir: 'linux-64',
name: 'tensorflow'
}
})
}

function createRoutes(condaCache) {
return originCondaRoutes(condaCache, true)
}

function createStubCondaCache(cacheData) {
let getStub = sinon.stub()

for (const [key, value] of Object.entries(cacheData)) {
getStub.withArgs(key).returns(value)
}

return {
get: getStub
}
}

0 comments on commit 78665b7

Please sign in to comment.