Skip to content

Commit

Permalink
poetry: Run poetry env use only after cache is loaded
Browse files Browse the repository at this point in the history
The virtualenv cache might contain invalid entries, such as virtualenvs
built in previous, buggy versions of this action.  The `poetry env use`
command will recreate virtualenvs in case they are invalid, but it has
to be run only *after* the cache is loaded.

Refactor `CacheDistributor` a bit such that the validation (and possible
recreation) of virtualenvs happens only after the cache is loaded.
  • Loading branch information
oranav committed Dec 9, 2022
1 parent bc3992e commit 58362bf
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 220 deletions.
166 changes: 73 additions & 93 deletions dist/cache-save/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45304,10 +45304,10 @@ function populateMaps (extensions, types) {
module.exports = minimatch
minimatch.Minimatch = Minimatch

var path = (function () { try { return __nccwpck_require__(1017) } catch (e) {}}()) || {
sep: '/'
}
minimatch.sep = path.sep
var path = { sep: '/' }
try {
path = __nccwpck_require__(1017)
} catch (er) {}

var GLOBSTAR = minimatch.GLOBSTAR = Minimatch.GLOBSTAR = {}
var expand = __nccwpck_require__(3717)
Expand Down Expand Up @@ -45359,64 +45359,43 @@ function filter (pattern, options) {
}

function ext (a, b) {
a = a || {}
b = b || {}
var t = {}
Object.keys(a).forEach(function (k) {
t[k] = a[k]
})
Object.keys(b).forEach(function (k) {
t[k] = b[k]
})
Object.keys(a).forEach(function (k) {
t[k] = a[k]
})
return t
}

minimatch.defaults = function (def) {
if (!def || typeof def !== 'object' || !Object.keys(def).length) {
return minimatch
}
if (!def || !Object.keys(def).length) return minimatch

var orig = minimatch

var m = function minimatch (p, pattern, options) {
return orig(p, pattern, ext(def, options))
return orig.minimatch(p, pattern, ext(def, options))
}

m.Minimatch = function Minimatch (pattern, options) {
return new orig.Minimatch(pattern, ext(def, options))
}
m.Minimatch.defaults = function defaults (options) {
return orig.defaults(ext(def, options)).Minimatch
}

m.filter = function filter (pattern, options) {
return orig.filter(pattern, ext(def, options))
}

m.defaults = function defaults (options) {
return orig.defaults(ext(def, options))
}

m.makeRe = function makeRe (pattern, options) {
return orig.makeRe(pattern, ext(def, options))
}

m.braceExpand = function braceExpand (pattern, options) {
return orig.braceExpand(pattern, ext(def, options))
}

m.match = function (list, pattern, options) {
return orig.match(list, pattern, ext(def, options))
}

return m
}

Minimatch.defaults = function (def) {
if (!def || !Object.keys(def).length) return Minimatch
return minimatch.defaults(def).Minimatch
}

function minimatch (p, pattern, options) {
assertValidPattern(pattern)
if (typeof pattern !== 'string') {
throw new TypeError('glob pattern string required')
}

if (!options) options = {}

Expand All @@ -45425,6 +45404,9 @@ function minimatch (p, pattern, options) {
return false
}

// "" only matches ""
if (pattern.trim() === '') return p === ''

return new Minimatch(pattern, options).match(p)
}

Expand All @@ -45433,14 +45415,15 @@ function Minimatch (pattern, options) {
return new Minimatch(pattern, options)
}

assertValidPattern(pattern)
if (typeof pattern !== 'string') {
throw new TypeError('glob pattern string required')
}

if (!options) options = {}

pattern = pattern.trim()

// windows support: need to use /, not \
if (!options.allowWindowsEscape && path.sep !== '/') {
if (path.sep !== '/') {
pattern = pattern.split(path.sep).join('/')
}

Expand All @@ -45451,7 +45434,6 @@ function Minimatch (pattern, options) {
this.negate = false
this.comment = false
this.empty = false
this.partial = !!options.partial

// make the set of regexps etc.
this.make()
Expand All @@ -45461,6 +45443,9 @@ Minimatch.prototype.debug = function () {}

Minimatch.prototype.make = make
function make () {
// don't do it more than once.
if (this._made) return

var pattern = this.pattern
var options = this.options

Expand All @@ -45480,7 +45465,7 @@ function make () {
// step 2: expand braces
var set = this.globSet = this.braceExpand()

if (options.debug) this.debug = function debug() { console.error.apply(console, arguments) }
if (options.debug) this.debug = console.error

this.debug(this.pattern, set)

Expand Down Expand Up @@ -45560,29 +45545,19 @@ function braceExpand (pattern, options) {
pattern = typeof pattern === 'undefined'
? this.pattern : pattern

assertValidPattern(pattern)
if (typeof pattern === 'undefined') {
throw new TypeError('undefined pattern')
}

// Thanks to Yeting Li <https://github.com/yetingli> for
// improving this regexp to avoid a ReDOS vulnerability.
if (options.nobrace || !/\{(?:(?!\{).)*\}/.test(pattern)) {
if (options.nobrace ||
!pattern.match(/\{.*\}/)) {
// shortcut. no need to expand.
return [pattern]
}

return expand(pattern)
}

var MAX_PATTERN_LENGTH = 1024 * 64
var assertValidPattern = function (pattern) {
if (typeof pattern !== 'string') {
throw new TypeError('invalid pattern')
}

if (pattern.length > MAX_PATTERN_LENGTH) {
throw new TypeError('pattern is too long')
}
}

// parse a component of the expanded set.
// At this point, no pattern may contain "/" in it
// so we're going to return a 2d array, where each entry is the full
Expand All @@ -45597,17 +45572,14 @@ var assertValidPattern = function (pattern) {
Minimatch.prototype.parse = parse
var SUBPARSE = {}
function parse (pattern, isSub) {
assertValidPattern(pattern)
if (pattern.length > 1024 * 64) {
throw new TypeError('pattern is too long')
}

var options = this.options

// shortcuts
if (pattern === '**') {
if (!options.noglobstar)
return GLOBSTAR
else
pattern = '*'
}
if (!options.noglobstar && pattern === '**') return GLOBSTAR
if (pattern === '') return ''

var re = ''
Expand Down Expand Up @@ -45663,12 +45635,10 @@ function parse (pattern, isSub) {
}

switch (c) {
/* istanbul ignore next */
case '/': {
case '/':
// completely not allowed, even escaped.
// Should already be path-split by now.
return false
}

case '\\':
clearStateChar()
Expand Down Expand Up @@ -45787,23 +45757,25 @@ function parse (pattern, isSub) {

// handle the case where we left a class open.
// "[z-a]" is valid, equivalent to "\[z-a\]"
// split where the last [ was, make sure we don't have
// an invalid re. if so, re-walk the contents of the
// would-be class to re-translate any characters that
// were passed through as-is
// TODO: It would probably be faster to determine this
// without a try/catch and a new RegExp, but it's tricky
// to do safely. For now, this is safe and works.
var cs = pattern.substring(classStart + 1, i)
try {
RegExp('[' + cs + ']')
} catch (er) {
// not a valid class!
var sp = this.parse(cs, SUBPARSE)
re = re.substr(0, reClassStart) + '\\[' + sp[0] + '\\]'
hasMagic = hasMagic || sp[1]
inClass = false
continue
if (inClass) {
// split where the last [ was, make sure we don't have
// an invalid re. if so, re-walk the contents of the
// would-be class to re-translate any characters that
// were passed through as-is
// TODO: It would probably be faster to determine this
// without a try/catch and a new RegExp, but it's tricky
// to do safely. For now, this is safe and works.
var cs = pattern.substring(classStart + 1, i)
try {
RegExp('[' + cs + ']')
} catch (er) {
// not a valid class!
var sp = this.parse(cs, SUBPARSE)
re = re.substr(0, reClassStart) + '\\[' + sp[0] + '\\]'
hasMagic = hasMagic || sp[1]
inClass = false
continue
}
}

// finish up the class.
Expand Down Expand Up @@ -45887,7 +45859,9 @@ function parse (pattern, isSub) {
// something that could conceivably capture a dot
var addPatternStart = false
switch (re.charAt(0)) {
case '[': case '.': case '(': addPatternStart = true
case '.':
case '[':
case '(': addPatternStart = true
}

// Hack to work around lack of negative lookbehind in JS
Expand Down Expand Up @@ -45949,7 +45923,7 @@ function parse (pattern, isSub) {
var flags = options.nocase ? 'i' : ''
try {
var regExp = new RegExp('^' + re + '$', flags)
} catch (er) /* istanbul ignore next - should be impossible */ {
} catch (er) {
// If it was an invalid regular expression, then it can't match
// anything. This trick looks for a character after the end of
// the string, which is of course impossible, except in multi-line
Expand Down Expand Up @@ -46007,7 +45981,7 @@ function makeRe () {

try {
this.regexp = new RegExp(re, flags)
} catch (ex) /* istanbul ignore next - should be impossible */ {
} catch (ex) {
this.regexp = false
}
return this.regexp
Expand All @@ -46025,8 +45999,8 @@ minimatch.match = function (list, pattern, options) {
return list
}

Minimatch.prototype.match = function match (f, partial) {
if (typeof partial === 'undefined') partial = this.partial
Minimatch.prototype.match = match
function match (f, partial) {
this.debug('match', f, this.pattern)
// short-circuit in the case of busted things.
// comments, etc.
Expand Down Expand Up @@ -46108,7 +46082,6 @@ Minimatch.prototype.matchOne = function (file, pattern, partial) {

// should be impossible.
// some invalid regexp stuff in the set.
/* istanbul ignore if */
if (p === false) return false

if (p === GLOBSTAR) {
Expand Down Expand Up @@ -46182,7 +46155,6 @@ Minimatch.prototype.matchOne = function (file, pattern, partial) {
// no match was found.
// However, in partial mode, we can't say this is necessarily over.
// If there's more *pattern* left, then
/* istanbul ignore if */
if (partial) {
// ran out of file
this.debug('\n>>> no match, partial?', file, fr, pattern, pr)
Expand All @@ -46196,7 +46168,11 @@ Minimatch.prototype.matchOne = function (file, pattern, partial) {
// patterns with magic have been turned into regexps.
var hit
if (typeof p === 'string') {
hit = f === p
if (options.nocase) {
hit = f.toLowerCase() === p.toLowerCase()
} else {
hit = f === p
}
this.debug('string match', p, f, hit)
} else {
hit = f.match(p)
Expand Down Expand Up @@ -46227,16 +46203,16 @@ Minimatch.prototype.matchOne = function (file, pattern, partial) {
// this is ok if we're doing the match as part of
// a glob fs traversal.
return partial
} else /* istanbul ignore else */ if (pi === pl) {
} else if (pi === pl) {
// ran out of pattern, still have file left.
// this is only acceptable if we're on the very last
// empty segment of a file with a trailing slash.
// a/* should match a/b/
return (fi === fl - 1) && (file[fi] === '')
var emptyFileEnd = (fi === fl - 1) && (file[fi] === '')
return emptyFileEnd
}

// should be unreachable.
/* istanbul ignore next */
throw new Error('wtf?')
}

Expand Down Expand Up @@ -59711,6 +59687,9 @@ class CacheDistributor {
this.cacheDependencyPath = cacheDependencyPath;
this.CACHE_KEY_PREFIX = 'setup-python';
}
handleLoadedCache() {
return __awaiter(this, void 0, void 0, function* () { });
}
restoreCache() {
return __awaiter(this, void 0, void 0, function* () {
const { primaryKey, restoreKey } = yield this.computeKeys();
Expand All @@ -59723,6 +59702,7 @@ class CacheDistributor {
core.saveState(State.CACHE_PATHS, cachePath);
core.saveState(State.STATE_CACHE_PRIMARY_KEY, primaryKey);
const matchedKey = yield cache.restoreCache(cachePath, primaryKey, restoreKey);
yield this.handleLoadedCache();
this.handleMatchResult(matchedKey, primaryKey);
});
}
Expand Down
Loading

0 comments on commit 58362bf

Please sign in to comment.