ebi-eva-common-pyutils 0.6.17__2-py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. ebi_eva_common_pyutils/__init__.py +0 -0
  2. ebi_eva_common_pyutils/assembly/__init__.py +1 -0
  3. ebi_eva_common_pyutils/assembly/assembly.py +69 -0
  4. ebi_eva_common_pyutils/assembly_utils.py +91 -0
  5. ebi_eva_common_pyutils/biosamples_communicators.py +186 -0
  6. ebi_eva_common_pyutils/command_utils.py +54 -0
  7. ebi_eva_common_pyutils/common_utils.py +30 -0
  8. ebi_eva_common_pyutils/config.py +152 -0
  9. ebi_eva_common_pyutils/contig_alias/__init__.py +0 -0
  10. ebi_eva_common_pyutils/contig_alias/contig_alias.py +115 -0
  11. ebi_eva_common_pyutils/ena_utils.py +35 -0
  12. ebi_eva_common_pyutils/file_utils.py +31 -0
  13. ebi_eva_common_pyutils/logger.py +150 -0
  14. ebi_eva_common_pyutils/ncbi_utils.py +117 -0
  15. ebi_eva_common_pyutils/network_utils.py +64 -0
  16. ebi_eva_common_pyutils/reference/__init__.py +2 -0
  17. ebi_eva_common_pyutils/reference/assembly.py +247 -0
  18. ebi_eva_common_pyutils/reference/sequence.py +101 -0
  19. ebi_eva_common_pyutils/spreadsheet/__init__.py +0 -0
  20. ebi_eva_common_pyutils/spreadsheet/metadata_xlsx_utils.py +15 -0
  21. ebi_eva_common_pyutils/taxonomy/__init__.py +0 -0
  22. ebi_eva_common_pyutils/taxonomy/taxonomy.py +60 -0
  23. ebi_eva_common_pyutils/variation/__init__.py +0 -0
  24. ebi_eva_common_pyutils/variation/contig_utils.py +113 -0
  25. ebi_eva_common_pyutils-0.6.17.data/scripts/archive_directory.py +114 -0
  26. ebi_eva_common_pyutils-0.6.17.dist-info/LICENSE +201 -0
  27. ebi_eva_common_pyutils-0.6.17.dist-info/METADATA +24 -0
  28. ebi_eva_common_pyutils-0.6.17.dist-info/RECORD +41 -0
  29. ebi_eva_common_pyutils-0.6.17.dist-info/WHEEL +5 -0
  30. ebi_eva_common_pyutils-0.6.17.dist-info/top_level.txt +2 -0
  31. ebi_eva_internal_pyutils/__init__.py +0 -0
  32. ebi_eva_internal_pyutils/archive_directory.py +114 -0
  33. ebi_eva_internal_pyutils/config_utils.py +188 -0
  34. ebi_eva_internal_pyutils/metadata_utils.py +288 -0
  35. ebi_eva_internal_pyutils/mongo_utils.py +71 -0
  36. ebi_eva_internal_pyutils/mongodb/__init__.py +3 -0
  37. ebi_eva_internal_pyutils/mongodb/mongo_database.py +170 -0
  38. ebi_eva_internal_pyutils/nextflow/__init__.py +1 -0
  39. ebi_eva_internal_pyutils/nextflow/nextflow_pipeline.py +195 -0
  40. ebi_eva_internal_pyutils/pg_utils.py +107 -0
  41. ebi_eva_internal_pyutils/spring_properties.py +294 -0
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,24 @@
1
+ Metadata-Version: 2.1
2
+ Name: ebi-eva-common-pyutils
3
+ Version: 0.6.17
4
+ Summary: EBI EVA - Common Python Utilities
5
+ Home-page: https://github.com/EBIVariation/eva-common-pyutils
6
+ License: Apache
7
+ Keywords: EBI,EVA,PYTHON,UTILITIES
8
+ Classifier: Development Status :: 5 - Production/Stable
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: Topic :: Software Development :: Build Tools
11
+ Classifier: License :: OSI Approved :: Apache Software License
12
+ Classifier: Programming Language :: Python :: 3
13
+ License-File: LICENSE
14
+ Requires-Dist: requests (==2.*)
15
+ Requires-Dist: lxml (==4.*,>4.9)
16
+ Requires-Dist: pyyaml (==6.*)
17
+ Requires-Dist: cached-property (==1.5.*)
18
+ Requires-Dist: retry (==0.*,>0.9)
19
+ Requires-Dist: openpyxl (==3.*)
20
+ Provides-Extra: eva-internal
21
+ Requires-Dist: psycopg2-binary ; extra == 'eva-internal'
22
+ Requires-Dist: pymongo ; extra == 'eva-internal'
23
+ Requires-Dist: networkx (<=2.5) ; extra == 'eva-internal'
24
+
@@ -0,0 +1,41 @@
1
+ ebi_eva_common_pyutils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ ebi_eva_common_pyutils/assembly_utils.py,sha256=CklyCGlCjlFp0e9pugg6kSsh5L0xfCe2qPvA2eLVtn0,4187
3
+ ebi_eva_common_pyutils/biosamples_communicators.py,sha256=nyO0E_YpcAeBwbqOfbqe9RDxONq7Oe3NijSTGw7D01I,7815
4
+ ebi_eva_common_pyutils/command_utils.py,sha256=PtelWWqcC0eOwIVesjwBw3F9KaXRzEE_uAUJhQFZ4l8,2340
5
+ ebi_eva_common_pyutils/common_utils.py,sha256=ty_glvfRa3VGhnpAht4qtVkNNmv-IYfVtO958mY-BaA,1192
6
+ ebi_eva_common_pyutils/config.py,sha256=PtD2SgHf96kk21OA9tVIjEgsDXEFuAU-INy_kfQdoPw,4828
7
+ ebi_eva_common_pyutils/ena_utils.py,sha256=S2MmnWQ_9MJjlkaQY_by1-GGbTyi8SKp8XRcpjWnpZs,1465
8
+ ebi_eva_common_pyutils/file_utils.py,sha256=eIlQKSVKkEjMNX7emrDzaQyQdGvQdb64gnfEhb6uYsE,1375
9
+ ebi_eva_common_pyutils/logger.py,sha256=hT20ktN_oGeB_5ofVfd_aGXB6xYOe0Y5b3EVRYCuFb8,5093
10
+ ebi_eva_common_pyutils/ncbi_utils.py,sha256=AKuNKv4ue7l-6rwuDDGubWEHGuscXWfv-Gg6o_x1hT0,5005
11
+ ebi_eva_common_pyutils/network_utils.py,sha256=iJjs5PPzT1V4CceZnCHOTs711AmpwlDo59wb1XHdTak,2648
12
+ ebi_eva_common_pyutils/assembly/__init__.py,sha256=KSWPwBY5nZj00odxWFntk8Sqg_rw273xH8S5D6Jo-T4,67
13
+ ebi_eva_common_pyutils/assembly/assembly.py,sha256=IEmleROX4ZchPyhINKCuMmET_Ih1Jg4ok-opAKY6Z9A,3142
14
+ ebi_eva_common_pyutils/contig_alias/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
15
+ ebi_eva_common_pyutils/contig_alias/contig_alias.py,sha256=7tQXljEJtRCMs_JPrglYvTEZ-fJvzl8txwnCvqRB3Ck,5214
16
+ ebi_eva_common_pyutils/reference/__init__.py,sha256=NsDjGCu2H2CZLOPaEL6GCoZo5ND_EA9RFmbzGfpSXRQ,134
17
+ ebi_eva_common_pyutils/reference/assembly.py,sha256=FK4zsvg4_GDeKd9z2z5ZeM9R84TsusgQYdMOGuggQU0,12162
18
+ ebi_eva_common_pyutils/reference/sequence.py,sha256=bg96QcuB-oytQYpaUkV10OzM_RIFhxsy6t3PQOg2Gy0,3911
19
+ ebi_eva_common_pyutils/spreadsheet/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
20
+ ebi_eva_common_pyutils/spreadsheet/metadata_xlsx_utils.py,sha256=0yOoV5rfLc5IWPAShSClaAOmaGfJuGZjJOnREfZBtFQ,540
21
+ ebi_eva_common_pyutils/taxonomy/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
22
+ ebi_eva_common_pyutils/taxonomy/taxonomy.py,sha256=aXmRQ3NAaJotwmmOA2-u2XtcUT6iih-0_e-3QOxynoA,2578
23
+ ebi_eva_common_pyutils/variation/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
24
+ ebi_eva_common_pyutils/variation/contig_utils.py,sha256=kMNEW_P2yPnd8Xx1tep19hy5ee7ojxz6ZOO1grTQsRQ,5230
25
+ ebi_eva_common_pyutils-0.6.17.data/scripts/archive_directory.py,sha256=0lWJ0ju_AB2ni7lMnJXPFx6U2OdTGbe-WoQs-4BfKOM,4976
26
+ ebi_eva_internal_pyutils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
27
+ ebi_eva_internal_pyutils/archive_directory.py,sha256=IxVEfh_gaCiT652k0Q_-58fonRusy1yzXu7BCO8yVLo,4989
28
+ ebi_eva_internal_pyutils/config_utils.py,sha256=EGRC5rsmU_ug7OY9-t1UW1XZXRsauSyZB9xPcBux8ts,7909
29
+ ebi_eva_internal_pyutils/metadata_utils.py,sha256=t9PcXZdbfjDBP04GJenC4bxm2nOLd8oI_MP9eNe9IBQ,15221
30
+ ebi_eva_internal_pyutils/mongo_utils.py,sha256=YxKHtb5ygDiGLOtEiiAMFCP2ow6FL9Kq0K5R0mWNdXY,3575
31
+ ebi_eva_internal_pyutils/pg_utils.py,sha256=FUQVwiX_7F2-4sSzoaCVX2me0zAqR8nGIj6NW5d304A,4398
32
+ ebi_eva_internal_pyutils/spring_properties.py,sha256=Tn207DmZehFt7oExseNsXFAnsxr7bX9yiGl4t9mpGVA,15165
33
+ ebi_eva_internal_pyutils/mongodb/__init__.py,sha256=0oyTlkYZCV7udlPl09Zl-sDyE3c97QZMMTEFIa6uYIw,76
34
+ ebi_eva_internal_pyutils/mongodb/mongo_database.py,sha256=kesaJaaxYFeF_uYZBgL8tbufGKUXll7bXb4WlOS9vKM,9596
35
+ ebi_eva_internal_pyutils/nextflow/__init__.py,sha256=OOiJS8jZOz98q0t77NNog7aI_fFrVxi4kGmiSskuAqM,122
36
+ ebi_eva_internal_pyutils/nextflow/nextflow_pipeline.py,sha256=ew623hhK8jmFLQjJwLZbgBmW9RTiJBEULVqHfIUv_dc,10114
37
+ ebi_eva_common_pyutils-0.6.17.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
38
+ ebi_eva_common_pyutils-0.6.17.dist-info/METADATA,sha256=xdXBYa6ma2iy0NDeoYjrral5mPQnsC-F63kQetBZx5Q,909
39
+ ebi_eva_common_pyutils-0.6.17.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
40
+ ebi_eva_common_pyutils-0.6.17.dist-info/top_level.txt,sha256=sXoiqiGU8vlMQpFWDlKrekxhlusk06AhkOH3kSvDT6c,48
41
+ ebi_eva_common_pyutils-0.6.17.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.38.4)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ ebi_eva_common_pyutils
2
+ ebi_eva_internal_pyutils
File without changes
@@ -0,0 +1,114 @@
1
+ #!/usr/bin/env python
2
+ import gzip
3
+ import shutil
4
+ import tarfile
5
+ import os.path
6
+ from argparse import ArgumentParser
7
+
8
+ from ebi_eva_common_pyutils.logger import logging_config
9
+ from retry import retry
10
+
11
+ logger = logging_config.get_logger(__name__)
12
+
13
+
14
+ def make_tarfile(output_filename, source_dir):
15
+ logger.info(f'Create Final Tar file {output_filename}.')
16
+ with tarfile.open(output_filename, "w") as tar:
17
+ tar.add(source_dir, arcname=os.path.basename(source_dir))
18
+ file_stats = os.stat(output_filename)
19
+ logger.info(f'{output_filename} completed. File Size in Bytes is {file_stats.st_size}')
20
+
21
+
22
+ def is_compressed(file_path):
23
+ compressed_ext = ['.gz', '.zip', '.bz', '.tbi', '.csi']
24
+ for ext in compressed_ext:
25
+ if file_path.lower().endswith(ext):
26
+ return True
27
+ return False
28
+
29
+
30
+ @retry(tries=5, delay=3, backoff=2, logger=logger)
31
+ def retriable_compress(src_file_path, dest_file_path):
32
+ MEG = 2 ** 20
33
+ with open(src_file_path, 'rb') as f_in:
34
+ with gzip.open(dest_file_path, 'wb') as f_out:
35
+ shutil.copyfileobj(f_in, f_out, length=16 * MEG)
36
+
37
+
38
+ def matches(name, patterns):
39
+ return any((pattern for pattern in patterns if pattern in name))
40
+
41
+
42
+ @retry(tries=5, delay=3, backoff=2, logger=logger)
43
+ def retriable_remove(path):
44
+ shutil.rmtree(path)
45
+
46
+
47
+ @retry(tries=5, delay=3, backoff=2, logger=logger)
48
+ def retryable_copy(src_file_path, dest_file_path, **kwargs):
49
+ if os.path.exists(dest_file_path):
50
+ os.remove(dest_file_path)
51
+ shutil.copyfile(src_file_path, dest_file_path, **kwargs)
52
+
53
+
54
+ def archive_directory(source_dir, scratch_dir, destination_dir, filter_patterns=None):
55
+ """
56
+ Archive a directory by copying the data it contains to a scratch directory compressing all files that are not
57
+ already compressed. Then it create a tar file to the destination_dir under the name of the original directory.
58
+ """
59
+ source_dir_name = os.path.basename(source_dir)
60
+ logger.info(f'Archive {source_dir_name} from {source_dir}')
61
+ parent_source_dir = os.path.dirname(source_dir)
62
+ for base, dirs, files in os.walk(source_dir, topdown=True, followlinks=False):
63
+ # Filter the downstream directory to
64
+ filtered_dir = []
65
+ for d in dirs:
66
+ if matches(d, filter_patterns):
67
+ logger.info(f'Ignore directory {d} because of filters: {filter_patterns}')
68
+ else:
69
+ filtered_dir.append(d)
70
+ # modify dirs in place
71
+ dirs[:] = filtered_dir
72
+ src_basename = os.path.relpath(base, parent_source_dir)
73
+ scratch_dest_dir = os.path.join(scratch_dir, src_basename)
74
+ os.makedirs(scratch_dest_dir, exist_ok=True)
75
+ for fname in files:
76
+ src_file_path = os.path.join(base, fname)
77
+ dest_file_path = os.path.join(scratch_dest_dir, fname)
78
+ if matches(fname, filter_patterns):
79
+ logger.info(f'Ignore file {src_file_path} because of filters: {filter_patterns}')
80
+ continue
81
+ if os.path.islink(src_file_path) or is_compressed(src_file_path):
82
+ logger.info(f'Copy {src_file_path}')
83
+ retryable_copy(src_file_path, dest_file_path, follow_symlinks=False)
84
+ else:
85
+ logger.info(f'Compress {src_file_path}')
86
+ retriable_compress(src_file_path, dest_file_path + '.gz')
87
+ final_tar_file = os.path.join(destination_dir, source_dir_name + '.tar')
88
+ scratch_dir_archived = os.path.join(scratch_dir, source_dir_name)
89
+ make_tarfile(final_tar_file, scratch_dir_archived)
90
+ logger.info(f'Delete scratch folder {scratch_dir_archived}.')
91
+ retriable_remove(scratch_dir_archived)
92
+ logger.info(f'Scratch folder {scratch_dir_archived} deleted.')
93
+
94
+
95
+ def main():
96
+ parser = ArgumentParser(description='Archive a directory by copying the data it contains to a scratch directory '
97
+ 'compressing all files that are not already compressed. Then it create a tar '
98
+ 'file to the destination_dir under the name of the original directory.')
99
+ parser.add_argument('--source_dir', required=True, type=str,
100
+ help='base directory you want to archive. All sub directories will be included.')
101
+ parser.add_argument('--destination_dir', required=True, type=str,
102
+ help='Directory where the archive should be placed at the end of the process.')
103
+ parser.add_argument('--scratch_dir', required=True, type=str,
104
+ help='Directory where the archive will be constructed.')
105
+ parser.add_argument('--filter_patterns', type=str, nargs='*', default=[],
106
+ help='keyword found in file and directory names that not be included to the archive.')
107
+ args = parser.parse_args()
108
+
109
+ logging_config.add_stdout_handler()
110
+ archive_directory(args.source_dir, args.scratch_dir, args.destination_dir, args.filter_patterns)
111
+
112
+
113
+ if __name__ == '__main__':
114
+ main()
@@ -0,0 +1,188 @@
1
+ # Copyright 2020 EMBL - European Bioinformatics Institute
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from urllib.parse import quote_plus
16
+ from lxml import etree as et
17
+ import json
18
+ import yaml
19
+ import urllib.request
20
+
21
+ from retry import retry
22
+
23
+
24
+ class EVAPrivateSettingsXMLConfig:
25
+ config_data = None
26
+
27
+ def __init__(self, settings_xml_file: str):
28
+ with open(settings_xml_file) as xml_file_handle:
29
+ self.config_data = et.parse(xml_file_handle)
30
+
31
+ def get_value_with_xpath(self, location: str, optional: bool = False):
32
+ etree = self.config_data.getroot()
33
+ result = etree.xpath(location)
34
+ if not result and not optional:
35
+ raise ValueError("Invalid XPath location: " + location)
36
+ return result
37
+
38
+
39
+ def get_metadata_creds_for_profile(profile_name: str, settings_xml_file: str):
40
+ """
41
+ Gets host, username, and password for metadata postgres database.
42
+ Useful for filling properties files, for connection purposes you can use
43
+ `metadata_utils.get_metadata_connection_handle`.
44
+ """
45
+ properties = get_properties_from_xml_file(profile_name, settings_xml_file)
46
+ pg_url = properties['eva.evapro.jdbc.url']
47
+ pg_user = properties['eva.evapro.user']
48
+ pg_pass = properties['eva.evapro.password']
49
+ return pg_url, pg_user, pg_pass
50
+
51
+
52
+ def get_mongo_creds_for_profile(profile_name: str, settings_xml_file: str):
53
+ """
54
+ Gets host, username, and password for mongo database.
55
+ Useful for filling properties files, for connection purposes it is preferable to use
56
+ `mongo_utils.get_mongo_connection_handle` as that will handle multiple hosts appropriately.
57
+ """
58
+ properties = get_properties_from_xml_file(profile_name, settings_xml_file)
59
+ mongo_host = properties['eva.mongo.host']
60
+ mongo_user = properties['eva.mongo.user']
61
+ mongo_pass = properties['eva.mongo.passwd']
62
+ return mongo_host, mongo_user, mongo_pass
63
+
64
+
65
+ def get_accession_pg_creds_for_profile(profile_name: str, settings_xml_file: str):
66
+ """
67
+ Gets host, username, and password for accessioning job tracker database.
68
+ Useful for filling properties files.
69
+ """
70
+ properties = get_properties_from_xml_file(profile_name, settings_xml_file)
71
+ pg_url = properties['eva.accession.jdbc.url']
72
+ pg_user = properties['eva.accession.user']
73
+ pg_pass = properties['eva.accession.password']
74
+ return pg_url, pg_user, pg_pass
75
+
76
+
77
+ def get_variant_load_job_tracker_creds_for_profile(profile_name: str, settings_xml_file: str):
78
+ """
79
+ Gets host, username, and password for variant load job tracker database.
80
+ Useful for filling properties files.
81
+ """
82
+ properties = get_properties_from_xml_file(profile_name, settings_xml_file)
83
+ variant_url = properties['eva.variant.jdbc.url']
84
+ variant_user = properties['eva.variant.user']
85
+ variant_pass = properties['eva.variant.password']
86
+ return variant_url, variant_user, variant_pass
87
+
88
+
89
+ def get_contig_alias_db_creds_for_profile(profile_name: str, settings_xml_file: str):
90
+ """
91
+ Gets url, username, and password for contig alias database.
92
+
93
+ """
94
+ properties = get_properties_from_xml_file(profile_name, settings_xml_file)
95
+ contig_alias_url = properties['contig-alias.url']
96
+ contig_alias_user = properties['contig-alias.admin-user']
97
+ contig_alias_pass = properties['contig-alias.admin-password']
98
+
99
+ return contig_alias_url, contig_alias_user, contig_alias_pass
100
+
101
+
102
+ def get_count_service_creds_for_profile(profile_name: str, settings_xml_file: str):
103
+ """
104
+ Gets host, username, and password for eva count service.
105
+ Useful for filling properties files.
106
+ """
107
+ properties = get_properties_from_xml_file(profile_name, settings_xml_file)
108
+ counts_url = properties['eva.count-stats.url']
109
+ counts_user = properties['eva.count-stats.username']
110
+ counts_pass = properties['eva.count-stats.password']
111
+ return counts_url, counts_user, counts_pass
112
+
113
+
114
+ def get_pg_uri_for_accession_profile(profile_name: str, settings_xml_file: str):
115
+ return get_pg_uri_details_for_profile(profile_name, settings_xml_file, "eva.accession.jdbc.url")
116
+
117
+
118
+ def get_pg_uri_for_variant_profile(profile_name: str, settings_xml_file: str):
119
+ return get_pg_uri_details_for_profile(profile_name, settings_xml_file, "eva.variant.jdbc.url")
120
+
121
+
122
+ def get_pg_metadata_uri_for_eva_profile(profile_name: str, settings_xml_file: str):
123
+ return get_pg_uri_details_for_profile(profile_name, settings_xml_file, "eva.evapro.jdbc.url")
124
+
125
+
126
+ def get_pg_uri_details_for_profile(eva_profile_name: str, settings_xml_file: str, tag_name: str):
127
+ config = EVAPrivateSettingsXMLConfig(settings_xml_file)
128
+ xpath_location_template = '//settings/profiles/profile/id[text()="{0}"]/../properties/{1}/text()'
129
+ # Format is jdbc:postgresql://host:port/db
130
+ metadata_db_jdbc_url = config.get_value_with_xpath(
131
+ xpath_location_template.format(eva_profile_name, tag_name))[0]
132
+ return metadata_db_jdbc_url.split("jdbc:")[-1]
133
+
134
+
135
+ def get_mongo_uri_for_eva_profile(eva_profile_name: str, settings_xml_file: str):
136
+ config = EVAPrivateSettingsXMLConfig(settings_xml_file)
137
+ xpath_location_template = '//settings/profiles/profile/id[text()="{0}"]/../properties/{1}/text()'
138
+ # Format is host1:port1,host2:port2
139
+ mongo_hosts_and_ports = config.get_value_with_xpath(
140
+ xpath_location_template.format(eva_profile_name, "eva.mongo.host"))[0]
141
+ username = config.get_value_with_xpath(
142
+ xpath_location_template.format(eva_profile_name, "eva.mongo.user"), optional=True)
143
+ if not username: # no authentication
144
+ return f"mongodb://{mongo_hosts_and_ports}"
145
+ username = username[0]
146
+ password = config.get_value_with_xpath(
147
+ xpath_location_template.format(eva_profile_name, "eva.mongo.passwd"))[0]
148
+ authentication_db = config.get_value_with_xpath(
149
+ xpath_location_template.format(eva_profile_name, "eva.mongo.auth.db"))[0]
150
+ return "mongodb://{0}:{1}@{2}/{3}".format(username, quote_plus(password), mongo_hosts_and_ports, authentication_db)
151
+
152
+
153
+ def get_properties_from_xml_file(profile, xml_path):
154
+ tree = et.parse(xml_path)
155
+ root = tree.getroot()
156
+ return get_profile_properties(profile, root)
157
+
158
+
159
+ def get_properties_from_xml_string(profile, str):
160
+ root = et.fromstring(str)
161
+ return get_profile_properties(profile, root)
162
+
163
+
164
+ def get_profile_properties(profile, root):
165
+ properties = {}
166
+ for property in root.xpath('//settings/profiles/profile/id[text()="' + profile + '"]/../properties/*'):
167
+ properties[property.tag] = property.text
168
+ return properties
169
+
170
+
171
+ @retry(tries=4, delay=2, backoff=1.2, jitter=(1, 3))
172
+ def get_eva_settings_xml_string(token):
173
+ url = 'https://api.github.com/repos/EBIvariation/configuration/contents/eva-maven-settings.xml'
174
+ headers = {'Authorization': 'token ' + token, 'Accept' : 'application/vnd.github.raw' }
175
+ request = urllib.request.Request(url, None, headers)
176
+ with urllib.request.urlopen(request) as response:
177
+ return response.read()
178
+
179
+
180
+ def get_args_from_private_config_file(private_config_file):
181
+ with open(private_config_file) as private_config_file_handle:
182
+ if 'json' in private_config_file:
183
+ return json.load(private_config_file_handle)
184
+ else:
185
+ if 'yml' in private_config_file:
186
+ return yaml.safe_load(private_config_file_handle)
187
+ else:
188
+ raise TypeError('Configuration file should be either json or yaml')