file_helpers.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Copyright (c) 2020, Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License").
5 # You may not use this file except in compliance with the License.
6 # A copy of the License is located at
7 #
8 # http://aws.amazon.com/apache2.0
9 #
10 # or in the "license" file accompanying this file. This file is distributed
11 # on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 # express or implied. See the License for the specific language governing
13 # permissions and limitations under the License.
14 
15 import glob
16 import os
17 import random
18 import string
19 import tempfile
20 
21 def create_temp_files(total_files):
22  temp_file_names = []
23  for _ in range(total_files):
24  temp_file_names.append(create_temp_file())
25  return temp_file_names
26 
28  with tempfile.NamedTemporaryFile(suffix=".txt", delete=False) as temp_file:
29  file_contents = ''.join(
30  [random.choice(string.ascii_letters + string.digits + ' ') for _ in range(64)])
31  temp_file.write(file_contents)
32  return temp_file.name
33 
34 def create_large_temp_file(file_size_in_mb):
35  with tempfile.NamedTemporaryFile(suffix=".txt", delete=False) as temp_file:
36  temp_file.seek(file_size_in_mb * 1024 * 1024 - 1)
37  temp_file.write(b'0')
38  return temp_file.name
39 
40 def get_latest_bag_by_regex(directory, regex_pattern):
41  return get_latest_bags_by_regex(directory, regex_pattern, 1)[0]
42 
43 def get_latest_bags_by_regex(directory, regex_pattern, count=None):
44  files = glob.iglob(os.path.join(directory, regex_pattern))
45  paths = [os.path.join(directory, filename) for filename in files]
46  paths.sort(key=os.path.getctime, reverse=True)
47  if count is None:
48  return paths
49  else:
50  return paths[:count]
def create_temp_files(total_files)
Definition: file_helpers.py:21
def get_latest_bag_by_regex(directory, regex_pattern)
Definition: file_helpers.py:40
def get_latest_bags_by_regex(directory, regex_pattern, count=None)
Definition: file_helpers.py:43
def create_temp_file()
Definition: file_helpers.py:27
def create_large_temp_file(file_size_in_mb)
Definition: file_helpers.py:34


integ_tests
Author(s): AWS RoboMaker
autogenerated on Tue Jun 1 2021 02:51:32