forked from jantman/misc-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsavescreen.py
More file actions
executable file
·119 lines (104 loc) · 3.96 KB
/
savescreen.py
File metadata and controls
executable file
·119 lines (104 loc) · 3.96 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
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
#!/usr/bin/env python
#
# screen history saving script - saves your current windows and their titles,
# then appends this to ~/.screenrc and writes the result to ~/.screenrc.save
#
# This is intended to be run on a regular basis; I cron it every minute.
#
# WARNING - this expects only one screen session to be running as your user.
#
# Copyright 2014 Jason Antman <jason@jasonantman.com> <http://www.jasonantman.com>
# Use this however you want; send changes back to me please.
#
# The canonical version of this script is available at:
# <https://github.com/jantman/misc-scripts/blob/master/savescreen.py>
#
# CHANGELOG:
# 2014-07-24 Jason Antman <jason@jasonantman.com>
# * first public version
#
# 2014-07-25 Jason Antman <jason@jasonantman.com>
# * restored windows should go to their last PWD, set in ~/.bashrc
# (see <http://blog.jasonantman.com/2014/07/session-save-and-restore-with-bash-and-gnu-screen/>)
#
# 2015-03-26 Jason Antman <jason@jasonantman.com>
# * add lockfile to prevent runaway cron processes
#
####################################################
import subprocess
import re
import os.path
import os
from platform import node
from datetime import datetime
import sys
import atexit
# TODO: if I change anything else in this script, switch to optparse and logging
verbose = False
if '-v' in sys.argv or '--verbose' in sys.argv:
verbose = True
lockfile_path = os.path.abspath(os.path.expanduser('~/.savescreen.lock'))
def clear_lockfile():
os.unlink(lockfile_path)
# exit handler to clear lockfile
atexit.register(clear_lockfile)
def touch(path):
with open(path, 'a'):
os.utime(path, None)
# check for lockfile
if os.path.exists(lockfile_path):
print("ERROR: lockfile already exists at %s; exiting" % lockfile_path)
raise SystemExit(1)
touch(lockfile_path)
# get the window list
windowstr = subprocess.check_output(['screen', '-Q', 'windows']).decode()
#windowstr = '0$ root 1$ blog 2$ blog-venv 3$ themes 4$ plugins 5$ writing 6$ temp/rm_me/blog_logs 7-$ bash 8*$ bash'
if verbose:
print("windowstr: ={w}=".format(w=windowstr))
windows = {}
m = True
windowre = re.compile(r'(\s?(\d+)[-\*]?\$\s+(\S+)\s*)')
max_window = 0
# loop over the window list, extract substrings matching a window specifier
while m is not None:
if verbose:
print("LOOP windowstr={w}=".format(w=windowstr))
m = windowre.match(windowstr)
if m is None:
if len(windows) == 0:
if verbose:
print("no match and no windows yet; trimming windowstr and continuing")
windowstr = windowstr[1:]
m = True
continue
else:
if verbose:
print("no match, breaking out of loop - windowstr: {w}".format(w=windowstr))
break
g = m.groups()
windowstr = windowstr[len(g[0]):]
if verbose:
print("found match: {a} = {b}".format(a=g[1], b=g[2]))
windows[int(g[1])] = g[2]
if int(g[1]) > max_window:
max_window = int(g[1])
if verbose:
print(windows)
# read in screenrc
with open(os.path.join(os.path.expanduser('~'), '.screenrc'), 'r') as fh:
screenrc = fh.read()
# get rid of the first "local 0" line if it's there
screenrc = screenrc.replace("screen -t local 0\n", "")
# write it out to the save location, with the windows added
dirpath = os.path.join(os.path.expanduser('~'), '.screendirs')
with open(os.path.join(os.path.expanduser('~'), '.screenrc.save'), 'w') as fh:
fh.write(screenrc)
fh.write("\n\n")
fh.write("# .screenrc.save generated on %s at %s\n" % (node(), datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
for n in range(0, max_window + 1):
if n in windows:
fh.write("screen -t \"{name}\" {num} sh -c \"cd $(readlink -fn {dirpath}/{num}); bash\"\n".format(name=windows[n], num=n, dirpath=dirpath))
else:
fh.write("screen -t \"{name}\" {num} sh -c \"cd $(readlink -fn {dirpath}/{num}); bash\"\n".format(name='bash', num=n, dirpath=dirpath))
fh.write("\n")
# done