def file_root_fallback(filepath, layer_dir=None): """Test a given filepath against the list of possible root paths specified in the env var NXT_FILE_ROOTS. :param filepath: string of file path :param layer_dir: optional layer dir to be tested after all possible roots fail. :return: string of valid filepath or empty string """ for root in iter_env_roots(): full_path = nxt_path.full_file_expand(filepath, start=root) if os.path.exists(full_path): return full_path if layer_dir and os.path.exists(layer_dir): full_path = nxt_path.full_file_expand(filepath, start=layer_dir) if os.path.exists(full_path): return full_path return ''
def test_cross_platform_expansion(self): tests = ['~/%FOO%/$BAR/%BAZ%', '~/$FOO/%BAR%/$BAZ'] os.environ['FOO'] = 'FIRST' os.environ['BAR'] = 'SECOND' os.environ['BAZ'] = 'THIRD' expected = os.path.expanduser('~/FIRST/SECOND/THIRD').replace( os.sep, '/') for test_str in tests: result = nxt_path.full_file_expand(test_str) self.assertEqual(expected, result)
def resolve_path_token_in_roots(stage, node, cleaned, layer, **kwargs): value = stage.resolve(node, cleaned, layer) try: root = next(iter_env_roots()) except StopIteration: layer_cwd = layer.get_cwd() if os.path.exists(layer_cwd): root = layer_cwd else: root = os.getcwd() return nxt_path.full_file_expand(value, start=root)
def test_auto_update(self): self.maxDiff = None self.stage = Session().load_file("legacy/0.45.0_TopLayer.nxt") self.file_data = self.stage.get_layer_save_data(0) self.file_data = json.dumps(self.file_data, indent=4, sort_keys=False, separators=(',', ': ')) file_path = "legacy/0.45.0_to_LATEST.nxt" real_path = nxt_path.full_file_expand(file_path) self.proof_data = dynamic_version(real_path) self.assertEqual(self.proof_data, self.file_data)
def test_vars_only(self): platform_tests = { 'linux': "$zoom/$all/$around", 'win32': "%zoom%/%all%/%around%" } platform_tests['linux2'] = platform_tests['linux'] platform_tests['darwin'] = platform_tests['linux'] win_expected = os.path.join(os.getcwd(), "come\\back\\down") win_expected = win_expected.replace(os.path.sep, '/') platform_expected = { 'linux': os.path.join(os.getcwd(), "come/back/down"), 'win32': win_expected } platform_expected['linux2'] = platform_expected['linux'] platform_expected['darwin'] = platform_expected['linux'] env_vars = {'zoom': 'come', 'all': 'back', 'around': 'down'} os.environ.update(env_vars) result = nxt_path.full_file_expand(platform_tests[sys.platform]) self.assertEqual(result, platform_expected[sys.platform])
def exec_in_headless(filepath, start_node, cache_path, parameters, context_name): """Executed the given graph (filepath) with the given start_node in the dcc exe (as a sub-process). The temp path (if provided) must be a location that the server can read/write. If no temp path is given one will be generated. The file at the temp path is used to store the cache data to be returned to the caller. Only the file path is returned to the caller, not the actual data. :param filepath: Path to nxt save file :param start_node: start node path :param cache_path: Path to store output cache data (if none is given one will be generated) :param parameters: Optional parameters dict :param context_name: name of context, defaults to python :return: filepath to temp file """ # Fixme: Contexts must be accessed like this to avoid importing them # again and thus emptying the list of user contexts. context = nxt.remote.contexts.find_context_by_name(context_name) if not context: known = nxt.remote.contexts.iter_context_names() logger.debug('Known contexts: \n{}'.format('\n'.join(known))) raise NameError('Unknown context "{}"'.format(context_name)) context_exe = context.exe if not context or not context_exe: raise TypeError('Unable to find context exe for: ' '{}'.format(context)) context_graph = context.graph context_graph = nxt_path.full_file_expand(context_graph) if not context_graph: raise TypeError('No launch script found for context: ' '{}'.format(context)) # Setup cache file if none provided if not cache_path: cache_path = nxt_io.generate_temp_file() cache_path = cache_path.replace(os.sep, '/') # Setup parameters file if parameters provided if parameters: parameters_file = nxt_io.generate_temp_file() parameters_file = parameters_file.replace(os.sep, '/') with open(parameters_file, 'w+') as fp: json.dump(parameters, fp, separators=(',', ': ')) else: parameters_file = '' context_graph = context_graph.replace(os.sep, '/') logger.info('Starting \n' 'Context: {} \n' 'Interpreter: {} \n' 'Context Graph: {}\n'.format(context, context_exe, context_graph)) logger.info('Cache location: {}\n'.format(cache_path)) # Format the cli call safe_graph_path = nxt_path.full_file_expand(filepath) # open context with graph and parameters os.environ[nxt_log.VERBOSE_ENV_VAR] = 'socket' cli_args = [ 'exec', context_graph, '-p', '/.graph_file', safe_graph_path, '/.cache_file', cache_path ] if parameters_file: cli_args += ['/.parameters_file', parameters_file] script = os.path.join(os.path.dirname(__file__), '..', 'cli.py') script = os.path.abspath(script) script = script.replace(os.sep, '/') if context.args: extra_args = [] if context.args: extra_args = list(context.args) args = [context_exe] + extra_args + [script, '--'] + cli_args else: args = [context_exe, script] + cli_args if start_node: args += ['/enter/call_graph._start', start_node] # HACK solution only until refined context system rolls out to relate # format strings to context names to include space for cli args. if 'UE4Editor' in context_exe: args = context_exe.format(cli_path=script, cli_args=' '.join(cli_args)) logger.debug('call: {}'.format(args)) # TODO: Find a clean way to raise exceptions from the subprocess. try: subprocess.call(args) except subprocess.CalledProcessError: raise RuntimeError('Remote context graph "{}" failed! See log...' ''.format(safe_graph_path)) return cache_path