Detects new/modified/updated files matching a glob pattern. Useful for detecting wheels created by pip or cubuildwheel etc.
| 3269 | |
| 3270 | |
| 3271 | class NewFiles: |
| 3272 | ''' |
| 3273 | Detects new/modified/updated files matching a glob pattern. Useful for |
| 3274 | detecting wheels created by pip or cubuildwheel etc. |
| 3275 | ''' |
| 3276 | def __init__(self, glob_pattern): |
| 3277 | # Find current matches of <glob_pattern>. |
| 3278 | self.glob_pattern = glob_pattern |
| 3279 | self.items0 = self._items() |
| 3280 | def get(self): |
| 3281 | ''' |
| 3282 | Returns list of new matches of <glob_pattern> - paths of files that |
| 3283 | were not present previously, or have different mtimes or have different |
| 3284 | contents. |
| 3285 | ''' |
| 3286 | ret = list() |
| 3287 | items = self._items() |
| 3288 | for path, id_ in items.items(): |
| 3289 | id0 = self.items0.get(path) |
| 3290 | if id0 != id_: |
| 3291 | ret.append(path) |
| 3292 | return ret |
| 3293 | def get_n(self, n): |
| 3294 | ''' |
| 3295 | Returns new files matching <glob_pattern>, asserting that there are |
| 3296 | exactly <n>. |
| 3297 | ''' |
| 3298 | ret = self.get() |
| 3299 | assert len(ret) == n, f'{len(ret)=}: {ret}' |
| 3300 | return ret |
| 3301 | def get_one(self): |
| 3302 | ''' |
| 3303 | Returns new match of <glob_pattern>, asserting that there is exactly |
| 3304 | one. |
| 3305 | ''' |
| 3306 | return self.get_n(1)[0] |
| 3307 | def _file_id(self, path): |
| 3308 | mtime = os.stat(path).st_mtime |
| 3309 | with open(path, 'rb') as f: |
| 3310 | content = f.read() |
| 3311 | hash_ = hashlib.md5(content).digest() |
| 3312 | # With python >= 3.11 we can do: |
| 3313 | #hash_ = hashlib.file_digest(f, hashlib.md5).digest() |
| 3314 | return mtime, hash_ |
| 3315 | def _items(self): |
| 3316 | ret = dict() |
| 3317 | for path in glob.glob(self.glob_pattern): |
| 3318 | if os.path.isfile(path): |
| 3319 | ret[path] = self._file_id(path) |
| 3320 | return ret |
| 3321 | |
| 3322 | |
| 3323 | def swig_get(swig, quick, swig_local='pipcl-swig-git'): |
no outgoing calls
no test coverage detected
searching dependent graphs…