Skip to content

Commit

Permalink
feat(python): convert conan installer
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice committed Aug 6, 2024
1 parent 59cbc95 commit bf8ea4a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 110 deletions.
6 changes: 6 additions & 0 deletions src/cli/install-tool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ import {
ComposerInstallService,
ComposerVersionResolver,
} from '../tools/php/composer';
import {
ConanInstallService,
ConanVersionResolver,
} from '../tools/python/conan';
import { PipVersionResolver } from '../tools/python/pip';
import { PipBaseInstallService } from '../tools/python/utils';
import { CocoapodsInstallService } from '../tools/ruby/gem';
Expand Down Expand Up @@ -72,6 +76,7 @@ function prepareInstallContainer(): Container {
container.bind(INSTALL_TOOL_TOKEN).to(BazeliskInstallService);
container.bind(INSTALL_TOOL_TOKEN).to(BunInstallService);
container.bind(INSTALL_TOOL_TOKEN).to(CocoapodsInstallService);
container.bind(INSTALL_TOOL_TOKEN).to(ConanInstallService);
container.bind(INSTALL_TOOL_TOKEN).to(DartInstallService);
container.bind(INSTALL_TOOL_TOKEN).to(DockerInstallService);
container.bind(INSTALL_TOOL_TOKEN).to(DotnetInstallService);
Expand Down Expand Up @@ -107,6 +112,7 @@ function prepareResolveContainer(): Container {
container.bind(ToolVersionResolverService).toSelf();

// tool version resolver
container.bind(TOOL_VERSION_RESOLVER).to(ConanVersionResolver);
container.bind(TOOL_VERSION_RESOLVER).to(ComposerVersionResolver);
container.bind(TOOL_VERSION_RESOLVER).to(GradleVersionResolver);
container.bind(TOOL_VERSION_RESOLVER).to(JavaVersionResolver);
Expand Down
2 changes: 2 additions & 0 deletions src/cli/prepare-tool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
JavaPrepareService,
} from '../tools/java';
import { NodePrepareService } from '../tools/node';
import { ConanPrepareService } from '../tools/python/conan';
import { logger } from '../utils';
import { PrepareLegacyToolsService } from './prepare-legacy-tools.service';
import { PREPARE_TOOL_TOKEN, PrepareToolService } from './prepare-tool.service';
Expand All @@ -24,6 +25,7 @@ function prepareContainer(): Container {
container.bind(PrepareLegacyToolsService).toSelf();

// tool services
container.bind(PREPARE_TOOL_TOKEN).to(ConanPrepareService);
container.bind(PREPARE_TOOL_TOKEN).to(DartPrepareService);
container.bind(PREPARE_TOOL_TOKEN).to(DotnetPrepareService);
container.bind(PREPARE_TOOL_TOKEN).to(DockerPrepareService);
Expand Down
74 changes: 74 additions & 0 deletions src/cli/tools/python/conan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { join } from 'node:path';
import { codeBlock } from 'common-tags';
import { inject, injectable } from 'inversify';
import { BasePrepareService } from '../../prepare-tool/base-prepare.service';
import { AptService, EnvService, PathService } from '../../services';
import { type Distro, getDistro } from '../../utils';
import { PipVersionResolver } from './pip';
import { PipBaseInstallService } from './utils';

@injectable()
export class ConanPrepareService extends BasePrepareService {
override readonly name: string = 'conan';

constructor(
@inject(PathService) pathSvc: PathService,
@inject(EnvService) envSvc: EnvService,
@inject(AptService) private readonly aptSvc: AptService,
) {
super(pathSvc, envSvc);
}

override async execute(): Promise<void> {
const distro = await getDistro();
const profile = codeBlock`
[settings]
arch=${getArchitecture(this.envSvc.arch)}
build_type=Release
compiler=gcc
compiler.cppstd=gnu17
compiler.libcxx=libstdc++11
compiler.version=${getCompilerVersion(distro)}
os=Linux
`;

const profilesPath = join(this.envSvc.userHome, '.conan2', 'profiles');
await this.pathSvc.createDir(profilesPath);
await this.pathSvc.writeFile(join(profilesPath, 'default'), profile);
await this.aptSvc.install('cmake', 'gcc', 'g++', 'make', 'perl');
}
}

@injectable()
export class ConanInstallService extends PipBaseInstallService {
override readonly name: string = 'conan';
}

@injectable()
export class ConanVersionResolver extends PipVersionResolver {
override tool: string = 'conan';
}

function getArchitecture(arch: string): string {
switch (arch) {
case 'arm64':
return 'armv8';
case 'amd64':
return 'x86_64';
}

throw new Error(`Unsupported architecture: ${arch}`);
}

function getCompilerVersion(distro: Distro): string {
switch (distro.versionCode) {
case 'focal':
return '9';
case 'jammy':
return '11';
case 'noble':
return '13';
}

throw new Error(`Unsupported distro: ${distro.name}`);
}
109 changes: 0 additions & 109 deletions src/usr/local/containerbase/tools/v2/conan.sh

This file was deleted.

4 changes: 3 additions & 1 deletion test/python/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ RUN install-tool python 3.12.4
#--------------------------------------
FROM base AS build-rootless

RUN prepare-tool python
RUN prepare-tool python conan

USER 1000

Expand Down Expand Up @@ -261,6 +261,8 @@ RUN install-tool copier 9.3.1
#--------------------------------------
FROM build-rootless AS test-other

RUN install-tool conan

RUN install-pip checkov
RUN install-pip copier
RUN install-pip hashin
Expand Down

0 comments on commit bf8ea4a

Please sign in to comment.