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

[((66, 91), 'os.path.dirname', 'os.path.dirname', (['__file__'], {}), '(__file__)\n', (81, 91), False, 'import os\n'), ((200, 220), 'os.path.isfile', 'os.path.isfile', (['path'], {}), '(path)\n', (214, 220), False, 'import os\n'), ((3502, 3523), 'subprocess.call', 'call', (['cmd'], {'shell': '(True)'}), '(cmd, shell=True)\n', (3506, 3523), False, 'from subprocess import call\n'), ((705, 751), 'os.path.join', 'os.path.join', (['pwd', '"""../tests/ir_asts/API.java"""'], {}), "(pwd, '../tests/ir_asts/API.java')\n", (717, 751), False, 'import os\n'), ((784, 831), 'os.path.join', 'os.path.join', (['pwd', '"""../tests/ir_asts/java.json"""'], {}), "(pwd, '../tests/ir_asts/java.json')\n", (796, 831), False, 'import os\n'), ((885, 934), 'os.path.join', 'os.path.join', (['pwd', '"""../../model/lang/Object.java"""'], {}), "(pwd, '../../model/lang/Object.java')\n", (897, 934), False, 'import os\n'), ((971, 1020), 'os.path.join', 'os.path.join', (['pwd', '"""../../model/lang/String.java"""'], {}), "(pwd, '../../model/lang/String.java')\n", (983, 1020), False, 'import os\n'), ((1057, 1106), 'os.path.join', 'os.path.join', (['pwd', '"""../../model/lang/Number.java"""'], {}), "(pwd, '../../model/lang/Number.java')\n", (1069, 1106), False, 'import os\n'), ((1143, 1193), 'os.path.join', 'os.path.join', (['pwd', '"""../../model/lang/Integer.java"""'], {}), "(pwd, '../../model/lang/Integer.java')\n", (1155, 1193), False, 'import os\n'), ((1231, 1283), 'os.path.join', 'os.path.join', (['pwd', '"""../../model/lang/Character.java"""'], {}), "(pwd, '../../model/lang/Character.java')\n", (1243, 1283), False, 'import os\n'), ((1321, 1372), 'os.path.join', 'os.path.join', (['pwd', '"""../../model/lang/Iterable.java"""'], {}), "(pwd, '../../model/lang/Iterable.java')\n", (1333, 1372), False, 'import os\n'), ((1410, 1461), 'os.path.join', 'os.path.join', (['pwd', '"""../../model/util/Iterator.java"""'], {}), "(pwd, '../../model/util/Iterator.java')\n", (1422, 1461), False, 'import os\n'), ((1498, 1547), 'os.path.join', 'os.path.join', (['pwd', '"""../../model/util/Arrays.java"""'], {}), "(pwd, '../../model/util/Arrays.java')\n", (1510, 1547), False, 'import os\n'), ((1585, 1632), 'os.path.join', 'os.path.join', (['pwd', '"""../../model/util/List.java"""'], {}), "(pwd, '../../model/util/List.java')\n", (1597, 1632), False, 'import os\n'), ((1671, 1723), 'os.path.join', 'os.path.join', (['pwd', '"""../../model/util/ArrayList.java"""'], {}), "(pwd, '../../model/util/ArrayList.java')\n", (1683, 1723), False, 'import os\n'), ((1762, 1815), 'os.path.join', 'os.path.join', (['pwd', '"""../../model/util/LinkedList.java"""'], {}), "(pwd, '../../model/util/LinkedList.java')\n", (1774, 1815), False, 'import os\n'), ((1853, 1903), 'os.path.join', 'os.path.join', (['pwd', '"""../../model/util/HashMap.java"""'], {}), "(pwd, '../../model/util/HashMap.java')\n", (1865, 1903), False, 'import os\n'), ((1941, 1991), 'os.path.join', 'os.path.join', (['pwd', '"""../../model/util/HashSet.java"""'], {}), "(pwd, '../../model/util/HashSet.java')\n", (1953, 1991), False, 'import os\n'), ((234, 255), 'os.path.abspath', 'os.path.abspath', (['path'], {}), '(path)\n', (249, 255), False, 'import os\n')]