import os
from subprocess import call

from . import glob2

pwd = os.path.dirname(__file__)

def get_files_from_path(path, ext):
    # use set to remove duplicate files. weird...but it happens
    if os.path.isfile(path): return set([os.path.abspath(path)])
    else:  # i.e., folder
        files = glob2.glob(os.path.abspath(os.path.join(path, "**/*.{}".format(ext))))
        return set(sorted(files))  # to guarantee the order of files read

"""
handling javajskparser AST
"""
def toAST(files, ext, add_libs):
    prg_files = []
    for f in files:
        prg_files.extend(get_files_from_path(f, "java"))
    if not prg_files: exit('jskparser.util: File(s) not found!')
    java_in = os.path.abspath(os.path.join(pwd, '../tests/ir_asts/API.java'))
    json_out = os.path.abspath(os.path.join(pwd, '../tests/ir_asts/java.json'))
    if add_libs:
        obj_path = os.path.abspath(os.path.join(pwd, '../../model/lang/Object.java'))
        str_path = os.path.abspath(os.path.join(pwd, '../../model/lang/String.java'))
        num_path = os.path.abspath(os.path.join(pwd, '../../model/lang/Number.java'))
        int_path = os.path.abspath(os.path.join(pwd, '../../model/lang/Integer.java'))
        char_path = os.path.abspath(os.path.join(pwd, '../../model/lang/Character.java'))
        itbl_path = os.path.abspath(os.path.join(pwd, '../../model/lang/Iterable.java'))
        iter_path = os.path.abspath(os.path.join(pwd, '../../model/util/Iterator.java'))
        arr_path = os.path.abspath(os.path.join(pwd, '../../model/util/Arrays.java'))
        list_path = os.path.abspath(os.path.join(pwd, '../../model/util/List.java'))
        alist_path = os.path.abspath(os.path.join(pwd, '../../model/util/ArrayList.java'))
        llist_path = os.path.abspath(os.path.join(pwd, '../../model/util/LinkedList.java'))
        hmap_path = os.path.abspath(os.path.join(pwd, '../../model/util/HashMap.java'))
        hset_path = os.path.abspath(os.path.join(pwd, '../../model/util/HashSet.java'))
        if obj_path not in prg_files: prg_files.append(obj_path)
        if str_path not in prg_files: prg_files.append(str_path)
        if num_path not in prg_files: prg_files.append(num_path)
        if int_path not in prg_files: prg_files.append(int_path)
        if char_path not in prg_files: prg_files.append(char_path)
        if itbl_path not in prg_files: prg_files.append(itbl_path)
        if iter_path not in prg_files: prg_files.append(iter_path)
        if arr_path not in prg_files: prg_files.append(arr_path)
        if list_path not in prg_files: prg_files.append(list_path)
        if alist_path not in prg_files: prg_files.append(alist_path)
        if llist_path not in prg_files: prg_files.append(llist_path)
        if hmap_path not in prg_files: prg_files.append(hmap_path)
        if hset_path not in prg_files: prg_files.append(hset_path)
    api = ""
    for fname in prg_files:
        with open(fname, 'r') as fd:
            api += fd.read()
    with open(java_in, 'w') as fd:
        fd.write(api)
    # this classpath stuff seems awful. Jsonify is hardcoded, passing a
    # single string to subprocess.call is platform dependant, and shell=True
    # can be a security vulnerability (if allowed to take user input).
    # This just got a whole lot nastier
    cmd = 'cd ' + pwd + '/..; /usr/bin/java -cp .:javaparser/javaparser-core/target/classes:$HOME/.m2/repository/com/cedarsoftware/json-io/4.3.0/json-io-4.3.0.jar jskparser.Jsonify ' + java_in + ' ' + json_out
    ret = call(cmd, shell=True)
    if ret != 0: exit('Problem parsing.')
    return json_out

[((6, 6, 6, 31), 'os.path.dirname', 'os.path.dirname', ({(6, 22, 6, 30): '__file__'}, {}), '(__file__)', False, 'import os\n'), ((10, 7, 10, 27), 'os.path.isfile', 'os.path.isfile', ({(10, 22, 10, 26): 'path'}, {}), '(path)', False, 'import os\n'), ((63, 10, 63, 31), 'subprocess.call', 'call', (), '', False, 'from subprocess import call\n'), ((23, 30, 23, 76), 'os.path.join', 'os.path.join', ({(23, 43, 23, 46): 'pwd', (23, 48, 23, 75): '"""../tests/ir_asts/API.java"""'}, {}), "(pwd, '../tests/ir_asts/API.java')", False, 'import os\n'), ((24, 31, 24, 78), 'os.path.join', 'os.path.join', ({(24, 44, 24, 47): 'pwd', (24, 49, 24, 77): '"""../tests/ir_asts/java.json"""'}, {}), "(pwd, '../tests/ir_asts/java.json')", False, 'import os\n'), ((26, 35, 26, 84), 'os.path.join', 'os.path.join', ({(26, 48, 26, 51): 'pwd', (26, 53, 26, 83): '"""../../model/lang/Object.java"""'}, {}), "(pwd, '../../model/lang/Object.java')", False, 'import os\n'), ((27, 35, 27, 84), 'os.path.join', 'os.path.join', ({(27, 48, 27, 51): 'pwd', (27, 53, 27, 83): '"""../../model/lang/String.java"""'}, {}), "(pwd, '../../model/lang/String.java')", False, 'import os\n'), ((28, 35, 28, 84), 'os.path.join', 'os.path.join', ({(28, 48, 28, 51): 'pwd', (28, 53, 28, 83): '"""../../model/lang/Number.java"""'}, {}), "(pwd, '../../model/lang/Number.java')", False, 'import os\n'), ((29, 35, 29, 85), 'os.path.join', 'os.path.join', ({(29, 48, 29, 51): 'pwd', (29, 53, 29, 84): '"""../../model/lang/Integer.java"""'}, {}), "(pwd, '../../model/lang/Integer.java')", False, 'import os\n'), ((30, 36, 30, 88), 'os.path.join', 'os.path.join', ({(30, 49, 30, 52): 'pwd', (30, 54, 30, 87): '"""../../model/lang/Character.java"""'}, {}), "(pwd, '../../model/lang/Character.java')", False, 'import os\n'), ((31, 36, 31, 87), 'os.path.join', 'os.path.join', ({(31, 49, 31, 52): 'pwd', (31, 54, 31, 86): '"""../../model/lang/Iterable.java"""'}, {}), "(pwd, '../../model/lang/Iterable.java')", False, 'import os\n'), ((32, 36, 32, 87), 'os.path.join', 'os.path.join', ({(32, 49, 32, 52): 'pwd', (32, 54, 32, 86): '"""../../model/util/Iterator.java"""'}, {}), "(pwd, '../../model/util/Iterator.java')", False, 'import os\n'), ((33, 35, 33, 84), 'os.path.join', 'os.path.join', ({(33, 48, 33, 51): 'pwd', (33, 53, 33, 83): '"""../../model/util/Arrays.java"""'}, {}), "(pwd, '../../model/util/Arrays.java')", False, 'import os\n'), ((34, 36, 34, 83), 'os.path.join', 'os.path.join', ({(34, 49, 34, 52): 'pwd', (34, 54, 34, 82): '"""../../model/util/List.java"""'}, {}), "(pwd, '../../model/util/List.java')", False, 'import os\n'), ((35, 37, 35, 89), 'os.path.join', 'os.path.join', ({(35, 50, 35, 53): 'pwd', (35, 55, 35, 88): '"""../../model/util/ArrayList.java"""'}, {}), "(pwd, '../../model/util/ArrayList.java')", False, 'import os\n'), ((36, 37, 36, 90), 'os.path.join', 'os.path.join', ({(36, 50, 36, 53): 'pwd', (36, 55, 36, 89): '"""../../model/util/LinkedList.java"""'}, {}), "(pwd, '../../model/util/LinkedList.java')", False, 'import os\n'), ((37, 36, 37, 86), 'os.path.join', 'os.path.join', ({(37, 49, 37, 52): 'pwd', (37, 54, 37, 85): '"""../../model/util/HashMap.java"""'}, {}), "(pwd, '../../model/util/HashMap.java')", False, 'import os\n'), ((38, 36, 38, 86), 'os.path.join', 'os.path.join', ({(38, 49, 38, 52): 'pwd', (38, 54, 38, 85): '"""../../model/util/HashSet.java"""'}, {}), "(pwd, '../../model/util/HashSet.java')", False, 'import os\n'), ((10, 41, 10, 62), 'os.path.abspath', 'os.path.abspath', ({(10, 57, 10, 61): 'path'}, {}), '(path)', False, 'import os\n')]