-
Notifications
You must be signed in to change notification settings - Fork 4
/
everything_efu_gen.py
executable file
·151 lines (119 loc) · 4.49 KB
/
everything_efu_gen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python3
#
# Copyright 2017, 2018 Torbjörn Lönnemark <[email protected]>
#
# Permission to use, copy, modify, and distribute this software for any purpose
# with or without fee is hereby granted, provided that the above copyright
# notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
import argparse
import csv
import enum
import os
import sys
import ruamel.yaml
from itertools import chain
from pathlib import PureWindowsPath
def init_yaml():
y = ruamel.yaml.YAML(typ='safe', pure=True)
y.default_flow_style = False
y.indent = 4
y.block_seq_indent = 2
return y
class WindowsFileAttribute(enum.Enum):
"""
Windows file attribute flags.
Only useful flags included here.
Ref: https://msdn.microsoft.com/en-us/library/windows/desktop/gg258117(v=vs.85).aspx
"""
READONLY = 0x00000001
HIDDEN = 0x00000002
DIRECTORY = 0x00000010
def windows_path(path, base):
"""
Convert a path to a windows path relative to the root `base`.
"""
return str(PureWindowsPath(path[len(base.rstrip(os.path.sep)):].lstrip(os.path.sep)))
def windows_time(unix_time):
"""
Convert a UNIX timestamp to a Windows FILETIME.
"""
# seconds between windows (Jan 1, 1601, 00:00), and unix (Jan 1, 1970, 00:00)
return int((11644473600 + unix_time) * 10000000)
def windows_attrs(path, name, is_dir):
"""
Return the windows file attributes for the given path.
"""
attrs = 0
if name.startswith('.'):
attrs |= WindowsFileAttribute.HIDDEN.value
if not os.access(path, os.W_OK):
attrs |= WindowsFileAttribute.READONLY.value
if is_dir:
attrs |= WindowsFileAttribute.DIRECTORY.value
return attrs
def print_example_config():
"""
Generate and print a sample configuration.
"""
return init_yaml().dump({
'directories': [
'/mnt/mydisk',
'/mnt/myseconddisk',
]
}, stream=sys.stdout)
def scan(path):
"""
Generate a file list for all files stored under `path` and save it to
`path`/.everything_index.efu.
"""
outpath_work = os.path.join(path, '.everything_index.efu-scanning')
outpath = os.path.join(path, '.everything_index.efu')
with open(outpath_work, 'w') as outfile:
writer = csv.writer(outfile, quoting=csv.QUOTE_NONE)
writer.writerow(['Filename', 'Size', 'Date Modified', 'Date Created', 'Attributes'])
# Doesn't seem to be possible to specify quoting per field number, and
# writer.dialect.quoting is not writable, so this will have to do (since we
# want to mirror the format of EFU files created by Everything itself:
# unquoted fields in the header line, the filename quoted and the rest
# unqouted for remaining lines).
writer = csv.writer(outfile, quoting=csv.QUOTE_NONNUMERIC)
for dirpath, dirnames, filenames in os.walk(path):
for name, is_dir in chain(zip(filenames, [False] * len(filenames)), zip(dirnames, [True] * len(filenames))):
p = os.path.join(dirpath, name)
try:
st = os.lstat(p)
except FileNotFoundError:
continue
except PermissionError:
writer.writerow([windows_path(p, path), 0, 0, 0, windows_attrs(p, name, is_dir)])
continue
try:
writer.writerow([windows_path(p, path), 0 if is_dir else st.st_size, windows_time(st.st_mtime), windows_time(st.st_ctime), windows_attrs(p, name, is_dir)])
except UnicodeEncodeError:
pass
os.replace(outpath_work, outpath)
def main():
parser = argparse.ArgumentParser()
mut_excl_group = parser.add_mutually_exclusive_group(required=True)
mut_excl_group.add_argument('config', metavar='CONFIG', help="config file", type=open, nargs='*', default=[])
mut_excl_group.add_argument('--print-sample-config', help="prints a sample config to stdout", required=False, action='store_true')
args = parser.parse_args()
if args.print_sample_config:
print_example_config()
return
dirs = set()
for f in args.config:
config = init_yaml().load(f)
dirs.update(config['directories'])
for path in dirs:
scan(path)
if __name__ == '__main__':
main()