Skip to content

Commit a7232c4

Browse files
authored
clean up slim code using glob-all to make it more cross platform (#227)
* basic cleanup aroudn slim option * clean up slim code using glob-all to make it more cross platform * fix use of Array.concat * try omiting \\ in strip command. might need to differ command for windows * prettier * use + with -exec for strip bc strip supports multiple files and + doesnt need to be escaped * doh prettier again * damn... find in docker on widnows is finicky * Update README.md
1 parent d00d095 commit a7232c4

File tree

6 files changed

+31
-65
lines changed

6 files changed

+31
-65
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,15 @@ custom:
107107
```
108108
#### Custom Removal Patterns
109109
To specify additional directories to remove from the installed packages,
110-
define the patterns using regex as a `slimPatterns` option in serverless config:
110+
define a list of of patterns int he serverless config using the `slimPatterns`
111+
option and glob syntax. Note, it matches against whole paths, so to match a file in any
112+
directory, start your pattern with `**/`.
111113
```yaml
112114
custom:
113115
pythonRequirements:
114116
slim: true
115117
slimPatterns:
116-
- "*.egg-info*"
118+
- "**/*.egg-info*"
117119
```
118120
This will remove all folders within the installed requirements that match
119121
the names in `slimPatterns`

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class ServerlessPythonRequirements {
2828
const options = Object.assign(
2929
{
3030
slim: false,
31+
slimPatterns: false,
3132
zip: false,
3233
cleanupZipHelper: true,
3334
invalidateCaches: false,

lib/pip.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const get = require('lodash.get');
55
const set = require('lodash.set');
66
const { spawnSync } = require('child_process');
77
const { buildImage, getBindPath, getDockerUid } = require('./docker');
8-
const { getSlimPackageCommands } = require('./slim');
8+
const { getStripCommand, deleteFiles } = require('./slim');
99

1010
/**
1111
* Install requirements described in requirementsPath to targetFolder
@@ -106,12 +106,6 @@ function installRequirements(
106106
if (process.platform === 'linux') {
107107
// Use same user so requirements folder is not root and so --cache-dir works
108108
cmdOptions.push('-u', `${process.getuid()}`);
109-
// const stripCmd = quote([
110-
// 'find', targetRequirementsFolder,
111-
// '-name', '"*.so"',
112-
// '-exec', 'strip', '{}', '\;',
113-
// ]);
114-
// pipCmd = ['/bin/bash', '-c', '"' + pipCmd + ' && ' + stripCmd + ' && ' + chownCmd + '"'];
115109
} else {
116110
// Use same user so --cache-dir works
117111
cmdOptions.push('-u', getDockerUid(bindPath));
@@ -123,11 +117,10 @@ function installRequirements(
123117
cmdOptions = pipCmd.slice(1);
124118
}
125119

126-
// If enabled slimming, strip out the caches, tests and dist-infos
120+
// If enabled slimming, strip so files
127121
if (options.slim === true || options.slim === 'true') {
128122
const preparedPath = dockerPathForWin(options, targetRequirementsFolder);
129-
const slimCmd = getSlimPackageCommands(options, preparedPath);
130-
cmdOptions.push(...slimCmd);
123+
cmdOptions.push(getStripCommand(options, preparedPath));
131124
}
132125
let spawnArgs = { cwd: servicePath, shell: true };
133126
if (process.env.SLS_DEBUG) {
@@ -148,6 +141,10 @@ function installRequirements(
148141
if (res.status !== 0) {
149142
throw new Error(res.stderr);
150143
}
144+
// If enabled slimming, delete files in slimPatterns
145+
if (options.slim === true || options.slim === 'true') {
146+
deleteFiles(options, targetRequirementsFolder);
147+
}
151148
}
152149

153150
/**

lib/slim.js

+17-51
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,25 @@
11
const isWsl = require('is-wsl');
2+
const glob = require('glob-all');
3+
const fse = require('fs-extra');
24

3-
/**
4-
* Get commands to slim the installed requirements
5-
* only for non-windows platforms:
6-
* works for docker builds and when run on UNIX platforms (wsl included)
7-
* @param {Object} options
8-
* @param {string} folderPath
9-
* @return {Array.<String>}
10-
*/
11-
function getSlimPackageCommands(options, folderPath) {
12-
let stripCmd = [];
5+
const getStripCommand = (options, folderPath) =>
6+
process.platform !== 'win32' || isWsl || options.dockerizePip
7+
? ` && find ${folderPath} -name "*.so" -exec strip {} ';'`
8+
: '';
139

14-
// Default stripping is done for non-windows environments
15-
if (process.platform !== 'win32' || isWsl) {
16-
stripCmd = getDefaultSLimOptions(folderPath);
17-
18-
// If specified any custom patterns to remove
19-
if (options.slimPatterns instanceof Array) {
20-
// Add the custom specified patterns to remove to the default commands
21-
const customPatterns = options.slimPatterns.map(pattern => {
22-
return getRemovalCommand(folderPath, pattern);
23-
});
24-
stripCmd = stripCmd.concat(customPatterns);
10+
const deleteFiles = (options, folderPath) => {
11+
let patterns = ['**/*.py[c|o]', '**/__pycache__*', '**/*.dist-info*'];
12+
if (options.slimPatterns) {
13+
patterns = patterns.concat(options.slimPatterns);
14+
}
15+
for (const pattern of patterns) {
16+
for (const file of glob.sync(`${folderPath}/${pattern}`)) {
17+
fse.removeSync(file);
2518
}
2619
}
27-
return stripCmd;
28-
}
29-
30-
/**
31-
* Gets the commands to slim the default (safe) files:
32-
* including removing caches, stripping compiled files, removing dist-infos
33-
* @param {String} folderPath
34-
* @return {Array}
35-
*/
36-
function getDefaultSLimOptions(folderPath) {
37-
return [
38-
`&& find ${folderPath} -name "*.so" -exec strip {} \\;`,
39-
`&& find ${folderPath} -name "*.py[c|o]" -exec rm -rf {} +`,
40-
`&& find ${folderPath} -type d -name "__pycache__*" -exec rm -rf {} +`,
41-
`&& find ${folderPath} -type d -name "*.dist-info*" -exec rm -rf {} +`
42-
];
43-
}
44-
45-
/**
46-
* Get the command created fromt he find and remove template:
47-
* returns a string in form `&& find <folder> -name "<match>" -exec rm -rf {} +`
48-
* @param {String} folderPath
49-
* @param {String} removalMatch
50-
* @return {String}
51-
*/
52-
function getRemovalCommand(folderPath, removalMatch) {
53-
return `&& find ${folderPath} -type d -wholename "${removalMatch}" -exec rm -rf {} +`;
54-
}
20+
};
5521

5622
module.exports = {
57-
getSlimPackageCommands,
58-
getDefaultSLimOptions
23+
getStripCommand,
24+
deleteFiles
5925
};

tests/base/_slimPatterns.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
slimPatterns:
2-
- "*.egg-info*"
2+
- "**/*.egg-info*"

tests/pipenv/_slimPatterns.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
slimPatterns:
2-
- "*.egg-info*"
2+
- "**/*.egg-info*"

0 commit comments

Comments
 (0)