Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
regit
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Software Systems Engineering
regit
Commits
9e567357
Commit
9e567357
authored
May 7, 2023
by
chrg
Browse files
Options
Downloads
Patches
Plain Diff
Improve logging and failure handeling
parent
c5128291
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/regit/__main__.py
+5
-6
5 additions, 6 deletions
src/regit/__main__.py
src/regit/utils.py
+56
-4
56 additions, 4 deletions
src/regit/utils.py
with
61 additions
and
10 deletions
src/regit/__main__.py
+
5
−
6
View file @
9e567357
from
contextlib
import
contextmanager
from
dataclasses
import
dataclass
,
field
from
dataclasses
import
dataclass
,
field
from
pathlib
import
Path
from
pathlib
import
Path
from
typing
import
Callable
from
typing
import
Callable
...
@@ -63,6 +62,7 @@ class BlobHandler:
...
@@ -63,6 +62,7 @@ class BlobHandler:
change
.
blob_id
=
self
.
blobs_handled
[
change
.
blob_id
]
change
.
blob_id
=
self
.
blobs_handled
[
change
.
blob_id
]
def
read_blob
(
self
,
blob_id
:
bytes
):
def
read_blob
(
self
,
blob_id
:
bytes
):
log
.
debug
(
"
Reading blob %s
"
,
blob_id
)
assert
self
.
gitcat
is
not
None
assert
self
.
gitcat
is
not
None
# To get the typecheck to pass
# To get the typecheck to pass
stdin
,
stdout
=
self
.
gitcat
.
stdin
,
self
.
gitcat
.
stdout
stdin
,
stdout
=
self
.
gitcat
.
stdin
,
self
.
gitcat
.
stdout
...
@@ -135,18 +135,16 @@ def regit(
...
@@ -135,18 +135,16 @@ def regit(
with
tempfile
.
TemporaryDirectory
()
as
folder
:
with
tempfile
.
TemporaryDirectory
()
as
folder
:
folder
=
Path
(
folder
)
folder
=
Path
(
folder
)
def
transformer
(
file
:
Path
,
content
:
bytes
):
def
transformer
(
file
:
Path
,
content
:
bytes
)
->
bytes
:
pargs
=
list
(
args
)
pargs
=
list
(
args
)
try
:
try
:
ix
=
pargs
.
index
(
"
{}
"
)
ix
=
pargs
.
index
(
"
{}
"
)
except
ValueError
:
except
ValueError
:
return
utils
.
run
(
return
utils
.
run_stdout
([
program
]
+
pargs
,
input
=
content
)
[
program
]
+
pargs
,
input
=
content
,
capture_output
=
True
,
check
=
True
).
stdout
else
:
else
:
with
utils
.
tfile
(
folder
,
file
.
name
,
content
)
as
tmp_file
:
with
utils
.
tfile
(
folder
,
file
.
name
,
content
)
as
tmp_file
:
pargs
[
ix
]
=
str
(
tmp_file
)
pargs
[
ix
]
=
str
(
tmp_file
)
utils
.
run
([
program
]
+
pargs
,
check
=
True
)
utils
.
run
([
program
]
+
pargs
)
with
open
(
tmp_file
,
"
rb
"
)
as
f
:
with
open
(
tmp_file
,
"
rb
"
)
as
f
:
return
f
.
read
()
return
f
.
read
()
...
@@ -169,6 +167,7 @@ def regit(
...
@@ -169,6 +167,7 @@ def regit(
log
.
debug
(
"
Finished git filter
"
)
log
.
debug
(
"
Finished git filter
"
)
if
mapping
:
if
mapping
:
log
.
debug
(
"
Writing mapping to %s
"
,
mapping
)
writer
=
csv
.
writer
(
mapping
)
writer
=
csv
.
writer
(
mapping
)
writer
.
writerow
([
"
from
"
,
"
to
"
])
writer
.
writerow
([
"
from
"
,
"
to
"
])
for
fm
,
to
in
filter
.
_commit_renames
.
items
():
for
fm
,
to
in
filter
.
_commit_renames
.
items
():
...
...
This diff is collapsed.
Click to expand it.
src/regit/utils.py
+
56
−
4
View file @
9e567357
from
contextlib
import
contextmanager
from
contextlib
import
contextmanager
from
pathlib
import
Path
from
pathlib
import
Path
import
logging
import
logging
import
time
import
os
import
os
import
subprocess
import
subprocess
import
uuid
log
=
logging
.
getLogger
(
"
regit
"
)
log
=
logging
.
getLogger
(
"
regit
"
)
def
run
(
cmd
,
**
kwargs
):
@contextmanager
def
timeit
(
name
):
start
=
time
.
time
()
log
.
debug
(
"
Started %s
"
,
name
)
yield
end
=
time
.
time
()
log
.
debug
(
"
Done running %s in %s
"
,
end
-
start
)
def
handle_results
(
uid
,
x
:
subprocess
.
CompletedProcess
[
bytes
],
input
:
None
|
bytes
=
None
):
if
x
.
returncode
==
0
:
log
.
debug
(
"
Command %s succeded!
"
)
return
f
=
Path
(
str
(
uid
)).
absolute
()
log
.
debug
(
"
Command %s failed! writing inputs to files %s
"
,
x
.
args
,
f
)
if
input
is
not
None
:
with
open
(
f
.
with_suffix
(
"
.stdin
"
),
"
bw
"
)
as
o
:
o
.
write
(
input
)
if
x
.
stdout
is
not
None
:
with
open
(
f
.
with_suffix
(
"
.stdout
"
),
"
bw
"
)
as
o
:
o
.
write
(
x
.
stdout
)
x
.
check_returncode
()
def
run
(
cmd
)
->
None
:
"""
A wrapper around subprocess.run that logs the command.
"""
"""
A wrapper around subprocess.run that logs the command.
"""
log
.
debug
(
"
Running cmd %s
"
,
cmd
)
uid
=
uuid
.
uuid4
()
return
subprocess
.
run
(
cmd
,
**
kwargs
)
log
.
debug
(
"
Running cmd %s with id %s
"
,
cmd
,
uid
)
with
timeit
(
uid
):
x
=
subprocess
.
run
(
cmd
,
capture_output
=
True
,
universal_newlines
=
False
,
text
=
False
,
)
handle_results
(
uid
,
x
)
def
run_stdout
(
cmd
,
input
:
bytes
)
->
bytes
:
"""
A wrapper around subprocess.run that logs the command.
"""
uid
=
uuid
.
uuid4
()
log
.
debug
(
"
Running cmd %s with id %s
"
,
cmd
,
uid
)
with
timeit
(
uid
):
x
=
subprocess
.
run
(
cmd
,
input
=
input
,
capture_output
=
True
,
universal_newlines
=
False
,
text
=
False
,
)
handle_results
(
uid
,
x
,
input
=
input
)
return
x
.
stdout
def
popen
(
cmd
,
**
kwargs
):
def
popen
(
cmd
,
**
kwargs
):
...
@@ -20,7 +72,7 @@ def popen(cmd, **kwargs):
...
@@ -20,7 +72,7 @@ def popen(cmd, **kwargs):
@contextmanager
@contextmanager
def
chdir
(
path
:
Path
):
def
chdir
(
path
:
os
.
PathLike
[
str
]
|
str
|
Path
):
"""
Change the current working directory to the given path and back when done.
"""
"""
Change the current working directory to the given path and back when done.
"""
old
=
Path
.
cwd
()
old
=
Path
.
cwd
()
log
.
debug
(
"
Changing dir from %s to %s
"
,
old
,
path
)
log
.
debug
(
"
Changing dir from %s to %s
"
,
old
,
path
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment