zipfile_interface.py
Go to the documentation of this file.
1 import os
2 import yaml
3 import shutil
4 import zipfile
5 import tempfile
6 import collections
7 from roscompile.util import make_executable
8 
9 
11  def __init__(self, package_name, pkg_files, executables):
12  self.package_name = package_name
13  self.is_written = False
14  self.root = self.get_input_root()
15  self.pkg_files = pkg_files
16  self.executables = executables
17 
18  def copy(self):
20 
21  def get_input_root(self):
22  return os.path.join(tempfile.gettempdir(), self.package_name)
23 
24  def __enter__(self):
25  self.write()
26  self.is_written = True
27  return self
28 
29  def __exit__(self, type, value, traceback):
30  self.is_written = False
31  self.clear()
32 
33  def get_filenames(self):
34  if self.is_written:
35  the_files = []
36  for folder, _, files in os.walk(self.root):
37  short_folder = folder.replace(self.root, '')
38  if len(short_folder) > 0 and short_folder[0] == '/':
39  short_folder = short_folder[1:]
40  for fn in files:
41  the_files.append(os.path.join(short_folder, fn))
42  return set(the_files)
43  else:
44  return set(self.pkg_files.keys())
45 
46  def get_contents(self, filename):
47  if self.is_written:
48  full_path = os.path.join(self.root, filename)
49  if os.path.exists(full_path):
50  return open(full_path).read().replace('\r\n', '\n')
51  elif filename in self.pkg_files:
52  return self.pkg_files[filename].replace('\r\n', '\n')
53 
54  def compare_filesets(self, other_package):
55  in_keys = self.get_filenames()
56  out_keys = other_package.get_filenames()
57  matches = in_keys.intersection(out_keys)
58  missed_deletes = in_keys - out_keys
59  missed_generations = out_keys - in_keys
60  return matches, missed_deletes, missed_generations
61 
62  def write(self):
63  self.clear()
64  os.mkdir(self.root)
65  for fn, contents in self.pkg_files.items():
66  outfile = os.path.join(self.root, fn)
67  parts = outfile.split(os.sep)
68  # Parts will be '', tmp, pkg_name, possible_folders, actual_filename
69  # Create the possible_folders as needed
70  for i in range(4, len(parts)):
71  new_folder = os.sep.join(parts[:i])
72  if not os.path.exists(new_folder):
73  os.mkdir(new_folder)
74  with open(outfile, 'w') as f:
75  f.write(contents)
76  if fn in self.executables:
77  make_executable(outfile)
78 
79  def clear(self):
80  if os.path.exists(self.root):
81  shutil.rmtree(self.root)
82 
83  def __repr__(self):
84  return self.package_name
85 
86 
87 def get_test_cases(zip_filename):
88  file_data = collections.defaultdict(dict)
89  zf = zipfile.ZipFile(zip_filename)
90  config = None
91  executables = set()
92  for file in zf.filelist:
93  if file.filename[-1] == '/':
94  continue
95  if file.filename == 'list_o_tests.yaml':
96  config = yaml.safe_load(zf.read(file))
97  continue
98  parts = file.filename.split(os.path.sep)
99  package = parts[0]
100  path = os.path.join(*parts[1:])
101  file_data[package][path] = zf.read(file).decode()
102  if (file.external_attr >> 16) & 0o111:
103  executables.add(path)
104 
105  test_data = {}
106  for package, D in file_data.items():
107  test_data[package] = ROSCompilePackageFiles(package, D, executables)
108  for D in config:
109  if 'function' in D:
110  D['functions'] = [D['function']]
111  del D['function']
112  return config, test_data
def __exit__(self, type, value, traceback)
def make_executable(fn)
Definition: util.py:44
def __init__(self, package_name, pkg_files, executables)
def get_test_cases(zip_filename)


roscompile
Author(s):
autogenerated on Wed Mar 3 2021 03:56:01