Return an ordered list of the files within the .ar archive. Args: ar_path (str): path to the compiled *.ar file Return Value: Ordered list of file names
(ar_path)
| 1 | def getArchiveFiles(ar_path): |
| 2 | """Return an ordered list of the files within the .ar archive. |
| 3 | |
| 4 | Args: |
| 5 | ar_path (str): path to the compiled *.ar file |
| 6 | |
| 7 | Return Value: |
| 8 | Ordered list of file names |
| 9 | """ |
| 10 | ar_fd = open(ar_path, "rb") |
| 11 | is_windows = ar_path.endswith(".lib") |
| 12 | |
| 13 | # check the signature |
| 14 | if ar_fd.read(8) != b"!<arch>\n": |
| 15 | raise ValueError("Invalid archive signature") |
| 16 | |
| 17 | # split the content to parts |
| 18 | ar_content = ar_fd.read() |
| 19 | names = [] |
| 20 | for ar_part in ar_content.split(b"\x60\x0A")[:-1]: |
| 21 | # .ar file format (unix) seems simpler |
| 22 | if not is_windows: |
| 23 | # sanity check |
| 24 | if len(ar_part) < 58: |
| 25 | continue |
| 26 | # now read the metadata of the record |
| 27 | name = ar_part[-58:].split(b'/')[0] |
| 28 | if not name.endswith(b".o"): |
| 29 | continue |
| 30 | # .lib file format is more complex |
| 31 | else: |
| 32 | if ar_part.find(b".obj") == -1: |
| 33 | continue |
| 34 | name = ar_part.split(b".obj")[-2].split(b"\x00")[-1].split(b"\\")[-1] + b".obj" |
| 35 | name = name.strip() |
| 36 | # append the new record |
| 37 | if name not in names and len(name) > 0: |
| 38 | names.append(name.decode("utf-8")) |
| 39 | ar_fd.close() |
| 40 | return names |