-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsaga
executable file
·118 lines (96 loc) · 3.13 KB
/
saga
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
#!/usr/bin/env python3
#
# Saga script. This creates an entry in the saga directory.
#
import getpass
import optparse
import os
import datetime
import socket
import sys
import time
from optparse import OptionParser
from pathlib import Path
# --------------------------------------------------------
# config
# --------------------------------------------------------
DIR_SAGA = os.path.join(Path.home(), 'saga')
# --------------------------------------------------------
# alg
# --------------------------------------------------------
USERNAME = getpass.getuser()
HOSTNAME = socket.gethostname().split('.')[0]
def awful_hack_letter_list():
lst = []
for i in range(97, 122+1):
a = chr(i)
for j in range(97, 122+1):
b = chr(j)
lst.append("%s%s"%(a, b))
return lst
def determine_unique_filename(dir, words):
now = time.strftime("%Y%m%d")
lst_awful = awful_hack_letter_list()
idx_awful = 0
letters = lst_awful[idx_awful]
def is_current_prefix_used():
prefix = "%s.%s.%s.%s"%( now
, HOSTNAME
, USERNAME
, letters
)
lst = [f for f in os.listdir(dir)
if f.startswith(prefix)
]
if 0 == len(lst):
return False
else:
return True
while is_current_prefix_used():
idx_awful += 1
letters = lst_awful[idx_awful]
return "%s.%s.%s.%s.%s"%(now, HOSTNAME, USERNAME, letters, words)
def fatal(msg):
print("[saga] FATAL: %s"%msg)
print("aborting")
sys.exit(1)
def main():
if not os.path.exists(DIR_SAGA):
fatal("No directory %s"%DIR_SAGA)
parser = OptionParser()
parser.add_option( '-t', '--dry-run'
, action='store_true'
, dest='dry_run'
)
parser.add_option( '-z', '--echo_most_recent_only'
, action='store_true'
, dest='just_get_last'
)
(options, args) = parser.parse_args()
if args:
lst_words = sys.argv[1:]
dot_words = '.'.join( [w for w in lst_words if not w.startswith('-')] )
saganame = determine_unique_filename(DIR_SAGA, dot_words)
if options.dry_run:
pass
else:
dir_name = os.sep.join( [DIR_SAGA, saganame] )
# gone for append here to be safe
os.mkdir(dir_name)
print(os.sep.join( [DIR_SAGA, saganame] ))
else:
filenames = sorted( [os.sep.join( [DIR_SAGA, fname] ) for fname
in os.listdir(DIR_SAGA)
]
#, reverse=True
)
if not filenames:
print("ERROR: Saga directory is empty.")
print("aborting")
sys.exit(1)
if options.just_get_last:
print(filenames[-1])
else:
print('\n'.join(filenames))
if __name__ == '__main__':
main()