Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F85137278
file_transformer.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, Sep 27, 01:01
Size
13 KB
Mime Type
text/x-python
Expires
Sun, Sep 29, 01:01 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
21098293
Attached To
R11434 py-licenser
file_transformer.py
View Options
# -*- coding: utf-8 -*-
""" file_transformer.py: Coordination of the different transformations"""
__author__
=
"Guillaume Anciaux and Nicolas Richart"
__credits__
=
[
"Guillaume Anciaux <guillaume.anciaux@epfl.ch>"
,
"Nicolas Richart <nicolas.richart@epfl.ch>"
,
]
__copyright__
=
"Copyright (©) 2010-2021 EPFL (Ecole Polytechnique Fédérale"
\
" de Lausanne) Laboratory (LSMS - Laboratoire de Simulation"
\
" en Mécanique des Solides)"
__license__
=
"GPLv3"
__version__
=
"2.0"
import
datetime
as
dt
from
.
import
licenser_ask_question
from
.
import
author_db
as
adb
from
.
import
copyright_db
as
cdb
from
.
import
export
from
.
import
file_info
as
fi
from
.
import
print_colored
from
.
import
version_info
as
vc
import
os
from
pygments
import
highlight
from
pygments.lexers.diff
import
DiffLexer
from
pygments.formatters.terminal256
import
Terminal256Formatter
from
pygments.formatters.terminal
import
TerminalFormatter
@export
class
FileTransformer
(
object
):
"""
Class that reformat the headers
"""
__keep_authors
=
None
_brief
=
None
__ignore
=
False
_new_header
=
None
def
__init__
(
self
,
filename
,
db
,
release_date
=
dt
.
datetime
.
now
()
.
date
(),
no_author_check
=
False
,
no_brief_check
=
False
,
force
=
False
,
version
=
None
,
**
kwargs
):
self
.
__filename
=
filename
self
.
__release_date
=
release_date
self
.
__db
=
db
self
.
__no_brief_check
=
no_brief_check
self
.
__no_author_check
=
no_author_check
self
.
__version
=
version
# Check the authors
self
.
__author_db
=
adb
.
AuthorDB
(
db
)
if
"vc_backend"
in
kwargs
and
kwargs
[
"vc_backend"
]
is
not
None
:
vc_back
=
kwargs
[
"vc_backend"
]
else
:
vc_back
=
db
.
versioning_backend
if
vc_back
!=
"none"
:
self
.
_date_style
=
self
.
__db
.
get_config
(
'date_style'
)
self
.
__repo
=
self
.
__db
.
get_config
(
'repo'
)
self
.
_vc_info
=
vc
.
VersionInfo
(
self
.
__repo
,
self
.
__filename
,
self
.
__db
.
get_list_of_ignore_emails
(),
backend
=
vc_back
,
rev_to
=
release_date
)
self
.
__name
=
self
.
_vc_info
.
name
self
.
__filename
=
self
.
__repo
+
'/'
+
self
.
__name
self
.
_creation_date
=
self
.
_vc_info
.
creation_date
self
.
_last_modif
=
self
.
_vc_info
.
last_modification_date
else
:
self
.
_creation_date
=
None
self
.
_last_modif
=
None
self
.
_date_style
=
None
self
.
_vc_info
=
None
try
:
self
.
_file
=
fi
.
FileInfo
(
self
.
__filename
,
self
.
__author_db
)
except
NotImplementedError
:
print
(
"File {0} ignored due to {1}"
.
format
(
self
.
_vc_info
.
name
,
print_colored
(
'unknown type'
,
'red'
,
attrs
=
[
'bold'
])))
self
.
__ignore
=
True
return
rev_from
=
None
license_id
=
None
self
.
__oldest_name
=
""
if
vc_back
!=
"none"
:
self
.
__oldest_name
=
self
.
_vc_info
.
oldest_name
file_maj
=
self
.
__db
.
find_file
(
self
.
__oldest_name
)
if
file_maj
is
not
None
:
rev_from
,
license_id
=
file_maj
if
not
force
and
rev_from
.
date
()
>=
release_date
:
print
((
"File {0} ignored due to recent "
+
"modifications ({1})"
)
.
format
(
self
.
_vc_info
.
name
,
print_colored
(
rev_from
.
strftime
(
"%Y-%m-
%d
"
),
'red'
,
attrs
=
[
'bold'
])))
self
.
__ignore
=
True
return
if
not
self
.
__no_author_check
:
self
.
_vc_authors
=
self
.
_vc_info
.
authors
self
.
_vc_info
.
populate
(
self
.
__author_db
,
rev_to
=
self
.
__release_date
,
rev_from
=
rev_from
)
self
.
_vc_authors
=
self
.
_vc_info
.
authors
self
.
_f_authors
=
self
.
_file
.
authors
self
.
_real_authors
=
self
.
__compare_authors
(
self
.
_f_authors
,
self
.
_vc_authors
,
file_is_good
=
False
,
**
kwargs
)
if
self
.
_real_authors
-
self
.
_f_authors
:
print
(
"Added authors:
\n
{0}"
.
format
(
"
\n
"
.
join
([
" @author {0}"
.
format
(
author
)
for
author
in
self
.
_real_authors
-
self
.
_f_authors
])))
if
self
.
_f_authors
-
self
.
_real_authors
:
print
(
"Removed authors:
\n
{0}"
.
format
(
"
\n
"
.
join
([
" @author {0}"
.
format
(
author
)
for
author
in
self
.
_f_authors
-
self
.
_real_authors
])))
else
:
self
.
_real_authors
=
self
.
_file
.
authors
if
len
(
self
.
_real_authors
)
==
0
:
res
=
input
(
"This file as no author please provide "
+
"the username of the authors: "
)
while
res
!=
""
:
self
.
_real_authors
.
add
(
self
.
__author_db
.
find_by_user_name
(
res
))
res
=
input
(
"> "
)
# Check the brief
if
not
self
.
__no_brief_check
:
self
.
__check_brief
(
**
kwargs
)
else
:
self
.
_brief
=
self
.
_file
.
get_brief
()
if
"force_license"
in
kwargs
and
kwargs
[
'force_license'
]
is
not
None
:
license_id
=
kwargs
[
"force_license"
]
# Getting the license content
self
.
__copyright_base
=
cdb
.
CopyrightDB
(
db
)
if
license_id
is
None
:
copyright_policy
=
self
.
__db
.
get_config
(
'copyright_policy'
)
if
copyright_policy
==
'creation_date'
:
cdate
=
self
.
_creation_date
.
date
()
elif
copyright_policy
==
'release_date'
:
cdate
=
self
.
__release_date
else
:
cdate
=
None
self
.
_lic
=
self
.
__copyright_base
.
find_by_date
(
cdate
)
else
:
self
.
_lic
=
self
.
__copyright_base
.
find_by_id
(
license_id
)
if
self
.
_lic
is
None
:
raise
(
"The license with the id {0} is not defined"
.
format
(
license_id
))
date_format
=
self
.
__db
.
get_config
(
'date_format'
)
version_config
=
self
.
__db
.
get_config
(
'date_format'
)
# Generates the new header file
_header_params
=
{
'authors'
:
self
.
_real_authors
,
'copyright_txt'
:
self
.
_lic
.
text
,
'date_format'
:
date_format
,
'date_style'
:
self
.
_date_style
,
'last_modification_date'
:
self
.
_last_modif
,
'creation_date'
:
self
.
_creation_date
,
'brief'
:
self
.
_brief
,
'version'
:
self
.
__version
}
if
version_config
==
1
:
self
.
_new_header
=
self
.
_file
.
generate_header
(
**
_header_params
)
else
:
self
.
_new_header
=
self
.
_file
.
generate_header_2
(
license_header
=
self
.
_lic
.
header
,
need_header
=
self
.
_lic
.
need_header
,
license
=
self
.
_lic
.
license
,
**
_header_params
)
# noinspection PyPep8Naming,PyUnusedLocal
def
__compare_authors
(
self
,
file_known_tmp
,
vc_known
,
file_is_good
=
False
,
ignore_threshold
=
0
,
yes
=
False
,
**
kwargs
):
file_known
=
file_known_tmp
if
len
(
file_known
)
==
0
and
len
(
vc_known
)
==
1
:
file_known
=
vc_known
if
not
file_is_good
:
last_keep
=
None
if
len
(
self
.
__oldest_name
)
!=
0
:
last_keep
=
self
.
__db
.
get_last_keep_authors
(
self
.
__oldest_name
,
self
.
__author_db
)
if
last_keep
is
None
:
last_keep
=
[]
for
author
in
last_keep
:
print
(
"Keeping @author {0} ({1})"
.
format
(
author
,
print_colored
(
"previously validated"
,
'red'
,
attrs
=
[
'bold'
])))
file_only
=
file_known
-
vc_known
-
set
(
last_keep
)
to_remove
=
set
()
if
file_only
:
KEEP
=
0
REMOVE
=
1
for
author
in
file_only
:
if
not
yes
:
answer
=
licenser_ask_question
(
"Do you want to remove @author {0}"
.
format
(
author
),
{
'k'
:
KEEP
,
'r'
:
REMOVE
},
'k'
)
if
answer
==
REMOVE
:
to_remove
.
add
(
author
)
else
:
print
((
"Do you want to remove "
+
"@author {0}? ({1}/r)"
)
.
format
(
author
,
print_colored
(
'K'
,
'red'
,
attrs
=
[
'bold'
])
))
file_known
=
file_known
-
to_remove
self
.
__keep_authors
=
file_known
-
to_remove
-
vc_known
vc_only
=
vc_known
-
file_known
if
vc_only
:
to_add
=
set
()
YES
=
0
NO
=
1
DIFF
=
2
for
author
in
vc_only
:
modifications
,
stats
=
self
.
_vc_info
.
modifications_by_author
(
author
.
user_name
)
number_of_modified_lines
=
sum
(
[
len
(
e
[
2
])
for
e
in
modifications
])
number_of_modifications
=
len
(
modifications
)
if
((
number_of_modified_lines
<=
ignore_threshold
)
or
(
number_of_modified_lines
==
number_of_modifications
)):
print
(
'Potential new author'
+
' @author {0} '
.
format
(
author
)
+
'({0} modifications, lines count {1}) {2}'
.
format
(
number_of_modifications
,
number_of_modified_lines
,
print_colored
(
"[ignored do to threshold {0}]"
.
format
(
ignore_threshold
),
'red'
,
attrs
=
[
'bold'
])))
continue
answer
=
DIFF
while
answer
==
DIFF
:
answer
=
licenser_ask_question
(
'Do you want to add'
+
' @author {0}'
.
format
(
print_colored
(
author
,
"blue"
))
+
' ({0} modifications, lines count {1}) '
.
format
(
number_of_modifications
,
number_of_modified_lines
),
{
'd'
:
DIFF
,
'n'
:
NO
,
'y'
:
YES
},
'n'
)
if
answer
==
DIFF
:
for
info
,
modif
,
nb_lines
,
diff_stats
in
modifications
:
if
len
(
modif
)
>
2
:
print
(
info
)
formatter
=
(
Terminal256Formatter
if
'256color'
in
os
.
environ
.
get
(
'TERM'
,
''
)
else
TerminalFormatter
)
print
(
highlight
(
modif
,
DiffLexer
(),
formatter
()))
elif
answer
==
YES
:
to_add
.
add
(
author
)
file_known
=
file_known
.
union
(
to_add
)
return
file_known
# noinspection PyUnusedLocal
def
__check_brief
(
self
,
ignore_filled_briefs
=
False
,
yes
=
False
,
**
kwargs
):
self
.
_brief
=
self
.
_file
.
get_brief
()
if
self
.
_brief
and
not
ignore_filled_briefs
:
if
len
(
self
.
__oldest_name
)
!=
0
:
old_brief
=
self
.
__db
.
get_last_brief
(
self
.
__oldest_name
)
else
:
old_brief
=
None
if
old_brief
is
not
None
and
old_brief
==
self
.
_brief
:
print
(
'Brief: {0} ({1})'
.
format
(
self
.
_brief
,
print_colored
(
"previously validated"
,
'red'
,
attrs
=
[
'bold'
])))
else
:
if
not
yes
:
keep_brief
=
licenser_ask_question
(
(
"
\"
{0}
\"\n
"
+
"Do you want to keep this brief"
)
.
format
(
self
.
_brief
))
if
not
keep_brief
:
self
.
_brief
=
False
else
:
print
((
"
\"
{0}
\"\n
Do you want to keep"
+
" this brief ? ({1}/n)"
)
.
format
(
self
.
_brief
,
print_colored
(
'Y'
,
'red'
,
attrs
=
[
'bold'
])))
if
not
self
.
_brief
:
res
=
input
(
"Please type the brief for file: "
)
brief
=
[]
while
res
!=
""
:
brief
.
append
(
res
)
res
=
input
(
"> "
)
self
.
_brief
=
"
\n
"
.
join
(
brief
)
def
replace_file
(
self
,
dry_run
=
True
):
if
self
.
__ignore
:
return
if
dry_run
:
print
(
self
.
_new_header
)
else
:
if
self
.
_vc_info
is
not
None
:
self
.
__db
.
update_license_file
(
self
.
__oldest_name
,
self
.
_lic
.
lid
,
self
.
__release_date
)
if
self
.
_brief
is
not
None
and
not
self
.
__no_brief_check
:
self
.
__db
.
update_brief_file
(
self
.
__oldest_name
,
self
.
_brief
,
self
.
__release_date
)
if
((
self
.
__keep_authors
is
not
None
)
and
(
not
self
.
__no_author_check
)):
self
.
__db
.
update_keep_authors_file
(
self
.
__oldest_name
,
self
.
__keep_authors
,
self
.
__release_date
)
self
.
_file
.
replace_file
(
self
.
_new_header
)
Event Timeline
Log In to Comment