File: scan.py

package info (click to toggle)
biomaj3 3.1.24-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 784 kB
  • sloc: python: 4,495; sh: 359; makefile: 154
file content (48 lines) | stat: -rwxr-xr-x 1,433 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python

import os,sys
import argparse
import logging.config

from biomaj_core.utils import Utils

def main():

    parser = argparse.ArgumentParser()

    parser.add_argument('-s', '--scan', dest="directory",help="Directory to scan")
    parser.add_argument('--type', dest="ftype",help="Files type")
    parser.add_argument('--tags', dest="tags", action="append", default=[],
         help="tags, format key:value, can be repeated multiple times")

    args = parser.parse_args()

    if not os.path.exists(args.directory):
        sys.exit(1)

    res = {}
    for (path, dirs, files) in os.walk(args.directory):
        for file in files:
            filename = os.path.join(path, file)
            (file_format, mime) = Utils.detect_format(filename)
            if file_format is not None:
                file_format = file_format.replace('application/','')
            filename = filename.replace(args.directory+'/','')
            if file_format is not None:
                if file_format not in res:
                    res[file_format] = [filename]
                else:
                    res[file_format].append(filename)

    f_type = ''
    if args.ftype:
        f_type = args.ftype
    tags = ''
    if args.tags:
        tags = ','.join(args.tags)
    for fformat in res.keys():
        print('##BIOMAJ#'+fformat+'#'+f_type+'#'+tags+'#'+','.join(res[fformat]))


if __name__ == '__main__':
    main()