Mercurial > repos > earlhaminst > export_to_cluster
comparison export_to_cluster.py @ 0:0dbe46f58158 draft
planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/export_to_cluster/ commit e80af91bced56efdb4fbf62ac03232655a22f25d-dirty
author | earlhaminst |
---|---|
date | Thu, 11 Aug 2016 15:27:30 -0400 |
parents | |
children | a9d1991c90e3 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:0dbe46f58158 |
---|---|
1 #!/usr/bin/env python | |
2 from __future__ import print_function | |
3 | |
4 import optparse | |
5 import os | |
6 import os.path | |
7 import re | |
8 import shutil | |
9 import sys | |
10 | |
11 parser = optparse.OptionParser() | |
12 parser.add_option('-d', '--export_dir', help='Directory where to export the datasets') | |
13 parser.add_option('-p', '--dir_prefix', help='How the export dir should start') | |
14 (options, args) = parser.parse_args() | |
15 if not options.export_dir: | |
16 parser.error('Export directory cannot be empty') | |
17 if not options.dir_prefix: | |
18 parser.error('Directory prefix cannot be empty') | |
19 if len(args) < 2: | |
20 parser.error('Require at least two arguments') | |
21 if len(args) % 2 != 0: | |
22 parser.error('Require an even number of arguments') | |
23 | |
24 real_export_dir = os.path.realpath(options.export_dir) | |
25 dir_prefix = options.dir_prefix.rstrip(os.sep) | |
26 if not real_export_dir.startswith(dir_prefix): | |
27 sys.exit("%s must be a subdirectory of %s" % (options.export_dir, dir_prefix)) | |
28 if not os.path.exists(real_export_dir): | |
29 sys.exit("%s does not exist or it is not accessible by the Galaxy user" % options.export_dir) | |
30 if not os.path.isdir(real_export_dir): | |
31 sys.exit("%s is not a directory" % options.export_dir) | |
32 | |
33 dataset_paths = args[::3] | |
34 dataset_names = args[1::3] | |
35 dataset_exts = args[2::3] | |
36 for dp, dn, de in zip(dataset_paths, dataset_names, dataset_exts): | |
37 """ | |
38 Copied from get_valid_filename from django | |
39 https://github.com/django/django/blob/master/django/utils/text.py | |
40 """ | |
41 dn_de = "%s.%s" % (dn, de) | |
42 dn_de_safe = re.sub(r'(?u)[^-\w.]', '', dn_de.strip().replace(' ', '_')) | |
43 dest = os.path.join(real_export_dir, dn_de_safe) | |
44 try: | |
45 shutil.copy2(dp, dest) | |
46 print("Dataset '%s' copied to '%s'" % (dn, dest)) | |
47 except Exception as e: | |
48 msg = "Error copying dataset '%s' to '%s', %s" % (dn, dest, e) | |
49 print(msg) | |
50 sys.stderr.write(msg) |