Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/planemo/ci.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 """Utilities for dealing with continous integration systems.""" | |
2 | |
3 from __future__ import print_function | |
4 | |
5 import copy | |
6 import math | |
7 import os | |
8 | |
9 import yaml | |
10 | |
11 from planemo import git | |
12 from planemo import io | |
13 from planemo.shed import REPO_METADATA_FILES | |
14 | |
15 | |
16 def filter_paths(ctx, raw_paths, path_type="repo", **kwds): | |
17 """Filter ``paths``. | |
18 | |
19 ``path_type`` is ``repo`` or ``file``. | |
20 """ | |
21 cwd = os.getcwd() | |
22 | |
23 filter_kwds = copy.deepcopy(kwds) | |
24 changed_in_commit_range = kwds.get("changed_in_commit_range", None) | |
25 diff_paths = None | |
26 if changed_in_commit_range is not None: | |
27 diff_files = git.diff(ctx, cwd, changed_in_commit_range) | |
28 if path_type == "repo": | |
29 diff_dirs = set(os.path.dirname(p) for p in diff_files) | |
30 diff_paths = set() | |
31 for diff_dir in diff_dirs: | |
32 diff_path = metadata_file_in_path(diff_dir) | |
33 if diff_path: | |
34 diff_paths.add(diff_path) | |
35 else: | |
36 diff_paths = diff_files | |
37 | |
38 unique_paths = set(os.path.relpath(p, cwd) for p in raw_paths) | |
39 if diff_paths is not None: | |
40 unique_paths = unique_paths.intersection(diff_paths) | |
41 filtered_paths = sorted(io.filter_paths(unique_paths, cwd=cwd, **filter_kwds)) | |
42 excluded_paths = sorted(set(unique_paths) - set(filtered_paths)) | |
43 if excluded_paths: | |
44 ctx.log("List of excluded paths: %s" % excluded_paths) | |
45 | |
46 path_count = len(filtered_paths) | |
47 chunk_size = ((1.0 * path_count) / kwds["chunk_count"]) | |
48 chunk = kwds["chunk"] | |
49 | |
50 chunked_paths = [] | |
51 for i, path in enumerate(filtered_paths): | |
52 if int(math.floor(i / chunk_size)) == chunk: | |
53 chunked_paths.append(path) | |
54 | |
55 return chunked_paths | |
56 | |
57 | |
58 def metadata_file_in_path(diff_dir): | |
59 while diff_dir: | |
60 for metadata_file in REPO_METADATA_FILES: | |
61 if os.path.isfile(os.path.join(diff_dir, metadata_file)): | |
62 return diff_dir | |
63 diff_dir = os.path.dirname(diff_dir) | |
64 | |
65 | |
66 def group_paths(paths): | |
67 repos = {} | |
68 for path in paths: | |
69 repo = os.path.split(path)[0] | |
70 if repo not in repos: | |
71 repos[repo] = [] | |
72 repos[repo].append(path) | |
73 return [" ".join(repos[_]) for _ in repos] | |
74 | |
75 | |
76 def print_path_list(paths, **kwds): | |
77 with io.open_file_or_standard_output(kwds["output"], "w") as f: | |
78 for path in paths: | |
79 print(path, file=f) | |
80 | |
81 | |
82 def print_as_yaml(item, **kwds): | |
83 with io.open_file_or_standard_output(kwds["output"], "w") as f: | |
84 f.write(yaml.safe_dump(item)) |