Best Python code snippet using localstack_python
folder_tree.py
Source:folder_tree.py
1import re2import hashlib3import logging4from pathlib import Path5from itertools import islice6from functools import partial7from treelib import tree8def tree_clean_up(contents: list, exclude_filenames: list, exclude_paths: list):9 contents = [_x for _x in contents \10 if not any(re.match(_pattern, _x.parts[-1]) for _pattern in exclude_filenames)]11 contents = [_x for _x in contents \12 if not any(str(_x).endswith(_path) for _path in exclude_paths)]13 return sorted(contents)14def parse(15 dir_path: Path, config : dict, 16 level: int=-1, limit_to_directories: bool=False,17 ):18 """Given a directory Path object to parse a tree structure"""19 clean_up =partial(20 tree_clean_up,21 exclude_filenames = config['folder_analysis']['exclude_filenames'], 22 exclude_paths = config['folder_analysis']['exclude_paths'])23 dir_tree = tree.Tree()24 dir_tree.create_node('.', dir_path) # root node25 def inner(dir_path: Path, level=-1):26 if not level: 27 return # 0, stop iterating28 if limit_to_directories:29 contents = [d for d in dir_path.iterdir() if d.is_dir()]30 else: 31 contents = list(dir_path.iterdir())32 contents = clean_up(contents)33 for path in contents:34 if path.is_dir():35 dir_tree.create_node(str(path.parts[-1]), path, path.parent)36 yield 37 yield from inner(path, level=level-1)38 elif not limit_to_directories:39 dir_tree.create_node(str(path.parts[-1]), path, path.parent)40 yield41 _ = list(inner(dir_path, level=level))42 hash_tree(dir_tree)43 return dir_tree44class Node_data(object):45 def __init__(self, hash_value, is_empty_dir):46 self.hash = hash_value47 self.is_empty_dir = is_empty_dir48 49def hash_tree(tree):50 root = tree.get_node(tree.root)51 if root.is_leaf():52 hash_leaf(root)53 else:54 m = hashlib.sha256()55 is_empty_dir = True56 for child in tree.children(root.identifier):57 hash_tree(tree.subtree(child.identifier))58 m.update(str.encode(child.data.hash))59 is_empty_dir = is_empty_dir & child.data.is_empty_dir60 root.data = Node_data(m.hexdigest(), is_empty_dir)61 62def hash_leaf(node):63 if node.identifier.is_file():64 node.data = Node_data(hash_file(node.identifier), False)65 else:66 # Empty folder67 node.data = Node_data(hashlib.sha256(b"empty").hexdigest(), True)68def hash_file(f_path):69 m = hashlib.sha256()70 f = open(f_path, 'rb')71 for _ in range(1000):72 # Read file in as little chunks, up to 4 MB73 buf = f.read(4096)74 if not buf : break75 m.update(buf)76 f.close()77 return m.hexdigest()78def tree_as_dict(folder_tree):79 node_dict = {}80 for node in folder_tree.all_nodes():81 str_path = str(get_relative_path(folder_tree, node))82 hash_vale = node.data.hash83 node_dict[str_path] = hash_vale84 return node_dict85def scan_empty_dirs(folder_tree):86 for node in folder_tree.all_nodes():87 if node.data.is_empty_dir:88 path_name = get_relative_path(folder_tree, node)89 if list(node.identifier.glob("**/.gitkeep")):90 pass91 else:92 logging.error(f'Found a empty directory: {path_name}')93def get_relative_path(folder_tree, node):94 return node.identifier.relative_to(folder_tree.root)95def scan_holdover_items(matser_folder_tree, template_folder_tree):96 matser_folder_dict = tree_as_dict(matser_folder_tree)97 template_folder_dict = tree_as_dict(template_folder_tree)98 for str_path, hash_value in matser_folder_dict.items():99 if hash_value == template_folder_dict.get(str_path, 'None'):...
99_control.py
Source:99_control.py
...14 pass15def change_dir(dir_name):16 if os.getcwd() != os.path.join(WORK_DIR, dir_name):17 return os.chdir(os.path.join(WORK_DIR, dir_name))18def is_empty_dir(dir_name):19 for path, dirs, files in os.walk(os.path.join(WORK_DIR, dir_name)):20 for file in files:21 os.unlink(os.path.join(path, file))22def get_zips():23 ftp_dirs = ['contracts', 'notifications', 'plangraphs2020']24 ftp = FTP('ftp.zakupki.gov.ru')25 ftp.set_pasv(True)26 ftp.login('free', 'free')27 ftp.cwd(FTP_WORK_DIR)28 change_dir('Temp')29 for ftp_dir in ftp_dirs:30 for folder in ['currMonth', 'prevMonth']:31 zips = list()32 ftp.cwd('//'.join([FTP_WORK_DIR, ftp_dir, folder]))33 ftp.dir(zips.append)34 for zippy in zips:35 tokkens = zippy.split()36 date_file_str = tokkens[5] + " " + tokkens[6] + " " + tokkens[7]37 date_file = parser.parse(date_file_str)38 name = tokkens[8]39 if parser.parse(END_DATE) >= date_file >= parser.parse(START_DATE):40 with open(ftp_dir + '_' + name, 'wb') as f:41 ftp.retrbinary('RETR ' + name, f.write)42 ftp.close()43def main():44 create_dir('Temp')45 create_dir('Result')46 is_empty_dir('Temp')47 is_empty_dir('Result')48 get_zips()49 for item in os.listdir():50 z = zipfile.ZipFile(item)51 for xml in z.namelist():52 if xml.endswith('.xml'):53 z.extract(xml, os.path.join(WORK_DIR, 'Result'))54 os.chdir(os.path.join(WORK_DIR, 'Result'))55 # if 'res.zip' in os.listdir():56 # os.unlink(os.path.join(WORK_DIR, 'res.zip'))57 with zipfile.ZipFile(os.path.join(WORK_DIR, 'res.zip'), 'w') as my_zip_file:58 for file in os.listdir():59 my_zip_file.write(file)60 print('Ðожно забиÑаÑÑ')61 # is_empty_dir('Temp')62 # is_empty_dir('Result')63def copy_to_serv():64 host = config.get('host')65 user = config.get('user')66 secret = config.get('secret')67 port = config.get('port')68 remote_path = config.get('remote_path')69 local_path = f'{WORK_DIR}//Result'70 transport = paramiko.Transport((host, int(port)))71 transport.connect(username=user, password=secret)72 sftp = paramiko.SFTPClient.from_transport(transport)73 for file in os.listdir(local_path):74 print(f'{local_path}//{file}')75 print(f'{remote_path}//{file}')76 sftp.put(f'{local_path}//{file}', f'{remote_path}//{file}')...
_filesys_utils.py
Source:_filesys_utils.py
...9 !mkdir not_empty_dir10 !cp -r empty_dir/ not_empty_dir/11 !cp not_empty_file not_empty_dir/12 FH = FilesysHelper()13 print(FH.is_empty_dir("empty_dir/"))14 print(FH.is_empty_dir("./"))15 print(FH.is_empty_dir("not"))16 print(FH.is_empty_file("empty_file"))17 print(FH.is_empty_file("empty_dir/"))18 """19 def get_file_update_time_utc(self, path):20 return dtm.datetime.strftime(dtm.datetime.utcfromtimestamp(os.path.getmtime(path)),"%Y%m%d%H%M%S")21 def is_exist(self,path):22 return os.path.exists(path)23 24 def is_file(self,path):25 if not self.is_exist(path):26 print(f"No such file! {path}")27 return False28 return os.path.isfile(path)29 30 def is_dir(self,path):31 return os.path.isdir(path)32 33 def is_empty_file(self,path):34 if not self.is_exist(path):35 print(f"No such file! {path}")36 return False37 if not self.is_file(path):38 print(f"Not file type! {path}")39 return False40 else:41 return os.path.getsize(path) == 042 def is_empty_dir(self,path):43 if not self.is_exist(path):44 print(f"No such dir! {path}")45 return False46 if not self.is_dir(path):47 print(f"Not dir type! {path}")48 return False49 else:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!