Skip to content

Commit 629e8c5

Browse files
committed
fix: naming convention
1 parent e003bd6 commit 629e8c5

File tree

2 files changed

+56
-58
lines changed

2 files changed

+56
-58
lines changed

packages/hub/src/lib/cache-management.spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, test, expect, vi, beforeEach } from "vitest";
22
import {
3-
scan_cache_dir,
4-
scan_cached_repo,
3+
scanCacheDir,
4+
scanCachedRepo,
55
REPO_TYPE_T,
66
scanSnapshotDir,
77
parseRepoType,
@@ -20,13 +20,13 @@ beforeEach(() => {
2020
vi.restoreAllMocks();
2121
});
2222

23-
describe("scan_cache_dir", () => {
23+
describe("scanCacheDir", () => {
2424
test("should throw an error if cacheDir is not a directory", async () => {
2525
vi.mocked(stat).mockResolvedValueOnce({
2626
isDirectory: () => false,
2727
} as Stats);
2828

29-
await expect(scan_cache_dir("/fake/dir")).rejects.toThrow("Scan cache expects a directory");
29+
await expect(scanCacheDir("/fake/dir")).rejects.toThrow("Scan cache expects a directory");
3030
});
3131

3232
test("empty directory should return an empty set of repository and no warnings", async () => {
@@ -37,21 +37,21 @@ describe("scan_cache_dir", () => {
3737
// mock empty cache folder
3838
vi.mocked(readdir).mockResolvedValue([]);
3939

40-
const result = await scan_cache_dir("/fake/dir");
40+
const result = await scanCacheDir("/fake/dir");
4141

4242
// cacheDir must have been read
4343
expect(readdir).toHaveBeenCalledWith("/fake/dir");
4444

4545
expect(result.warnings.length).toBe(0);
4646
expect(result.repos.size).toBe(0);
47-
expect(result.size_on_disk).toBe(0);
47+
expect(result.sizeOnDisk).toBe(0);
4848
});
4949
});
5050

51-
describe("scan_cached_repo", () => {
51+
describe("scanCachedRepo", () => {
5252
test("should throw an error for invalid repo path", async () => {
5353
await expect(() => {
54-
return scan_cached_repo("/fake/repo_path");
54+
return scanCachedRepo("/fake/repo_path");
5555
}).rejects.toThrow("Repo path is not a valid HuggingFace cache directory");
5656
});
5757

@@ -62,7 +62,7 @@ describe("scan_cached_repo", () => {
6262
} as Stats);
6363

6464
await expect(() => {
65-
return scan_cached_repo("/fake/cacheDir/models--hello-world--name");
65+
return scanCachedRepo("/fake/cacheDir/models--hello-world--name");
6666
}).rejects.toThrow("Snapshots dir doesn't exist in cached repo");
6767
});
6868

@@ -73,12 +73,12 @@ describe("scan_cached_repo", () => {
7373
isDirectory: () => true,
7474
} as Stats);
7575

76-
const result = await scan_cached_repo(repoPath);
76+
const result = await scanCachedRepo(repoPath);
7777
expect(readdir).toHaveBeenCalledWith(join(repoPath, "refs"), {
7878
withFileTypes: true,
7979
});
8080

81-
expect(result.repo_id).toBe("hello-world/name");
81+
expect(result.repoId).toBe("hello-world/name");
8282
});
8383
});
8484

packages/hub/src/lib/cache-management.ts

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,44 +23,44 @@ export enum REPO_TYPE_T {
2323
}
2424

2525
export interface CachedFileInfo {
26-
file_name: string;
27-
file_path: string;
28-
blob_path: string;
29-
size_on_disk: number;
26+
filename: string;
27+
filePath: string;
28+
blobPath: string;
29+
sizeOnDisk: number;
3030

31-
blob_last_accessed: number;
32-
blob_last_modified: number;
31+
blobLastAccessed: number;
32+
blobLastModified: number;
3333
}
3434

3535
export interface CachedRevisionInfo {
36-
commit_hash: string;
37-
snapshot_path: string;
38-
size_on_disk: number;
36+
commitHash: string;
37+
snapshotPath: string;
38+
sizeOnDisk: number;
3939
readonly files: Set<CachedFileInfo>;
4040
readonly refs: Set<string>;
4141

42-
last_modified: number;
42+
lastModified: number;
4343
}
4444

4545
export interface CachedRepoInfo {
46-
repo_id: string;
47-
repo_type: REPO_TYPE_T;
48-
repo_path: string;
49-
size_on_disk: number;
50-
nb_files: number;
46+
repoId: string;
47+
repoType: REPO_TYPE_T;
48+
repoPath: string;
49+
sizeOnDisk: number;
50+
nbFiles: number;
5151
readonly revisions: Set<CachedRevisionInfo>;
5252

53-
last_accessed: number;
54-
last_modified: number;
53+
lastAccessed: number;
54+
lastModified: number;
5555
}
5656

5757
export interface HFCacheInfo {
58-
size_on_disk: number;
58+
sizeOnDisk: number;
5959
readonly repos: Set<CachedRepoInfo>;
6060
warnings: Error[];
6161
}
6262

63-
export async function scan_cache_dir(cacheDir: string | undefined = undefined): Promise<HFCacheInfo> {
63+
export async function scanCacheDir(cacheDir: string | undefined = undefined): Promise<HFCacheInfo> {
6464
if (!cacheDir) cacheDir = HF_HUB_CACHE;
6565

6666
const s = await stat(cacheDir);
@@ -88,7 +88,7 @@ export async function scan_cache_dir(cacheDir: string | undefined = undefined):
8888
}
8989

9090
try {
91-
const cached = await scan_cached_repo(absolute);
91+
const cached = await scanCachedRepo(absolute);
9292
repos.add(cached);
9393
} catch (err: unknown) {
9494
warnings.push(err as Error);
@@ -97,14 +97,14 @@ export async function scan_cache_dir(cacheDir: string | undefined = undefined):
9797

9898
return {
9999
repos: repos,
100-
size_on_disk: [...repos.values()].reduce((sum, repo) => sum + repo.size_on_disk, 0),
100+
sizeOnDisk: [...repos.values()].reduce((sum, repo) => sum + repo.sizeOnDisk, 0),
101101
warnings: warnings,
102102
};
103103
}
104104

105-
export async function scan_cached_repo(repo_path: string): Promise<CachedRepoInfo> {
105+
export async function scanCachedRepo(repoPath: string): Promise<CachedRepoInfo> {
106106
// get the directory name
107-
const name = basename(repo_path);
107+
const name = basename(repoPath);
108108
if (!name.includes("--")) {
109109
throw new Error(`Repo path is not a valid HuggingFace cache directory: ${name}`);
110110
}
@@ -114,8 +114,8 @@ export async function scan_cached_repo(repo_path: string): Promise<CachedRepoInf
114114
const repoType = parseRepoType(type);
115115
const repoId = remaining.join("/");
116116

117-
const snapshotsPath = join(repo_path, "snapshots");
118-
const refsPath = join(repo_path, "refs");
117+
const snapshotsPath = join(repoPath, "snapshots");
118+
const refsPath = join(repoPath, "refs");
119119

120120
const snapshotStat = await stat(snapshotsPath);
121121
if (!snapshotStat.isDirectory()) {
@@ -147,17 +147,15 @@ export async function scan_cached_repo(repo_path: string): Promise<CachedRepoInf
147147
await scanSnapshotDir(revisionPath, cachedFiles, blobStats);
148148

149149
const revisionLastModified =
150-
cachedFiles.size > 0
151-
? Math.max(...[...cachedFiles].map((file) => file.blob_last_modified))
152-
: revisionStat.mtimeMs;
150+
cachedFiles.size > 0 ? Math.max(...[...cachedFiles].map((file) => file.blobLastModified)) : revisionStat.mtimeMs;
153151

154152
cachedRevisions.add({
155-
commit_hash: dir,
153+
commitHash: dir,
156154
files: cachedFiles,
157155
refs: refsByHash.get(dir) || new Set(),
158-
size_on_disk: [...cachedFiles].reduce((sum, file) => sum + file.size_on_disk, 0),
159-
snapshot_path: revisionPath,
160-
last_modified: revisionLastModified,
156+
sizeOnDisk: [...cachedFiles].reduce((sum, file) => sum + file.sizeOnDisk, 0),
157+
snapshotPath: revisionPath,
158+
lastModified: revisionLastModified,
161159
});
162160

163161
refsByHash.delete(dir);
@@ -166,11 +164,11 @@ export async function scan_cached_repo(repo_path: string): Promise<CachedRepoInf
166164
// Verify that all refs refer to a valid revision
167165
if (refsByHash.size > 0) {
168166
throw new Error(
169-
`Reference(s) refer to missing commit hashes: ${JSON.stringify(Object.fromEntries(refsByHash))} (${repo_path})`
167+
`Reference(s) refer to missing commit hashes: ${JSON.stringify(Object.fromEntries(refsByHash))} (${repoPath})`
170168
);
171169
}
172170

173-
const repoStats = await stat(repo_path);
171+
const repoStats = await stat(repoPath);
174172
const repoLastAccessed =
175173
blobStats.size > 0 ? Math.max(...[...blobStats.values()].map((stat) => stat.atimeMs)) : repoStats.atimeMs;
176174

@@ -179,14 +177,14 @@ export async function scan_cached_repo(repo_path: string): Promise<CachedRepoInf
179177

180178
// Return the constructed CachedRepoInfo object
181179
return {
182-
repo_id: repoId,
183-
repo_type: repoType,
184-
repo_path: repo_path,
185-
nb_files: blobStats.size,
180+
repoId: repoId,
181+
repoType: repoType,
182+
repoPath: repoPath,
183+
nbFiles: blobStats.size,
186184
revisions: cachedRevisions,
187-
size_on_disk: [...blobStats.values()].reduce((sum, stat) => sum + stat.size, 0),
188-
last_accessed: repoLastAccessed,
189-
last_modified: repoLastModified,
185+
sizeOnDisk: [...blobStats.values()].reduce((sum, stat) => sum + stat.size, 0),
186+
lastAccessed: repoLastAccessed,
187+
lastModified: repoLastModified,
190188
};
191189
}
192190

@@ -219,12 +217,12 @@ export async function scanSnapshotDir(
219217
const blobStat = await getBlobStat(blobPath, blobStats);
220218

221219
cachedFiles.add({
222-
file_name: file.name,
223-
file_path: filePath,
224-
blob_path: blobPath,
225-
size_on_disk: blobStat.size,
226-
blob_last_accessed: blobStat.atimeMs,
227-
blob_last_modified: blobStat.mtimeMs,
220+
filename: file.name,
221+
filePath: filePath,
222+
blobPath: blobPath,
223+
sizeOnDisk: blobStat.size,
224+
blobLastAccessed: blobStat.atimeMs,
225+
blobLastModified: blobStat.mtimeMs,
228226
});
229227
}
230228
}

0 commit comments

Comments
 (0)