Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
toolbox
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tristan Cavelier
toolbox
Commits
a969ec2e
Commit
a969ec2e
authored
Jun 29, 2016
by
Tristan Cavelier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add cros-safe-copy.py
parent
f0bad898
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
78 additions
and
0 deletions
+78
-0
cros-safe-copy.py
cros-safe-copy.py
+78
-0
No files found.
cros-safe-copy.py
0 → 100644
View file @
a969ec2e
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Usage: python cros-safe-copy.py SRC DST [SLEEP]
Where SRC is the source file path
DST is the destination file path
SLEEP is the time to wait between two copied chunk (in second)
Do a copy of flushed everytime chunks from SRC to DST. This is usefull if your
`dd` command makes your machine rebooting. More, this script save the state
during copying so that even if the machine reboots, you can copy from where it
was stopped. (Be careful to run your script where CWD is persistent! Avoid /tmp
or ~/Downloads on Chromebooks!)
Example:
sudo python cros-safe-copy.py NayuOS.img /dev/sda
# savestate.json file is saved in CWD
"""
import
sys
,
os
,
time
,
json
input
=
sys
.
argv
[
1
]
output
=
sys
.
argv
[
2
]
sleep
=
int
(
sys
.
argv
[
3
])
if
len
(
sys
.
argv
)
>
3
else
0
def
saveState
(
d
):
open
(
"savestate.json.lock"
,
"wx"
).
write
(
json
.
dumps
(
d
))
os
.
rename
(
"savestate.json.lock"
,
"savestate.json"
)
def
loadState
():
try
:
state
=
json
.
loads
(
open
(
"savestate.json"
).
read
())
print
"Using savestate.json"
return
state
except
IOError
:
print
"Creating savestate.json"
MiB
=
1024
*
1024
bs
=
65536
saveState
(
dict
(
input
=
input
,
output
=
output
,
bs
=
bs
,
count
=
50
*
MiB
/
bs
,
skip
=
0
,
seek
=
0
,
))
return
loadState
()
def
quote
(
args
):
return
" "
.
join
([
"'%s'"
%
a
.
replace
(
"'"
,
"'
\
\
''"
)
for
a
in
args
])
state
=
loadState
()
assert
state
[
"input"
]
==
input
assert
state
[
"output"
]
==
output
while
state
[
"skip"
]
*
state
[
"bs"
]
<
os
.
stat
(
input
).
st_size
:
command
=
quote
([
"dd"
,
"bs=%d"
%
state
[
"bs"
],
"count=%d"
%
state
[
"count"
],
"seek=%d"
%
state
[
"seek"
],
"skip=%d"
%
state
[
"skip"
],
"if=%s"
%
input
,
"of=%s"
%
output
,
])
print
"$"
,
command
if
os
.
system
(
command
):
break
print
"$ sync"
if
os
.
system
(
"sync"
):
# seems not useful
break
state
[
"skip"
]
+=
state
[
"count"
]
state
[
"seek"
]
+=
state
[
"count"
]
saveState
(
state
)
if
sleep
>
0
:
print
"sleeping for %d second(s)"
%
sleep
time
.
sleep
(
sleep
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment