def test__init__(self): temp_file = Temp_File() assert Files.exists (temp_file.tmp_folder) assert Files.not_exists(temp_file.tmp_file) assert Files.not_exists(temp_file.file_path) assert temp_file.tmp_folder in temp_file.file_path assert '/' == temp_file.file_path.replace(temp_file.tmp_folder,'').replace(temp_file.tmp_file,'')
def test__using_with__no_params(self): with Temp_File() as temp: assert Files.file_extension(temp.file_path) == '.tmp' assert Files.exists (temp.file_path) assert Files.contents(temp.file_path) == '...' assert Files.not_exists(temp.file_path) with Temp_File('abc','txt') as temp: assert Files.file_extension(temp.file_path) == '.txt' assert Files.exists (temp.file_path) assert Files.contents(temp.file_path) == 'abc' assert Files.not_exists(temp.file_path)
def load_dependency(target): if os.getenv('AWS_REGION') is None: return from osbot_aws.apis.S3 import S3 import shutil import sys s3 = S3() s3_bucket = 'gw-bot-lambdas' s3_key = 'lambdas-dependencies/{0}.zip'.format(target) tmp_dir = Files.path_combine('/tmp/lambdas-dependencies', target) #return s3.file_exists(s3_bucket,s3_key) if s3.file_exists(s3_bucket, s3_key) is False: raise Exception( "In Lambda load_dependency, could not find dependency for: {0}". format(target)) if Files.not_exists( tmp_dir ): # if the tmp folder doesn't exist it means that we are loading this for the first time (on a new Lambda execution environment) zip_file = s3.file_download( s3_bucket, s3_key, False) # download zip file with dependencies shutil.unpack_archive(zip_file, extract_dir=tmp_dir) # unpack them sys.path.append( tmp_dir ) # add tmp_dir to the path that python uses to check for dependencies return Files.exists(tmp_dir)
def start(self): if Files.not_exists(self.web_root): Files.folder_create(self.web_root) # make sure root folder exists self.server_proc = subprocess.Popen( ["python3", "-m", "http.server", str(self.port)], cwd=self.web_root) self.wait_for_server_started() return self
def upload_dependency(target): s3 = S3() s3_bucket = 'gw-bot-lambdas' s3_file = 'lambdas-dependencies/{0}.zip'.format(target) path_libs = Files.path_combine('../../../_lambda_dependencies/', target) if Files.not_exists(path_libs): raise Exception( "In Lambda upload_dependency, could not find dependency for: {0}". format(target)) s3.folder_upload(path_libs, s3_bucket, s3_file) return s3.file_exists(s3_bucket, s3_file)
def load(self, reload=False): if self.data is None or reload: path = self.name if Files.exists(path) and '.md' in path: self.path_md_file = path else: if '.md' not in path: path = self.hugo_page.fix_name(path) + '.md' if self.name.startswith(self.base_folder) is False: # fix path path = self.base_folder + path self.path_md_file = self.hugo_page.md_file_path(path) if Files.not_exists(self.path_md_file): self.path_md_file = self.hugo_page.find_in_md_files(self.name) self.data = self.hugo_page.load(self.path_md_file) title = self.field('title') if title: self.name = title return self