Skip to content

Commit

Permalink
Merge branch 'RESTAPI-1144-ls-parsing-fix' into 'master'
Browse files Browse the repository at this point in the history
Restapi 1144 ls parsing fix

See merge request firecrest/firecrest!307
  • Loading branch information
ekouts committed Jul 4, 2024
2 parents db6ba4b + 387a79c commit 7fe2f01
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Fix parsing in `GET /utilities/ls` endpoint.

## [1.16.0]

### Added
Expand Down
57 changes: 30 additions & 27 deletions src/utilities/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,33 +178,36 @@ def ls_parse_folder(folder_content:str,path:str=""):
# drwxrwxr-x 3 username groupname 4096 2023-07-24T11:45:35 "folder"
file_list = []
file_pattern = (r'^(?P<type>\S)(?P<permissions>\S+)\s+\d+\s+(?P<user>\S+)\s+'
r'(?P<group>\S+)\s+(?P<size>\d+)\s+(?P<last_modified>(\d|-|T|:)+)\s+(?P<filename>.+)$')
matches = re.finditer(file_pattern, folder_content, re.MULTILINE)

for m in matches:
tokens = shlex.split(m.group("filename"))
if len(tokens) == 1:
name = tokens[0]
link_target = ""
elif len(tokens) == 3:
# We could add an assertion that m.group("type") == 'l' if
# we want to be sure that this is a link
name = tokens[0]
link_target = tokens[2]
else:
app.logger.error(f"Cannot get the filename from this line from ls: {m.group()}")
continue

file_list.append({
"name": path + name,
"type": m.group("type"),
"link_target": link_target,
"user": m.group("user"),
"group": m.group("group"),
"permissions": m.group("permissions"),
"last_modified": m.group("last_modified"),
"size": m.group("size")
})
r'(?P<group>\S+)\s+(?P<size>\d+)\s+(?P<last_modified>(\d|-|T|:)+)\s+(?P<filename>.+)$')

lines = folder_content.splitlines()
file_list = []
for entry in lines:
matches = re.finditer(file_pattern, entry)
for m in matches:
tokens = shlex.split(m.group("filename"))
if len(tokens) == 1:
name = tokens[0]
link_target = ""
elif len(tokens) == 3:
# We could add an assertion that m.group("type") == 'l' if
# we want to be sure that this is a link
name = tokens[0]
link_target = tokens[2]
else:
app.logger.error(f"Cannot get the filename from this line from ls: {m.group()}")
continue

file_list.append({
"name": path + name,
"type": m.group("type"),
"link_target": link_target,
"user": m.group("user"),
"group": m.group("group"),
"permissions": m.group("permissions"),
"last_modified": m.group("last_modified"),
"size": m.group("size")
})
return file_list


Expand Down

0 comments on commit 7fe2f01

Please sign in to comment.