def dir_equal(self, left, right): """ Compare two directories recursively. Files in each directory are assumed to be equal if their names and contents are equal. :param left: First directory path :param right: Second directory path @return: True if the directory trees are the same and there were no errors while accessing the directories or files; False otherwise. """ logger.info("Compare {} and {}".format(left, right)) dirs_cmp = dircmp(left, right) ret = True if len(dirs_cmp.left_only) > 0 or len(dirs_cmp.right_only) > 0: self.left_only.extend([ os.path.join(os.path.abspath(left), item) for item in dirs_cmp.left_only if item not in EXCLUDES ]) self.right_only.extend([ os.path.join(os.path.abspath(right), item) for item in dirs_cmp.right_only ]) logger.debug("Append {} to left_only".format(dirs_cmp.left_only)) logger.debug("Append {} to right_only".format(dirs_cmp.right_only)) logger.debug("Common dirs: {}".format(dirs_cmp.common_dirs)) for common_dir in dirs_cmp.common_dirs: inner_left = os.path.join(left, common_dir) inner_right = os.path.join(right, common_dir) if not self.dir_equal(inner_left, inner_right): ret = False return ret
def main(): """ Sets build environment for the target platform and runs CMake :return: CMake return code """ parser = argparse.ArgumentParser(description='Command-line interface') parser.add_argument('--project-dir', help='Root directory of Virtualbox source code', default=os.path.realpath(__file__), required=False) parser.add_argument('--only-count', help='Only count text occurrences, do not replace', action='store_true', default=False, required=False) args = parser.parse_args() logger.info(f"Project dir is {args.project_dir}") if args.only_count: only_count(project_dir=args.project_dir) else: replace_all(project_dir=args.project_dir) return 0
def main(): """ Sets build environment for the target platform and runs CMake :return: CMake return code """ prepare_build_dirs() # Set environment for Windows or POSIX global qt_default_path system_version = '0' if os.name == 'nt': set_windows_environment() system_version = '{0}.{1}'.format(sys.getwindowsversion().major, sys.getwindowsversion().minor) logger.info("SYSTEM_VERSION={0}".format(system_version)) qt_default_path = 'C:/Qt/5.6.3/msvc2010_64' elif os.name == 'posix': set_unix_environment() qt_default_path = '/opt/Qt/5.8/msvc2015' system_version = '1' parser = argparse.ArgumentParser(description='Command-line interface') parser.add_argument('--cleanup', help='Perform build directory cleanup', action='store_true', default=False, required=False) args = parser.parse_args() return 0
def replace_all(project_dir): """ :param project_dir: """ logger.info("Search and replace all text occurrences") for target_file, replacements in REPLACE_CONTENT.items(): logger.info(f"Perform replacements in {target_file}") target_file = os.path.join(os.path.realpath(project_dir), target_file) replace_occurrences(target_file, replacements)
def only_count(project_dir): """ :param project_dir: """ logger.info("Only count text occurrences, do not perform actual replace") for target_file, replacements in REPLACE_CONTENT.items(): logger.info(f"Count replacements in {target_file}") target_file = os.path.join(os.path.realpath(project_dir), target_file) with open(target_file, 'r') as f: file_content = f.read() count_occurrences(target_file, file_content, replacements)
def count_occurrences(target_file, file_content, replacement_pairs): """ :param target_file: file name where to count text occurrences :param file_content: this file content :param replacement_pairs: list of ReplacePair objects; we are interested only in ReplacePair.old_text """ for replacement_pair in replacement_pairs: num_occurrences = file_content.count(replacement_pair.old_text) preview_str = preview_string(replacement_pair.old_text) logger.info( f"In {target_file} {num_occurrences} occurrences of '{preview_str}' have been found" )
def main(): """ :return: Exec return code """ parser = argparse.ArgumentParser(description='Command-line interface') parser.add_argument( '--old-version', help='Old version of Virtualbox, which we want to update', required=True) parser.add_argument( '--new-version', help='New version of Virtualbox, where from we copy updates', required=True) parser.add_argument( '--git-repo', help= 'Directory which contains Git repo to merge; old_version by default', default=None, required=False) parser.add_argument('--only-compare', help='Only compare old and new versions; do not merge', action='store_true', default=False, required=False) args = parser.parse_args() git_repo = args.old_version if args.git_repo is None else args.git_repo diff = RecursiveDircmp() diff.dir_equal(args.old_version, args.new_version) logger.info("Remove from the old version: {}".format(diff.left_only)) logger.info("Added in new version: {}".format(diff.right_only)) with open('remove_from_old.txt', 'w') as f: f.writelines(["{}\n".format(item) for item in diff.left_only]) with open('added_to_new.txt', 'w') as f: f.writelines(["{}\n".format(item) for item in diff.right_only]) if args.only_compare: return 0 os.chdir(git_repo) logger.info("Change directory to Git repo %s" % git_repo) for remove_item in diff.left_only: os.system("git rm -rf {}".format(remove_item)) shutil.copytree(args.new_version, args.old_version, dirs_exist_ok=True) logger.info("Adding new files to Git repo...") os.system("git add -A") logger.info("Adding new files complete") return 0
def find_avail_cams(): video_capture = cv2.VideoCapture() for i in range(1501): if video_capture.open(i): logger.info("\tCAMERA {} OPENED!".format(i)) for j in range(3): # Read a couple frames, sometimes cameras return a full-green frame on the first read() ret, frame = video_capture.read() video_capture.release() # Close the camera if ret: cv2.imwrite("cam_{}.jpg".format(i), frame) else: logger.error("Ooops, something went wrong accessing the frame! :S") else: logger.debug("Nothing on {}...".format(i))
def save_data_to_file(fields_to_save, out_base_folder, experiment_t_start, filename_prefix, imu_t_start, F_samp): if F_samp <= 0: # F_samp=0 means data was never collected -> Don't save an empty file! logger.warn("Nothing to save, skipping saving data to file") return out_base_folder = os.path.join(out_base_folder, str(experiment_t_start)[:-7].replace(':', '-')) os.makedirs(out_base_folder, exist_ok=True) # Make sure base folder exists out_filename = os.path.join(out_base_folder, "{}{}.h5".format(filename_prefix, str(experiment_t_start)[:-7].replace(':', '-'))) logger.info("Saving collected data at '{}'...".format(out_filename)) with h5py.File(out_filename) as hf: hf.attrs['t_start'] = str(imu_t_start) hf.attrs['F_samp'] = F_samp for field_name, field_children in fields_to_save.items(): field_group = hf.create_group(field_name) for axis, axis_values in field_children.items(): field_group.create_dataset(axis, data=np.array(axis_values, dtype=np.uint8 if isinstance(axis_values[0], int) else np.float32)) logger.success("Done! Collected data saved at '{}' :)".format(out_filename))
def replace_infile(target_file, find_str, replace_str): """ :param target_file: file name where to perform replace :param find_str: substring to find :param replace_str: string to put instead """ # Read in the file logger.debug(f"Full path to the file: {target_file}") with open(target_file, 'r') as fr: file_content = fr.read() # Replace the target string file_content, num_occurrences = search_replace(file_content, find_str, replace_str) preview_str = preview_string(find_str) logger.info( f"In {target_file} have been found and replaced {num_occurrences} occurrences of '{preview_str}'" ) # Write the file out again with open(target_file, 'w') as fw: fw.write(file_content)
def collect_BNO055_data(experiment_t_start=None, serial_port='/dev/cu.SLAB_USBtoUART', baud_rate=115200, out_base_folder='data', out_filename_prefix='BNO055_', imu_ready_queue=None): # NOTE: In order to allow for multiple simultaneous IMUs, experiment_t_start indicates the name of the enclosing folder where the data will be saved, # and each one will have their own imu_t_start indicating the precise time when each IMU was rebooted (and therefore its data started logging) logger.info("Connecting to {}...".format(serial_port)) s = serial.Serial(serial_port, baud_rate) logger.success("Successfully connected! Please reboot the ESP32") # Initialize variables fields_to_save = init_fields_to_save() data_block = SensorData.DataBlock() imu_t_start = None F_samp = 0 try: # Read serial until we receive the " -- SETUP COMPLETE -- " message while True: l = s.readline().rstrip() # Read a line (removing any trailing \r\n) logger.debug(l) if l == b'-- SETUP COMPLETE --': logger.notice("Esp32 is done booting, collecting data until Ctrl+C is pressed!") break # Update imu_t_start to the actual time the data collection started imu_t_start = datetime.now() if experiment_t_start is None: experiment_t_start = imu_t_start # If user didn't specify an experiment t_start, it's probably only collecting data for 1 IMU -> Use imu_t_start to name the data folder if imu_ready_queue is not None: # Pass the current time through the queue to notify the main process to start recording the camera imu_ready_queue.put(imu_t_start) # Collect data (read samples and append them to fields_to_save) last_msg_id = None while True: pb_len = struct.unpack('<H', s.read(2))[0] # Read 2 bytes that indicate how long the proto message will be and convert to uint16 pb = s.read(pb_len) # Read the proto contents data_block.ParseFromString(pb) # Decode them logger.info("Message received: {} (t={:.6f})".format(data_block.id, data_block.t_latest.seconds + data_block.t_latest.nanos/1e9)) # Check if any data got lost (hopefully not :D) if last_msg_id is None: F_samp = data_block.F_samp # Store the sampling frequency so we can save it in the file elif data_block.id - last_msg_id > 1: n_lost = data_block.id - last_msg_id - 1 logger.critical("Lost {} packet{}! (Previous message ID was {}, current is {})".format(n_lost, 's' if n_lost>1 else '', data_block.id, last_msg_id)) last_msg_id = data_block.id # Append new data to fields_to_save for field_name, field_children in fields_to_save.items(): field_children_values = getattr(data_block, field_name) for axis, axis_values in field_children.items(): axis_new_values = getattr(field_children_values, axis) # Fetch new sensor readings axis_values.append(axis_new_values) if isinstance(axis_new_values, int) else axis_values.extend(axis_new_values) # Append values except KeyboardInterrupt: logger.notice("Stopping data collection!") finally: # Close serial port and save all data to a file s.close() save_data_to_file(fields_to_save, out_base_folder, experiment_t_start, out_filename_prefix, imu_t_start, F_samp) logger.success("Goodbye from BNO055 data collection!")