Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import os
import json
def _expand_paths(path):
"""
Expand wildcarded paths
"""
import re
dir_name = os.path.dirname(path)
paths = []
print("Attempting to expand:", path)
if os.path.isdir(dir_name):
files = os.listdir(dir_name)
match = os.path.basename(path)
for file_path in files:
if re.match(match, file_path):
expanded_path = os.path.join(dir_name, file_path)
paths.append(expanded_path)
return paths
else:
return("Could not expand", path)
if __name__ == "__main__":
with open('test_uploader.json', 'r') as fh:
specs = json.load(fh)
root = '/home/psachin/github/insights/insights-core-assets/test'
paths = None
for spec in specs['files']:
print('\n=== Spec {}: "{}" ==='.format(spec['symbolic_name'],
spec['file']))
if spec['file'][0] == '/':
path = root + spec['file']
else:
path = root + '/' + spec['file']
print('Paths: {}'.format(_expand_paths(path)))
for spec in specs['globs']:
print('\n=== Spec {}: "{}" ==='.format(spec['symbolic_name'],
spec['glob']))
if spec['glob'][0] == '/':
path = root + spec['glob']
else:
path = root + '/' + spec['glob']
print('Paths: {}'.format(_expand_paths(path)))
@psachin
Copy link
Author

psachin commented Dec 26, 2019

test_uploader.json

{
    "files": [
	{
            "file": "/var/log/libvirt/qemu/(.*.log)",
            "pattern": [],
            "symbolic_name": "libvirtd_qemu_log"
        },
	{
            "file": "/var/log/libvirt/libvirtd.log",
            "pattern": [],
            "symbolic_name": "libvirtd_log"
        }
    ],
    "globs": [
        {
            "glob": "var/log/ceph/()*ceph-osd.*\\.log$",
            "pattern": [
                "common/Thread.cc"
            ],
            "symbolic_name": "ceph_osd_log"
        },
        {
            "glob": "/var/log/ceph/()ceph.log*",
            "pattern": [
                "[WRN] slow request"
            ],
            "symbolic_name": "ceph_log"
        }
    ]
}

@psachin
Copy link
Author

psachin commented Dec 26, 2019

tree test/

Output:

$ tree test
test
└── var
    └── log
        ├── ceph
        │   ├── ceph.log.1
        │   ├── ceph-osd.1.log
        │   └── ceph-osd.2.log
        └── libvirt
            ├── libvirtd.log
            └── qemu
                ├── bar.log
                └── foo.log

5 directories, 6 files

@psachin
Copy link
Author

psachin commented Dec 26, 2019

$ python ./test_expand_path.py 

=== Spec libvirtd_qemu_log: "/var/log/libvirt/qemu/(.*.log)" ===
Attempting to expand: /home/psachin/github/insights/insights-core-assets/test/var/log/libvirt/qemu/(.*.log)
Paths: ['/home/psachin/github/insights/insights-core-assets/test/var/log/libvirt/qemu/bar.log', '/home/psachin/github/insights/insights-core-assets/test/var/log/libvirt/qemu/foo.log']

=== Spec libvirtd_log: "/var/log/libvirt/libvirtd.log" ===
Attempting to expand: /home/psachin/github/insights/insights-core-assets/test/var/log/libvirt/libvirtd.log
Paths: ['/home/psachin/github/insights/insights-core-assets/test/var/log/libvirt/libvirtd.log']

=== Spec ceph_osd_log: "var/log/ceph/()*ceph-osd.*\.log$" ===
Attempting to expand: /home/psachin/github/insights/insights-core-assets/test/var/log/ceph/()*ceph-osd.*\.log$
Paths: ['/home/psachin/github/insights/insights-core-assets/test/var/log/ceph/ceph-osd.1.log', '/home/psachin/github/insights/insights-core-assets/test/var/log/ceph/ceph-osd.2.log']

=== Spec ceph_log: "/var/log/ceph/()ceph.log*" ===
Attempting to expand: /home/psachin/github/insights/insights-core-assets/test/var/log/ceph/()ceph.log*
Paths: ['/home/psachin/github/insights/insights-core-assets/test/var/log/ceph/ceph.log.1']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment