def RunAvl(alpha, beta, delta_flap, omega, avl_files): """Run AVL with specified conditions.""" # Use a temporary file for the output, which will be copied to the desired # location. AVL chokes when a path length exceeds 80 characters, which is a # common occurrence under Bazel. with tempfile.NamedTemporaryFile(suffix='.txt') as avl_output: # Generate a string with a sequence of commands to be piped into AVL. input_str = tempfile.TemporaryFile(mode='w+b') input_str.write( GetAvlInput(alpha, beta, delta_flap, omega, avl_output.name)) input_str.seek(0) # For debugging purposes and comparison with previous Matlab functions, # write the AVL input to a file. if avl_files['input'] is not None: with open(avl_files['input'], 'w') as f: f.write(input_str.read()) input_str.seek(0) # Call AVL and have it solve for stability derivatives. surface_basename = os.path.basename(avl_files['surface']) surface_dir = os.path.dirname(avl_files['surface']) with open(avl_files['log'], 'w') as log, os_util.ChangeDir(surface_dir): popen = subprocess.Popen([FLAGS.avl_bin, surface_basename], stdin=input_str, stdout=log, stderr=subprocess.PIPE) popen.wait() # Check whether AVL had trouble locating any data files. It happily charges # ahead with default data if that happens. with open(avl_files['log'], 'r') as log: error_regex = re.compile( r'.*(Airfoil file not found|Mass file.*open error).*') line_number = 0 for line in log: line_number += 1 if error_regex.match(line): raise RuntimeError( 'AVL error: "%s"\nSee %s:%d for more information.' % (line.strip(), avl_files['log'], line_number)) shutil.copyfile(avl_output.name, avl_files['output']) # Check for a failed run. "Killed" came up fairly frequently while debugging # due to a lack of memory. if popen.returncode != 0: if 'Killed' in avl_output.stderr.read(): raise RuntimeError( 'Warning: AVL exited abnormally.', ' Possible causes include:\n' ' - insufficient system memory\n') raise RuntimeError('Warning: AVL exited abnormally.') with open(avl_files['output'], 'r+') as f: file_contents = f.read() return re.findall(r'(\S+)\s*=\s*([-+]?\d+\.\d*)', file_contents)
def CreateRepoPackage(self, package_path, commit='HEAD', restrict_paths=None): """Creates a package from HEAD of the git respository. Args: package_path: Path of the package file. commit: Commit for which to produce the archive. restrict_paths: List of paths, relative to makani.HOME, to which the archive will be restricted. """ if restrict_paths is None: restrict_paths = [] executor = Executor() # If running under Bazel, we're working with a symlink to this file. In that # case, we get the absolute path to this file in the repo by changing .pyc # to .py if necessary, then resolving any symlinks. The we use "git # rev-parse --show-toplevel" from the containing directory to determine the # base directory of the repo. abs_path = os.path.realpath( __file__[:-1] if __file__.endswith('.pyc') else __file__) with os_util.ChangeDir(os.path.dirname(abs_path)): repo_base = executor.CheckRun( ['git', 'rev-parse', '--show-toplevel'], stdout=subprocess.PIPE)[1].strip() with os_util.ChangeDir(repo_base): # Warn if there are uncommitted changes. status = executor.CheckRun(['git', 'status'], stdout=subprocess.PIPE)[1] if commit == 'HEAD' and 'Changes not staged for commit' in status: logging.warning( 'You have uncommited changes. Only committed changes ' 'will be included in the repository package.') executor.CheckRun([ 'git', 'archive', '--format=tar.gz', '--output=' + package_path, commit ] + restrict_paths)
def __exit__(self, *unused_args): self._tcpdump_process.send_signal(signal.SIGINT) self._tcpdump_process.wait() h5_file = self._pcap_file.replace('.pcap', '.h5') subprocess.check_call([os.path.join(makani.HOME, 'lib/pcap_to_hdf5/pcap_to_hdf5'), self._pcap_file, '--output_file', h5_file] + self._extra_args) with os_util.ChangeDir('logs'): self._MakeLink('last_z2.h5', 'last_z3.h5') self._MakeLink('last_z1.h5', 'last_z2.h5') self._MakeLink('last.h5', 'last_z1.h5') self._MakeLink(os.path.basename(h5_file), 'last.h5') if FLAGS.log_file: os.symlink(os.path.basename(h5_file), FLAGS.log_file)
def _RunLocalWorker(self, num_configs, worker_dir): """Runs a local worker. Args: num_configs: Number of config files. worker_dir: Directory in which the worker should be run. Must already contain the repo, binary, and config packages. """ with os_util.ChangeDir(worker_dir): with tarfile.open('configs.tar.gz', 'r:gz') as f: f.extractall() worker_env = os.environ.copy() worker_env['MAKANI_HOME'] = worker_dir args = self._GetWorkerArgs((0, num_configs - 1)) logging.info('Running worker with command:\n %s', ' '.join(args)) # The worker will automagically save the output to the right location. shell_interfaces.Executor().CheckRun(args, env=worker_env)
with contextlib.nested(*processes): # Run the simulator, taking ctrl+C as its termination signal. sim_args = ['--num_controllers=%d' % FLAGS.num_controllers] sim_args += ['--gs_estimator=%r' % FLAGS.gs_estimator] sim_args += extra_sim_args if FLAGS.time is not None: sim_args += ['--time=%g' % FLAGS.time] if FLAGS.load_state: sim_args += ['--load_state'] if FLAGS.save_state_time is not None: sim_args += ['--save_state_time', str(FLAGS.save_state_time)] if FLAGS.async: sim_args += ['--async'] if all_params_path: sim_args += ['--all_params', all_params_path] if FLAGS.comms_timeout_sec is not None: sim_args += ['--comms_timeout_sec=%f' % FLAGS.comms_timeout_sec] try: with _Process('sim/sim', extra_args=sim_args, wait_for_completion=True, use_callgrind=FLAGS.callgrind_sim): pass except KeyboardInterrupt: pass if __name__ == '__main__': with os_util.ChangeDir(bazel_util.GetWorkspaceRoot()): main(sys.argv)
time_step=FLAGS.time_step, duration=FLAGS.duration, hub_height=hub_height, width=FLAGS.width, height=FLAGS.height, v_flow_ang=FLAGS.v_flow_ang, wind_shear_exp=FLAGS.wind_shear_exp, turb_model=FLAGS.turb_model, standard=FLAGS.iec_standard, turbc=FLAGS.iec_turbc, wind_type=FLAGS.iec_wind_type, mean_wind_speed=FLAGS.mean_wind_speed) print 'Writing TurbSim input...' with os_util.TempDir() as turbsim_dir: with os_util.ChangeDir(turbsim_dir): input_file = os.path.join(turbsim_dir, 'TurbSim.inp') with open(input_file, 'w') as f: f.write(turbsim_input) print 'Running TurbSim...' try: subprocess.check_call([ 'wine', '/opt/makani/third_party/TurbSim/TurbSim64.exe', 'TurbSim.inp' ]) except: raise Exception('Could not find 64bit version of TurbSim. ' 'To update, run a new build or try:\n' ' cd /opt/makani/third_party && git fetch && ' ' git checkout origin/master')
'git diff HEAD --name-only'.split()).strip(): raise RuntimeError('There are uncommitted changes; --fixup is not ' 'allowed') if FLAGS.check_if_file_supported: if IsSupportedFile(argv[1]): sys.exit(0) else: sys.exit(1) files = GatherFiles() for filename in files['c'] + files['js']: subprocess.check_call( ['clang-format-3.8.1', '-style=google', '-i', filename]) if files['build']: subprocess.check_call([ 'bazel', 'run', '@com_github_bazelbuild_buildtools//buildifier', '--crosstool_top=@bazel_tools//tools/cpp:default-toolchain', '--', '--lint=fix', '--warnings=all' ] + files['build']) if FLAGS.fixup: subprocess.check_call('git commit -a --amend --no-edit'.split()) if __name__ == '__main__': with os_util.ChangeDir(_WORKSPACE_ROOT): main(sys.argv)
def testChangeDir(self): original_dir = os.getcwd() with os_util.ChangeDir('/tmp'): self.assertEqual(os.path.realpath('/tmp'), os.getcwd()) self.assertEqual(original_dir, os.getcwd())