def amdh(): arguments = args_parse() if arguments.adb_path: adb_path = arguments.adb_path dump_apks = False apks_dump_folder = "" if arguments.apks_dump_folder: dump_apks = True apks_dump_folder = arguments.apks_dump_folder adb_instance = ADB(adb_path) device_id = device_choice(adb_instance) adb_instance = ADB(adb_path, device_id) settings_check = None packages = [] if arguments.app_type: packages = adb_instance.list_installed_packages(arguments.app_type) report_apps = {} for package in packages: dumpsys_out = adb_instance.dumpsys(["package", package]) perm_list = adb_instance.get_req_perms_dumpsys_package(dumpsys_out) app = App(adb_instance, package, dump_apks, apks_dump_folder, perm_list) perms, dangerous_perms = app.check_apps() print("") if dangerous_perms.items(): print_warning_header("Package " + package + " have some dangerous permissions: ") for perm, desc in dangerous_perms.items(): print_warning("\t " + perm + " : ") print_warning("\t\t" + desc) report_apps[package] = { "permissions": perms, "dangerous_perms": dangerous_perms } print("") print( "************************************************************************" ) else: print_info("Package " + package + " have no dangerous permissions") print("") print( "************************************************************************" ) if arguments.H: settings_check = Settings(settings_file, adb_instance, True) else: settings_check = Settings(settings_file, adb_instance) settings_check.check()
def onBatch1(self): dfilename = askdirectory() if dfilename: pickles = [] for filename in glob.glob(os.path.join(dfilename, '*.py')): module = import_file(filename) if module: basename = os.path.basename(filename) root, ext = os.path.splitext(basename) logging.debug(root) BRANCH = Settings(root) pickles.append(BRANCH.get_filename()) for i in dir(module): if i[0] != '_': value = getattr(module, i) if isinstance(value, all_types): BRANCH.set(i, value) message = "Processed pickles:\n" + plain(pickles) + "\n\n" +\ "Note: Empty pickles was not created!" showinfo("Info", message) self.setText() self.setStatus()
def __init__(self): self.logger = logging.getLogger('raveberry') self.settings = Settings(self) self.user_manager = UserManager(self) self.lights = Lights(self) self.pad = Pad(self) self.musiq = Musiq(self)
def __init__(self): """Initialize the game, and create game resources.""" pygame.init() self.settings = Settings() self.screen = pygame.display.set_mode( (self.settings.screen_width, self.settings.screen_height)) # full screen mode # self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) # self.settings.screen_width = self.screen.get_rect().width # self.settings.screen_height = self.screen.get_rect().height pygame.display.set_caption("Alien Invasion") # Create an instance to store game statistics, and create a scoreboard. # need to create it before defining other game elements, such as the ship. self.stats = GameStats(self) self.sb = Scoreboard(self) # Ship constructor needs an instance of AlienInvasion class self.ship = Ship(self) self.bullets = pygame.sprite.Group() self.aliens = pygame.sprite.Group() self._create_fleet() # Make the Play button. self.play_button = Button(self, "Play")
def retrieve_app_settings(app_data_dir): ''' Read in the app_main settings from the file system. ''' app_settings_path = fileutils.join(app_data_dir, APP_CONFIG_FILE_NAME) if not fileutils.is_file_exists(app_settings_path): raise ItemNotFound("Settings file not found '" + app_settings_path + "'") app_settings = AppSettings(DEFAULT_DEBUG_VALUE, app_data_dir) settings = Settings(app_settings_path) app_settings.debug = settings.get_bool(APP_CONFIG_SECTION, DEBUG_CONFIG_PROPERTY, DEFAULT_DEBUG_VALUE) app_settings.serial_port = settings.get_string( APP_CONFIG_SECTION, SERIAL_PORT_CONFIG_PROPERTY, DEFAULT_SERIAL_PORT_VALUE) app_settings.log_console_level = settings.get_log_level( APP_CONFIG_SECTION, CONSOLE_LOG_LEVEL_PROPERTY, DEFAULT_LOG_LEVEL) app_settings.log_file_level = settings.get_log_level( APP_CONFIG_SECTION, FILE_LOG_LEVEL_PROPERTY, DEFAULT_LOG_LEVEL) return app_settings
def __init__(self): """Initialize the game, and create game resources.""" pygame.init() self.settings = Settings() self.screen = pygame.display.set_mode( (self.settings.screen_width, self.settings.screen_height)) # Full screen # self.screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN) # self.settings.screen_width = self.screen.get_rect().width # self.settings.screen_height = self.screen.get_rect().height pygame.display.set_caption("Alien Invasion") # Create an instance to store game statistics, and create a scoreboard. self.stats = GameStats(self) self.sb = Scoreboard(self) self.ship = Ship(self) self.bullets = pygame.sprite.Group() self.aliens = pygame.sprite.Group() self._create_fleet() self._start_event_handler() # MAke the play button self.play_button = Button(self, "Play")
def run_game(): # ---initializing--- pygame.init() ai_settings = Settings() screen = pygame.display.set_mode(( ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") # ---Make the Play button--- play_button = Button(ai_settings, screen, "Play") # ---instances loading--- stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) ship = Ship(ai_settings, screen) bullets = Group() aliens = Group() powerups = Group() gf.create_fleet(ai_settings, screen, ship, aliens) # ---start--- while True: gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets) if stats.game_active: ship.update() powerups.update() gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets, powerups) gf.check_powerup_collisions(ai_settings, screen, ship, powerups, bullets, sb, stats) gf.update_aliens(ai_settings, stats, sb, screen, ship, aliens, bullets) gf.screen_update(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button, powerups)
def __init__(self): self.playback = None self.recorder = None self.log = Logger('RPTool') self.fileManager = FileManager() self.file = '' self.alert = Alert('RPTool') self.api = TestlinkAPI() self.setting = Settings('testlink.cfg') self.testFolder = 'artifacts'
def onLoadFile(self): initialdir = os.path.expanduser("~") filename = askopenfilename(initialdir=initialdir, filetypes=[ ('Config files', '*.pickle'), ('All files', '*.*'), ]) if filename: self.s = Settings(filename=filename) self.showInfo()
def process_settings(adb_instance, device_id=""): if main_settings.harden: settings_check = Settings(SETTINGS_FILE, adb_instance, True, out=main_settings.out[device_id]) else: settings_check = Settings(SETTINGS_FILE, adb_instance, out=main_settings.out[device_id]) if main_settings.scan_settings: with open( f"{main_settings.output_dir}/{device_id}_report_settings.json", 'w') as fp: json.dump(settings_check.check(), fp, indent=4) main_settings.out["std"].print_info( "Report generated: %s_report_settings.json" % device_id)
def __init__(self, file_manager): self.fileManager = file_manager self.log = Logger('PlayBack') self.traces = [] self.hostKeys = [] self.keyboardListener = KeyboardListener(on_press=self.onPress, on_release=self.onRelease) self.stopped = False self.interval = 0 self.file = '' self.alert = Alert('RPTool - Playback') self.setting = Settings('rptool.cfg') self.verifier = Verifier()
def __init__(self): self.config = { 'username': '******', 'api_key': 'bae5852b0e4a8ac7faa16befb5216c2d', 'url': 'http://127.0.0.1/testlink/lib/api/xmlrpc/v1/xmlrpc.php', 'project_id': '0', 'plan_id': '0', 'platform_id': '0', 'build_id': '0', 'build_name': '' } os.environ["TESTLINK_API_PYTHON_SERVER_URL"] = self.config['url'] os.environ["TESTLINK_API_PYTHON_DEVKEY"] = self.config['api_key'] self.setting = Settings('testlink.cfg') try: self.tlh = testlink.TestLinkHelper() self.tls = self.tlh.connect(testlink.TestlinkAPIClient) except TLResponseError as err: print(err)
def scan(): if request.method == 'POST': if 'scan' in request.form.keys() and request.form['scan'] == 'apps_scan': device = request.form['device'] app_type = request.form['app'] dict_packages = refresh_scan(device, app_type) return render_template('scan.html', scan="apps_scan", app_type=app_type, show=request.form['show'], packages=dict_packages, device=device) if 'scan' in request.form.keys() and request.form['scan'] == 'settings_scan': device = request.form['device'] adb = ADB(ADB_PATH, device_id=device) settings = Settings(json_settings, adb) settings.check() return render_template('scan.html', scan="settings_scan", secure_result=settings.get_scan_report("secure"), global_result=settings.get_scan_report("global"), device=device) return redirect(url_for('index'))
def target(param_names, param_values, model_nr): # model_nr = pickle.load(open(nr_steps_path, "rb")) + 1 # pickle.dump(model_nr, open(nr_steps_path, "wb")) s = Settings() param_values = list(param_values) for i in range(len(param_names)): eval('s.{}'.format(param_names[i])) if isinstance(param_values[i], str): param_values[i] = '\'' + param_values[i] + '\'' expr = 's.{} = {}'.format(param_names[i], param_values[i]) exec(expr) # print('s.DROPOUT == {}'.format(s.DROPOUT)) # print('s.LEARNING_RATE == {}'.format(s.LEARNING_RATE)) # print('s.LOSS_FUNCTION == {}'.format(s.LOSS_FUNCTION)) s.MODEL_NAME = MAIN_FOLDER + str(model_nr) s.VALTEST_MODEL_NAMES = [s.MODEL_NAME] h = Helper(s) # with suppress_stdout(): # print('here1') not_model_nrs = [] if model_nr not in not_model_nrs: # print('here2') t = Train(s, h) t.train() del t metric_means, metric_sds = Test(s, h).test() else: # print('here3') s.CALC_PROBS = False metric_means, metric_sds = Test(s, h).test() s.CALC_PROBS = True return metric_means[s.MODEL_NAME]['Dice'], metric_sds[s.MODEL_NAME]['Dice']
def onLoadDefault(self): self.s = Settings() self.showInfo()
def get_grid(): s = Settings() h = Helper(s) ip = [] gt = [] an = [] nrs = np.array([ 70, 21, 95, 73, 78, 26, 38, 82, 47, 40, 66, 59, 13, 89, 71, 88, 37, 22, 84, 10, 97, 68, 65, 48, 45 ]) d = np.array([ 0.9009270902037788, 0.9104197765530493, 0.9128334854875481, 0.8607061285160114, 0.726180976928685, 0.7618735476244846, 0.8426088283800738, 0.9227242238885163, 0.9267448462842333, 0.8202146853529186, 0.9124323842524247, 0.8758631939535643, 0.8686964143471794, 0.9156216299184503, 0.9226021312080136, 0.8982460315886207, 0.9316061013262126, 0.8248859357030646, 0.8955985800466059, 0.7870071142712975, 0.6458948916498899, 0.9089561365052262, 0.9061868164772646, 0.8842184960264304, 0.8842468629005924 ]) nrs = np.flip(nrs[np.argsort(d)], 0) for nr in nrs: path = '{}input_image_{}_-1.nii'.format( h.getModelPredictPath(s.MODEL_NAME), nr) im = sitk.GetArrayFromImage(sitk.ReadImage(path)) ip.append(im) path = '{}anno_image_{}_-1.nii'.format( h.getModelPredictPath(s.MODEL_NAME), nr) im = sitk.GetArrayFromImage(sitk.ReadImage(path)) gt.append(im) path = '{}prob_thresh_image_{}_-1.nii'.format( h.getModelPredictPath(s.MODEL_NAME), nr) im = sitk.GetArrayFromImage(sitk.ReadImage(path)) an.append(im) grid_all = [] yx_size = (200, 200) for i in range(len(nrs)): # nz = np.argwhere(np.sum(gt[i], axis=(1, 2)) > 0) # nz = list(np.reshape(nz, nz.shape[:1])) # print(nz) # s = nz[int(round(len(nz) * perc))] # print(s) s = 44 filter = sitk.LabelShapeStatisticsImageFilter() filter.Execute(sitk.GetImageFromArray(gt[i][s])) center = list(reversed(filter.GetCentroid(1))) # print(np.min(n)) # print(np.max(n)) ip_rgb = grey2rgb(ip[i][s]) cropped = crop_around(ip_rgb, yx_size, center) cropped = normalize(cropped) gt_cropped = crop_around(gt[i][s], yx_size, center) an_cropped = crop_around(an[i][s], yx_size, center) gt_masked = get_mask_overlay(cropped, gt_cropped, [1, 0, 0], 0.5) gt_an_masked = get_mask_overlay(gt_masked, an_cropped, [0, 1, 0], 0.5) gt_an_masked = np.pad(gt_an_masked, ((10, 10), (10, 10), (0, 0)), mode='constant', constant_values=255) grid_all.append(gt_an_masked) # grid_all.append(crop_around(ip_rgb, yx_size, center)) # grid_all.append(crop_around(get_mask_overlay(ip_rgb, gt[i][s]), yx_size, center)) # grid_all.append(crop_around(get_mask_overlay(ip_rgb, an[i][s]), yx_size, center)) print(grid_all[-1].shape) grid_size = [5, 5] print(len(grid_all)) rows = [] for y in range(grid_size[1]): print(y) rows.append( np.concatenate(grid_all[y * grid_size[0]:y * grid_size[0] + grid_size[0]], axis=1)) img_out = np.concatenate(rows, axis=0) return img_out
@contextmanager def suppress_stdout(): with open(os.devnull, "w") as devnull: old_stdout = sys.stdout sys.stdout = devnull try: yield finally: sys.stdout = old_stdout # MAIN_FOLDER = 'la_2018_challenge_1/' MAIN_FOLDER = 'sf_grid_search_05July2018/' h = Helper(Settings()) bo_path = h.getBOPath(MAIN_FOLDER) # nr_steps_path = h.getNrStepsPath(MAIN_FOLDER) def target(param_names, param_values, model_nr): # model_nr = pickle.load(open(nr_steps_path, "rb")) + 1 # pickle.dump(model_nr, open(nr_steps_path, "wb")) s = Settings() param_values = list(param_values) for i in range(len(param_names)): eval('s.{}'.format(param_names[i])) if isinstance(param_values[i], str):
def __init__(self): self.checkPointIndex = 0 self.alert = Alert('Verifier') self.setting = Settings('testlink.cfg') self.config = self.setting.readConfig() self.api = TestlinkAPI()
def loadDataSettings(): current_dir = os.path.dirname(__file__) config_file = os.path.join(current_dir, 'app.properties') data_settings = Settings(config_file) return data_settings
if el.get_image().shape is not self.__elements[0].get_image().shape: raise Exception('Image shapes of all elements should be equal. Found {}, while expected {}.' .format(self.__elements[0].get_image().shape, el.get_image().shape)) self.__elements.append(el) def get_result(self): return self.__result def show_result(self): print(self.get_result().shape) imshow3D(self.get_result()) if __name__ == '__main__': s = Settings() h = Helper(s) # Get a mask which represents a left atrium _, _, la_path = h.getImagePaths([24], True) la_image = h.loadImages(["../data/4chamber/GlassHeartNonIsotropicCT_seg.gipl"]) imshow3D(la_image[0]) # Make left atrium object with mask image la = LeftAtrium() la.set_mask_image((la_image[0] == 2).astype(int)) la.init_generation() # Add scar and fibrosis to the image la.add_adjustment(Wall(2)) la.add_adjustment(ScarFibrosis(2))
def target(learning_rate_power, dropout, loss_function): global bo if bo != -1: pickle.dump(bo, open(bo_path, "wb")) # return (1 - (learning_rate_power - .6) ** 2) * (1 - (dropout - .2) ** 2) * (1 - (art_fraction - .2) ** 2) domains = { # 'unet_depth': (3, 5), 'learning_rate_power': (-5, -3), # 'patch_size_factor': (1, 6), 'dropout': (0, 1), # 'art_fraction': (0, 1), # 'feature_map_inc_rate': (1., 2.), 'loss_function': (0, 1) } # print(domains.keys()) hp = {} for k in domains.keys(): mx = domains[k][1] mn = domains[k][0] new_value = mn + (mx - mn) * eval(k) hp[k] = new_value print(' '.join(list(hp.keys()))) print(' '.join([str(i) for i in list(hp.values())])) # return hp['unet_depth'] * hp['learning_rate_power'] * hp['patch_size_factor'] * hp['dropout'] * \ # hp['feature_map_inc_rate'] * -1 * hp['loss_function'] s = Settings() # hp['unet_depth'] = int(round(hp['unet_depth'])) # hp['patch_size_factor'] = int(round(hp['patch_size_factor'])) loc = locals() args_name = [arg for arg in inspect.getfullargspec(target).args] model_nr = pickle.load(open(nr_steps_path, "rb")) + 1 s.MODEL_NAME = MAIN_FOLDER + str(model_nr) s.VALTEST_MODEL_NAMES = [s.MODEL_NAME] s.DROPOUT = hp['dropout'] s.LEARNING_RATE = math.pow(10, hp['learning_rate_power']) # s.ART_FRACTION = hp['art_fraction'] # s.UNET_DEPTH = hp['unet_depth'] # s.PATCH_SIZE = (1, hp['patch_size_factor'] * 64, hp['patch_size_factor'] * 64) # s.FEATURE_MAP_INC_RATE = hp['feature_map_inc_rate'] s.LOSS_FUNCTION = 'dice' if hp['loss_function'] < .5 else 'weighted_binary_cross_entropy' # s.NR_DIM = int(round(hp['nr_dim'])) # if s.NR_DIM == 3: # s.PATCH_SIZE = (3, hp['patch_size_factor'] * 32, hp['patch_size_factor'] * 32) # elif s.NR_DIM == 2: # s.PATCH_SIZE = (1, hp['patch_size_factor'] * 64, hp['patch_size_factor'] * 64) # else: # raise Exception('Wrong number of dimensions: {}'.format(s.NR_DIM)) with suppress_stdout(): h = Helper(s) Train(s, h).train() metric_means, metric_sds = Test(s, h).test() pickle.dump(model_nr, open(nr_steps_path, "wb")) return metric_means[s.MODEL_NAME]['Dice']
def amdh(): global out global adb_path if platform == "linux" or platform == "linux2": out = Out("Linux") elif platform == "darwin": out = Out("Darwin") elif platform == "win32": out = Out("Windows") arguments = args_parse() # ADB binary path if arguments.adb_path: adb_path = arguments.adb_path else: if platform == "linux" or platform == "linux2" or platform == "Darwin": if not os.path.isfile(adb_path): out.print_error( "adb not found please use '-d' to specify the path") args_parse(True) sys.exit(1) else: # Windows if not os.path.isfile(adb_windows_path): out.print_error( "adb not found please use '-d' to specify the path") sys.exit(1) # Related to APKs dump dump_apks = False apks_dump_folder = "" if arguments.apks_dump_folder: dump_apks = True apks_dump_folder = arguments.apks_dump_folder # Related to scan scan_settings = False if arguments.sS: scan_settings = True scan_applications = False if arguments.sA: scan_applications = True # Hardening param harden = False if arguments.H: harden = True list_apps = False if arguments.l: list_apps = True # Check if one of the operation are chosen if not scan_settings and not scan_applications and not dump_apks and not harden and not list_apps: out.print_error("Please choose an operation") args_parse(True) exit(1) adb_instance = ADB(adb_path) device_id = device_choice(adb_instance) adb_instance = ADB(adb_path, device_id) settings_check = None packages = [] if arguments.app_type: packages = adb_instance.list_installed_packages(arguments.app_type) report_apps = {} if scan_applications or dump_apks or list_apps: for package in packages: out.print_info(package) dumpsys_out = adb_instance.dumpsys(["package", package]) perm_list = adb_instance.get_req_perms_dumpsys_package(dumpsys_out) app = App(adb_instance, package, scan_applications, dump_apks, apks_dump_folder, perm_list) perms, dangerous_perms, is_device_owner = app.check_app() print("") if scan_applications: if dangerous_perms.items(): out.print_warning_header( "Package " + package + " has some dangerous permissions: ") for perm, desc in dangerous_perms.items(): out.print_warning("\t " + perm + " : ") out.print_warning("\t\t" + desc) report_apps[package] = { "permissions": perms, "dangerous_perms": dangerous_perms } else: out.print_info("Package " + package + " has no dangerous permissions") if is_device_owner: message = "/!\ \t" + package + " is device owner\t/!\ " padding = len(message) out.print_warning("-" * padding) out.print_warning(message) out.print_warning("-" * padding) if arguments.rar: removed, dpm = app.remove_device_admin_for_app() if removed: out.print_info("Device admin receivers for " + app.package_name + " removed\n") else: out.print_error( "An error occured while removing the device admin " + dpm + " .") # Revoke all Dangerous permissions if arguments.R and app.dangerous_perms: successed = app.revoke_dangerous_perms() if successed: out.print_info("Dangerous permissions revoked\n") else: out.print_error( "An error occured while revoking permission " + perm + " to package " + app.package_name) elif arguments.R and not app.dangerous_perms: out.print_info( "No dangerous permissions granted for this package\n") if app.malware_confidence > 0: out.print_high_warning( "----------------------------MALWARE SCAN--------------------------------" ) out.print_high_warning( "The application uses some malwares permissions ") out.print_high_warning( str(app.malware_confidence) + " malwares permissions combinations ") if app.score < 0: out.print_high_warning( "The application uses frequent malwares permissions ") print( "************************************************************************" ) time.sleep(0.5) if list_apps: print( "************************************************************************" ) out.print_info("List of installed packages: ") for package in packages: out.print_info("\t[" + str(packages.index(package) + 1) + "] " + package) print("") apps_choice = input("Select application(s) (separated by comma ','): ") apps_choice_list = apps_choice.replace(" ", "").split(",") if arguments.app_type == 'e': out.print_high_warning( "Uninstalling or disabling system Apps can break your system") action = "" while True: out.print_info("choose an action") out.print_info("\td: disable selected apps") out.print_info("\tu: uninstall selected apps") out.print_info("\ts: skip") print("") action = input("Action: ") action = action.replace(" ", "") if action == 'd' or action == 'u' or action == 's': break else: print("action " + action + " this") out.print_error("Invalid action") continue for id_app in apps_choice_list: if action == 'd': try: adb_instance.disable_app(packages[int(id_app) - 1]) out.print_success(packages[int(id_app) - 1] + " disabled") except Exception as e: out.print_error("An Error occured while disabling " + packages[int(id_app) - 1]) elif action == 'u': try: adb_instance.uninstall_app(packages[int(id_app) - 1]) out.print_success(packages[int(id_app) - 1] + " uninstalled") except Exception as e: out.print_error("An Error occured while uninstalling " + packages[int(id_app) - 1]) elif action == 's': break if harden: settings_check = Settings(settings_file, adb_instance, True, out=out) else: settings_check = Settings(settings_file, adb_instance, out=out) if scan_settings: settings_check.check()
parser.add_argument('-m', '--metrics', action="extend", nargs="+", type=str, help='Metrics to be calculated after a simulation '\ 'finishes.') parser.add_argument('-d', '--discard', action="extend", nargs="+", type=str, help='If set, discards the created .') def tui(stdscr): total_jobs = len(Simulation.instances_status) s = screen.SimulationScreen(stdscr, total_jobs) s.init() s.loop(settings.PARALLEL_INSTANCES) if __name__ == "__main__": options = parser.parse_args() settings_module = options.settings or 'settings' settings = Settings(settings_module) if options.display_stdout: setattr(settings, 'STDOUT', None) build_simulations(settings, options=options) if options.use_tui: curses.wrapper(tui) else: Simulation.dispatch_all(settings.PARALLEL_INSTANCES) Simulation.join()
class APP(QMainWindow, Window, object): DB = Database() SETTINGS = Settings() ICONS = QT_ICONS(QMessageBox.Information, QMessageBox.Question, QMessageBox.Warning, QMessageBox.Critical) SLEEP_TIME = 0.0001 #time out for status bar messages STATUS_TIMEOUT = 10000 #playlist_quality to download indexes are taken from playlist_qualities QComboBox PLAYLIST_QUALITY_INDEX = 0 #{0:"best progressive", 1:"best audio", 2:"best video", 3:"best audio+video"} VIDEO_TAB_STREAMS = [] PARENT = None HOST_DIR = os.getcwd() pl_video_yt_object = None def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.PARENT = args[0] # settings up the window form gui folder self.setupUi(self) #temporaily changing default tab self.tabWidget.setCurrentIndex(0) self._set_video_tab_to_idle() self._set_playlist_tab_to_idle() #updating settings tabs placeholders from the settings.json file self._update_settings_fields_palceholders() self._connect_functions_to_buttons() def closeEvent(self, event): reply = QMessageBox.question( self, "Close Window confirmation", "Are you sure you want to close the application?", QMessageBox.Yes | QMessageBox.No) #closing all the threads and quitting if reply == 16384: try: self.handle_cancel_clicked() except Exception: pass try: self.handle_cancel_playlist_video_clicked() except Exception: pass try: self.handle_stop_downloading_playlist() except Exception: pass event.accept() #ignoring elif reply == 65536: event.ignore() else: print("don't know what to do") def _connected_to_internet(self): try: a = requests.get("https://www.google.com") except Exception as e: a = None if a: return True return False def _check_current_api_key(self): if self._validate_api_key(self.SETTINGS.api_key) == False: self._show_alert( "Invalid YouTube API key", "Currently saved api key is invalid save a valid api key to download playlists." ) def _set_statusbar_text(self, text, timeout=STATUS_TIMEOUT): self.statusbar.showMessage(text, timeout) def _set_video_tab_to_idle(self, clear_qualities=True, disable_download=True): self.progress.setValue(0) self.currently_downloading.setText("Downloading nothing") self.pause_resume.setText("Pause") self.load_qualities.setEnabled(True) self.pause_resume.setDisabled(True) self.cancel.setDisabled(True) if disable_download: self.download_video.setDisabled(True) if clear_qualities: self.qualities.clear() def _set_playlist_tab_to_idle(self): self.currently_downloading_playlist_video.setText( "Downloading nothing") self.playlist_video_number.setText("") self.playlist_video_progress.setValue(0) self.pause_resume_playlist_video.setText("Pause") self.pause_resume_playlist_video.setDisabled(True) self.cancel_playlist_video.setDisabled(True) self.stop_downloading_playlist.setDisabled(True) self.download_playlist.setEnabled(True) self.playlist_qualities.setEnabled(True) def _connect_functions_to_buttons(self): ##################### VIDEO TAB ######################## # connecting functions video_tab self._functions_to_video_tab_buttons() #################### END VIDEO TAB ###################### #################### PLAYLIST TAB ###################### # connecting function to playlist tab self._connect_functions_to_plalist_tab() ################## END PLAYLIST TAB #################### #################### SETTINGS TAB ####################### # connecting function to settings tab self._connect_functions_to_settings_tab_buttons() ################## END SETTINGS TAB ##################### def _get_text(self, name_of_qt_widget): text = self.__getattribute__(name_of_qt_widget).text() return text def _show_alert(self, title="", message="", icon=QMessageBox.Information): alert = QMessageBox(parent=self) alert.setWindowTitle(title) alert.setText(message) alert.setIcon(icon) alert.exec() def _validate_folder(self, folder_path): """ checks if the provided folder_path exists and if this is a directory or not >>> self._validate_folder("C:\\") #if the os is windows True >>> self._validate_folder("/root") #it the os is linux False """ return os.path.exists(folder_path) and os.path.isdir(folder_path) def _handle_error_on_different_thread(self, error): logger.exception(error) try: title = error.title message = str(error) self._show_alert(title, message, self.ICONS.critical) except Exception as e: if str(error) == "'formats'": self._show_alert( "Live video Error", "\nMay be you are trying to download a live straming video which is not possible right now.", self.ICONS.critical) self.DB.insert_error( f"Live video download error", "User tried to download a live stream." + str(error)) return self._show_alert( "Error Occured", str(e) + "\nError Occured on a child Thread.\nMay be you lost internet connection.", self.ICONS.critical) self.DB.insert_error(f"_handle_error_on_different_thread", str(error)) ############################################# VIDEO TAB ############################################# def _functions_to_video_tab_buttons(self): # connecting function to download button self.download_video.clicked.connect(self.handle_download_video_clicked) # connecting function to load_qualites button self.load_qualities.clicked.connect(self.handle_load_qualities_clicked) # connecting function to pause_resume button self.pause_resume.clicked.connect(self.handle_pause_resume_clicked) # connecting function to cancel button self.cancel.clicked.connect(self.handle_cancel_clicked) def receive_streams(self, streams): self.VIDEO_TAB_STREAMS = streams self.download_video.setEnabled(True) self._set_statusbar_text("Video qualities are loaded") self.LOADER_THREAD.exit() def _load_qualities_seperate_thread(self): self.LOADER_THREAD = QThread() self.loader = Load_Qualities(self.yt_object) self.loader.moveToThread(self.LOADER_THREAD) self.loader.new_stream_loaded.connect(self.qualities.addItem) self.loader.completed.connect(self.receive_streams) self.loader.error_occured.connect( self._handle_error_on_different_thread) self.LOADER_THREAD.started.connect(self.loader.run) self.LOADER_THREAD.start() def _get_selected_stream(self): return self.VIDEO_TAB_STREAMS[self.qualities.currentIndex()] def receive_yt_object(self, yt_object, Return=False): if Return == True: return yt_object self.yt_object = yt_object self.YT_OBJECT_LOADER_THREAD.exit() def load_yt_object(self, video_url): self.YT_OBJECT_LOADER_THREAD = QThread() self.yt_loader = YT_object_Loader(video_url) self.yt_loader.moveToThread(self.YT_OBJECT_LOADER_THREAD) self.yt_loader.completed.connect(self.receive_yt_object) self.YT_OBJECT_LOADER_THREAD.started.connect(self.yt_loader.run) self.YT_OBJECT_LOADER_THREAD.start() while not self.VIDEO_TAB_STREAMS: QApplication.sendPostedEvents() QApplication.processEvents() sleep(self.SLEEP_TIME) def handle_load_qualities_clicked(self): if not self._connected_to_internet(): self._show_alert( "Connection Error", "You are not connected to Internet.\nConnect to a network and try again", self.ICONS.critical) self.load_qualities.setEnabled(True) return self._set_statusbar_text("Loading video Qualities") self.VIDEO_TAB_STREAMS.clear() self.qualities.clear() self.download_video.setDisabled(True) # del(self.yt_object) self.load_qualities.setDisabled(True) video_id_inpt = self._get_text("video_id") try: video_id = checkers.check_video_id_or_url(video_id_inpt) self.VT_VIDEO_ID = video_id #creating self.yt_object formatted_url = formatters.format_video_id_to_url(video_id) QApplication.sendPostedEvents() QApplication.processEvents() self.yt_object = self.load_playlist_yt_object( formatted_url ) #YouTube(formatted_url)#self.load_yt_object(formatted_url) if self.yt_object == formatted_url: self._show_alert( "Error", "Can't download this video. YouTube didn't respond in time." ) self._set_video_tab_to_idle() return #loading qualities to qualities combobox self._load_qualities_seperate_thread() except exceptions.Invalid_Video_Id as e: try: self._show_alert(e.title, e.message, self.ICONS.warning) except KeyError: self._handle_error_on_different_thread(e) except Exception as e: self._handle_error_on_different_thread(e) finally: self.load_qualities.setEnabled(True) def handle_pause_resume_clicked(self): if self.VIDEO_DOWNLOAD_THREAD.paused == True: self.pause_resume.setText("Pause") self.VIDEO_DOWNLOAD_THREAD.resume_download() return self.pause_resume.setText("Resume") self.VIDEO_DOWNLOAD_THREAD.pause_download() def handle_cancel_clicked(self): self._set_video_tab_to_idle() self.VIDEO_DOWNLOAD_THREAD.cancel_download() os.chdir(self.HOST_DIR) def handle_signal_of_video_cancellation(self): self._set_video_tab_to_idle() self.VIDEO_DOWNLOAD_THREAD.exit() def handle_error_while_downloading(self, error_obj): try: # traceback_object = error_obj.__traceback__ raise error_obj title = error_obj.title message = str(error_obj) self._show_alert(title, message, self.ICONS.critical) except Exception as e: logger.exception(e) print(e) self._show_alert("Error Occured While Downloading", str(e), self.ICONS.critical) def handle_download_video_clicked(self): if not self._connected_to_internet(): self._show_alert( "Connection Error", "You are not connected to internt.\nConnect to internet and try again." ) return self.load_qualities.setDisabled(True) os.makedirs(self.SETTINGS.video_dir, exist_ok=True) os.chdir(self.SETTINGS.video_dir) self.VIDEO_DOWNLOAD_THREAD = Download_Thread( self.yt_object, self._get_selected_stream(), self.VT_VIDEO_ID, parent=None) self.VIDEO_DOWNLOAD_THREAD.progress_updated.connect( self.progress.setValue) self.VIDEO_DOWNLOAD_THREAD.download_completed.connect( self.handle_completed_download) self.VIDEO_DOWNLOAD_THREAD.download_cancelled.connect( self.handle_signal_of_video_cancellation) self.VIDEO_DOWNLOAD_THREAD.error_occured.connect( self.handle_error_while_downloading) self.pause_resume.setEnabled(True) self.cancel.setEnabled(True) self.download_video.setDisabled(True) # QApplication.processEvents() self.VIDEO_DOWNLOAD_THREAD.start() self.currently_downloading.setText( f"Downloading {self.yt_object.title}") def handle_completed_download(self, file_path): self._set_statusbar_text(file_path) self.download_video.setEnabled(True) self._set_video_tab_to_idle(clear_qualities=False, disable_download=False) self.VIDEO_DOWNLOAD_THREAD.exit() os.chdir(self.HOST_DIR) ############################################# END VIDEO TAB ############################################# ############################################# PLAYLIST TAB ############################################## def load_playlist_yt_object(self, video_url): yt_obj = None try: yt_obj = YouTube(video_url) except KeyError as e: logger.exception("KeyError in load_playlist_yt_object " + str(e)) print("KeyError in load_playlist_yt_object " + str(e)) if str(e) == "'streamingData'": return video_url elif str(e): # print("recursing") yt_obj = self.load_playlist_yt_object(video_url) return yt_obj else: raise e except http.client.RemoteDisconnected as e: logger.exception( "http.client.RemoteDisconnected in load_playlist_yt_object " + str(e)) print( "http.client.RemoteDisconnected in load_playlist_yt_object " + str(e)) yt_obj = self.load_playlist_yt_object(video_url) return yt_obj # except except RecursionError: raise exceptions.Connection_Lost except pytube.exceptions.RegexMatchError as e: logger.exception(e) print(e, "recursing") yt_obj = self.load_playlist_yt_object(video_url) return yt_obj raise e except Exception as e: print(e) raise e return yt_obj def _connect_functions_to_plalist_tab(self): # connecting function to download_playlist button self.download_playlist.clicked.connect( self.handle_download_playlist_clicked) # connecting function to playlist_qualities to get the video qualities self.playlist_qualities.currentIndexChanged.connect( self.handle_playlist_qualities_changed) self.pause_resume_playlist_video.clicked.connect( self.handle_pause_resume_playlist_video_clicked) self.cancel_playlist_video.clicked.connect( self.handle_cancel_playlist_video_clicked) self.stop_downloading_playlist.clicked.connect( self.handle_stop_downloading_playlist) def _download_video_with_playlist_quality_index(self, playlist_quality_index=0): filter_obj = Stream_Filter(self.playlist_yt_object) file_name = self.playlist_yt_object.title if self.PLAYLIST_QUALITY_INDEX == 0: selected_stream = filter_obj.get_highest_quality_progressive_stream( ) file_name += " audio+video" elif self.PLAYLIST_QUALITY_INDEX == 1: selected_stream = filter_obj.get_highest_quality_audio_stream() file_name += " audio" elif self.PLAYLIST_QUALITY_INDEX == 2: selected_stream = filter_obj.get_highest_quality_video_stream() file_name += " video" else: raise ValueError("Caller should have handled this") self.currently_downloading_playlist_video.setText( self.playlist_yt_object.title) self.PLAYLIST_VIDEO_DOWNLOAD_THREAD = Download_Thread( self.playlist_yt_object, selected_stream, self.DOWNLOADING_PLAYLIST_VIDEO_ID, file_name=file_name, parent=None) self.PLAYLIST_VIDEO_DOWNLOAD_THREAD.progress_updated.connect( self.playlist_video_progress.setValue) self.PLAYLIST_VIDEO_DOWNLOAD_THREAD.download_completed.connect( self.handle_playlist_video_downloaded) self.PLAYLIST_VIDEO_DOWNLOAD_THREAD.download_cancelled.connect( self.handle_signal_of_playlist_video_cancellation) self.PLAYLIST_VIDEO_DOWNLOAD_THREAD.error_occured.connect( self.handle_error_while_downloading_playlist_video) self.pause_resume_playlist_video.setEnabled(True) self.cancel_playlist_video.setEnabled(True) self.download_playlist.setDisabled(True) # QApplication.processEvents() self.PLAYLIST_VIDEO_DOWNLOAD_THREAD.start() self.currently_downloading.setText( f"Downloading {self.playlist_yt_object.title}") def handle_error_while_downloading_playlist_video(self, error): self.DB.insert_error( f"error while downloading {self.DOWNLOADING_PLAYLIST_VIDEO_ID} playlist_video", str(error)) self.PLAYLIST_VIDEO_DOWNLOAD_THREAD.exit() self.DOWNLOADED_PLAYLIST_VIDEO_ID = self.DOWNLOADING_PLAYLIST_VIDEO_ID self.handle_error_while_downloading(error) def handle_cancel_playlist_video_clicked(self): self.PLAYLIST_VIDEO_DOWNLOAD_THREAD.cancel_download() def handle_pause_resume_playlist_video_clicked(self): if self.PLAYLIST_VIDEO_DOWNLOAD_THREAD.paused == True: self.pause_resume_playlist_video.setText("Pause") self.PLAYLIST_VIDEO_DOWNLOAD_THREAD.resume_download() return self.pause_resume_playlist_video.setText("Resume") self.PLAYLIST_VIDEO_DOWNLOAD_THREAD.pause_download() def handle_signal_of_playlist_video_cancellation(self): # self._set_playlist_tab_to_idle() self.CANCELLED_PLAYLIST_VIDEO_ID = self.DOWNLOADING_PLAYLIST_VIDEO_ID self.PLAYLIST_VIDEO_DOWNLOAD_THREAD.exit() def handle_playlist_video_downloaded(self, file_path): self._set_statusbar_text(file_path) # self._set_playlist_tab_to_idle() self.DOWNLOADED_PLAYLIST_VIDEO_ID = self.DOWNLOADING_PLAYLIST_VIDEO_ID self.PLAYLIST_VIDEO_DOWNLOAD_THREAD.exit() def handle_stop_downloading_playlist(self): self.SKIP_DOWNLOADING_PLALIST = True def handle_playlist_qualities_changed(self): self.PLAYLIST_QUALITY_INDEX = self.playlist_qualities.currentIndex() def handle_download_playlist_clicked(self): self.SKIP_DOWNLOADING_PLALIST = False if not self._connected_to_internet(): self._show_alert( "Connection Error", "You are not connected to internt.\nConnect to internet and try again." ) return playlist_id_inpt = self._get_text("playlist_id") try: playlist_id = checkers.check_playlist_id_or_url(playlist_id_inpt) except exceptions.Invalid_Playlist_Id as e: self._show_alert(e.title, e.message) return try: self.PLAYLIST_VIDEO_IDS, self.PLAYLIST_NAME = playlist_loader.get_video_ids( playlist_id, self.SETTINGS.api_key) except exceptions.API_Error as e: self._show_alert(e.title, e.message, self.ICONS.critical) return valid_directory_name_for_playlist = formatters.make_valid_dir_name( self.PLAYLIST_NAME) playlist_dir = os.path.join(self.SETTINGS.playlist_dir, valid_directory_name_for_playlist) os.makedirs(playlist_dir, exist_ok=True) os.chdir(playlist_dir) for index, video_id in enumerate(self.PLAYLIST_VIDEO_IDS): # print(video_id) self.DOWNLOADED_PLAYLIST_VIDEO_ID = None self.CANCELLED_PLAYLIST_VIDEO_ID = None self.DOWNLOADING_PLAYLIST_VIDEO_ID = video_id self.download_playlist.setDisabled(True) self.playlist_qualities.setDisabled(True) self.pause_resume_playlist_video.setText("Pause") self.pause_resume_playlist_video.setDisabled(True) self.cancel_playlist_video.setEnabled(True) self.stop_downloading_playlist.setEnabled(True) try: #creating self.playlist_yt_object formatted_url = formatters.format_video_id_to_url(video_id) # QApplication.sendPostedEvents() # QApplication.processEvents() self.playlist_yt_object = self.load_playlist_yt_object( formatted_url) # print(self.playlist_yt_object,formatted_url) # self.playlist_yt_object = YouTube(formatted_url) #self.load_yt_object(formatted_url) except exceptions.Invalid_Video_Id as e: try: self._show_alert(e.title, e.message, self.ICONS.warning) except KeyError: self._handle_error_on_different_thread(e) except Exception as e: self._handle_error_on_different_thread(e) finally: if self.playlist_yt_object == formatted_url: continue self._set_statusbar_text( f"Loaded {self.playlist_yt_object.title}") if self.PLAYLIST_QUALITY_INDEX <= 2: self._download_video_with_playlist_quality_index( self.PLAYLIST_QUALITY_INDEX) elif self.PLAYLIST_QUALITY_INDEX == 3: self._download_video_with_playlist_quality_index(1) self._download_video_with_playlist_quality_index(2) self.playlist_video_number.setText( f"Downloading {index+1}/{len(self.PLAYLIST_VIDEO_IDS)} of {self.PLAYLIST_NAME}" ) while not self.DOWNLOADED_PLAYLIST_VIDEO_ID == video_id and not self.CANCELLED_PLAYLIST_VIDEO_ID == video_id and not self.SKIP_DOWNLOADING_PLALIST: QApplication.sendPostedEvents() QApplication.processEvents() sleep(self.SLEEP_TIME) if self.SKIP_DOWNLOADING_PLALIST == True: self.DOWNLOADED_PLAYLIST_VIDEO_ID = video_id break if self.SKIP_DOWNLOADING_PLALIST == True: break if self.SKIP_DOWNLOADING_PLALIST: #stoping download of the current video self.handle_cancel_playlist_video_clicked() self.PLAYLIST_VIDEO_DOWNLOAD_THREAD.exit() self._set_playlist_tab_to_idle() if index + 1 == len(self.PLAYLIST_VIDEO_IDS): self._show_alert( "Downloaded Playlist", f"Successfully downloaded the palylist.\nSaved to: {playlist_dir}" ) # self.playlist_video_progress.setValue(0) # self.download_playlist.setEnabled(True) # self.playlist_qualities.setEnabled(True) ########################################### END PLAYLIST TAB ############################################ ############################################# SETTINGS TAB ############################################# def _connect_functions_to_settings_tab_buttons(self): # connecting function to videos_location_browse button self.videos_location_browse.clicked.connect( self.handle_videos_location_browse) # connecting function to playlists_location_browse button self.playlists_location_browse.clicked.connect( self.handle_playlists_location_browse) # connecting function to save_settigs button self.save_settings.clicked.connect(self.write_settings) self.api_key.editingFinished.connect(self.write_settings) def _update_settings_fields_palceholders(self): self.api_key.setPlaceholderText(self.SETTINGS.api_key) self.videos_location.setPlaceholderText(self.SETTINGS.video_dir) self.playlists_location.setPlaceholderText(self.SETTINGS.playlist_dir) def handle_videos_location_browse(self): location = QtWidgets.QFileDialog.getExistingDirectory( parent=self, caption="Select a folder to save downloaded videos.") self.videos_location.setText(location) def handle_playlists_location_browse(self): location = QtWidgets.QFileDialog.getExistingDirectory( parent=self, caption="Select a folder to save downloaded playlists.") self.playlists_location.setText(location) def _validate_api_key(self, api_key): params = { "key": api_key, # api_key to authenticate "part": "contentDetails", # defining the details level "playlistId": "PLnrO9iqgTHN-ub7YeZw7Aheo7e0yX5JSH" # setting the playlist id } if len(api_key) == 39: if not self._connected_to_internet(): self._show_alert( "Connection Error", "Can't validate saved youtube api key.\nYou aren't connected to Internet." ) return 404 response = requests.get(YOUTUBE_API_URL, params=params) if response.status_code >= 400: return 404 #connection error if response.status_code <= 399: return True return False def validate_settings_fields(self): api_key = self._get_text("api_key").strip() video_dir = self._get_text("videos_location").strip() playlist_dir = self._get_text("playlists_location").strip() return api_key, video_dir, playlist_dir def write_settings(self): api_key, video_dir, playlist_dir = self.validate_settings_fields() # to know if anythig changed changed = [] if api_key: if self._validate_api_key(api_key) == True: self.SETTINGS.api_key = api_key changed.append(True) else: self._show_alert( "Invalid YouTube API key", """You entered a wrong YouTube API key. You won't be able to download any playlsits if you don't set a valid YouTube API key. \ To get an api key just visit the link and follow the guides. """) if video_dir: if self._validate_folder(video_dir): self.SETTINGS.video_dir = os.path.join(video_dir, "Videos") changed.append(True) else: self._show_alert( "Invalid path for videos", """ You entered a wrong path to save downloaded videos. """) if playlist_dir: if self._validate_folder(playlist_dir): self.SETTINGS.playlist_dir = os.path.join( playlist_dir, "Playlists") changed.append(True) else: self._show_alert( "Invalid path for playlists", """ You entered a wrong path to save downloaded playlists. """) if any(changed): self.SETTINGS.save_settings() self.statusbar.showMessage("Saved Settings", self.STATUS_TIMEOUT) self._update_settings_fields_palceholders()
def main (auth_file_dir): settings = Settings(auth_file_dir).parse() auth = Authorization(settings) api = auth.api() rt = Retweet(api, settings) rt.retweet_from_timeline()
def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(676, 456) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.tabWidget = QtWidgets.QTabWidget(self.centralwidget) self.tabWidget.setGeometry(QtCore.QRect(10, 10, 661, 401)) self.tabWidget.setObjectName("tabWidget") self.tabRPTool = QtWidgets.QWidget() self.tabRPTool.setObjectName("tabRPTool") # List Widget Test Cases self.lvTestCases = QtWidgets.QListWidget(self.tabRPTool) self.lvTestCases.setGeometry(QtCore.QRect(10, 60, 641, 271)) self.lvTestCases.setObjectName("lvTestCases") self.lvTestCases.itemClicked.connect(self.itemSelected) # Button 'rec' self.pbRec = QtWidgets.QPushButton(self.tabRPTool) self.pbRec.setGeometry(QtCore.QRect(10, 340, 41, 25)) self.pbRec.setObjectName("pbRec") self.pbRec.clicked.connect(self.addTestCase) self.leTestCaseName = QtWidgets.QLineEdit(self.tabRPTool) self.leTestCaseName.setGeometry(QtCore.QRect(10, 30, 301, 25)) self.leTestCaseName.setObjectName("leTestCaseName") self.leTestFolder = QtWidgets.QLineEdit(self.tabRPTool) self.leTestFolder.setGeometry(QtCore.QRect(320, 30, 331, 25)) self.leTestFolder.setObjectName("leTestFolder") self.lblTestCaseName = QtWidgets.QLabel(self.tabRPTool) self.lblTestCaseName.setGeometry(QtCore.QRect(10, 10, 121, 17)) self.lblTestCaseName.setObjectName("lblTestCaseName") self.lblTestFolder = QtWidgets.QLabel(self.tabRPTool) self.lblTestFolder.setGeometry(QtCore.QRect(320, 10, 71, 17)) self.lblTestFolder.setObjectName("lblTestFolder") self.pbPlay = QtWidgets.QPushButton(self.tabRPTool) self.pbPlay.setGeometry(QtCore.QRect(60, 340, 51, 25)) self.pbPlay.setObjectName("pbPlay") self.pbPlay.setEnabled(False) self.pbPlay.clicked.connect(self.playTestCase) self.pbDelete = QtWidgets.QPushButton(self.tabRPTool) self.pbDelete.setGeometry(QtCore.QRect(180, 340, 51, 25)) self.pbDelete.setObjectName("pbDelete") self.pbDelete.setEnabled(False) self.pbDelete.clicked.connect(self.deleteTestCase) self.pbPlayAll = QtWidgets.QPushButton(self.tabRPTool) self.pbPlayAll.setGeometry(QtCore.QRect(120, 340, 51, 25)) self.pbPlayAll.setObjectName("pbPlayAll") self.pbPlayAll.setEnabled(False) self.lblToolStatus = QtWidgets.QLabel(self.tabRPTool) self.lblToolStatus.setGeometry(QtCore.QRect(250, 330, 371, 41)) self.lblToolStatus.setObjectName("lblToolStatus") # Loop Tests self.chboxLoop = QtWidgets.QCheckBox(self.tabRPTool) self.chboxLoop.setGeometry(QtCore.QRect(510, 340, 61, 21)) self.chboxLoop.stateChanged.connect(self.changeLoopState) self.chboxLoop.setObjectName("chboxLoop") self.leInterval = QtWidgets.QLineEdit(self.tabRPTool) self.leInterval.setGeometry(QtCore.QRect(580, 340, 71, 25)) self.leInterval.setObjectName("leInterval") self.leInterval.setText('0') self.tabWidget.addTab(self.tabRPTool, "") self.tabSettings = QtWidgets.QWidget() self.tabSettings.setObjectName("tabSettings") # Save settings self.pbSaveSettings = QtWidgets.QPushButton(self.tabSettings) self.pbSaveSettings.setGeometry(QtCore.QRect(10, 330, 61, 25)) self.pbSaveSettings.setObjectName("pbSaveSettings") self.pbSaveSettings.clicked.connect(self.saveSettings) self.lblUsername = QtWidgets.QLabel(self.tabSettings) self.lblUsername.setGeometry(QtCore.QRect(10, 0, 81, 31)) self.lblUsername.setObjectName("lblUsername") self.lblTestlinkApiKey = QtWidgets.QLabel(self.tabSettings) self.lblTestlinkApiKey.setGeometry(QtCore.QRect(310, 10, 111, 17)) self.lblTestlinkApiKey.setObjectName("lblTestlinkApiKey") self.leUsername = QtWidgets.QLineEdit(self.tabSettings) self.leUsername.setGeometry(QtCore.QRect(10, 30, 291, 25)) self.leUsername.setObjectName("leUsername") self.leTestlinkApiKey = QtWidgets.QLineEdit(self.tabSettings) self.leTestlinkApiKey.setGeometry(QtCore.QRect(310, 30, 341, 25)) self.leTestlinkApiKey.setObjectName("leTestlinkApiKey") # combo box projects self.cbProjects = QtWidgets.QComboBox(self.tabSettings) self.cbProjects.setGeometry(QtCore.QRect(10, 150, 291, 25)) self.cbProjects.setObjectName("cbProjects") self.cbProjects.currentTextChanged.connect(self.getTestlinkPlans) self.leTestlinkAddress = QtWidgets.QLineEdit(self.tabSettings) self.leTestlinkAddress.setGeometry(QtCore.QRect(10, 90, 641, 25)) self.leTestlinkAddress.setObjectName("leTestlinkAddress") self.lblTestlinkAddress = QtWidgets.QLabel(self.tabSettings) self.lblTestlinkAddress.setGeometry(QtCore.QRect(10, 70, 101, 17)) self.lblTestlinkAddress.setObjectName("lblTestlinkAddress") # connect push button self.pbConnect = QtWidgets.QPushButton(self.tabSettings) self.pbConnect.setGeometry(QtCore.QRect(80, 330, 80, 25)) self.pbConnect.setObjectName("pbConnect") self.pbConnect.clicked.connect(self.getTestlinkProjects) self.lblSettingsStatus = QtWidgets.QLabel(self.tabSettings) self.lblSettingsStatus.setGeometry(QtCore.QRect(180, 330, 171, 21)) self.lblSettingsStatus.setObjectName("lblSettingsStatus") self.lblTestProject = QtWidgets.QLabel(self.tabSettings) self.lblTestProject.setGeometry(QtCore.QRect(10, 120, 91, 31)) self.lblTestProject.setObjectName("lblTestProject") # Combobox plans self.cbPlans = QtWidgets.QComboBox(self.tabSettings) self.cbPlans.setGeometry(QtCore.QRect(310, 150, 341, 25)) self.cbPlans.setObjectName("cbPlans") self.cbPlans.currentTextChanged.connect(self.getTeslinkPlatforms) self.lblTestPlan = QtWidgets.QLabel(self.tabSettings) self.lblTestPlan.setGeometry(QtCore.QRect(310, 126, 91, 21)) self.lblTestPlan.setObjectName("lblTestPlan") #Combobox platforms self.cbPlatforms = QtWidgets.QComboBox(self.tabSettings) self.cbPlatforms.setGeometry(QtCore.QRect(10, 210, 291, 25)) self.cbPlatforms.setObjectName("cbPlatforms") self.cbPlatforms.currentTextChanged.connect(self.getTestlinkBuilds) self.cbBuilds = QtWidgets.QComboBox(self.tabSettings) self.cbBuilds.setGeometry(QtCore.QRect(310, 210, 341, 25)) self.cbBuilds.setObjectName("cbBuilds") self.lblPlatform = QtWidgets.QLabel(self.tabSettings) self.lblPlatform.setGeometry(QtCore.QRect(10, 186, 91, 21)) self.lblPlatform.setObjectName("lblPlatform") self.lblBuild = QtWidgets.QLabel(self.tabSettings) self.lblBuild.setGeometry(QtCore.QRect(310, 186, 54, 21)) self.lblBuild.setObjectName("lblBuild") self.tabWidget.addTab(self.tabSettings, "") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 676, 22)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) self.tabWidget.setCurrentIndex(0) QtCore.QMetaObject.connectSlotsByName(MainWindow) # RPTool init self.rpt = RPTool() self.setting = Settings('rptool.cfg') self.config = self.rpt.getConfig() self.interval = 0 self.setFolderDir() self.listTests() self.testlinkSettings()
def process(device_id): adb_instance = ADB(adb_path, device_id) report_apps = dict() out = Out(filename=device_id + ".log") packages = [] if app_type: packages = adb_instance.list_installed_packages(app_type.value) if adb_instance.check_pending_update(): out.print_warning("%s: The system has a pending update!" % device_id) if scan_applications or dump_apks or list_apps: for package in packages: if not list_apps: out.print_info(package) report_apps[package] = dict() dumpsys_out = adb_instance.dumpsys(["package", package]) perm_list = adb_instance.get_req_perms_dumpsys_package(dumpsys_out) app = App(adb_instance, package, scan_applications, dump_apks, apks_dump_folder, perm_list) perms, dangerous_perms, is_device_admin, known_malware = app.check_app( ) if known_malware: report_apps[package]["malware"] = True out.print_error("{} is known as malware".format(package)) if scan_applications: if dangerous_perms is not None and dangerous_perms.items(): out.print_warning_header( "Package {} has some dangerous permissions: ".format( package)) for perm, desc in dangerous_perms.items(): out.print_warning("\t " + perm + ": ") out.print_warning("\t\t" + desc) report_apps[package]["permissions"] = dict() report_apps[package]["permissions"] = { "all_permissions": list(perms.keys()), "dangerous_perms": dangerous_perms } report_apps[package]["is_device_admin"] = is_device_admin else: out.print_info( "Package {} has no dangerous permissions".format( package)) if is_device_admin: message = f"/!\ \t {package} is device admin \t /!\ " padding = len(message) out.print_warning("-" * padding) out.print_warning(message) out.print_warning("-" * padding) report_apps[package] = {"device_admin": is_device_admin} if rm_admin_recv: removed, dpm = app.remove_device_admin_for_app() if removed: report_apps[package] = { "device_admin_revoked": True } out.print_info( "Device admin receivers for {} removed\n". format(app.package_name)) else: out.print_error( "An error occured while removing the device admin " + dpm + " .") # Revoke all Dangerous permissions if revoke and app.dangerous_perms: succeeded = app.revoke_dangerous_perms() if succeeded: report_apps[package][ "revoked_dangerous_pemissions"] = "succeeded" out.print_info("Dangerous permissions revoked\n") else: out.print_error( f"An error occured while revoking permission {perm} to package {app.package_name}" ) elif revoke and not app.dangerous_perms: out.print_info( "No dangerous permissions granted for this package\n") if app.malware_confidence > 0 or app.score < 0: out.print_high_warning( "----------------------------MALWARE SCAN--------------------------------" ) out.print_high_warning( "The application uses some permissions used also by malware" ) if app.malware_confidence > 0: out.print_high_warning( str(app.malware_confidence) + " permissions combinations used also by " "malware") if app.score < 0: out.print_high_warning( "The application uses frequent malware permissions ") if scan_applications: with open(device_id + "_report_apps.json", 'w') as fp: json.dump(report_apps, fp, indent=4) out.print_info("Report generated: %s_report_apps.json" % device_id) if list_apps: lock.acquire() print("List of installed packages on device %s: " % device_id) nbr_listed_apps = 0 apps_choice_list = [] for package in packages: if nbr_listed_apps < LIST_APPS_MAX_PRINT and packages.index( package) < (len(packages) - 1): print("\t[" + str(packages.index(package) + 1) + "] " + package) nbr_listed_apps = nbr_listed_apps + 1 else: choice = '' if packages.index(package) == (len(packages) - 1): print("\t[" + str(packages.index(package) + 1) + "] " + package) while True: choice = input( "Select application(s) (separated by comma ','), 'c' to continue" " listing apps and 'A' for actions menu: ") if choice == 'c': nbr_listed_apps = 1 break if choice == 'A': break else: chosen_apps = choice.replace(" ", "").split(",") for c in chosen_apps: if c.isdigit() and (0 < int(c) < len(packages) + 1): apps_choice_list = apps_choice_list + [c] else: print("option " + c + " does not exist") if choice == 'A': break if app_type.value == 'e': print( "Uninstalling or disabling system Apps can break your system") while True: print("choose an action") print("\td: disable selected apps") print("\tu: uninstall selected apps") print("\tS: Static analysis") print("\ts: skip") print("") action = input("Action: ") action = action.replace(" ", "") if action == 'd' or action == 'u' or action == 's' or action == 'S': lock.release() break else: print("ERROR: Invalid action") continue for id_app in apps_choice_list: if action == 'd': try: adb_instance.disable_app(packages[int(id_app) - 1]) out.print_success(packages[int(id_app) - 1] + " disabled") except Exception as e: out.print_error("An Error occurred while disabling " + packages[int(id_app) - 1]) elif action == 'u': try: adb_instance.uninstall_app(packages[int(id_app) - 1]) out.print_success(packages[int(id_app) - 1] + " uninstalled") except Exception as e: out.print_error("An Error occurred while uninstalling " + packages[int(id_app) - 1]) elif action == "S": app = App(adb_instance, packages[int(id_app) - 1], dump_apk=True, out_dir=apks_dump_folder) out.print_info("Package {}".format(packages[int(id_app) - 1])) package_info = app.static_analysis() out.print_info("\tMalware identification") for key, value in package_info["detected_malware"].items(): if value > 0: out.print_error("\t\t " + key + ": " + str(value) + " positives tests") else: out.print_info("\t\t " + key + ": " + str(value) + " positive test") if package_info and package_info["packed_file"] and \ package_info["packed_file"][packages[int(id_app) - 1]].keys(): out.print_info("\tPacked files") out.print_error( "The package {} has another Application (APK) inside". format(packages[int(id_app) - 1])) for file in package_info["packed_file"][packages[ int(id_app) - 1]]: for perm in package_info["packed_file"][packages[ int(id_app) - 1]][file]: out.print_error("\tDangerous Permission: " + perm) elif action == 's': break if harden: settings_check = Settings(SETTINGS_FILE, adb_instance, True, out=out) else: settings_check = Settings(SETTINGS_FILE, adb_instance, out=out) if scan_settings: settings_check.check() if list_processes: lock.acquire() process_choice_list = [] current_processes = adb_instance.list_backgroud_apps().split("\n") print("Current running user processes on the device %s" % device_id) for i in range(0, len(current_processes) - 1): print(" {}- {}".format(i + 1, current_processes[i])) print("") choice = input( "Select id(s) of process(es) to kill (separated by comma ','): ") chosen_processes = choice.replace(" ", "").split(",") for c in chosen_processes: if c.isdigit() and (0 < int(c) < len(current_processes) + 1): process_choice_list = process_choice_list + [c] lock.release() else: print("[X] ERROR: process does not exist") print("Exiting device %s" % device_id) for process in process_choice_list: adb_instance.force_stop_app(current_processes[int(process) - 1]) if snapshot: with lock: input("Unlock device %s and press ENTER key to continue" % device_id) # set stay_awake to 1 adb_instance.content_insert_settings("global", "stay_on_while_plugged_in", "1", "i") out.print_info("Starting snapshot") if not os.path.isdir(snapshot_dir): os.makedirs(snapshot_dir) snapshot_path = snapshot_dir + "/" + device_id + str( datetime.now()).replace(" ", "_") if not os.path.isdir(snapshot_path): os.makedirs(snapshot_path) if app_type: snapshot_obj = Snapshot(adb_instance, app_type.value, out_dir=snapshot_path) else: snapshot_obj = Snapshot(adb_instance, out_dir=snapshot_path) report = snapshot_obj.get_report() with open(snapshot_path + "/snapshot.json", 'w') as fp: json.dump(report, fp, indent=4) adb_instance.content_insert_settings("global", "stay_on_while_plugged_in", "0", "i") out.print_info("Snapshot finished")
def __init__(self) -> None: self.settings = Settings(self) self.user_manager = UserManager(self) self.lights = Lights(self) self.pad = Pad(self) self.musiq = Musiq(self)
def harden_settings(device): adb = ADB(ADB_PATH, device_id=device) settings = Settings(json_settings, adb, True) settings.check() return redirect(url_for('index'))