|
1 | 1 | const isWsl = require('is-wsl'); |
| 2 | +const glob = require('glob-all'); |
| 3 | +const fse = require('fs-extra'); |
2 | 4 |
|
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 | + : ''; |
13 | 9 |
|
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); |
25 | 18 | } |
26 | 19 | } |
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 | +}; |
55 | 21 |
|
56 | 22 | module.exports = { |
57 | | - getSlimPackageCommands, |
58 | | - getDefaultSLimOptions |
| 23 | + getStripCommand, |
| 24 | + deleteFiles |
59 | 25 | }; |
0 commit comments