-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathupdate_version.py
More file actions
executable file
·87 lines (71 loc) · 3.07 KB
/
Copy pathupdate_version.py
File metadata and controls
executable file
·87 lines (71 loc) · 3.07 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
import fileinput
import re
import datetime
"""
Update the version of .user.js file(s) passed as arguments
File is edited in-place, and the version is set to YYYY.MM.DD.N format
automatically.
If version is missing, one will be added.
If ran more than once a day, N will be updated.
UTC date is used.
Add or update version in the metablock:
$> ./tools/update_version.py bandcamp_importer.user.js
To test without modifying file, use stdin:
$> ./tools/update_version.py < bandcamp_importer.user.js
"""
def make_version_line(old_value='0.0.0.0', spacing=' '*8, eol="\n"):
prev_version = [int(x) for x in old_value.split('.')]
now = datetime.datetime.utcnow()
version = [now.year, now.month, now.day, 1]
if prev_version[:3] == version[:3]:
version[3] = prev_version[3] + 1
version_str = '%04d.%d.%d.%d' % tuple(version)
return ('// @version' + spacing + version_str + eol, version_str)
re_start_header = re.compile(r'//\s*==UserScript==', re.IGNORECASE)
re_stop_header = re.compile(r'//\s*==/UserScript==', re.IGNORECASE)
re_keyval = re.compile(r'^[\s\*/]+@(\S+)(\s+)(.+)\s*$', re.IGNORECASE)
def process_files(files, verbose=True):
def echo(*args):
if verbose:
sys.stderr.write(*args)
for line in fileinput.input(files, inplace=1):
try:
current = fileinput.filename()
if fileinput.isfirstline():
echo('%s: processing...\n' % current)
version_done = False
in_header = False
header_processed = False
if not header_processed:
if not in_header:
in_header = re_start_header.search(line)
else:
if re_stop_header.search(line):
in_header = False
header_processed = True
if not version_done:
newline, version_str = make_version_line()
sys.stdout.write(newline)
echo('%s: %s (added)\n' %
(current, version_str))
else:
m = re_keyval.search(line)
if m:
key, spacing, value = m.groups()
if key == 'version':
if version_done:
echo('%s: %s dupe removed\n' %
(current, value))
continue # skip the line, more than one version
line, version_str = make_version_line(value, spacing)
echo('%s: %s (old)\n' % (current, value))
echo('%s: %s (new)\n' % (current, version_str))
version_done = True
finally:
sys.stdout.write(line)
if __name__ == "__main__":
process_files(sys.argv[1:], verbose=True)