def main(): """Provide main function for CLI.""" parser = argparse.ArgumentParser( description='Change the mode (permissions) of a file or directory') parser.add_argument('-R', action='store_true', help='apply mode recursively') parser.add_argument( 'mode', nargs=1, help='octal or symbolic mode of the object') parser.add_argument('object', nargs=1, help='file or directory') args = parser.parse_args() mode = args.mode[0] obj = args.object[0] if args.R: oschmod.set_mode_recursive(obj, mode) else: oschmod.set_mode(obj, mode)
def test_set_recursive(): """Check file permissions are recursively set.""" # create dirs topdir = 'testdir1' testdir = os.path.join(topdir, 'testdir2', 'testdir3') os.makedirs(testdir) # create files fileh = open(os.path.join(topdir, 'file1'), "w+") fileh.write("contents") fileh.close() fileh = open(os.path.join(testdir, 'file2'), "w+") fileh.write("contents") fileh.close() # set permissions to badness triple7 = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_IRGRP\ | stat.S_IWGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IWOTH\ | stat.S_IXOTH oschmod.set_mode(topdir, triple7) oschmod.set_mode(testdir, triple7) oschmod.set_mode(os.path.join(topdir, 'file1'), triple7) oschmod.set_mode(os.path.join(testdir, 'file2'), triple7) time.sleep(1) # modes aren't always ready to go immediately # set permissions - the test file_mode = 0o600 dir_mode = 0o700 oschmod.set_mode_recursive(topdir, file_mode, dir_mode) time.sleep(1) # modes aren't always ready to go immediately # check it out assert oschmod.get_mode(topdir) == dir_mode assert oschmod.get_mode(os.path.join(topdir, 'testdir2')) == dir_mode assert oschmod.get_mode(testdir) == dir_mode assert oschmod.get_mode(os.path.join(topdir, 'file1')) == file_mode assert oschmod.get_mode(os.path.join(testdir, 'file2')) == file_mode # clean up shutil.rmtree(topdir)
def install(self, cmdline: bool) -> None: _logger.debug( f'Installing Moonlight HDR Launcher {(self.source_folder / "dist" / "version").read_text()}' ) _logger.debug(f'Source folder: {self.source_folder}') _logger.debug( f'Destination folder for launcher: {self.destination_folder}') _logger.debug(f'Launcher path: {self.launcher_path}') _logger.debug( f'List of files to put in {self.destination_folder}: {self.programfiles_files}' ) _logger.debug( f'List of files to put in Streaming folder: {self.streaming_files}' ) if not self.launcher_path.exists(): show_error(f'{self.launcher_path} does not exist', cmdline) raise Exception(f'{self.launcher_path} does not exist') create_folder(self.destination_folder, cmdline) copy_files(self.programfiles_files, self.destination_folder, cmdline) # install gamestream launchpad try: with tempfile.TemporaryDirectory() as gslp_temp: _logger.debug( f'Downloading gamestream_launchpad files from: {GSLP_URL}') download_gamestream_launchpad(Path(gslp_temp)) gslp_files = list(Path(gslp_temp).glob('**/*')) _logger.debug( f'Downloaded gamestream_launchpad files: {gslp_files}') # install copy_files(gslp_files, self.destination_folder, cmdline) except Exception as e: _logger.error(f'Error downloading gamestream_launchpad: {e}') show_error(f'Error downloading gamestream_launchpad: {e}', cmdline) # install initial configuration ini_file = self.destination_folder / 'moonlight_hdr_launcher.ini' if not ini_file.exists(): config, res = configuration_select() _logger.debug(f'Selected base configuration: {str(config)}') if res == ResolutionConfig.ResNative: try: res_x, res_y = get_resolution() except BaseException: res_x, res_y = 1920, 1080 else: res_x, res_y = RESOLUTION_CONFIGS[res] _logger.debug(f'Writing config file: {ini_file}') config_ini = BASE_CONFIGS[config].format(res_x=res_x, res_y=res_y) ini_file.write_text(config_ini) _logger.debug(f'Written configuration: {config_ini}') # set destination folder read-write oschmod.set_mode_recursive(str(self.destination_folder), 0o777) # write destination_folder to registry try: _logger.debug( f'Writing destination_folder="{self.destination_folder}" to registry at "{self.reg_path}"' ) write_path_to_reg(self.destination_folder, self.reg_path, 'destination_folder') except WindowsError as e: show_error(f'Failed to write destination_folder to registry: {e}') raise e show_warning( 'Before clicking OK, open GeForce Experience and re-scan for games.', cmdline) mad_path = get_masseffectandromeda_location(cmdline) write_path_to_reg(mad_path, self.reg_path, 'stream_assets_folder') # set StreamingAssetsData folder read-write oschmod.set_mode_recursive(str(mad_path), 0o777) # copy files to StreamingAssetsData destination copy_files(self.streaming_files, mad_path, cmdline) self.modify_streamsettings_json(mad_path) self.modify_metadata_json(mad_path) # set StreamingAssetsData folder read-only oschmod.set_mode_recursive(str(mad_path), 'a+r,a-w') show_warning( 'The installer will now attempt to restart GeForce Experience. If the new entry does not appear, reboot your PC.', cmdline) # kill NVIDIA processes os.system('taskkill /f /im "nvcontainer.exe"') os.system('taskkill /f /im "NVIDIA Share.exe"')
def install_launcher(source_folder: Path, destination_folder: Path, launcher_exe: str, additional_programfiles_files: List[str], additional_streaming_files: List[str], cmdline: bool) -> None: launcher_path = Path(source_folder / 'dist' / launcher_exe) programfiles_files = [launcher_path] + [ source_folder / 'dist' / file_path for file_path in additional_programfiles_files ] streaming_files = [ source_folder / 'dist' / file_path for file_path in additional_streaming_files ] _logger.debug( f'Installing Moonlight HDR Launcher {(source_folder/"dist"/"version").read_text()}' ) _logger.debug(f'Source folder: {source_folder}') _logger.debug(f'Destination folder for launcher: {destination_folder}') _logger.debug(f'Launcher path: {launcher_path}') _logger.debug( f'List of files to put in {destination_folder}: {programfiles_files}') _logger.debug( f'List of files to put in Streaming folder: {streaming_files}') if not launcher_path.exists(): show_error(f'{launcher_path} does not exist', cmdline) raise Exception(f'{launcher_path} does not exist') # create destination folder try: destination_folder.mkdir(parents=True, exist_ok=True) except DistutilsFileError as e: show_warning( f'No permission to create {destination_folder}, re-run as Administrator', cmdline) raise e # copy files to programfiles destination for source_path in programfiles_files: if source_path.exists(): try: dest_name, copied = copy_file(source_path, destination_folder, update=True) if copied: _logger.info(f'Copied {source_path} to {dest_name}') else: _logger.info( f'Skipped copying {source_path} to {dest_name} because destination is newer than source' ) except DistutilsFileError as e: show_warning( f'No permission to copy {source_path} to {destination_folder}, re-run as Administrator', cmdline) raise e else: _logger.warning(f'Source file {source_path} does not exist') # set destination folder read-write oschmod.set_mode_recursive(str(destination_folder), 0o777) # write destination_folder to registry try: write_destination_folder_to_reg(destination_folder) except WindowsError as e: show_error(f'Failed to write destination_folder to registry: {e}') raise e # prompt to re-scan games show_warning( 'Before continuing, open GeForce Experience and re-scan for games.', cmdline) # find StreamingAssetsData subfolder try: app_data = Path(expandvars(r'%LOCALAPPDATA%')) _logger.info(f'Found AppData path: {app_data}') mad_path = next( app_data.glob('**/StreamingAssetsData/mass_effect_andromeda/*')) _logger.info( f'Found StreamingAssetsData folder for Mass Effect Andromeda: {mad_path}' ) except StopIteration as e: show_error( 'Unable to find entry for Mass Effect Andromeda. Have you tried scanning for games in GeForce Experience?', cmdline) raise e # set folder read-write oschmod.set_mode_recursive(str(mad_path), 0o777) # copy files to StreamingAssetsData destination for source_file in streaming_files: try: dest_name, copied = copy_file(source_file, mad_path, update=True) if copied: _logger.info(f'Copied {source_file} to {dest_name}') else: _logger.info( f'Skipped copying {source_file} to {dest_name} because destination is newer than source' ) except DistutilsFileError: show_warning( f'No permission to copy {source_file} to {mad_path}, re-run as Administrator', cmdline) # modify StreamingSettings.json streaming_settings_path = mad_path / 'StreamingSettings.json' streaming_settings = json.loads(streaming_settings_path.read_text()) streaming_settings['GameData'][0]['StreamingDisplayName'] = 'HDR Launcher' streaming_settings['GameData'][0]['StreamingCaption'] = 'HDR Launcher' streaming_settings['GameData'][0]['StreamingClassName'] = 'HDR Launcher' streaming_settings['GameData'][0][ 'StreamingCommandLine'] = f'start {launcher_exe}' final_json = json.dumps(streaming_settings, indent=4) _logger.debug(f'Final StreamingSettings.json: {final_json}') _logger.debug(f'Saving to {streaming_settings_path}') streaming_settings_path.write_text(final_json) # modify metadata.json metadata_path = mad_path / 'metadata.json' metadata = json.loads(metadata_path.read_text()) metadata['metadata']['files'] = [{ 'filename': f.name, 'url': '', 'sha256': get_sha256(f), 'size': f.stat().st_size } for f in [streaming_settings_path, *streaming_files]] final_metadata_json = json.dumps(metadata, indent=4) _logger.debug(f'Final metadata.json: {final_metadata_json}') _logger.debug(f'Saving to {metadata_path}') metadata_path.write_text(final_metadata_json) # set folder read-only oschmod.set_mode_recursive(mad_path.as_posix(), 'a+r,a-w') show_warning( 'Now restart GeForce Experience or the PC to register the config changes', cmdline)