def updateSTRepo(): # Handle STM32Cube repo for serie in stm32_list: repo_name = f"{repo_generic_name}{serie}" repo_path = repo_local_path / repo_name gh_STM32Cube = urljoin(gh_st, f"{repo_name}.git") print(f"Updating {repo_name}...") if repo_path.exists(): rname, bname = getRepoBranchName(repo_path) # Get new tags from the remote git_cmds = [ ["git", "-C", repo_path, "fetch"], [ "git", "-C", repo_path, "checkout", "-B", bname, f"{rname}/{bname}", ], ] else: # Clone it as it does not exists yet git_cmds = [["git", "-C", repo_local_path, "clone", gh_STM32Cube]] for cmd in git_cmds: execute_cmd(cmd, None) latestTag(serie, repo_name, repo_path) checkVersion(serie, repo_path)
def set_height(self, message): """ Example: ["height",127] desired_height range [0...127] """ values = message.split(',') # extract commmand attributes desired_height = int(values[1]) assert desired_height < 128 and desired_height > 0 cmd = [4, 0, 7, desired_height] execute_cmd(cmd, self.mekamon_uart, desc="Height") """ # TODO needs work to smooth out motion. # Linear interpolation still jolty if desired_height > self.current_height: for x in interpolate_range(self.current_height, desired_height, 15): cmd = [4,0,7,x] execute_cmd(cmd, self.mekamon_uart, desc="Rising") else: for x in interpolate_range(desired_height, self.current_height, -15): cmd = [4,0,7,x] execute_cmd(cmd, self.mekamon_uart, desc="Lowering") """ # update current height self.current_height = desired_height
def start(self): """启动实时同步配置文件接受端""" # 初始化rsync配置文件 self.sync_files.update_rsyncd_conf(self.remote_ip_lst) rsync_serv_cmd = ['rsync', '--daemon', '--config=%s' % self.sync_files.rsync_cfg_path] execute_cmd(rsync_serv_cmd)
def start(self): """启动实时同步配置文件接受端""" # 初始化rsync配置文件 self.sync_files.update_rsyncd_conf(self.remote_ip_lst) rsync_serv_cmd = [ 'rsync', '--daemon', '--config=%s' % self.sync_files.rsync_cfg_path ] execute_cmd(rsync_serv_cmd)
def applyBlePatch(): print(" Applying patches to ble library") BLE_patch_path = repo_local_path / repo_ble_name / "extras" / "STM32Cube_FW" patch_list = [] if BLE_patch_path.is_dir(): for file in sorted(BLE_patch_path.iterdir()): if file.name.endswith(".patch"): patch_list.append(BLE_patch_path / file) if len(patch_list): patch_failed = [] print( f" Apply {len(patch_list)} patch{'' if len(patch_list) == 1 else 'es'} for BLE library" ) for patch in patch_list: try: # Test the patch before apply it status = execute_cmd( [ "git", "-C", repo_local_path / repo_ble_name, "apply", "--check", patch, ], subprocess.STDOUT, ) if status: print(f"patch {patch} can't be applied") patch_failed.append([patch, status]) continue # Apply the patch status = execute_cmd( [ "git", "-C", repo_local_path / repo_ble_name, "am", "--keep-non-patch", "--quiet", "--signoff", patch, ], None, ) except subprocess.CalledProcessError as e: patch_failed.append([patch, e.cmd, e.output.decode("utf-8")]) # print(f"Failed command: {e.cmd}") if len(patch_failed): for fp in patch_failed: e_out = "" if len(fp) == 2 else f"\n--> {fp[2]}" print(f"Failed to apply {fp[0]}:\n{fp[1]}{e_out}")
def applyPatch(serie, HAL_updated, CMSIS_updated, repo_path): # First check if some patch need to be applied patch_path = script_path / "patch" patch_list = [] if HAL_updated: HAL_patch_path = patch_path / "HAL" / serie if HAL_patch_path.is_dir(): for file in HAL_patch_path.iterdir(): if file.name.endswith(".patch"): patch_list.append(HAL_patch_path / file) if CMSIS_updated: CMSIS_patch_path = patch_path / "CMSIS" / serie if CMSIS_patch_path.is_dir(): for file in CMSIS_patch_path.iterdir(): if file.name.endswith(".patch"): patch_list.append(CMSIS_patch_path / file) if len(patch_list): patch_failed = [] print( f"Apply {len(patch_list)} patch{'' if len(patch_list) == 1 else 'es'} for {serie}" ) for patch in patch_list: try: # Test the patch before apply it status = execute_cmd( ["git", "-C", repo_path, "apply", "--check", patch], subprocess.STDOUT, ) if status: # print(f"patch {patch} can't be applied") patch_failed.append([patch, status]) continue # Apply the patch status = execute_cmd( [ "git", "-C", repo_path, "am", "--keep-non-patch", "--quiet", "--signoff", patch, ], None, ) except subprocess.CalledProcessError as e: patch_failed.append([patch, e.cmd, e.output.decode("utf-8")]) # print(f"Failed command: {e.cmd}") if len(patch_failed): for fp in patch_failed: e_out = "" if len(fp) == 2 else f"\n--> {fp[2]}" print(f"Failed to apply {fp[0]}:\n{fp[1]}{e_out}")
def should_have_handled_exception_during_executing_cmd(monkeypatch) -> None: def mock_run(*args: tuple, **kwargs: dict) -> None: raise subprocess.CalledProcessError('', '') def mock_raise_error(*args: tuple, **kwargs: dict) -> None: assert args[1] == cmd monkeypatch.setattr(f"{MODULE_NAME}.subprocess.run", mock_run) monkeypatch.setattr(f"{MODULE_NAME}.raise_error", mock_raise_error) cmd = [next_alphabetic(10), next_alphanumeric(16)] utils.execute_cmd(cmd)
def latestTag(serie, repo_name, repo_path): global cube_versions # Checkout the latest tag sha1_id = execute_cmd( ["git", "-C", repo_path, "rev-list", "--tags", "--max-count=1"], None) version_tag = execute_cmd( ["git", "-C", repo_path, "describe", "--tags", sha1_id], None) execute_cmd(["git", "-C", repo_path, "checkout", version_tag], subprocess.DEVNULL) cube_versions[serie] = version_tag
def run_gocover(path: Path, platform: str) -> None: """Run gocover.""" cmd = f"""export TEMP_DIR=/tmp/temp-ingress \ && mkdir -p $TEMP_DIR/bus \ && cp -r handlers config $TEMP_DIR \ && cp bus/{platform}*.go $TEMP_DIR/bus/ \ && cp main_{platform}.go runner.go go.* $TEMP_DIR/ \ && cd $TEMP_DIR \ && go mod tidy \ && go test -tags test -coverprofile=$TEMP_DIR/go-cover.tmp ./... > /dev/null \ && go tool cover -func $TEMP_DIR/go-cover.tmp -o {path} \ && cd /tmp && rm -r $TEMP_DIR""" utils.execute_cmd(cmd)
def should_have_handled_keyboard_interrupt_during_executing_cmd( monkeypatch) -> None: def mock_run(*args: tuple, **kwargs: dict) -> None: raise KeyboardInterrupt('', '') def mock_print_cmd(*args: tuple, **kwargs: dict) -> None: assert args[0] == cmd monkeypatch.setattr(f"{MODULE_NAME}.subprocess.run", mock_run) monkeypatch.setattr(f"{MODULE_NAME}.print_coloured", lambda *a, **k: None) monkeypatch.setattr(f"{MODULE_NAME}.print_cmd", mock_print_cmd) cmd = [next_alphabetic(10), next_alphanumeric(16)] with pytest.raises(SystemExit) as e: utils.execute_cmd(cmd) assert e.type == SystemExit assert e.value.code == 1
def commitFiles(repo_path, commit_msg): # Check if there is something to commit status = execute_cmd( ["git", "-C", repo_path, "status", "--untracked-files", "--short"], None) if not status: return # Staged all files: new, modified and deleted execute_cmd(["git", "-C", repo_path, "add", "--all"], subprocess.DEVNULL) # Commit all stage files with signoff and message execute_cmd( [ "git", "-C", repo_path, "commit", "--all", "--signoff", f"--message={commit_msg}", ], subprocess.DEVNULL, ) # Remove trailing space execute_cmd( ["git", "-C", repo_path, "rebase", "--whitespace=fix", "HEAD~1"], subprocess.DEVNULL, )
def retrain(data): """ /var/opt/ppr/ |- data/ |- graph |- 11397283704/ |- 11397283928/ |- src/ """ PPR_DATA_DIR = '/var/opt/ppr/data' PPR_CODE_DIR = '/var/opt/ppr/src' import subprocess from os.path import abspath, dirname, join import sys sys.path.insert(0, join(dirname(dirname(abspath(__file__))), 'modules')) import utils sys.path.pop(0) latest_dir = utils.get_latest_dir(PPR_DATA_DIR) new_dir = utils.create_child_dir(PPR_DATA_DIR) with open('{}/edges'.format(new_dir), 'w') as f: for (u, v) in data: f.write('{} {}\n'.format(int(u), int(v))) if latest_dir is not None: cmd = ''' cd {cwd} && export SBT_OPTS="-Xmx8G -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=8G" && sbt "run retrain {prev_dir} {res_dir} edges {res_dir}" &>{res_dir}/log '''.format( cwd=PPR_CODE_DIR, prev_dir=latest_dir, res_dir=new_dir) else: cmd = ''' cd {cwd} && export SBT_OPTS="-Xmx8G -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=8G" && sbt "run train {graph_dir} graph 0.0015 0.05 89805 {res_dir} edges {res_dir}" &>{res_dir}/log '''.format( cwd=PPR_CODE_DIR, graph_dir=PPR_DATA_DIR, res_dir=new_dir) utils.execute_cmd(cmd) import time time.sleep(10) utils.commit_dir(new_dir)
def execute(self) -> str: config = self.get_config(CONFIG_FILE_PATH) instructions = { 'connect': f"connect \"{config['configuration-name']}\"", 'quit': 'quit', } instruction = instructions[self.argv[0]] apple_script = self.build_apple_script(instruction) log(f"Instructing Tunnelblick to {instruction}") return execute_cmd(['osascript', '-e', apple_script]).strip()
def fmri_reg_to_anat(self, suffix='', maskSuffix='_tightmask'): print("---------8.Registering fmri to anat brain ------------") if not suffix: suffix = self.fmri_proc_suffix_dict['reg'] if not os.path.isfile(self.anat_brain): self.get_anat_brain_and_mask() anat_brain_file = os.path.join(self.anat_dir, self.anat_brain) if not self.anat_roi_labels: anat_label = anat_brain_file.replace('.nii', '_label.nii') assert os.path.isfile(anat_label), "----The anat ROI label file was not found: {} !!".format(anat_label) else: anat_label = os.path.join(self.anat_dir, self.anat_roi_labels) self.fmri_roi_labels = ants_reg_fmri_to_anat(self.fmri_dir, anat_brain_file, self.fmri_proc_name, anat_label, label_suffix=suffix, applyOnly=False) os.chdir(self.fmri_dir) mask_ = glob.glob(self.fmri_name.replace('.nii', '*{}.nii'.format(maskSuffix))) assert mask_, "----fmri mask not found!!" mask = mask_[0] execute_cmd("fslmaths", "{0}" \ " -mas {1} {0}".format(self.fmri_roi_labels, mask))
def retrain(data): """ /var/opt/mf/ |- data/ |- 0000000000/ |- model |- lock |- 1523335358/ ... |- src/ """ MF_DATA_DIR = '/var/opt/mf/data' MF_CODE_DIR = '/var/opt/mf/src' import subprocess import time from os.path import abspath, dirname, join import sys sys.path.insert(0, join(dirname(dirname(abspath(__file__))), 'modules')) import utils sys.path.pop(0) latest_dir = utils.get_latest_dir(MF_DATA_DIR) new_dir = utils.create_child_dir(MF_DATA_DIR) with open('{}/data'.format(new_dir), 'w') as f: for (user, item) in data: f.write('{}\t{}\t1.0\t00000000\n'.format(int(user), int(item))) # We will not create a new model every time and always use the initial base model # because the model takes too much space. This is the reason why we skip committing # and modify FastMF source code to skip saving. cmd = ''' cd {cwd} && mill FastMF.run retrain {prev_dir}/model {res_dir}/data {res_dir}/model &>{res_dir}/log '''.format(cwd=MF_CODE_DIR, prev_dir=latest_dir, res_dir=new_dir) utils.execute_cmd(cmd) time.sleep(10)
def xyz_motion(self, message): """ Input command: [6,fwd,strafe,turn] fwd = -128-127 # signed byte strafe = -128-127 # signed byte turn = -128-127 # signed byte """ values = message.split(',') cmd_type = int(values[1]) fwd = int(values[2]) strafe = int(values[3]) turn = int(values[4]) assert cmd_type == 6 # 6 == motion assert fwd < 128 and fwd >= -128 assert strafe < 128 and strafe >= -128 assert turn < 128 and turn >= -128 cmd = [cmd_type, fwd, strafe, turn] execute_cmd(cmd, self.mekamon_uart, desc="Motion")
def updateBleRepo(): # Handle BLE library repo repo_path = repo_local_path / repo_ble_name print(f"Updating {repo_ble_name}...") if repo_path.exists(): rname, bname = getRepoBranchName(repo_path) # Get new tags from the remote git_cmds = [ ["git", "-C", repo_path, "fetch"], [ "git", "-C", repo_path, "checkout", "-B", bname, f"{rname}/{bname}", ], ] else: # Clone it as it does not exists yet git_cmds = [["git", "-C", repo_local_path, "clone", gh_ble]] for cmd in git_cmds: execute_cmd(cmd, None)
def set_wifi(self, on: bool) -> str: """Turns Wi-Fi to the given state. Args: on (bool): Whether to turn Wi-Fi on. Returns: Stdout of the command execution. """ power = 'on' if on else 'off' log(f"Turning Wi-Fi {power}") # 'Hardware Port: Wi-Fi\nDevice: en0 ...' hardware_ports = execute_cmd(['networksetup', '-listallhardwareports']).strip() # ['Hardware Port: Wi-Fi', 'Device: en0'] hardware_ports_lines = hardware_ports.splitlines() device_name = '' for i, line in enumerate(hardware_ports_lines): if 'Wi-Fi' in line: # 'Hardware Port: Wi-Fi' device_name_line = hardware_ports_lines[i + 1] # 'Device: en0' device_name = device_name_line.split()[1] # 'en0' break return execute_cmd( ['networksetup', '-setairportpower', device_name, power]).strip()
def retrain(data): """ /var/opt/mallet/ |- tool/ |- data/ |- dictionary |- 11397283704/ |- text |- data |- model |- 11397283928/ """ MALLET_BIN = '/var/opt/mallet/tool/bin/mallet' MALLET_DATA_DIR = '/var/opt/mallet/data' import subprocess from os.path import abspath, dirname, join import sys sys.path.insert(0, join(dirname(dirname(abspath(__file__))), 'modules')) import utils sys.path.pop(0) latest_dir = utils.get_latest_dir(MALLET_DATA_DIR) new_dir = utils.create_child_dir(MALLET_DATA_DIR) text_path = '{}/text'.format(new_dir) data_path = '{}/data'.format(new_dir) model_path = '{}/model'.format(new_dir) dict_path = '{}/dictionary'.format(MALLET_DATA_DIR) with open('{}/text'.format(new_dir), 'w') as f: for chars in data: f.write(''.join(map(lambda d: chr(int(d)), chars)) + '\n') utils.execute_cmd("cp {} {}".format(dict_path, new_dir)) dict_path = '{}/dictionary'.format(new_dir) utils.execute_cmd(( "{bin} import-file --input {input} --output {output} --token-regex '[\p{{L}}\p{{P}}]+' " "--keep-sequence --remove-stopwords --use-pipe-from {dictionary} " ).format(bin=MALLET_BIN, input=text_path, output=data_path, dictionary=dict_path)) utils.execute_cmd(( "{bin} train-topics --input {input} --num-topics 10 --output-model {model} " "--num-iterations 1000 --show-topics-interval 1000000 {base_model}" ).format(bin=MALLET_BIN, input=data_path, model=model_path, base_model=('' if latest_dir is None else '--input-model {}/model'.format(latest_dir)))) import time time.sleep(10) utils.commit_dir(new_dir)
def should_have_executed_cmd(monkeypatch) -> None: def mock_run(*args: tuple, **kwargs: dict): class MockResult: stdout = b'SUCCESS' return MockResult() monkeypatch.setattr(f"{MODULE_NAME}.subprocess.run", mock_run) raise_error_calls = [] monkeypatch.setattr(f"{MODULE_NAME}.raise_error", lambda *a, **k: raise_error_calls.append('')) print_cmd_calls = [] monkeypatch.setattr(f"{MODULE_NAME}.print_cmd", lambda *a, **k: print_cmd_calls.append('')) cmd = [next_alphabetic(10), next_alphanumeric(16)] assert utils.execute_cmd(cmd) == 'SUCCESS' assert len(raise_error_calls) == 0 assert len(print_cmd_calls) == 0
def anat_brain_masking(self, prevSuffix='_x10.nii', bet_threshold=0.5, bfc=True): print(f"---------6.Creating brain mask on anatomical image '{self.anat_name}'------------") dir_backup = os.getcwd() os.chdir(self.anat_dir) assert os.path.exists(self.anat_name), "File not found!" nii_ = self.anat_name.replace(".nii", prevSuffix) nii_basename = nii_.split('.nii')[0] # Bias field correction if bfc: execute_cmd('fast', '-t 2 -n 3 -H 0.1 -I 4 -l 20.0 --nopve -B -o {0}'.format(nii_)) execute_cmd('rm', '{0}'.format(nii_.replace('.nii', '_seg.nii'))) nii_basename = "{}_restore".format(nii_basename) # creating mask execute_cmd('bet', '{0} {0} -f {1} -g 0 -n -m'.format(nii_basename, bet_threshold)) # masking anatomical image execute_cmd("fslmaths", "{0}" \ " -mas {0}_mask {0}_brain".format(nii_basename)) os.chdir(dir_backup) self.anat_brain_mask = "{0}_mask.nii.gz".format(nii_basename) self.anat_brain = "{0}_brain.nii.gz".format(nii_basename)
def execute(self) -> str: log(f"Changing volume to {self.volume}") apple_script = self.build_apple_script(self.volume) return execute_cmd(['osascript', '-e', apple_script]).strip()
def execute(self) -> str: log(f"Changing brightness to {self.brightness}") return execute_cmd(['brightness', str(self.brightness)])
def execute(self) -> str: log(f"Turning Bluetooth {self.argv[0]}") on = self.argv[0] == 'on' return execute_cmd(['blueutil', '-p', ('1' if on else '0')]).strip()
def run_check(self): output = execute_cmd(self.shell_cmd).strip() result = self.check(output) self.report(result)
def run_gocover(path: Path) -> None: """Run gocover.""" utils.execute_cmd("""""")
def clear_primary(): """Clear primary.""" utils.execute_cmd('xsel -pc')
def tell_appearance_preferences(self, apple_script: Tuple[str]) -> str: cmd = '\n'.join(('tell application "System Events"', 'tell appearance preferences') + apple_script + ('end tell', 'end tell')) return execute_cmd(['osascript', '-e', cmd]).strip()
def raw_motion(self, message): values = message.split(',') desc = values[0] cmd = [int(x) for x in values[1:]] execute_cmd(cmd, self.mekamon_uart, desc=desc)
def clear_clipboard(): """Clear clipboard.""" utils.execute_cmd('xsel -bc')
def execute(self) -> str: log('Putting the machine to sleep') return execute_cmd(['pmset', 'sleepnow']).strip()