# Path: mobsf/MobSF/utils.py
# def filename_from_path(path):
#     head, tail = ntpath.split(path)
#     return tail or ntpath.basename(head)
# 
# Path: mobsf/StaticAnalyzer/views/common/shared_func.py
# def url_n_email_extract(dat, relative_path):
#     """Extract URLs and Emails from Source Code."""
#     urls = []
#     emails = []
#     urllist = []
#     url_n_file = []
#     email_n_file = []
#     # URLs Extraction My Custom regex
#     pattern = re.compile(
#         (
#             r'((?:https?://|s?ftps?://|'
#             r'file://|javascript:|data:|www\d{0,3}[.])'
#             r'[\w().=/;,#:@?&~*+!$%\'{}-]+)'
#         ),
#         re.UNICODE)
#     urllist = re.findall(pattern, dat)
#     uflag = 0
#     for url in urllist:
#         if url not in urls:
#             urls.append(url)
#             uflag = 1
#     if uflag == 1:
#         url_n_file.append(
#             {'urls': urls, 'path': escape(relative_path)})
# 
#     # Email Extraction Regex
#     regex = re.compile(r'[\w.-]{1,20}@[\w-]{1,20}\.[\w]{2,10}')
#     eflag = 0
#     for email in regex.findall(dat.lower()):
#         if (email not in emails) and (not email.startswith('//')):
#             emails.append(email)
#             eflag = 1
#     if eflag == 1:
#         email_n_file.append(
#             {'emails': emails, 'path': escape(relative_path)})
#     return urllist, url_n_file, email_n_file


import logging
from pathlib import Path
from django.conf import settings
from mobsf.MobSF.utils import filename_from_path
from mobsf.StaticAnalyzer.views.common.shared_func import (
    url_n_email_extract,
)
from mobsf.StaticAnalyzer.views.sast_engine import (
    niap_scan,
    scan,
)


logger = logging.getLogger(__name__)


def code_analysis(app_dir, typ, manifest_file):
    """Perform the code analysis."""
    try:
        root = Path(settings.BASE_DIR) / 'StaticAnalyzer' / 'views'
        code_rules = root / 'android' / 'rules' / 'android_rules.yaml'
        api_rules = root / 'android' / 'rules' / 'android_apis.yaml'
        niap_rules = root / 'android' / 'rules' / 'android_niap.yaml'
        code_findings = {}
        api_findings = {}
        email_n_file = []
        url_n_file = []
        url_list = []
        app_dir = Path(app_dir)
        if typ == 'apk':
            src = app_dir / 'java_source'
        elif typ == 'studio':
            src = app_dir / 'app' / 'src' / 'main' / 'java'
            kt = app_dir / 'app' / 'src' / 'main' / 'kotlin'
            if not src.exists() and kt.exists():
                src = kt
        elif typ == 'eclipse':
            src = app_dir / 'src'
        src = src.as_posix() + '/'
        skp = settings.SKIP_CLASS_PATH
        logger.info('Code Analysis Started on - %s',

# Path: mobsf/MobSF/utils.py
# def filename_from_path(path):
#     head, tail = ntpath.split(path)
#     return tail or ntpath.basename(head)
# 
# Path: mobsf/StaticAnalyzer/views/common/shared_func.py
# def url_n_email_extract(dat, relative_path):
#     """Extract URLs and Emails from Source Code."""
#     urls = []
#     emails = []
#     urllist = []
#     url_n_file = []
#     email_n_file = []
#     # URLs Extraction My Custom regex
#     pattern = re.compile(
#         (
#             r'((?:https?://|s?ftps?://|'
#             r'file://|javascript:|data:|www\d{0,3}[.])'
#             r'[\w().=/;,#:@?&~*+!$%\'{}-]+)'
#         ),
#         re.UNICODE)
#     urllist = re.findall(pattern, dat)
#     uflag = 0
#     for url in urllist:
#         if url not in urls:
#             urls.append(url)
#             uflag = 1
#     if uflag == 1:
#         url_n_file.append(
#             {'urls': urls, 'path': escape(relative_path)})
# 
#     # Email Extraction Regex
#     regex = re.compile(r'[\w.-]{1,20}@[\w-]{1,20}\.[\w]{2,10}')
#     eflag = 0
#     for email in regex.findall(dat.lower()):
#         if (email not in emails) and (not email.startswith('//')):
#             emails.append(email)
#             eflag = 1
#     if eflag == 1:
#         email_n_file.append(
#             {'emails': emails, 'path': escape(relative_path)})
#     return urllist, url_n_file, email_n_file


# Path: mobsf/StaticAnalyzer/views/android/code_analysis.py
import logging
from pathlib import Path
from django.conf import settings
from mobsf.MobSF.utils import filename_from_path
from mobsf.StaticAnalyzer.views.common.shared_func import (
    url_n_email_extract,
)
from mobsf.StaticAnalyzer.views.sast_engine import (
    niap_scan,
    scan,
)



logger = logging.getLogger(__name__)


def code_analysis(app_dir, typ, manifest_file):
    """Perform the code analysis."""
    try:
        root = Path(settings.BASE_DIR) / 'StaticAnalyzer' / 'views'
        code_rules = root / 'android' / 'rules' / 'android_rules.yaml'
        api_rules = root / 'android' / 'rules' / 'android_apis.yaml'
        niap_rules = root / 'android' / 'rules' / 'android_niap.yaml'
        code_findings = {}
        api_findings = {}
        email_n_file = []
        url_n_file = []
        url_list = []
        app_dir = Path(app_dir)
        if typ == 'apk':
            src = app_dir / 'java_source'
        elif typ == 'studio':
            src = app_dir / 'app' / 'src' / 'main' / 'java'
            kt = app_dir / 'app' / 'src' / 'main' / 'kotlin'
            if not src.exists() and kt.exists():
                src = kt
        elif typ == 'eclipse':
            src = app_dir / 'src'
        src = src.as_posix() + '/'
        skp = settings.SKIP_CLASS_PATH
        logger.info('Code Analysis Started on - %s',

                    filename_from_path(src))