comparison fetch_organism_jbrowse.py @ 0:f889e757ca93 draft

planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit f745b23c84a615bf434d717c8c0e553a012f0268
author gga
date Mon, 11 Sep 2017 05:45:08 -0400
parents
children 356b43302b16
comparison
equal deleted inserted replaced
-1:000000000000 0:f889e757ca93
1 #!/usr/bin/env python
2 from __future__ import print_function
3
4 import argparse
5 import filecmp
6 import logging
7 import os
8 import subprocess
9 import sys
10 import time
11
12 from webapollo import GuessOrg, OrgOrGuess, WAAuth, WebApolloInstance
13 logging.basicConfig(level=logging.INFO)
14 log = logging.getLogger(__name__)
15
16
17 def are_dir_trees_equal(dir1, dir2):
18 """
19 Compare two directories recursively. Files in each directory are
20 assumed to be equal if their names and contents are equal.
21
22 @param dir1: First directory path
23 @param dir2: Second directory path
24
25 @return: True if the directory trees are the same and
26 there were no errors while accessing the directories or files,
27 False otherwise.
28
29 # http://stackoverflow.com/questions/4187564/recursive-dircmp-compare-two-directories-to-ensure-they-have-the-same-files-and/6681395#6681395
30 """
31
32 dirs_cmp = filecmp.dircmp(dir1, dir2)
33 if len(dirs_cmp.left_only) > 0 or len(dirs_cmp.right_only) > 0 or \
34 len(dirs_cmp.funny_files) > 0:
35 print(('LEFT', dirs_cmp.left_only))
36 print(('RIGHT', dirs_cmp.right_only))
37 print(('FUNNY', dirs_cmp.funny_files))
38 return False
39 (_, mismatch, errors) = filecmp.cmpfiles(
40 dir1, dir2, dirs_cmp.common_files, shallow=False)
41 if len(mismatch) > 0 or len(errors) > 0:
42 print(mismatch)
43 print(errors)
44 return False
45 for common_dir in dirs_cmp.common_dirs:
46 new_dir1 = os.path.join(dir1, common_dir)
47 new_dir2 = os.path.join(dir2, common_dir)
48 if not are_dir_trees_equal(new_dir1, new_dir2):
49 return False
50 return True
51
52
53 if __name__ == '__main__':
54 parser = argparse.ArgumentParser(description='Sample script to add an attribute to a feature via web services')
55 WAAuth(parser)
56 OrgOrGuess(parser)
57 parser.add_argument('target_dir', help='Target directory')
58
59 args = parser.parse_args()
60
61 wa = WebApolloInstance(args.apollo, args.username, args.password)
62 # User must have an account
63 org_cn = GuessOrg(args, wa)
64 if isinstance(org_cn, list):
65 org_cn = org_cn[0]
66 org = wa.organisms.findOrganismByCn(org_cn)
67
68 if not os.path.exists(args.target_dir):
69 os.makedirs(args.target_dir)
70
71 if not os.path.exists(os.path.join(org['directory'], 'seq')):
72 sys.stderr.write("Missing seq directory BEFORE copy")
73 sys.exit(1)
74
75 cmd = [
76 'rsync', '-avr',
77 org['directory'].rstrip('/') + '/',
78 os.path.join(args.target_dir, 'data', '')
79 ]
80 # We run this OBSESSIVELY because my org had a hiccup where the origin
81 # (silent) cp -R failed at one point. This caused MANY HEADACHES.
82 #
83 # Our response is to run this 3 times (in case the issue is temporary),
84 # with delays in between. And ensure that we have the correct number of
85 # files / folders before and after.
86 sys.stderr.write(' '.join(cmd))
87 sys.stderr.write('\n')
88 sys.stderr.write(subprocess.check_output(cmd))
89 if not are_dir_trees_equal(
90 os.path.join(org['directory'].rstrip('/')),
91 os.path.join(args.target_dir, 'data')
92 ):
93 # Not good
94 time.sleep(5)
95 sys.stderr.write('\n')
96 sys.stderr.write(' '.join(cmd))
97 sys.stderr.write('\n')
98 sys.stderr.write(subprocess.check_output(cmd))
99 if not are_dir_trees_equal(
100 os.path.join(org['directory'].rstrip('/'), 'data'),
101 os.path.join(args.target_dir, 'data')
102 ):
103 time.sleep(5)
104 sys.stderr.write('\n')
105 sys.stderr.write(' '.join(cmd))
106 sys.stderr.write('\n')
107 sys.stderr.write(subprocess.check_output(cmd))
108 if not are_dir_trees_equal(
109 os.path.join(org['directory'].rstrip('/'), 'data'),
110 os.path.join(args.target_dir, 'data')
111 ):
112 sys.stderr.write('FAILED THREE TIMES TO COPY. SOMETHING IS WRONG WRONG WRONG.')
113 sys.exit(2)