Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
OpnSense
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
OpnSense
Commits
348383fc
Commit
348383fc
authored
Jul 20, 2015
by
Ad Schellevis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(dev) add inspect_function_usage.py to inspect function usage and module requires
parent
92e82964
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
143 additions
and
27 deletions
+143
-27
crawl_legacy_deps.py
scripts/development/crawl_legacy_deps.py
+3
-3
inspect_function_usage.py
scripts/development/inspect_function_usage.py
+49
-0
legacy_deps.py
scripts/development/lib/legacy_deps.py
+91
-24
No files found.
scripts/development/crawl_legacy_deps.py
View file @
348383fc
...
@@ -45,11 +45,11 @@ if not os.path.exists(target_directory):
...
@@ -45,11 +45,11 @@ if not os.path.exists(target_directory):
os
.
mkdir
(
target_directory
)
os
.
mkdir
(
target_directory
)
# start crawling
# start crawling
crawler
=
DependancyCrawler
()
crawler
=
DependancyCrawler
(
src_root
)
print
'[
%.2
f] started '
%
(
time
.
time
())
print
'[
%.2
f] started '
%
(
time
.
time
())
crawler
.
crawl
(
src_root
)
crawler
.
crawl
()
print
'[
%.2
f] collected
%
d dependancies in
%
d files'
%
(
time
.
time
(),
print
'[
%.2
f] collected
%
d dependancies in
%
d files'
%
(
time
.
time
(),
crawler
.
get_total_depend
a
ncies
(),
crawler
.
get_total_depend
e
ncies
(),
crawler
.
get_total_files
())
crawler
.
get_total_files
())
# generate graphs
# generate graphs
...
...
scripts/development/inspect_function_usage.py
0 → 100644
View file @
348383fc
#!/usr/local/bin/python2.7
"""
Copyright (c) 2015 Ad Schellevis
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
"""
import
sys
from
lib.legacy_deps
import
DependancyCrawler
src_root
=
'/usr/local/'
crawler
=
DependancyCrawler
(
src_root
)
if
len
(
sys
.
argv
)
<
2
:
print
(
'inspect legacy function usage of module, choose one of:'
)
for
module_name
in
crawler
.
get_files
():
print
(
module_name
)
sys
.
exit
()
chk_source
=
sys
.
argv
[
1
]
crawler
.
crawl
()
usage
=
crawler
.
where_used
(
chk_source
)
for
src_filename
in
usage
:
module_name
=
crawler
.
get_dependency_by_src
(
src_filename
)
trace_data
=
crawler
.
trace
(
module_name
)
if
module_name
not
in
trace_data
:
print
(
'inspect :
%
s'
%
(
src_filename
))
scripts/development/lib/legacy_deps.py
View file @
348383fc
...
@@ -30,21 +30,40 @@ import os
...
@@ -30,21 +30,40 @@ import os
import
os.path
import
os.path
class
DependancyCrawler
(
object
):
class
DependancyCrawler
(
object
):
""" Legacy depend
a
ncy crawler and grapher
""" Legacy depend
e
ncy crawler and grapher
"""
"""
def
__init__
(
self
):
def
__init__
(
self
,
root
):
self
.
_all_dependancies
=
{}
""" init
:param root: start crawling at
:return:
"""
self
.
_all_dependencies
=
{}
self
.
_all_dependencies_src
=
{}
self
.
_all_functions
=
{}
self
.
root
=
root
def
get_dependency_by_src
(
self
,
src_filename
):
""" dependencies are stored by a single name, this method maps a filename back to it's name
usually the basename of the file.
:param src_filename:
:return:
"""
if
src_filename
in
self
.
_all_dependencies_src
:
return
self
.
_all_dependencies_src
[
src_filename
]
else
:
return
None
def
fetch_php
(
self
,
src_filename
):
def
fetch_php
_modules
(
self
,
src_filename
):
# create a new list for this base filename
# create a new list for this base filename
base_filename
=
os
.
path
.
basename
(
src_filename
)
base_filename
=
os
.
path
.
basename
(
src_filename
)
if
base_filename
in
self
.
_all_depend
a
ncies
:
if
base_filename
in
self
.
_all_depend
e
ncies
:
base_filename
=
'
%
s__
%
s'
%
(
src_filename
.
split
(
'/'
)[
-
2
],
base_filename
)
base_filename
=
'
%
s__
%
s'
%
(
src_filename
.
split
(
'/'
)[
-
2
],
base_filename
)
self
.
_all_dependancies
[
base_filename
]
=
[]
self
.
_all_dependencies
[
base_filename
]
=
[]
self
.
_all_dependencies_src
[
src_filename
]
=
base_filename
source_data
=
open
(
src_filename
)
.
read
()
source_data
=
open
(
src_filename
)
.
read
()
# fetch all include, include_once, require, require_once statements and
# fetch all include, include_once, require, require_once statements and
# add depend
ancies to object dependa
ncy list.
# add depend
encies to object depende
ncy list.
for
tag
in
(
'include'
,
'require'
):
for
tag
in
(
'include'
,
'require'
):
data
=
source_data
data
=
source_data
while
True
:
while
True
:
...
@@ -60,45 +79,93 @@ class DependancyCrawler(object):
...
@@ -60,45 +79,93 @@ class DependancyCrawler(object):
dep_stmt
=
dep_stmt
[
1
:]
.
replace
(
"'"
,
'"'
)
dep_stmt
=
dep_stmt
[
1
:]
.
replace
(
"'"
,
'"'
)
if
dep_stmt
.
find
(
'
\n
'
)
==
-
1
and
dep_stmt
.
count
(
'"'
)
==
2
:
if
dep_stmt
.
find
(
'
\n
'
)
==
-
1
and
dep_stmt
.
count
(
'"'
)
==
2
:
dep_filename
=
dep_stmt
.
split
(
'"'
)[
1
]
dep_filename
=
dep_stmt
.
split
(
'"'
)[
1
]
if
dep_filename
not
in
self
.
_all_depend
a
ncies
[
base_filename
]:
if
dep_filename
not
in
self
.
_all_depend
e
ncies
[
base_filename
]:
self
.
_all_depend
a
ncies
[
base_filename
]
.
append
(
dep_filename
)
self
.
_all_depend
e
ncies
[
base_filename
]
.
append
(
dep_filename
)
data
=
data
[
strlen
+
startpos
:]
data
=
data
[
strlen
+
startpos
:]
def
crawl
(
self
,
root
,
analyse_dirs
=
(
'etc'
,
'www'
,
'captiveportal'
,
'sbin'
)):
def
fetch_php_functions
(
self
,
src_filename
):
""" Crawl through legacy code
""" find php functions
:param root: start crawling at
:param src_filename:
:param analyse_dirs: only analyse these directories
:return:
:return: None
"""
base_filename
=
os
.
path
.
basename
(
src_filename
)
if
base_filename
in
self
.
_all_functions
:
base_filename
=
'
%
s__
%
s'
%
(
src_filename
.
split
(
'/'
)[
-
2
],
base_filename
)
function_list
=
[]
for
line
in
open
(
src_filename
,
'r'
)
.
read
()
.
split
(
'
\n
'
):
if
line
.
find
(
'function '
)
>
-
1
and
line
.
find
(
'('
)
>
-
1
:
function_nm
=
line
.
split
(
'('
)[
0
]
.
split
(
' '
)[
-
1
]
if
function_nm
.
find
(
'*'
)
==
-
1
:
function_list
.
append
(
function_nm
)
self
.
_all_functions
[
base_filename
]
=
function_list
def
find_files
(
self
,
analyse_dirs
=
(
'etc'
,
'www'
,
'captiveportal'
,
'sbin'
)):
"""
:param analyse_dirs: directories to analyse
:return:
"""
"""
for
analyse_dir
in
analyse_dirs
:
for
analyse_dir
in
analyse_dirs
:
analyse_dir
=
(
'
%
s/
%
s'
%
(
root
,
analyse_dir
))
.
replace
(
'//'
,
'/'
)
analyse_dir
=
(
'
%
s/
%
s'
%
(
self
.
root
,
analyse_dir
))
.
replace
(
'//'
,
'/'
)
for
wroot
,
wdirs
,
wfiles
in
os
.
walk
(
analyse_dir
):
for
wroot
,
wdirs
,
wfiles
in
os
.
walk
(
analyse_dir
):
for
src_filename
in
wfiles
:
for
src_filename
in
wfiles
:
src_filename
=
'
%
s/
%
s'
%
(
wroot
,
src_filename
)
src_filename
=
'
%
s/
%
s'
%
(
wroot
,
src_filename
)
if
src_filename
.
split
(
'.'
)[
-
1
]
in
(
'php'
,
'inc'
,
'class'
)
\
if
src_filename
.
split
(
'.'
)[
-
1
]
in
(
'php'
,
'inc'
,
'class'
)
\
or
open
(
src_filename
)
.
read
(
1024
)
.
find
(
'/bin/php'
)
>
-
1
:
or
open
(
src_filename
)
.
read
(
1024
)
.
find
(
'/bin/php'
)
>
-
1
:
self
.
fetch_php
(
src_filename
)
yield
src_filename
def
crawl
(
self
):
""" Crawl through legacy code
:param analyse_dirs: only analyse these directories
:return: None
"""
for
src_filename
in
self
.
find_files
():
self
.
fetch_php_modules
(
src_filename
)
self
.
fetch_php_functions
(
src_filename
)
def
where_used
(
self
,
src
):
"""
:param src: source object name (base name)
:return: dictionary containing files and functions
"""
where_used_lst
=
{}
for
src_filename
in
self
.
find_files
():
data
=
open
(
src_filename
,
'r'
)
.
read
()
.
replace
(
'
\n
'
,
' '
)
use_list
=
[]
for
function
in
self
.
_all_functions
[
src
]:
if
data
.
find
(
'
%
s('
%
(
function
))
>
-
1
or
\
data
.
find
(
'!
%
s '
)
>
-
1
or
\
data
.
find
(
'!
%
s('
)
>
-
1
or
\
data
.
find
(
'(
%
s('
)
>
-
1
or
\
data
.
find
(
'(
%
s '
)
>
-
1
or
\
data
.
find
(
'
%
s '
%
(
function
))
>
-
1
:
use_list
.
append
(
function
)
if
len
(
use_list
)
>
0
:
where_used_lst
[
src_filename
]
=
sorted
(
use_list
)
return
where_used_lst
def
get_total_files
(
self
):
def
get_total_files
(
self
):
""" get total number of analysed files
""" get total number of analysed files
:return: int
:return: int
"""
"""
return
len
(
self
.
_all_depend
a
ncies
)
return
len
(
self
.
_all_depend
e
ncies
)
def
get_total_depend
a
ncies
(
self
):
def
get_total_depend
e
ncies
(
self
):
""" get total number of dependencies
""" get total number of dependencies
:return: int
:return: int
"""
"""
count
=
0
count
=
0
for
src_filename
in
self
.
_all_depend
a
ncies
:
for
src_filename
in
self
.
_all_depend
e
ncies
:
count
+=
len
(
self
.
_all_depend
a
ncies
[
src_filename
])
count
+=
len
(
self
.
_all_depend
e
ncies
[
src_filename
])
return
count
return
count
def
get_files
(
self
):
def
get_files
(
self
):
""" retrieve all analysed files as iterator (ordered by name)
""" retrieve all analysed files as iterator (ordered by name)
:return: iterator
:return: iterator
"""
"""
for
src_filename
in
sorted
(
self
.
_all_depend
a
ncies
):
for
src_filename
in
sorted
(
self
.
_all_depend
e
ncies
):
yield
src_filename
yield
src_filename
def
trace
(
self
,
src_filename
,
parent_filename
=
None
,
result
=
None
,
level
=
0
):
def
trace
(
self
,
src_filename
,
parent_filename
=
None
,
result
=
None
,
level
=
0
):
...
@@ -117,8 +184,8 @@ class DependancyCrawler(object):
...
@@ -117,8 +184,8 @@ class DependancyCrawler(object):
result
[
src_filename
][
'dup'
]
.
append
(
parent_filename
)
result
[
src_filename
][
'dup'
]
.
append
(
parent_filename
)
return
return
if
src_filename
in
self
.
_all_depend
a
ncies
:
if
src_filename
in
self
.
_all_depend
e
ncies
:
for
dependency
in
self
.
_all_depend
a
ncies
[
src_filename
]:
for
dependency
in
self
.
_all_depend
e
ncies
[
src_filename
]:
self
.
trace
(
dependency
,
src_filename
,
result
,
level
=
level
+
1
)
self
.
trace
(
dependency
,
src_filename
,
result
,
level
=
level
+
1
)
return
result
return
result
...
@@ -129,7 +196,7 @@ class DependancyCrawler(object):
...
@@ -129,7 +196,7 @@ class DependancyCrawler(object):
:return:
:return:
"""
"""
result
=
{
'levels'
:
0
,
'dup_count'
:
0
}
result
=
{
'levels'
:
0
,
'dup_count'
:
0
}
if
src_filename
in
self
.
_all_depend
a
ncies
:
if
src_filename
in
self
.
_all_depend
e
ncies
:
data
=
self
.
trace
(
src_filename
)
data
=
self
.
trace
(
src_filename
)
for
dep_filename
in
data
:
for
dep_filename
in
data
:
if
data
[
dep_filename
][
'level'
]
>
result
[
'levels'
]:
if
data
[
dep_filename
][
'level'
]
>
result
[
'levels'
]:
...
...
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