Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add origins/condaforge Route for Consistent Conda API Structure #1279

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ function createApp(config) {
app.use('/', require('./routes/index')(config.buildsha, config.appVersion))
app.use('/origins/github', require('./routes/originGitHub')())
app.use('/origins/crate', require('./routes/originCrate')())
app.use('/origins/conda', require('./routes/originConda')())
const repoAccess = require('./lib/condaRepoAccess')()
app.use('/origins/condaforge', require('./routes/originCondaforge')(repoAccess))
app.use('/origins/conda', require('./routes/originConda')(repoAccess))
app.use('/origins/pod', require('./routes/originPod')())
app.use('/origins/npm', require('./routes/originNpm')())
app.use('/origins/maven', require('./routes/originMaven')())
Expand Down
87 changes: 87 additions & 0 deletions lib/condaRepoAccess.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// (c) Copyright 2025, SAP SE and ClearlyDefined contributors. Licensed under the MIT license.
// SPDX-License-Identifier: MIT

const { callFetch: requestPromise } = require('./fetch')
const { uniq } = require('lodash')
const Cache = require('../providers/caching/memory')

const condaChannels = {
'anaconda-main': 'https://repo.anaconda.com/pkgs/main',
'anaconda-r': 'https://repo.anaconda.com/pkgs/r',
'conda-forge': 'https://conda.anaconda.org/conda-forge'
}

class CondaRepoAccess {
constructor(cache) {
this.cache = cache || Cache({ defaultTtlSeconds: 8 * 60 * 60 }) // 8 hours
}

checkIfValidChannel(channel) {
if (!condaChannels[channel]) {
throw new Error(`Unrecognized Conda channel ${channel}`)
}
}

async fetchChannelData(channel) {
const key = `${channel}-channelData`
let channelData = this.cache.get(key)
if (!channelData) {
const url = `${condaChannels[channel]}/channeldata.json`
channelData = await requestPromise({ url, method: 'GET', json: true })
this.cache.set(key, channelData)
}
return channelData
}

async fetchRepoData(channel, subdir) {
const key = `${channel}-${subdir}-repoData`
let repoData = this.cache.get(key)
if (!repoData) {
const url = `${condaChannels[channel]}/${subdir}/repodata.json`
repoData = await requestPromise({ url, method: 'GET', json: true })
this.cache.set(key, repoData)
}
return repoData
}

async getRevisions(channel, subdir, name) {
channel = encodeURIComponent(channel)
name = encodeURIComponent(name)
subdir = encodeURIComponent(subdir)
this.checkIfValidChannel(channel)
const channelData = await this.fetchChannelData(channel)
if (!channelData.packages[name]) {
throw new Error(`Package ${name} not found in channel ${channel}`)
}
if (subdir !== '-' && !channelData.subdirs.find(x => x === subdir)) {
throw new Error(`Subdir ${subdir} is non-existent in channel ${channel}, subdirs: ${channelData.subdirs}`)
}
let revisions = []
const subdirs = subdir === '-' ? channelData.packages[name].subdirs : [subdir]
for (let subdir of subdirs) {
const repoData = await this.fetchRepoData(channel, subdir)
;['packages', 'packages.conda'].forEach(key => {
if (repoData[key]) {
const matchingVersions = Object.entries(repoData[key])
.filter(([, packageData]) => packageData.name === name)
.map(([, packageData]) => `${subdir}:${packageData.version}-${packageData.build}`)
revisions.push(...matchingVersions)
}
})
}
return uniq(revisions)
}

async getPackages(channel, name) {
channel = encodeURIComponent(channel)
name = encodeURIComponent(name)
this.checkIfValidChannel(channel)
const channelData = await this.fetchChannelData(channel)
const matches = Object.entries(channelData.packages)
.filter(([packageName]) => packageName.includes(name))
.map(([packageName]) => ({ id: packageName }))
return matches
}
}

module.exports = cache => new CondaRepoAccess(cache)
98 changes: 14 additions & 84 deletions routes/originConda.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,107 +3,37 @@

const asyncMiddleware = require('../middleware/asyncMiddleware')
const router = require('express').Router()
const { callFetch: requestPromise } = require('../lib/fetch')
const { uniq } = require('lodash')
const { Cache } = require('memory-cache')
const condaChannels = {
'anaconda-main': 'https://repo.anaconda.com/pkgs/main',
'anaconda-r': 'https://repo.anaconda.com/pkgs/r',
'conda-forge': 'https://conda.anaconda.org/conda-forge'
}

async function fetchCondaChannelData(channel) {
const key = `${channel}-channelData`
let channelData = condaCache.get(key)
if (!channelData) {
const url = `${condaChannels[channel]}/channeldata.json`
channelData = await requestPromise({ url, method: 'GET', json: true })
condaCache.put(key, channelData, 8 * 60 * 60 * 1000) // 8 hours
}
return channelData
}

async function fetchCondaRepoData(channel, subdir) {
const key = `${channel}-${subdir}-repoData`
let repoData = condaCache.get(key)
if (!repoData) {
const url = `${condaChannels[channel]}/${subdir}/repodata.json`
repoData = await requestPromise({ url, method: 'GET', json: true })
condaCache.put(key, repoData, 8 * 60 * 60 * 1000) // 8 hours
}
return repoData
}
let repoAccess

router.get('/:channel/:subdir/:name/revisions', asyncMiddleware(getOriginCondaRevisions))

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}`)
try {
const revisions = await repoAccess.getRevisions(channel, subdir, name)
response.status(200).send(revisions)
} catch (error) {
response.status(404).send(error.message)
}
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}`)
}
})
}
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/: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}`)
try {
const matches = await repoAccess.getPackages(channel, name)
response.status(200).send(matches)
} catch (error) {
response.status(404).send(error.message)
}
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) {
function setup(condaRepoAccess, testflag = false) {
if (testflag) {
router._getOriginConda = getOriginConda
router._getOriginCondaRevisions = getOriginCondaRevisions
}

repoAccess = condaRepoAccess
return router
}

module.exports = setup
42 changes: 42 additions & 0 deletions routes/originCondaforge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// (c) Copyright 2025, SAP SE and ClearlyDefined contributors. Licensed under the MIT license.
// SPDX-License-Identifier: MIT

const express = require('express')
const asyncMiddleware = require('../middleware/asyncMiddleware')
const router = express.Router()

const channel = 'conda-forge'
let repoAccess

router.get(
'/:subdir/:name/revisions',
asyncMiddleware(async (req, res) => {
const { name, subdir } = req.params
try {
const revisions = await repoAccess.getRevisions(channel, subdir, name)
res.status(200).send(revisions)
} catch (error) {
res.status(404).send(error.message)
}
})
)

router.get(
'/:name',
asyncMiddleware(async (req, res) => {
const { name } = req.params
try {
const matches = await repoAccess.getPackages(channel, name)
res.status(200).send(matches)
} catch (error) {
res.status(404).send(error.message)
}
})
)

function setup(condaForgeRepoAccess) {
repoAccess = condaForgeRepoAccess
return router
}

module.exports = setup
Loading
Loading