-
Notifications
You must be signed in to change notification settings - Fork 293
/
Copy pathshared.js
143 lines (132 loc) · 4.05 KB
/
shared.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const Appdir = require('appdirectory');
const rimraf = require('rimraf');
const glob = require('glob-all');
const path = require('path');
const fse = require('fs-extra');
const sha256File = require('sha256-file');
/**
* This helper will check if we're using static cache and have max
* versions enabled and will delete older versions in a fifo fashion
* @param {Object} options
* @param {Object} serverless
* @return {undefined}
*/
function checkForAndDeleteMaxCacheVersions({ serverless, options, log }) {
// If we're using the static cache, and we have static cache max versions enabled
if (
options.useStaticCache &&
options.staticCacheMaxVersions &&
parseInt(options.staticCacheMaxVersions) > 0
) {
// Get the list of our cache files
const files = glob.sync(
[path.join(getUserCachePath(options), '*_slspyc/')],
{ mark: true }
);
// Check if we have too many
if (files.length >= options.staticCacheMaxVersions) {
// Sort by modified time
files.sort(function (a, b) {
return (
fse.statSync(a).mtime.getTime() - fse.statSync(b).mtime.getTime()
);
});
// Remove the older files...
var items = 0;
for (
var i = 0;
i < files.length - options.staticCacheMaxVersions + 1;
i++
) {
rimraf.sync(files[i]);
items++;
}
// Log the number of cache files flushed
if (log) {
log.info(
`Removed ${items} items from cache because of staticCacheMaxVersions`
);
} else {
serverless.cli.log(
`Removed ${items} items from cache because of staticCacheMaxVersions`
);
}
}
}
}
/**
* The working path that all requirements will be compiled into
* @param {string} subfolder
* @param {string} servicePath
* @param {Object} options
* @param {Object} serverless
* @return {string}
*/
function getRequirementsWorkingPath(
subfolder,
requirementsTxtDirectory,
options,
serverless
) {
// If we want to use the static cache
if (options && options.useStaticCache) {
if (subfolder) {
const architecture = serverless.service.provider.architecture || 'x86_64';
subfolder = `${subfolder}_${architecture}_slspyc`;
}
// If we have max number of cache items...
return path.join(getUserCachePath(options), subfolder);
}
// If we don't want to use the static cache, then fallback to the way things used to work
return path.join(requirementsTxtDirectory, 'requirements');
}
/**
* Path of a cached requirements layer archive file
* @param {string} subfolder
* @param {string} fallback
* @param {Object} options
* @param {Object} serverless
* @return {string}
*/
function getRequirementsLayerPath(hash, fallback, options, serverless) {
// If we want to use the static cache
if (hash && options && options.useStaticCache) {
const architecture = serverless.service.provider.architecture || 'x86_64';
hash = `${hash}_${architecture}_slspyc.zip`;
return path.join(getUserCachePath(options), hash);
}
// If we don't want to use the static cache, then fallback to requirements file in .serverless directory
return fallback;
}
/**
* The static cache path that will be used for this system + options, used if static cache is enabled
* @param {Object} options
* @return {string}
*/
function getUserCachePath(options) {
// If we've manually set the static cache location
if (options && options.cacheLocation) {
return path.resolve(options.cacheLocation);
}
// Otherwise, find/use the python-ey appdirs cache location
const dirs = new Appdir({
appName: 'serverless-python-requirements',
appAuthor: 'UnitedIncome',
});
return dirs.userCache();
}
/**
* Helper to get the md5 a a file's contents to determine if a requirements has a static cache
* @param {string} fullpath
* @return {string}
*/
function sha256Path(fullpath) {
return sha256File(fullpath);
}
module.exports = {
checkForAndDeleteMaxCacheVersions,
getRequirementsWorkingPath,
getRequirementsLayerPath,
getUserCachePath,
sha256Path,
};